2023-09-25 15:39:03 +08:00
|
|
|
|
package system
|
|
|
|
|
|
2023-09-27 16:16:51 +08:00
|
|
|
|
import (
|
|
|
|
|
"github.com/yohamta/donburi/filter"
|
|
|
|
|
"joylink.club/ecs"
|
|
|
|
|
)
|
2023-09-26 11:29:50 +08:00
|
|
|
|
|
2023-09-26 10:58:56 +08:00
|
|
|
|
// ButtonState 广义按钮开关状态,开关有三个接点,分别为公共接点、常开点和常闭点
|
2023-09-25 15:39:03 +08:00
|
|
|
|
type ButtonState struct {
|
2023-09-26 11:29:50 +08:00
|
|
|
|
//true-公共接点接通常开点,false-公共接点接通常闭点
|
2023-09-26 10:58:56 +08:00
|
|
|
|
Ckd bool
|
2023-09-25 15:39:03 +08:00
|
|
|
|
}
|
2023-09-26 11:29:50 +08:00
|
|
|
|
|
2023-09-27 16:16:51 +08:00
|
|
|
|
// ButtonSelfRestState 广义自复位按钮,复位状态
|
|
|
|
|
type ButtonSelfRestState struct {
|
|
|
|
|
//自复位按钮保持按下剩余时间,ms
|
|
|
|
|
KeepAxTime int64
|
|
|
|
|
}
|
|
|
|
|
|
2023-09-26 11:29:50 +08:00
|
|
|
|
var (
|
|
|
|
|
ButtonStateComponent = ecs.NewComponentType[ButtonState]()
|
2023-09-27 16:16:51 +08:00
|
|
|
|
ButtonSelfRestTag = ecs.NewComponentType[ButtonSelfRestState]() //自复位按钮标签
|
|
|
|
|
ButtonNonRestTag = ecs.NewTag() //非自复位按钮标签
|
2023-09-26 11:29:50 +08:00
|
|
|
|
)
|
2023-09-27 16:16:51 +08:00
|
|
|
|
|
|
|
|
|
type ButtonSystem struct {
|
2023-09-27 17:08:11 +08:00
|
|
|
|
selfRestQuery *ecs.Query
|
2023-09-27 16:16:51 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func NewButtonSystem() *ButtonSystem {
|
2023-09-27 17:08:11 +08:00
|
|
|
|
return &ButtonSystem{selfRestQuery: ecs.NewQuery(filter.Contains(ButtonStateComponent, ButtonSelfRestTag))}
|
2023-09-27 16:16:51 +08:00
|
|
|
|
}
|
|
|
|
|
func NewButtonState() *ButtonState {
|
|
|
|
|
return &ButtonState{Ckd: false}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Update world 执行
|
|
|
|
|
func (me *ButtonSystem) Update(w ecs.World) {
|
2023-09-27 17:08:11 +08:00
|
|
|
|
me.selfRestQuery.Each(w, func(e *ecs.Entry) {
|
|
|
|
|
state := ButtonStateComponent.Get(e)
|
|
|
|
|
me.calculateSelfRest(w, e, state)
|
2023-09-27 16:16:51 +08:00
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 自复位按钮运算
|
|
|
|
|
func (me *ButtonSystem) calculateSelfRest(w ecs.World, e *ecs.Entry, state *ButtonState) {
|
|
|
|
|
if state.Ckd {
|
|
|
|
|
reset := ButtonSelfRestTag.Get(e)
|
|
|
|
|
if reset.KeepAxTime > 0 {
|
|
|
|
|
reset.KeepAxTime -= int64(w.Tick())
|
|
|
|
|
if reset.KeepAxTime < 0 {
|
|
|
|
|
reset.KeepAxTime = 0
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if reset.KeepAxTime <= 0 {
|
|
|
|
|
state.Ckd = false
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|