76 lines
1.6 KiB
Go
76 lines
1.6 KiB
Go
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
|
||
}
|