rts-sim-module/system/button_system.go
2023-08-17 18:17:34 +08:00

105 lines
2.9 KiB
Go

package system
import (
"github.com/yohamta/donburi/filter"
"joylink.club/ecs"
"joylink.club/rtsssimulation/components"
)
// 按钮系统
type ButtonSystem struct {
}
// 按钮组件
var btnIdCom, btnStaCom, btnPreOperCom, btnConOperCom = components.ComDeviceIdentity,
components.ButtonStateComponent, components.ButtonPressOperatingComponent, components.ButtonConfirmOperatingComponent
// 查询
var btnIdQuery = ecs.NewQuery(filter.Contains(btnIdCom))
var btnUpdateQuery = ecs.NewQuery(filter.Contains(btnStaCom, btnPreOperCom, btnConOperCom))
func NewButtonSystem() *ButtonSystem {
return &ButtonSystem{}
}
// 按钮操作
func PressDownAndUpButton(w ecs.World, btnId string, down bool) bool {
buttonEntry := queryButton(w, btnId)
if buttonEntry == nil {
return false
}
t, b := getButtonBaseInfo(btnId)
if !buttonEntry.HasComponent(btnPreOperCom) { // 操作组件
buttonEntry.AddComponent(btnPreOperCom)
}
preOper := btnPreOperCom.Get(buttonEntry)
preOper.Start, preOper.Down, preOper.NeedConfirm, preOper.OperateTime = true, down, b, t
return true
}
// 确认操作
func ConfirmButton(w ecs.World, btnId string, confirm bool) bool {
buttonEntry := queryButton(w, btnId)
if buttonEntry == nil {
return false
}
if !buttonEntry.HasComponent(btnConOperCom) { // 确认组件
buttonEntry.AddComponent(btnConOperCom)
}
conOper := btnConOperCom.Get(buttonEntry)
conOper.Confirm, conOper.Cancel = confirm, !confirm
return true
}
// 更新按钮状态
func (bs *ButtonSystem) Update(w ecs.World) {
btnUpdateQuery.Each(w, func(e *ecs.Entry) {
if e.HasComponent(btnPreOperCom) {
btnPreOper := btnPreOperCom.Get(e)
if btnPreOper.Start { // 按钮开始操作
if btnPreOper.OperateTime <= 0 { // 按钮操作时间大于0
btnPreOper.Start, btnPreOper.OperateTime = false, 0
} else {
btnPreOper.OperateTime -= int64(w.Tick())
if btnPreOper.NeedConfirm {
conOper := btnConOperCom.Get(e)
if e.HasComponent(btnConOperCom) && (conOper.Cancel || conOper.Confirm) {
if conOper.Confirm {
btnSta := btnStaCom.Get(e)
btnSta.PressDown = btnPreOper.Down
}
btnPreOper.Start, btnPreOper.OperateTime = false, 0
conOper.Cancel, conOper.Confirm = false, false
e.RemoveComponent(btnPreOperCom)
e.RemoveComponent(btnConOperCom)
}
} else {
btnSta := btnStaCom.Get(e)
btnSta.PressDown = btnPreOper.Down
btnPreOper.Start, btnPreOper.OperateTime = false, 0
e.RemoveComponent(btnPreOperCom)
}
}
} else {
e.RemoveComponent(btnPreOperCom)
}
}
})
}
// 查找按钮
func queryButton(w ecs.World, btnId string) *ecs.Entry {
var buttonEntry *ecs.Entry = nil
btnIdQuery.Each(w, func(e *ecs.Entry) {
if id := btnIdCom.Get(e).Id; id == btnId {
buttonEntry = e
}
})
return buttonEntry
}
// 获取按钮设置信息
func getButtonBaseInfo(btnId string) (int64, bool) {
return 15, false
}