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 } } }