88 lines
2.3 KiB
Go
88 lines
2.3 KiB
Go
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
|
||
}
|