Skip to content

Commit

Permalink
refactor: 重构 moving 包实现,移除对 game.Actor、game.Position2D 等接口的依赖
Browse files Browse the repository at this point in the history
  • Loading branch information
kercylan98 committed Dec 22, 2023
1 parent d56ebde commit 0a22f6d
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 74 deletions.
63 changes: 32 additions & 31 deletions game/moving/2d.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
package moving

import (
"github.com/kercylan98/minotaur/utils/generic"
"github.com/kercylan98/minotaur/utils/geometry"
"sync"
"time"
)

// NewTwoDimensional 创建一个用于2D对象移动的实例(TwoDimensional)
func NewTwoDimensional(options ...TwoDimensionalOption) *TwoDimensional {
moving2D := &TwoDimensional{
entities: map[int64]*moving2DTarget{},
func NewTwoDimensional[EID generic.Basic, PosType generic.SignedNumber](options ...TwoDimensionalOption[EID, PosType]) *TwoDimensional[EID, PosType] {
moving2D := &TwoDimensional[EID, PosType]{
entities: map[EID]*moving2DTarget[EID, PosType]{},
timeUnit: float64(time.Millisecond),
idle: time.Millisecond * 100,
interval: time.Millisecond * 100,
Expand All @@ -25,22 +26,22 @@ func NewTwoDimensional(options ...TwoDimensionalOption) *TwoDimensional {
// - 通过对象调用 MoveTo 方法后将开始执行该对象的移动
// - 移动将在根据设置的每次移动间隔时间(WithTwoDimensionalInterval)进行移动,当无对象移动需要移动时将会进入短暂的休眠
// - 当对象移动速度永久为0时,将会导致永久无法完成的移动
type TwoDimensional struct {
type TwoDimensional[EID generic.Basic, PosType generic.SignedNumber] struct {
rw sync.RWMutex
entities map[int64]*moving2DTarget
entities map[EID]*moving2DTarget[EID, PosType]
timeUnit float64
idle time.Duration
interval time.Duration
close bool

position2DChangeEventHandles []Position2DChangeEventHandle
position2DDestinationEventHandles []Position2DDestinationEventHandle
position2DStopMoveEventHandles []Position2DStopMoveEventHandle
position2DChangeEventHandles []Position2DChangeEventHandle[EID, PosType]
position2DDestinationEventHandles []Position2DDestinationEventHandle[EID, PosType]
position2DStopMoveEventHandles []Position2DStopMoveEventHandle[EID, PosType]
}

// MoveTo 设置对象移动到特定位置
func (slf *TwoDimensional) MoveTo(entity TwoDimensionalEntity, x float64, y float64) {
guid := entity.GetGuid()
func (slf *TwoDimensional[EID, PosType]) MoveTo(entity TwoDimensionalEntity[EID, PosType], x, y PosType) {
guid := entity.GetTwoDimensionalEntityID()
current := time.Now().UnixMilli()
slf.rw.Lock()
defer slf.rw.Unlock()
Expand All @@ -49,7 +50,7 @@ func (slf *TwoDimensional) MoveTo(entity TwoDimensionalEntity, x float64, y floa
}
entityTarget, exist := slf.entities[guid]
if !exist {
entityTarget = &moving2DTarget{
entityTarget = &moving2DTarget[EID, PosType]{
TwoDimensionalEntity: entity,
x: x,
y: y,
Expand All @@ -64,63 +65,63 @@ func (slf *TwoDimensional) MoveTo(entity TwoDimensionalEntity, x float64, y floa
}

// StopMove 停止特定对象的移动
func (slf *TwoDimensional) StopMove(guid int64) {
func (slf *TwoDimensional[EID, PosType]) StopMove(id EID) {
slf.rw.Lock()
defer slf.rw.Unlock()
entity, exist := slf.entities[guid]
entity, exist := slf.entities[id]
if exist {
slf.OnPosition2DStopMoveEvent(entity)
delete(slf.entities, guid)
delete(slf.entities, id)
}
}

// RegPosition2DChangeEvent 在对象位置改变时将执行注册的事件处理函数
func (slf *TwoDimensional) RegPosition2DChangeEvent(handle Position2DChangeEventHandle) {
func (slf *TwoDimensional[EID, PosType]) RegPosition2DChangeEvent(handle Position2DChangeEventHandle[EID, PosType]) {
slf.position2DChangeEventHandles = append(slf.position2DChangeEventHandles, handle)
}

func (slf *TwoDimensional) OnPosition2DChangeEvent(entity TwoDimensionalEntity, oldX, oldY float64) {
func (slf *TwoDimensional[EID, PosType]) OnPosition2DChangeEvent(entity TwoDimensionalEntity[EID, PosType], oldX, oldY PosType) {
for _, handle := range slf.position2DChangeEventHandles {
handle(slf, entity, oldX, oldY)
}
}

// RegPosition2DDestinationEvent 在对象到达终点时将执行被注册的事件处理函数
func (slf *TwoDimensional) RegPosition2DDestinationEvent(handle Position2DDestinationEventHandle) {
func (slf *TwoDimensional[EID, PosType]) RegPosition2DDestinationEvent(handle Position2DDestinationEventHandle[EID, PosType]) {
slf.position2DDestinationEventHandles = append(slf.position2DDestinationEventHandles, handle)
}

func (slf *TwoDimensional) OnPosition2DDestinationEvent(entity TwoDimensionalEntity) {
func (slf *TwoDimensional[EID, PosType]) OnPosition2DDestinationEvent(entity TwoDimensionalEntity[EID, PosType]) {
for _, handle := range slf.position2DDestinationEventHandles {
handle(slf, entity)
}
}

// RegPosition2DStopMoveEvent 在对象停止移动时将执行被注册的事件处理函数
func (slf *TwoDimensional) RegPosition2DStopMoveEvent(handle Position2DStopMoveEventHandle) {
func (slf *TwoDimensional[EID, PosType]) RegPosition2DStopMoveEvent(handle Position2DStopMoveEventHandle[EID, PosType]) {
slf.position2DStopMoveEventHandles = append(slf.position2DStopMoveEventHandles, handle)
}

func (slf *TwoDimensional) OnPosition2DStopMoveEvent(entity TwoDimensionalEntity) {
func (slf *TwoDimensional[EID, PosType]) OnPosition2DStopMoveEvent(entity TwoDimensionalEntity[EID, PosType]) {
for _, handle := range slf.position2DStopMoveEventHandles {
handle(slf, entity)
}
}

type moving2DTarget struct {
TwoDimensionalEntity
x, y float64
type moving2DTarget[EID generic.Basic, PosType generic.SignedNumber] struct {
TwoDimensionalEntity[EID, PosType]
x, y PosType
lastMoveTime int64
}

// Release 释放对象移动对象所占用的资源
func (slf *TwoDimensional) Release() {
func (slf *TwoDimensional[EID, PosType]) Release() {
slf.rw.Lock()
defer slf.rw.Unlock()
slf.close = true
}

func (slf *TwoDimensional) handle() {
func (slf *TwoDimensional[EID, PosType]) handle() {
for {
slf.rw.Lock()
if slf.close {
Expand All @@ -129,23 +130,23 @@ func (slf *TwoDimensional) handle() {
}
for guid, entity := range slf.entities {
entity := entity
x, y := entity.GetPosition()
angle := geometry.CalcAngle(x, y, entity.x, entity.y)
x, y := entity.GetPosition().GetXY()
angle := geometry.CalcAngle(float64(x), float64(y), float64(entity.x), float64(entity.y))
moveTime := time.Now().UnixMilli()
interval := float64(moveTime - entity.lastMoveTime)
if interval == 0 {
continue
}
distance := geometry.CalcDistanceWithCoordinate(x, y, entity.x, 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)
if moveDistance >= float64(distance) || (x == entity.x && y == entity.y) {
entity.SetPosition(geometry.NewPoint(entity.x, entity.y))
delete(slf.entities, guid)
slf.OnPosition2DDestinationEvent(entity)
continue
} else {
nx, ny := geometry.CalcNewCoordinate(x, y, angle, moveDistance)
entity.SetPosition(nx, ny)
nx, ny := geometry.CalcNewCoordinate(float64(x), float64(y), angle, moveDistance)
entity.SetPosition(geometry.NewPoint(PosType(nx), PosType(ny)))
entity.lastMoveTime = moveTime
slf.OnPosition2DChangeEvent(entity, x, y)
}
Expand Down
17 changes: 11 additions & 6 deletions game/moving/2d_entity.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
package moving

import "github.com/kercylan98/minotaur/game"
import (
"github.com/kercylan98/minotaur/utils/generic"
"github.com/kercylan98/minotaur/utils/geometry"
)

// TwoDimensionalEntity 2D移动对象接口定义
type TwoDimensionalEntity interface {
game.Actor
game.Position2D
game.Position2DSet

type TwoDimensionalEntity[EID generic.Basic, PosType generic.SignedNumber] interface {
// GetTwoDimensionalEntityID 获取 Moving 对象 ID
GetTwoDimensionalEntityID() EID
// GetSpeed 获取移动速度
GetSpeed() float64
// GetPosition 获取位置
GetPosition() geometry.Point[PosType]
// SetPosition 设置位置
SetPosition(geometry.Point[PosType])
}
8 changes: 5 additions & 3 deletions game/moving/2d_events.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package moving

import "github.com/kercylan98/minotaur/utils/generic"

type (
Position2DChangeEventHandle func(moving *TwoDimensional, entity TwoDimensionalEntity, oldX, oldY float64)
Position2DDestinationEventHandle func(moving *TwoDimensional, entity TwoDimensionalEntity)
Position2DStopMoveEventHandle func(moving *TwoDimensional, entity TwoDimensionalEntity)
Position2DChangeEventHandle[EID generic.Basic, PosType generic.SignedNumber] func(moving *TwoDimensional[EID, PosType], entity TwoDimensionalEntity[EID, PosType], oldX, oldY PosType)
Position2DDestinationEventHandle[EID generic.Basic, PosType generic.SignedNumber] func(moving *TwoDimensional[EID, PosType], entity TwoDimensionalEntity[EID, PosType])
Position2DStopMoveEventHandle[EID generic.Basic, PosType generic.SignedNumber] func(moving *TwoDimensional[EID, PosType], entity TwoDimensionalEntity[EID, PosType])
)
14 changes: 7 additions & 7 deletions game/moving/2d_example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
)

func ExampleNewTwoDimensional() {
m := moving.NewTwoDimensional()
m := moving.NewTwoDimensional[int64, float64]()
defer func() {
m.Release()
}()
Expand All @@ -19,13 +19,13 @@ func ExampleNewTwoDimensional() {
}

func ExampleTwoDimensional_MoveTo() {
m := moving.NewTwoDimensional(moving.WithTwoDimensionalTimeUnit(time.Second))
m := moving.NewTwoDimensional(moving.WithTwoDimensionalTimeUnit[int64, float64](time.Second))
defer func() {
m.Release()
}()

var wait sync.WaitGroup
m.RegPosition2DDestinationEvent(func(moving *moving.TwoDimensional, entity moving.TwoDimensionalEntity) {
m.RegPosition2DDestinationEvent(func(moving *moving.TwoDimensional[int64, float64], entity moving.TwoDimensionalEntity[int64, float64]) {
fmt.Println("done")
wait.Done()
})
Expand All @@ -41,20 +41,20 @@ func ExampleTwoDimensional_MoveTo() {
}

func ExampleTwoDimensional_StopMove() {
m := moving.NewTwoDimensional(moving.WithTwoDimensionalTimeUnit(time.Second))
m := moving.NewTwoDimensional(moving.WithTwoDimensionalTimeUnit[int64, float64](time.Second))
defer func() {
m.Release()
}()

var wait sync.WaitGroup
m.RegPosition2DChangeEvent(func(moving *moving.TwoDimensional, entity moving.TwoDimensionalEntity, oldX, oldY float64) {
m.RegPosition2DChangeEvent(func(moving *moving.TwoDimensional[int64, float64], entity moving.TwoDimensionalEntity[int64, float64], oldX, oldY float64) {
fmt.Println("move")
})
m.RegPosition2DStopMoveEvent(func(moving *moving.TwoDimensional, entity moving.TwoDimensionalEntity) {
m.RegPosition2DStopMoveEvent(func(moving *moving.TwoDimensional[int64, float64], entity moving.TwoDimensionalEntity[int64, float64]) {
fmt.Println("stop")
wait.Done()
})
m.RegPosition2DDestinationEvent(func(moving *moving.TwoDimensional, entity moving.TwoDimensionalEntity) {
m.RegPosition2DDestinationEvent(func(moving *moving.TwoDimensional[int64, float64], entity moving.TwoDimensionalEntity[int64, float64]) {
fmt.Println("done")
wait.Done()
})
Expand Down
15 changes: 8 additions & 7 deletions game/moving/2d_options.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,16 @@ package moving

import (
"errors"
"github.com/kercylan98/minotaur/utils/generic"
"time"
)

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

// WithTwoDimensionalTimeUnit 通过特定时间单位创建
// - 默认单位为1毫秒,最小单位也为1毫秒
func WithTwoDimensionalTimeUnit(duration time.Duration) TwoDimensionalOption {
return func(moving *TwoDimensional) {
func WithTwoDimensionalTimeUnit[EID generic.Basic, PosType generic.SignedNumber](duration time.Duration) TwoDimensionalOption[EID, PosType] {
return func(moving *TwoDimensional[EID, PosType]) {
if duration < time.Millisecond {
panic(errors.New("time unit milliseconds minimum"))
}
Expand All @@ -20,15 +21,15 @@ func WithTwoDimensionalTimeUnit(duration time.Duration) TwoDimensionalOption {

// WithTwoDimensionalIdleWaitTime 通过特定的空闲等待时间创建
// - 默认情况下在没有新的移动计划时将限制 100毫秒 + 移动间隔事件(默认100毫秒)
func WithTwoDimensionalIdleWaitTime(duration time.Duration) TwoDimensionalOption {
return func(moving *TwoDimensional) {
func WithTwoDimensionalIdleWaitTime[EID generic.Basic, PosType generic.SignedNumber](duration time.Duration) TwoDimensionalOption[EID, PosType] {
return func(moving *TwoDimensional[EID, PosType]) {
moving.idle = duration
}
}

// WithTwoDimensionalInterval 通过特定的移动间隔时间创建
func WithTwoDimensionalInterval(duration time.Duration) TwoDimensionalOption {
return func(moving *TwoDimensional) {
func WithTwoDimensionalInterval[EID generic.Basic, PosType generic.SignedNumber](duration time.Duration) TwoDimensionalOption[EID, PosType] {
return func(moving *TwoDimensional[EID, PosType]) {
moving.interval = duration
}
}
38 changes: 18 additions & 20 deletions game/moving/2d_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,34 +3,32 @@ package moving_test
import (
"fmt"
"github.com/kercylan98/minotaur/game/moving"
"github.com/kercylan98/minotaur/utils/geometry"
"sync"
"testing"
"time"
)

type MoveEntity struct {
guid int64
x, y float64
pos geometry.Point[float64]
speed float64
}

func (slf *MoveEntity) SetGuid(guid int64) {
}

func (slf *MoveEntity) GetGuid() int64 {
func (slf *MoveEntity) GetTwoDimensionalEntityID() int64 {
return slf.guid
}

func (slf *MoveEntity) GetPosition() (x, y float64) {
return slf.x, slf.y
func (slf *MoveEntity) GetSpeed() float64 {
return slf.speed
}

func (slf *MoveEntity) SetPosition(x, y float64) {
slf.x, slf.y = x, y
func (slf *MoveEntity) GetPosition() geometry.Point[float64] {
return slf.pos
}

func (slf *MoveEntity) GetSpeed() float64 {
return slf.speed
func (slf *MoveEntity) SetPosition(pos geometry.Point[float64]) {
slf.pos = pos
}

func NewEntity(guid int64, speed float64) *MoveEntity {
Expand All @@ -41,7 +39,7 @@ func NewEntity(guid int64, speed float64) *MoveEntity {
}

func TestNewTwoDimensional(t *testing.T) {
m := moving.NewTwoDimensional()
m := moving.NewTwoDimensional[int64, float64]()
defer func() {
m.Release()
}()
Expand All @@ -50,21 +48,21 @@ func TestNewTwoDimensional(t *testing.T) {
func TestTwoDimensional_StopMove(t *testing.T) {
var wait sync.WaitGroup

m := moving.NewTwoDimensional(moving.WithTwoDimensionalTimeUnit(time.Second))
m := moving.NewTwoDimensional(moving.WithTwoDimensionalTimeUnit[int64, float64](time.Second))
defer func() {
m.Release()
}()

m.RegPosition2DChangeEvent(func(moving *moving.TwoDimensional, entity moving.TwoDimensionalEntity, 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))
m.RegPosition2DChangeEvent(func(moving *moving.TwoDimensional[int64, float64], entity moving.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 *moving.TwoDimensional, entity moving.TwoDimensionalEntity) {
fmt.Println(fmt.Sprintf("%d : %d | destination", entity.GetGuid(), time.Now().UnixMilli()))
m.RegPosition2DDestinationEvent(func(moving *moving.TwoDimensional[int64, float64], entity moving.TwoDimensionalEntity[int64, float64]) {
fmt.Println(fmt.Sprintf("%d : %d | destination", entity.GetTwoDimensionalEntityID(), time.Now().UnixMilli()))
wait.Done()
})
m.RegPosition2DStopMoveEvent(func(moving *moving.TwoDimensional, entity moving.TwoDimensionalEntity) {
fmt.Println(fmt.Sprintf("%d : %d | stop", entity.GetGuid(), time.Now().UnixMilli()))
m.RegPosition2DStopMoveEvent(func(moving *moving.TwoDimensional[int64, float64], entity moving.TwoDimensionalEntity[int64, float64]) {
fmt.Println(fmt.Sprintf("%d : %d | stop", entity.GetTwoDimensionalEntityID(), time.Now().UnixMilli()))
wait.Done()
})

Expand Down

0 comments on commit 0a22f6d

Please sign in to comment.