rts-sim-module/system/switch_system.go

88 lines
2.3 KiB
Go
Raw Normal View History

2023-08-14 18:06:26 +08:00
package system
import (
2023-08-18 14:40:13 +08:00
"fmt"
"github.com/yohamta/donburi/filter"
2023-08-14 18:06:26 +08:00
"joylink.club/ecs"
2023-08-18 14:40:13 +08:00
"joylink.club/rtsssimulation/components"
)
var switchQuery *ecs.Query = ecs.NewQuery(filter.Contains(components.DeviceIdentityComponent, components.SwitchRelayStateComponent, components.PercentageDeviceComponent))
const (
//道岔定位
SwitchNormalRate int32 = 0
//道岔反位
SwitchReverseRate int32 = 100
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
2023-08-18 14:40:13 +08:00
// terminalRate: 道岔转动到百分比规定定位百分比为0反位百分比为100
func FireSwitchTurn(w ecs.World, switchId string, terminalRate int32) error {
if terminalRate < 0 || terminalRate > 100 {
return fmt.Errorf("道岔转动终点百分比[%d]不在范围[0,100]内", terminalRate)
}
var switchEntry *ecs.Entry = nil
switchQuery.Each(w, func(e *ecs.Entry) {
if switchId == components.DeviceIdentityComponent.Get(e).Id {
switchEntry = e
}
})
if switchEntry == nil {
return fmt.Errorf("道岔[%s]的实体不存在", switchId)
}
//
switchRelay := components.SwitchRelayStateComponent.Get(switchEntry)
switchRate := components.PercentageDeviceComponent.Get(switchEntry)
if terminalRate == switchRate.Rate {
return nil
}
switchRelay.DcJ = terminalRate < switchRate.Rate
switchRelay.FcJ = terminalRate > switchRate.Rate
switchRelay.DbJ = false
switchRelay.FbJ = false
//
lhDistance := getSwitchTurnTime(switchId)
return FirePercentageDeviceOperation(w, switchEntry, lhDistance, terminalRate)
2023-08-14 18:06:26 +08:00
}
// world 执行
func (me *SwitchSystem) Update(w ecs.World) {
2023-08-18 14:40:13 +08:00
switchQuery.Each(w, func(e *ecs.Entry) {
switchRate := components.PercentageDeviceComponent.Get(e)
switchRelay := components.SwitchRelayStateComponent.Get(e)
if switchRate.Rate <= 0 {
switchRelay.DcJ = false
switchRelay.DbJ = true
}
if switchRate.Rate >= 100 {
switchRelay.FcJ = false
switchRelay.FbJ = true
}
//过了操作时限,定操反操继电器自动复位
if !e.HasComponent(components.PercentageDeviceOperatingComponent) {
switchRelay.DcJ = false
switchRelay.FcJ = false
} else {
switchRelay.DbJ = false
switchRelay.FbJ = false
}
})
2023-08-14 18:06:26 +08:00
}
2023-08-16 15:00:24 +08:00
// 获取道岔转动耗时ms
func getSwitchTurnTime(switchId string) int64 {
return 5000
}