rts-sim-module/system/button_system.go

105 lines
2.9 KiB
Go
Raw Normal View History

2023-08-17 17:14:47 +08:00
package system
import (
"github.com/yohamta/donburi/filter"
"joylink.club/ecs"
"joylink.club/rtsssimulation/components"
)
// 按钮系统
type ButtonSystem struct {
}
// 按钮组件
2023-08-18 10:06:17 +08:00
var btnIdCom, btnStaCom, btnPreOperCom, btnConOperCom = components.DeviceIdentityComponent,
2023-08-17 18:17:34 +08:00
components.ButtonStateComponent, components.ButtonPressOperatingComponent, components.ButtonConfirmOperatingComponent
2023-08-17 17:14:47 +08:00
2023-08-17 18:17:34 +08:00
// 查询
var btnIdQuery = ecs.NewQuery(filter.Contains(btnIdCom))
2023-08-17 17:14:47 +08:00
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)
2023-08-17 18:17:34 +08:00
if !buttonEntry.HasComponent(btnPreOperCom) { // 操作组件
buttonEntry.AddComponent(btnPreOperCom)
}
2023-08-17 17:14:47 +08:00
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
}
2023-08-17 18:17:34 +08:00
if !buttonEntry.HasComponent(btnConOperCom) { // 确认组件
buttonEntry.AddComponent(btnConOperCom)
}
2023-08-17 17:14:47 +08:00
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) {
2023-08-17 18:17:34 +08:00
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)
2023-08-17 17:14:47 +08:00
}
2023-08-17 18:17:34 +08:00
} else {
btnSta := btnStaCom.Get(e)
btnSta.PressDown = btnPreOper.Down
2023-08-17 17:14:47 +08:00
btnPreOper.Start, btnPreOper.OperateTime = false, 0
2023-08-17 18:17:34 +08:00
e.RemoveComponent(btnPreOperCom)
2023-08-17 17:14:47 +08:00
}
}
2023-08-17 18:17:34 +08:00
} else {
e.RemoveComponent(btnPreOperCom)
2023-08-17 17:14:47 +08:00
}
}
})
}
// 查找按钮
func queryButton(w ecs.World, btnId string) *ecs.Entry {
var buttonEntry *ecs.Entry = nil
2023-08-17 18:17:34 +08:00
btnIdQuery.Each(w, func(e *ecs.Entry) {
2023-08-17 17:14:47 +08:00
if id := btnIdCom.Get(e).Id; id == btnId {
buttonEntry = e
}
})
return buttonEntry
}
// 获取按钮设置信息
func getButtonBaseInfo(btnId string) (int64, bool) {
return 15, false
}