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

133 lines
3.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"
)
2023-08-16 15:00:24 +08:00
// 定反操继电器查询
var nrRelayQuery *ecs.Query = ecs.NewQuery(filter.Contains(components.ComSwitchNRRelayOperating))
// 道岔转动查询
var turningQuery *ecs.Query = ecs.NewQuery(filter.Contains(components.ComSwitchTurnOperating))
// 道岔状态查询
var switchStateQuery *ecs.Query = ecs.NewQuery(filter.Contains(components.ComDeviceIdentity, components.ComSwitchState))
2023-08-14 18:06:26 +08:00
// 道岔系统操作
type SwitchSystem struct {
}
func NewSwitchSystem() *SwitchSystem {
2023-08-16 15:00:24 +08:00
return &SwitchSystem{}
2023-08-14 18:06:26 +08:00
}
2023-08-15 18:12:30 +08:00
// 触发道岔正常转动
2023-08-14 18:06:26 +08:00
// w: 当前世界
// switchId: 道岔id
// turnNormal: true-道岔转动到定位false-道岔转动到反位
2023-08-16 15:00:24 +08:00
func FireSwitchTurn(w ecs.World, switchId string, turnNormal bool) bool {
var switchEntry *ecs.Entry = nil
switchStateQuery.Each(w, func(e *ecs.Entry) {
2023-08-14 18:06:26 +08:00
if id := components.ComDeviceIdentity.Get(e).Id; id == switchId {
2023-08-16 15:00:24 +08:00
switchEntry = e
2023-08-14 18:06:26 +08:00
}
})
//
2023-08-16 15:00:24 +08:00
if nil == switchEntry {
2023-08-14 18:06:26 +08:00
return false
}
//
2023-08-16 15:00:24 +08:00
switchState := components.ComSwitchState.Get(switchEntry)
if turnNormal == switchState.NormalTable && !turnNormal == switchState.ReverseTable {
return true
}
//
nrRelayTime := getSwitchNRDelayTime(switchId)
if !switchEntry.HasComponent(components.ComSwitchNRRelayOperating) {
switchEntry.AddComponent(components.ComSwitchNRRelayOperating)
}
*(components.ComSwitchNRRelayOperating.Get(switchEntry)) = state.SwitchNRRelayOperating{Start: true, OperateTime: nrRelayTime, Close: true, Normal: turnNormal}
//
2023-08-14 18:06:26 +08:00
return true
}
// world 执行
func (me *SwitchSystem) Update(w ecs.World) {
2023-08-16 15:00:24 +08:00
updateNormalReverseRelay(w)
updateTurnOperating(w)
}
// 道岔转动动作
func updateTurnOperating(w ecs.World) {
turningQuery.Each(w, func(e *ecs.Entry) {
if !e.HasComponent(components.ComSwitchNRRelayOperating) {
operation := components.ComSwitchTurnOperating.Get(e)
switchState := components.ComSwitchState.Get(e)
if operation.Start {
if operation.OperateTime <= 0 {
operation.Start = false
switchState.NormalTable = operation.TurnNormal
switchState.ReverseTable = !operation.TurnNormal
switchState.NormalRelay = false
switchState.ReverseRelay = false
} else { //此时道岔失表
operation.OperateTime -= int64(w.Tick())
switchState.NormalTable = false
switchState.ReverseTable = false
}
}
if !operation.Start {
e.RemoveComponent(components.ComSwitchTurnOperating)
}
} else {
e.RemoveComponent(components.ComSwitchTurnOperating)
}
})
}
// 定反操继电器操作
func updateNormalReverseRelay(w ecs.World) {
nrRelayQuery.Each(w, func(e *ecs.Entry) {
operation := components.ComSwitchNRRelayOperating.Get(e)
if operation.Start {
if operation.OperateTime <= 0 {
operation.Start = false
//
switchState := components.ComSwitchState.Get(e)
if operation.Normal {
switchState.NormalRelay = operation.Close
2023-08-14 18:06:26 +08:00
} else {
2023-08-16 15:00:24 +08:00
switchState.ReverseRelay = operation.Close
2023-08-14 18:06:26 +08:00
}
2023-08-16 15:00:24 +08:00
//
if operation.Close { //触发道岔转动操作
if !e.HasComponent(components.ComSwitchTurnOperating) {
e.AddComponent(components.ComSwitchTurnOperating)
}
switchId := components.ComDeviceIdentity.Get(e).Id
turnTime := getSwitchTurnTime(switchId)
*(components.ComSwitchTurnOperating.Get(e)) = state.SwitchTurnOperating{Start: true, OperateTime: turnTime, TurnNormal: operation.Normal}
}
} else {
operation.OperateTime -= int64(w.Tick())
2023-08-14 18:06:26 +08:00
}
}
2023-08-16 15:00:24 +08:00
if !operation.Start {
e.RemoveComponent(components.ComSwitchNRRelayOperating)
}
2023-08-14 18:06:26 +08:00
})
}
2023-08-16 15:00:24 +08:00
// 获取道岔转动耗时ms
func getSwitchTurnTime(switchId string) int64 {
return 5000
}
// 获取道岔定操反操继电器耗时ms
func getSwitchNRDelayTime(switchId string) int64 {
return 2000
}