rts-sim-module/system/switch_system.go
2023-08-18 14:40:13 +08:00

88 lines
2.3 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package system
import (
"fmt"
"github.com/yohamta/donburi/filter"
"joylink.club/ecs"
"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
)
// 道岔系统操作
type SwitchSystem struct {
}
func NewSwitchSystem() *SwitchSystem {
return &SwitchSystem{}
}
// 触发道岔正常转动
// w: 当前世界
// switchId: 道岔id
// 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)
}
// world 执行
func (me *SwitchSystem) Update(w ecs.World) {
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
}
})
}
// 获取道岔转动耗时ms
func getSwitchTurnTime(switchId string) int64 {
return 5000
}