test: components.Moving2D 测试用例优化
This commit is contained in:
parent
ce7aea5870
commit
49bc143a72
|
@ -1,8 +1,9 @@
|
||||||
package components
|
package components_test
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/kercylan98/minotaur/component"
|
"github.com/kercylan98/minotaur/component"
|
||||||
|
"github.com/kercylan98/minotaur/component/components"
|
||||||
"sync"
|
"sync"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
@ -41,28 +42,62 @@ func NewEntity(guid int64, speed float64) *MoveEntity {
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestMoving2D_MoveTo(t *testing.T) {
|
func TestMoving2D_MoveTo(t *testing.T) {
|
||||||
moving := NewMoving2D(WithMoving2DTimeUnit(time.Second))
|
|
||||||
var wait sync.WaitGroup
|
var wait sync.WaitGroup
|
||||||
|
|
||||||
|
moving := components.NewMoving2D(components.WithMoving2DTimeUnit(time.Second))
|
||||||
|
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) {
|
moving.RegPosition2DDestinationEvent(func(moving component.Moving2D, entity component.Moving2DEntity) {
|
||||||
wait.Done()
|
wait.Done()
|
||||||
})
|
})
|
||||||
var res []string
|
|
||||||
moving.RegPosition2DChangeEvent(func(moving component.Moving2D, entity component.Moving2DEntity, oldX, oldY float64) {
|
for i := 0; i < 10; i++ {
|
||||||
x, y := entity.GetPosition()
|
|
||||||
res = append(res, fmt.Sprintf("%d : %d | %f, %f > %f, %f", entity.GetGuid(), time.Now().UnixMilli(), oldX, oldY, x, y))
|
|
||||||
})
|
|
||||||
//moving.RegPosition2DChangeEvent(func(moving game.Moving2D, entity game.Moving2DEntity, oldX, oldY float64) {
|
|
||||||
// x, y := entity.GetPosition()
|
|
||||||
// fmt.Println("Moving", entity.GetGuid(), oldX, oldY, x, y)
|
|
||||||
//})
|
|
||||||
for i := 0; i < 1; i++ {
|
|
||||||
wait.Add(1)
|
wait.Add(1)
|
||||||
entity := NewEntity(int64(i)+1, float64(10+i))
|
entity := NewEntity(int64(i)+1, float64(10+i))
|
||||||
moving.MoveTo(entity, 50, 30)
|
moving.MoveTo(entity, 50, 30)
|
||||||
}
|
}
|
||||||
|
|
||||||
wait.Wait()
|
wait.Wait()
|
||||||
for _, re := range res {
|
}
|
||||||
fmt.Println(re)
|
|
||||||
}
|
func TestMoving2D_StopMove(t *testing.T) {
|
||||||
|
var wait sync.WaitGroup
|
||||||
|
|
||||||
|
moving := components.NewMoving2D(components.WithMoving2DTimeUnit(time.Second))
|
||||||
|
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) {
|
||||||
|
fmt.Println(fmt.Sprintf("%d : %d | destination", entity.GetGuid(), time.Now().UnixMilli()))
|
||||||
|
wait.Done()
|
||||||
|
})
|
||||||
|
moving.RegPosition2DStopMoveEvent(func(moving component.Moving2D, entity component.Moving2DEntity) {
|
||||||
|
fmt.Println(fmt.Sprintf("%d : %d | stop", entity.GetGuid(), time.Now().UnixMilli()))
|
||||||
|
wait.Done()
|
||||||
|
})
|
||||||
|
|
||||||
|
for i := 0; i < 10; i++ {
|
||||||
|
wait.Add(1)
|
||||||
|
entity := NewEntity(int64(i)+1, float64(10+i))
|
||||||
|
moving.MoveTo(entity, 50, 30)
|
||||||
|
}
|
||||||
|
|
||||||
|
time.Sleep(time.Second * 1)
|
||||||
|
|
||||||
|
for i := 0; i < 10; i++ {
|
||||||
|
moving.StopMove(int64(i) + 1)
|
||||||
|
}
|
||||||
|
|
||||||
|
wait.Wait()
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue