按钮
This commit is contained in:
parent
7bc8d247a4
commit
899deff8b0
23
entities/button_entity.go
Normal file
23
entities/button_entity.go
Normal file
@ -0,0 +1,23 @@
|
||||
package entities
|
||||
|
||||
import (
|
||||
"joylink.club/ecs"
|
||||
"joylink.club/rtsssimulation/system"
|
||||
)
|
||||
|
||||
// CreateSelfResetButtonEntity 创建自复位按钮实体
|
||||
func CreateSelfResetButtonEntity(w ecs.World, buttonId string) *ecs.Entry {
|
||||
e := w.Create(system.EntityIdentityComponent, system.ButtonStateComponent, system.ButtonSelfRestTag)
|
||||
system.EntityIdentityComponent.Set(e, &system.EntityIdentity{Id: buttonId})
|
||||
system.ButtonStateComponent.Set(e, system.NewButtonState())
|
||||
system.ButtonSelfRestTag.Set(e, &system.ButtonSelfRestState{KeepAxTime: 0})
|
||||
return e
|
||||
}
|
||||
|
||||
// CreateNonResetButtonEntity 创建非自复位按钮实体
|
||||
func CreateNonResetButtonEntity(w ecs.World, buttonId string) *ecs.Entry {
|
||||
e := w.Create(system.EntityIdentityComponent, system.ButtonStateComponent, system.ButtonNonRestTag)
|
||||
system.EntityIdentityComponent.Set(e, &system.EntityIdentity{Id: buttonId})
|
||||
system.ButtonStateComponent.Set(e, system.NewButtonState())
|
||||
return e
|
||||
}
|
@ -1,6 +1,9 @@
|
||||
package system
|
||||
|
||||
import "joylink.club/ecs"
|
||||
import (
|
||||
"github.com/yohamta/donburi/filter"
|
||||
"joylink.club/ecs"
|
||||
)
|
||||
|
||||
// ButtonState 广义按钮开关状态,开关有三个接点,分别为公共接点、常开点和常闭点
|
||||
type ButtonState struct {
|
||||
@ -8,6 +11,51 @@ type ButtonState struct {
|
||||
Ckd bool
|
||||
}
|
||||
|
||||
// ButtonSelfRestState 广义自复位按钮,复位状态
|
||||
type ButtonSelfRestState struct {
|
||||
//自复位按钮保持按下剩余时间,ms
|
||||
KeepAxTime int64
|
||||
}
|
||||
|
||||
var (
|
||||
ButtonStateComponent = ecs.NewComponentType[ButtonState]()
|
||||
ButtonSelfRestTag = ecs.NewComponentType[ButtonSelfRestState]() //自复位按钮标签
|
||||
ButtonNonRestTag = ecs.NewTag() //非自复位按钮标签
|
||||
)
|
||||
|
||||
type ButtonSystem struct {
|
||||
query *ecs.Query
|
||||
}
|
||||
|
||||
func NewButtonSystem() *ButtonSystem {
|
||||
return &ButtonSystem{query: ecs.NewQuery(filter.Contains(ButtonStateComponent))}
|
||||
}
|
||||
func NewButtonState() *ButtonState {
|
||||
return &ButtonState{Ckd: false}
|
||||
}
|
||||
|
||||
// Update world 执行
|
||||
func (me *ButtonSystem) Update(w ecs.World) {
|
||||
me.query.Each(w, func(e *ecs.Entry) {
|
||||
if e.HasComponent(ButtonSelfRestTag) {
|
||||
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
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user