rts-sim-module/deprecated/system/button_system.go
walker 0bba8f0934 删除donburi包引用
修改filter导入为从ecs项目导入
整理包结构,将弃用的包放入deprecated文件中
2023-10-09 11:17:25 +08:00

60 lines
1.5 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 (
"joylink.club/ecs"
"joylink.club/ecs/filter"
)
// ButtonState 广义按钮开关状态,开关有三个接点,分别为公共接点、常开点和常闭点
type ButtonState struct {
//true-公共接点接通常开点false-公共接点接通常闭点
Ckd bool
}
// ButtonSelfRestState 广义自复位按钮,复位状态
type ButtonSelfRestState struct {
//自复位按钮保持按下剩余时间ms
KeepAxTime int64
}
var (
ButtonStateComponent = ecs.NewComponentType[ButtonState]()
ButtonSelfRestTag = ecs.NewComponentType[ButtonSelfRestState]() //自复位按钮标签
ButtonNonRestTag = ecs.NewTag() //非自复位按钮标签
)
type ButtonSystem struct {
selfRestQuery *ecs.Query
}
func NewButtonSystem() *ButtonSystem {
return &ButtonSystem{selfRestQuery: ecs.NewQuery(filter.Contains(ButtonStateComponent, ButtonSelfRestTag))}
}
func NewButtonState() *ButtonState {
return &ButtonState{Ckd: false}
}
// Update world 执行
func (me *ButtonSystem) Update(w ecs.World) {
me.selfRestQuery.Each(w, func(e *ecs.Entry) {
state := ButtonStateComponent.Get(e)
me.calculateSelfRest(w, e, state)
})
}
// 自复位按钮运算
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
}
}
}