rts-sim-module/system/move_system.go
2023-09-14 13:52:36 +08:00

76 lines
1.6 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"
)
// 时间维度上移动的物体
type MovableObject struct {
Value int64
//移动方向0-不动1-变大,-1 -变小
direction int8
}
func NewMovableObject() *MovableObject {
return &MovableObject{Value: 0, direction: 0}
}
// 时间维度上移动物体组件
var MovableObjectComponent = ecs.NewComponentType[MovableObject]()
var MovableObject1Component = ecs.NewComponentType[MovableObject]()
var MovableObject2Component = ecs.NewComponentType[MovableObject]()
type TimeMovableSystem struct {
query *ecs.Query
}
func NewTimeMovableSystem() *TimeMovableSystem {
return &TimeMovableSystem{
query: ecs.NewQuery(filter.Or(filter.Contains(MovableObjectComponent), filter.Contains(MovableObject1Component), filter.Contains(MovableObject2Component))),
}
}
// world 执行
func (me *TimeMovableSystem) 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)
}
}
}
// 暂停
func (me *MovableObject) Pause() {
me.direction = 0
}
// 前进
func (me *MovableObject) Forward() {
me.direction = 1
}
// 后退
func (me *MovableObject) Retreat() {
me.direction = -1
}