rts-sim-module/system/move_system.go

75 lines
1.7 KiB
Go
Raw Normal View History

2023-09-14 13:52:36 +08:00
package system
import (
"github.com/yohamta/donburi/filter"
"joylink.club/ecs"
)
2023-09-15 13:48:48 +08:00
// MovableObject 一维移动的物体
2023-09-14 13:52:36 +08:00
type MovableObject struct {
Value int64
//移动方向0-不动1-变大,-1 -变小
direction int8
}
func NewMovableObject() *MovableObject {
return &MovableObject{Value: 0, direction: 0}
}
2023-09-15 13:48:48 +08:00
// MovableObjectComponent 一维移动的物体组件
2023-09-14 13:52:36 +08:00
var MovableObjectComponent = ecs.NewComponentType[MovableObject]()
var MovableObject1Component = ecs.NewComponentType[MovableObject]()
var MovableObject2Component = ecs.NewComponentType[MovableObject]()
2023-09-15 13:07:31 +08:00
type MovableSystem struct {
2023-09-14 13:52:36 +08:00
query *ecs.Query
}
2023-09-15 13:07:31 +08:00
func NewMovableSystem() *MovableSystem {
return &MovableSystem{
2023-09-14 13:52:36 +08:00
query: ecs.NewQuery(filter.Or(filter.Contains(MovableObjectComponent), filter.Contains(MovableObject1Component), filter.Contains(MovableObject2Component))),
}
}
2023-09-15 13:48:48 +08:00
// Update world 执行
2023-09-15 13:07:31 +08:00
func (me *MovableSystem) Update(w ecs.World) {
2023-09-14 13:52:36 +08:00
me.query.Each(w, func(e *ecs.Entry) {
if e.HasComponent(MovableObjectComponent) {
MovableObjectComponent.Get(e).move(w.Tick())
}
if e.HasComponent(MovableObject1Component) {
MovableObject1Component.Get(e).move(w.Tick())
}
if e.HasComponent(MovableObject2Component) {
MovableObject2Component.Get(e).move(w.Tick())
}
})
}
////////////////////////////////////////////////
func (me *MovableObject) move(tick int) {
if me.direction != 0 {
if me.direction > 0 {
me.Value += int64(tick)
} else {
me.Value -= int64(tick)
}
}
}
2023-09-18 16:11:16 +08:00
// Pause 暂停
2023-09-14 13:52:36 +08:00
func (me *MovableObject) Pause() {
me.direction = 0
}
2023-09-18 16:11:16 +08:00
// Forward 前进
2023-09-14 13:52:36 +08:00
func (me *MovableObject) Forward() {
me.direction = 1
}
2023-09-18 16:11:16 +08:00
// Retreat 后退
2023-09-14 13:52:36 +08:00
func (me *MovableObject) Retreat() {
me.direction = -1
}