rts-sim-module/system/switch_system.go

69 lines
2.0 KiB
Go
Raw Normal View History

2023-08-28 13:51:17 +08:00
package system
import (
"github.com/yohamta/donburi/filter"
"joylink.club/ecs"
"joylink.club/rtsssimulation/components"
2023-08-28 15:38:21 +08:00
"joylink.club/rtsssimulation/umi"
2023-08-28 13:51:17 +08:00
)
2023-08-29 16:22:39 +08:00
var SwitchQuery *ecs.Query = ecs.NewQuery(filter.Contains(components.SwitchRelayStateComponent, components.PercentageDeviceStateComponent))
2023-08-28 13:51:17 +08:00
const (
//道岔定位
SwitchNormalVaule int64 = PercentageRateValueMin
//道岔反位
SwitchReverseValue int64 = PercentageRateValueMax
)
// 道岔系统操作
type SwitchSystem struct {
}
func NewSwitchSystem() *SwitchSystem {
2023-08-28 15:38:21 +08:00
return &SwitchSystem{}
2023-08-28 13:51:17 +08:00
}
// world 执行
func (me *SwitchSystem) Update(w ecs.World) {
2023-08-28 15:38:21 +08:00
//根据定操反操继电器来设置道岔转动参数
SwitchQuery.Each(w, func(e *ecs.Entry) {
relay := components.SwitchRelayStateComponent.Get(e)
if relay.DcJ || relay.FcJ {
2023-08-28 13:51:17 +08:00
movable := components.MovableDeviceStateComponent.Get(e)
2023-08-28 15:38:21 +08:00
percent := components.PercentageDeviceStateComponent.Get(e)
switchId := components.DeviceIdentityComponent.Get(e).Id
if relay.DcJ {
2023-08-28 13:51:17 +08:00
movable.ToH = false
percent.Target = SwitchNormalVaule
}
2023-08-28 15:38:21 +08:00
if relay.FcJ {
2023-08-28 13:51:17 +08:00
movable.ToH = true
percent.Target = SwitchReverseValue
}
2023-08-28 15:38:21 +08:00
movable.Speed = CalculateRateSpeed(int32(w.Tick()), getSwitchTurnTime(w, switchId))
2023-08-28 13:51:17 +08:00
}
})
//观察道岔百分比组件来更新道岔逻辑继电器状态
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
}
})
}
2023-08-28 15:38:21 +08:00
// 获取道岔转动耗时ms
func getSwitchTurnTime(world ecs.World, switchId string) int64 {
dcModel := WorldModelStorage(world).FindById(switchId)
2023-08-28 16:33:00 +08:00
var dc umi.ISwitchModel = dcModel.(umi.ISwitchModel)
return dc.TurningTime()
2023-08-28 15:38:21 +08:00
}