rts-sim-module/system/move_system.go
2023-09-18 16:11:16 +08:00

75 lines
1.7 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package system
import (
"github.com/yohamta/donburi/filter"
"joylink.club/ecs"
)
// MovableObject 一维移动的物体
type MovableObject struct {
Value int64
//移动方向0-不动1-变大,-1 -变小
direction int8
}
func NewMovableObject() *MovableObject {
return &MovableObject{Value: 0, direction: 0}
}
// MovableObjectComponent 一维移动的物体组件
var MovableObjectComponent = ecs.NewComponentType[MovableObject]()
var MovableObject1Component = ecs.NewComponentType[MovableObject]()
var MovableObject2Component = ecs.NewComponentType[MovableObject]()
type MovableSystem struct {
query *ecs.Query
}
func NewMovableSystem() *MovableSystem {
return &MovableSystem{
query: ecs.NewQuery(filter.Or(filter.Contains(MovableObjectComponent), filter.Contains(MovableObject1Component), filter.Contains(MovableObject2Component))),
}
}
// Update world 执行
func (me *MovableSystem) Update(w ecs.World) {
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)
}
}
}
// Pause 暂停
func (me *MovableObject) Pause() {
me.direction = 0
}
// Forward 前进
func (me *MovableObject) Forward() {
me.direction = 1
}
// Retreat 后退
func (me *MovableObject) Retreat() {
me.direction = -1
}