From 01bafe6fc030a00fd9ec9a4282754aeb7e9e00bc Mon Sep 17 00:00:00 2001 From: kercylan98 Date: Sat, 1 Jul 2023 11:30:29 +0800 Subject: [PATCH] =?UTF-8?q?test:=20components.Moving2D=20=E5=A2=9E?= =?UTF-8?q?=E5=8A=A0=E7=A4=BA=E4=BE=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- component/components/moving2d_example_test.go | 72 +++++++++++++++++++ component/components/moving2d_test.go | 22 +----- 2 files changed, 74 insertions(+), 20 deletions(-) create mode 100644 component/components/moving2d_example_test.go diff --git a/component/components/moving2d_example_test.go b/component/components/moving2d_example_test.go new file mode 100644 index 0000000..52f44c0 --- /dev/null +++ b/component/components/moving2d_example_test.go @@ -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 +} diff --git a/component/components/moving2d_test.go b/component/components/moving2d_test.go index 300aa5e..c8eea13 100644 --- a/component/components/moving2d_test.go +++ b/component/components/moving2d_test.go @@ -41,29 +41,11 @@ func NewEntity(guid int64, speed float64) *MoveEntity { } } -func TestMoving2D_MoveTo(t *testing.T) { - var wait sync.WaitGroup - - moving := components.NewMoving2D(components.WithMoving2DTimeUnit(time.Second)) +func TestNewMoving2D(t *testing.T) { + moving := components.NewMoving2D() defer func() { 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) {