移动功能优化,移动耗时bug问题处理,测试函数

This commit is contained in:
kercylan98 2023-06-03 15:33:44 +08:00
parent e4df8d2693
commit 5a6abb199a
3 changed files with 88 additions and 17 deletions

View File

@ -12,17 +12,12 @@ func NewMoving2D(options ...Moving2DOption) *Moving2D {
entities: map[int64]*moving2DTarget{},
timeUnit: float64(time.Millisecond),
idle: time.Millisecond * 100,
event: make(chan func(), 1000),
interval: time.Millisecond * 100,
}
for _, option := range options {
option(moving2D)
}
go moving2D.handle()
go func() {
for event := range moving2D.event {
event()
}
}()
return moving2D
}
@ -31,6 +26,7 @@ type Moving2D struct {
entities map[int64]*moving2DTarget
timeUnit float64
idle time.Duration
interval time.Duration
event chan func()
close bool
@ -114,25 +110,25 @@ func (slf *Moving2D) handle() {
angle := g2d.CalcAngle(x, y, entity.x, entity.y)
moveTime := time.Now().UnixMilli()
interval := float64(moveTime - entity.lastMoveTime)
if interval == 0 {
continue
}
distance := g2d.CalcDistance(x, y, entity.x, entity.y)
moveDistance := interval * entity.GetSpeed() / slf.timeUnit
if moveDistance > distance || (x == entity.x && y == entity.y) {
moveDistance := interval * (entity.GetSpeed() / (slf.timeUnit / 1000 / 1000))
if moveDistance >= distance || (x == entity.x && y == entity.y) {
entity.SetPosition(entity.x, entity.y)
delete(slf.entities, guid)
slf.event <- func() {
slf.OnPosition2DDestinationEvent(entity)
}
return
slf.OnPosition2DDestinationEvent(entity)
continue
} else {
nx, ny := g2d.CalculateNewCoordinate(x, y, angle, distance)
nx, ny := g2d.CalculateNewCoordinate(x, y, angle, moveDistance)
entity.SetPosition(nx, ny)
entity.lastMoveTime = moveTime
slf.event <- func() {
slf.OnPosition2DChangeEvent(entity, x, y)
}
slf.OnPosition2DChangeEvent(entity, x, y)
}
}
time.Sleep(slf.interval)
if len(slf.entities) == 0 {
slf.rw.Unlock()
time.Sleep(slf.idle)

View File

@ -19,9 +19,16 @@ func WithMoving2DTimeUnit(duration time.Duration) Moving2DOption {
}
// WithMoving2DIdleWaitTime 通过特定的空闲等待时间创建
// - 默认情况下在没有新的移动计划时将限制 100 毫秒
// - 默认情况下在没有新的移动计划时将限制 100毫秒 + 移动间隔事件(默认100毫秒)
func WithMoving2DIdleWaitTime(duration time.Duration) Moving2DOption {
return func(moving *Moving2D) {
moving.idle = duration
}
}
// WithMoving2DInterval 通过特定的移动间隔时间创建
func WithMoving2DInterval(duration time.Duration) Moving2DOption {
return func(moving *Moving2D) {
moving.interval = duration
}
}

View File

@ -0,0 +1,68 @@
package builtin
import (
"fmt"
"github.com/kercylan98/minotaur/game"
"sync"
"testing"
"time"
)
type MoveEntity struct {
guid int64
x, y float64
speed float64
}
func (slf *MoveEntity) SetGuid(guid int64) {
}
func (slf *MoveEntity) GetGuid() int64 {
return slf.guid
}
func (slf *MoveEntity) GetPosition() (x, y float64) {
return slf.x, slf.y
}
func (slf *MoveEntity) SetPosition(x, y float64) {
slf.x, slf.y = x, y
}
func (slf *MoveEntity) GetSpeed() float64 {
return slf.speed
}
func NewEntity(guid int64, speed float64) *MoveEntity {
return &MoveEntity{
guid: guid,
speed: speed,
}
}
func TestMoving2D_MoveTo(t *testing.T) {
moving := NewMoving2D(WithMoving2DTimeUnit(time.Second))
var wait sync.WaitGroup
moving.RegPosition2DDestinationEvent(func(moving game.Moving2D, entity game.Moving2DEntity) {
wait.Done()
})
var res []string
moving.RegPosition2DChangeEvent(func(moving game.Moving2D, entity game.Moving2DEntity, oldX, oldY float64) {
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)
entity := NewEntity(int64(i)+1, float64(10+i))
moving.MoveTo(entity, 50, 30)
}
wait.Wait()
for _, re := range res {
fmt.Println(re)
}
}