76 lines
1.7 KiB
Go
76 lines
1.7 KiB
Go
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)
|
||
}
|
||
}
|
||
}
|
||
|
||
// 暂停
|
||
func (me *MovableObject) Pause() {
|
||
me.direction = 0
|
||
}
|
||
|
||
// 前进
|
||
func (me *MovableObject) Forward() {
|
||
me.direction = 1
|
||
}
|
||
|
||
// 后退
|
||
func (me *MovableObject) Retreat() {
|
||
me.direction = -1
|
||
}
|