rts-sim-module/simulation/system/switch_system.go

68 lines
1.8 KiB
Go
Raw Normal View History

2023-08-14 18:06:26 +08:00
package system
import (
"github.com/yohamta/donburi/filter"
"joylink.club/ecs"
"joylink.club/rtsssimulation/components"
"joylink.club/rtsssimulation/state"
)
// 道岔系统操作
type SwitchSystem struct {
//正常转动道岔系统查询
turningQuery *ecs.Query
}
func NewSwitchSystem() *SwitchSystem {
return &SwitchSystem{
turningQuery: ecs.NewQuery(filter.Contains(components.ComSwitchState, components.ComSwitchTurnOperating)),
}
}
// API:触发道岔正常转动
// w: 当前世界
// switchId: 道岔id
// turnNormal: true-道岔转动到定位false-道岔转动到反位
// turnTime: 道岔转动耗时ms
2023-08-15 16:50:24 +08:00
func FireSwitchTurn(w ecs.World, switchId string, turnNormal bool, turnTime int) bool {
2023-08-14 18:06:26 +08:00
fireQuery := ecs.NewQuery(filter.Contains(components.ComDeviceIdentity, components.ComSwitchTurnOperating))
//
var turnOperating *state.SwitchTurnOperating
fireQuery.Each(w, func(e *ecs.Entry) {
if id := components.ComDeviceIdentity.Get(e).Id; id == switchId {
turnOperating = components.ComSwitchTurnOperating.Get(e)
}
})
//
if nil == turnOperating {
return false
}
//
turnOperating.TurnNormal = turnNormal
2023-08-15 16:50:24 +08:00
turnOperating.TurningTime = int64(turnTime)
2023-08-14 18:06:26 +08:00
turnOperating.StartTurn = true
return true
}
// world 执行
func (me *SwitchSystem) Update(w ecs.World) {
//道岔转动操作
me.turningQuery.Each(w, func(e *ecs.Entry) {
if turnOperting := components.ComSwitchTurnOperating.Get(e); turnOperting.StartTurn {
if turnOperting.TurningTime > 0 { //正在转动
2023-08-15 16:50:24 +08:00
turnOperting.TurningTime -= int64(w.Tick())
2023-08-14 18:06:26 +08:00
} else { //转动完成
turnOperting.StartTurn = false
state := components.ComSwitchState.Get(e)
if turnOperting.TurnNormal {
state.Normal = true
state.Reverse = false
} else {
state.Normal = false
state.Reverse = true
}
}
}
})
}