vRp.CD2g_test/utils/moving
kercylan98 22449ff5c3 docs: 优化 README.md 可读性 2024-01-15 17:27:02 +08:00
..
2d.go refactor: 将 moving、aoi、leaderboard 包从 game 包中移动至 utils 包 2023-12-22 11:08:54 +08:00
2d_entity.go refactor: 将 moving、aoi、leaderboard 包从 game 包中移动至 utils 包 2023-12-22 11:08:54 +08:00
2d_events.go refactor: 将 moving、aoi、leaderboard 包从 game 包中移动至 utils 包 2023-12-22 11:08:54 +08:00
2d_example_test.go refactor: 将 moving、aoi、leaderboard 包从 game 包中移动至 utils 包 2023-12-22 11:08:54 +08:00
2d_options.go refactor: 将 moving、aoi、leaderboard 包从 game 包中移动至 utils 包 2023-12-22 11:08:54 +08:00
2d_test.go refactor: 将 moving、aoi、leaderboard 包从 game 包中移动至 utils 包 2023-12-22 11:08:54 +08:00
README.md docs: 优化 README.md 可读性 2024-01-15 17:27:02 +08:00

README.md

Moving

Go doc

暂无介绍...

目录导航

列出了该 package 下所有的函数及类型定义,可通过目录导航进行快捷跳转 ❤️

展开 / 折叠目录导航

包级函数定义

函数名称 描述
NewTwoDimensional 创建一个用于2D对象移动的实例(TwoDimensional)
WithTwoDimensionalTimeUnit 通过特定时间单位创建
WithTwoDimensionalIdleWaitTime 通过特定的空闲等待时间创建
WithTwoDimensionalInterval 通过特定的移动间隔时间创建
NewEntity 暂无描述...

类型定义

类型 名称 描述
STRUCT TwoDimensional 用于2D对象移动的数据结构
INTERFACE TwoDimensionalEntity 2D移动对象接口定义
STRUCT Position2DChangeEventHandle 暂无描述...
STRUCT TwoDimensionalOption 暂无描述...

详情信息

func NewTwoDimensional[EID generic.Basic, PosType generic.SignedNumber](options ...TwoDimensionalOption[EID, PosType]) *TwoDimensional[EID, PosType]

创建一个用于2D对象移动的实例(TwoDimensional)

示例代码:


func ExampleNewTwoDimensional() {
	m := moving2.NewTwoDimensional[int64, float64]()
	defer func() {
		m.Release()
	}()
	fmt.Println(m != nil)
}

查看 / 收起单元测试

func TestNewTwoDimensional(t *testing.T) {
	m := moving2.NewTwoDimensional[int64, float64]()
	defer func() {
		m.Release()
	}()
}


func WithTwoDimensionalTimeUnit[EID generic.Basic, PosType generic.SignedNumber](duration time.Duration) TwoDimensionalOption[EID, PosType]

通过特定时间单位创建

  • 默认单位为1毫秒最小单位也为1毫秒

func WithTwoDimensionalIdleWaitTime[EID generic.Basic, PosType generic.SignedNumber](duration time.Duration) TwoDimensionalOption[EID, PosType]

通过特定的空闲等待时间创建

  • 默认情况下在没有新的移动计划时将限制 100毫秒 + 移动间隔事件(默认100毫秒)

func WithTwoDimensionalInterval[EID generic.Basic, PosType generic.SignedNumber](duration time.Duration) TwoDimensionalOption[EID, PosType]

通过特定的移动间隔时间创建


func NewEntity(guid int64, speed float64) *MoveEntity


TwoDimensional STRUCT

用于2D对象移动的数据结构

  • 通过对象调用 MoveTo 方法后将开始执行该对象的移动
  • 移动将在根据设置的每次移动间隔时间(WithTwoDimensionalInterval)进行移动,当无对象移动需要移动时将会进入短暂的休眠
  • 当对象移动速度永久为0时将会导致永久无法完成的移动
type TwoDimensional[EID generic.Basic, PosType generic.SignedNumber] struct {
	rw                                sync.RWMutex
	entities                          map[EID]*moving2DTarget[EID, PosType]
	timeUnit                          float64
	idle                              time.Duration
	interval                          time.Duration
	close                             bool
	position2DChangeEventHandles      []Position2DChangeEventHandle[EID, PosType]
	position2DDestinationEventHandles []Position2DDestinationEventHandle[EID, PosType]
	position2DStopMoveEventHandles    []Position2DStopMoveEventHandle[EID, PosType]
}

func (*TwoDimensional) MoveTo(entity TwoDimensionalEntity[EID, PosType], x PosType, y PosType)

设置对象移动到特定位置

示例代码:


func ExampleTwoDimensional_MoveTo() {
	m := moving2.NewTwoDimensional(moving2.WithTwoDimensionalTimeUnit[int64, float64](time.Second))
	defer func() {
		m.Release()
	}()
	var wait sync.WaitGroup
	m.RegPosition2DDestinationEvent(func(moving *moving2.TwoDimensional[int64, float64], entity moving2.TwoDimensionalEntity[int64, float64]) {
		fmt.Println("done")
		wait.Done()
	})
	wait.Add(1)
	entity := NewEntity(1, 100)
	m.MoveTo(entity, 50, 30)
	wait.Wait()
}


