rts-sim-module/system/switch_system.go
2023-08-28 15:38:21 +08:00

75 lines
2.1 KiB
Go

package system
import (
"github.com/yohamta/donburi/filter"
"joylink.club/ecs"
"joylink.club/rtsssimulation/components"
"joylink.club/rtsssimulation/umi"
)
var SwitchQuery *ecs.Query = ecs.NewQuery(filter.Contains(components.DeviceIdentityComponent, components.SwitchRelayStateComponent, components.PercentageDeviceStateComponent))
const (
//道岔定位
SwitchNormalVaule int64 = PercentageRateValueMin
//道岔反位
SwitchReverseValue int64 = PercentageRateValueMax
)
// 道岔系统操作
type SwitchSystem struct {
//道岔定反操控制查询
queryDfc *ecs.Query
}
func NewSwitchSystem() *SwitchSystem {
return &SwitchSystem{}
}
// world 执行
func (me *SwitchSystem) Update(w ecs.World) {
//根据定操反操继电器来设置道岔转动参数
SwitchQuery.Each(w, func(e *ecs.Entry) {
relay := components.SwitchRelayStateComponent.Get(e)
if relay.DcJ || relay.FcJ {
movable := components.MovableDeviceStateComponent.Get(e)
percent := components.PercentageDeviceStateComponent.Get(e)
switchId := components.DeviceIdentityComponent.Get(e).Id
if relay.DcJ {
movable.ToH = false
percent.Target = SwitchNormalVaule
}
if relay.FcJ {
movable.ToH = true
percent.Target = SwitchReverseValue
}
movable.Speed = CalculateRateSpeed(int32(w.Tick()), getSwitchTurnTime(w, switchId))
}
})
//观察道岔百分比组件来更新道岔逻辑继电器状态
SwitchQuery.Each(w, func(e *ecs.Entry) {
switchPercent := components.PercentageDeviceStateComponent.Get(e)
switchRelay := components.SwitchRelayStateComponent.Get(e)
if switchPercent.Rate == switchPercent.Target {
switchRelay.DcJ = false
switchRelay.FcJ = false
switchRelay.DbJ = switchPercent.Rate <= SwitchNormalVaule
switchRelay.FbJ = switchPercent.Rate >= SwitchReverseValue
} else {
switchRelay.DbJ = false
switchRelay.FbJ = false
}
})
}
// 获取道岔转动耗时ms
func getSwitchTurnTime(world ecs.World, switchId string) int64 {
dcModel := WorldModelStorage(world).FindById(switchId)
if nil != dcModel {
var dc umi.ISwitchModel = dcModel.(umi.ISwitchModel)
return dc.TurningTime()
}
//不存在返回默认值
return 5000
}