package system import ( "github.com/yohamta/donburi/filter" "joylink.club/ecs" "joylink.club/rtsssimulation/components" "joylink.club/rtsssimulation/state" ) // 定反操继电器查询 var nrRelayQuery *ecs.Query = ecs.NewQuery(filter.Contains(components.ComSwitchNRRelayOperating)) // 道岔转动查询 var turningQuery *ecs.Query = ecs.NewQuery(filter.Contains(components.ComSwitchTurnOperating)) // 道岔状态查询 var switchStateQuery *ecs.Query = ecs.NewQuery(filter.Contains(components.ComDeviceIdentity, components.ComSwitchState)) // 道岔系统操作 type SwitchSystem struct { } func NewSwitchSystem() *SwitchSystem { return &SwitchSystem{} } // 触发道岔正常转动 // w: 当前世界 // switchId: 道岔id // turnNormal: true-道岔转动到定位,false-道岔转动到反位 func FireSwitchTurn(w ecs.World, switchId string, turnNormal bool) bool { var switchEntry *ecs.Entry = nil switchStateQuery.Each(w, func(e *ecs.Entry) { if id := components.ComDeviceIdentity.Get(e).Id; id == switchId { switchEntry = e } }) // if nil == switchEntry { return false } // switchState := components.ComSwitchState.Get(switchEntry) if turnNormal == switchState.NormalTable && !turnNormal == switchState.ReverseTable { return true } // nrRelayTime := getSwitchNRDelayTime(switchId) if !switchEntry.HasComponent(components.ComSwitchNRRelayOperating) { switchEntry.AddComponent(components.ComSwitchNRRelayOperating) } *(components.ComSwitchNRRelayOperating.Get(switchEntry)) = state.SwitchNRRelayOperating{Start: true, OperateTime: nrRelayTime, Close: true, Normal: turnNormal} // return true } // world 执行 func (me *SwitchSystem) Update(w ecs.World) { updateNormalReverseRelay(w) updateTurnOperating(w) } // 道岔转动动作 func updateTurnOperating(w ecs.World) { turningQuery.Each(w, func(e *ecs.Entry) { if !e.HasComponent(components.ComSwitchNRRelayOperating) { operation := components.ComSwitchTurnOperating.Get(e) switchState := components.ComSwitchState.Get(e) if operation.Start { if operation.OperateTime <= 0 { operation.Start = false switchState.NormalTable = operation.TurnNormal switchState.ReverseTable = !operation.TurnNormal switchState.NormalRelay = false switchState.ReverseRelay = false } else { //此时道岔失表 operation.OperateTime -= int64(w.Tick()) switchState.NormalTable = false switchState.ReverseTable = false } } if !operation.Start { e.RemoveComponent(components.ComSwitchTurnOperating) } } else { e.RemoveComponent(components.ComSwitchTurnOperating) } }) } // 定反操继电器操作 func updateNormalReverseRelay(w ecs.World) { nrRelayQuery.Each(w, func(e *ecs.Entry) { operation := components.ComSwitchNRRelayOperating.Get(e) if operation.Start { if operation.OperateTime <= 0 { operation.Start = false // switchState := components.ComSwitchState.Get(e) if operation.Normal { switchState.NormalRelay = operation.Close } else { switchState.ReverseRelay = operation.Close } // if operation.Close { //触发道岔转动操作 if !e.HasComponent(components.ComSwitchTurnOperating) { e.AddComponent(components.ComSwitchTurnOperating) } switchId := components.ComDeviceIdentity.Get(e).Id turnTime := getSwitchTurnTime(switchId) *(components.ComSwitchTurnOperating.Get(e)) = state.SwitchTurnOperating{Start: true, OperateTime: turnTime, TurnNormal: operation.Normal} } } else { operation.OperateTime -= int64(w.Tick()) } } if !operation.Start { e.RemoveComponent(components.ComSwitchNRRelayOperating) } }) } // 获取道岔转动耗时ms func getSwitchTurnTime(switchId string) int64 { return 5000 } // 获取道岔定操反操继电器耗时ms func getSwitchNRDelayTime(switchId string) int64 { return 2000 }