func (*TwoDimensional) StopMove(id EID)

停止特定对象的移动

示例代码:


func ExampleTwoDimensional_StopMove() {
	m := moving2.NewTwoDimensional(moving2.WithTwoDimensionalTimeUnit[int64, float64](time.Second))
	defer func() {
		m.Release()
	}()
	var wait sync.WaitGroup
	m.RegPosition2DChangeEvent(func(moving *moving2.TwoDimensional[int64, float64], entity moving2.TwoDimensionalEntity[int64, float64], oldX, oldY float64) {
		fmt.Println("move")
	})
	m.RegPosition2DStopMoveEvent(func(moving *moving2.TwoDimensional[int64, float64], entity moving2.TwoDimensionalEntity[int64, float64]) {
		fmt.Println("stop")
		wait.Done()
	})
	m.RegPosition2DDestinationEvent(func(moving *moving2.TwoDimensional[int64, float64], entity moving2.TwoDimensionalEntity[int64, float64]) {
		fmt.Println("done")
		wait.Done()
	})
	wait.Add(1)
	entity := NewEntity(1, 100)
	m.MoveTo(entity, 50, 300)
	m.StopMove(1)
	wait.Wait()
}

查看 / 收起单元测试

func TestTwoDimensional_StopMove(t *testing.T) {
	var wait sync.WaitGroup
	m := moving2.NewTwoDimensional(moving2.WithTwoDimensionalTimeUnit[int64, float64](time.Second))
	defer func() {
		m.Release()
	}()
	m.RegPosition2DChangeEvent(func(moving *moving2.TwoDimensional[int64, float64], entity moving2.TwoDimensionalEntity[int64, float64], oldX, oldY float64) {
		x, y := entity.GetPosition().GetXY()
		fmt.Println(fmt.Sprintf("%d : %d | %f, %f > %f, %f", entity.GetTwoDimensionalEntityID(), time.Now().UnixMilli(), oldX, oldY, x, y))
	})
	m.RegPosition2DDestinationEvent(func(moving *moving2.TwoDimensional[int64, float64], entity moving2.TwoDimensionalEntity[int64, float64]) {
		fmt.Println(fmt.Sprintf("%d : %d | destination", entity.GetTwoDimensionalEntityID(), time.Now().UnixMilli()))
		wait.Done()
	})
	m.RegPosition2DStopMoveEvent(func(moving *moving2.TwoDimensional[int64, float64], entity moving2.TwoDimensionalEntity[int64, float64]) {
		fmt.Println(fmt.Sprintf("%d : %d | stop", entity.GetTwoDimensionalEntityID(), time.Now().UnixMilli()))
		wait.Done()
	})
	for i := 0; i < 10; i++ {
		wait.Add(1)
		entity := NewEntity(int64(i)+1, float64(10+i))
		m.MoveTo(entity, 50, 30)
	}
	time.Sleep(time.Second * 1)
	for i := 0; i < 10; i++ {
		m.StopMove(int64(i) + 1)
	}
	wait.Wait()
}


func (*TwoDimensional) RegPosition2DChangeEvent(handle Position2DChangeEventHandle[EID, PosType])

在对象位置改变时将执行注册的事件处理函数


func (*TwoDimensional) OnPosition2DChangeEvent(entity TwoDimensionalEntity[EID, PosType], oldX PosType, oldY PosType)


func (*TwoDimensional) RegPosition2DDestinationEvent(handle Position2DDestinationEventHandle[EID, PosType])

在对象到达终点时将执行被注册的事件处理函数


func (*TwoDimensional) OnPosition2DDestinationEvent(entity TwoDimensionalEntity[EID, PosType])


func (*TwoDimensional) RegPosition2DStopMoveEvent(handle Position2DStopMoveEventHandle[EID, PosType])

在对象停止移动时将执行被注册的事件处理函数


func (*TwoDimensional) OnPosition2DStopMoveEvent(entity TwoDimensionalEntity[EID, PosType])


func (*TwoDimensional) Release()

释放对象移动对象所占用的资源


TwoDimensionalEntity INTERFACE

2D移动对象接口定义

type TwoDimensionalEntity[EID generic.Basic, PosType generic.SignedNumber] interface {
	GetTwoDimensionalEntityID() EID
	GetSpeed() float64
	GetPosition() geometry.Point[PosType]
	SetPosition(geometry.Point[PosType])
}

Position2DChangeEventHandle STRUCT

type Position2DChangeEventHandle[EID generic.Basic, PosType generic.SignedNumber] func(moving *TwoDimensional[EID, PosType], entity TwoDimensionalEntity[EID, PosType], oldX PosType)

TwoDimensionalOption STRUCT

type TwoDimensionalOption[EID generic.Basic, PosType generic.SignedNumber] func(moving *TwoDimensional[EID, PosType])