test: components.Moving2D 增加示例
This commit is contained in:
parent
49bc143a72
commit
01bafe6fc0
|
@ -0,0 +1,72 @@
|
||||||
|
package components_test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/kercylan98/minotaur/component"
|
||||||
|
"github.com/kercylan98/minotaur/component/components"
|
||||||
|
"sync"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
func ExampleNewMoving2D() {
|
||||||
|
moving := components.NewMoving2D()
|
||||||
|
defer func() {
|
||||||
|
moving.Release()
|
||||||
|
}()
|
||||||
|
fmt.Println(moving != nil)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// true
|
||||||
|
}
|
||||||
|
|
||||||
|
func ExampleMoving2D_MoveTo() {
|
||||||
|
moving := components.NewMoving2D(components.WithMoving2DTimeUnit(time.Second))
|
||||||
|
defer func() {
|
||||||
|
moving.Release()
|
||||||
|
}()
|
||||||
|
|
||||||
|
var wait sync.WaitGroup
|
||||||
|
moving.RegPosition2DDestinationEvent(func(moving component.Moving2D, entity component.Moving2DEntity) {
|
||||||
|
fmt.Println("done")
|
||||||
|
wait.Done()
|
||||||
|
})
|
||||||
|
|
||||||
|
wait.Add(1)
|
||||||
|
entity := NewEntity(1, 100)
|
||||||
|
moving.MoveTo(entity, 50, 30)
|
||||||
|
|
||||||
|
wait.Wait()
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// done
|
||||||
|
}
|
||||||
|
|
||||||
|
func ExampleMoving2D_StopMove() {
|
||||||
|
moving := components.NewMoving2D(components.WithMoving2DTimeUnit(time.Second))
|
||||||
|
defer func() {
|
||||||
|
moving.Release()
|
||||||
|
}()
|
||||||
|
|
||||||
|
var wait sync.WaitGroup
|
||||||
|
moving.RegPosition2DChangeEvent(func(moving component.Moving2D, entity component.Moving2DEntity, oldX, oldY float64) {
|
||||||
|
fmt.Println("move")
|
||||||
|
})
|
||||||
|
moving.RegPosition2DStopMoveEvent(func(moving component.Moving2D, entity component.Moving2DEntity) {
|
||||||
|
fmt.Println("stop")
|
||||||
|
wait.Done()
|
||||||
|
})
|
||||||
|
moving.RegPosition2DDestinationEvent(func(moving component.Moving2D, entity component.Moving2DEntity) {
|
||||||
|
fmt.Println("done")
|
||||||
|
wait.Done()
|
||||||
|
})
|
||||||
|
|
||||||
|
wait.Add(1)
|
||||||
|
entity := NewEntity(1, 100)
|
||||||
|
moving.MoveTo(entity, 50, 300)
|
||||||
|
moving.StopMove(1)
|
||||||
|
|
||||||
|
wait.Wait()
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// stop
|
||||||
|
}
|
|
@ -41,29 +41,11 @@ func NewEntity(guid int64, speed float64) *MoveEntity {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestMoving2D_MoveTo(t *testing.T) {
|
func TestNewMoving2D(t *testing.T) {
|
||||||
var wait sync.WaitGroup
|
moving := components.NewMoving2D()
|
||||||
|
|
||||||
moving := components.NewMoving2D(components.WithMoving2DTimeUnit(time.Second))
|
|
||||||
defer func() {
|
defer func() {
|
||||||
moving.Release()
|
moving.Release()
|
||||||
}()
|
}()
|
||||||
|
|
||||||
moving.RegPosition2DChangeEvent(func(moving component.Moving2D, entity component.Moving2DEntity, oldX, oldY float64) {
|
|
||||||
x, y := entity.GetPosition()
|
|
||||||
fmt.Println(fmt.Sprintf("%d : %d | %f, %f > %f, %f", entity.GetGuid(), time.Now().UnixMilli(), oldX, oldY, x, y))
|
|
||||||
})
|
|
||||||
moving.RegPosition2DDestinationEvent(func(moving component.Moving2D, entity component.Moving2DEntity) {
|
|
||||||
wait.Done()
|
|
||||||
})
|
|
||||||
|
|
||||||
for i := 0; i < 10; i++ {
|
|
||||||
wait.Add(1)
|
|
||||||
entity := NewEntity(int64(i)+1, float64(10+i))
|
|
||||||
moving.MoveTo(entity, 50, 30)
|
|
||||||
}
|
|
||||||
|
|
||||||
wait.Wait()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestMoving2D_StopMove(t *testing.T) {
|
func TestMoving2D_StopMove(t *testing.T) {
|
||||||
|
|
Loading…
Reference in New Issue