rts-sim-module/system/signal_2xh1_system.go

78 lines
2.7 KiB
Go
Raw Normal View History

2023-09-18 13:16:31 +08:00
package system
import (
"github.com/yohamta/donburi/filter"
"joylink.club/ecs"
sysEvent "joylink.club/rtsssimulation/system/event"
"joylink.club/rtsssimulation/umi"
)
// Signal2XH1State 电路状态:**信号机2XH-1(红-绿) 出段(场)信号机 或 **出站区间阻挡信号机
type Signal2XH1State struct {
// 物理绿灯true-亮
L bool
// 物理红灯true-亮
H bool
// 点灯继电器true-吸合,常态落下表示逻辑点灯
DDJ bool
//灯丝继电器true-吸合
DJ bool
//列车信号继电器true-吸合
LXJ bool
}
// Signal2XH1FilamentState 信号机2XH-1 灯丝状态
type Signal2XH1FilamentState struct {
// 物理绿灯true-灯丝正常
Lf bool
// 物理红灯true-灯丝正常
Hf bool
}
func NewSignal2XH1FilamentState() *Signal2XH1FilamentState {
return &Signal2XH1FilamentState{Lf: true, Hf: true}
}
// NewSignal2XH1CdcState 出段(场)信号机
func NewSignal2XH1CdcState() *Signal2XH1State {
return &Signal2XH1State{DDJ: false, LXJ: false, DJ: true, L: false, H: true}
}
// NewSignal2XH1CzqzState 出站区间阻挡信号机
func NewSignal2XH1CzqzState() *Signal2XH1State {
return &Signal2XH1State{DDJ: true, LXJ: false, DJ: false, L: false, H: false}
}
var Signal2XH1StateComponent = ecs.NewComponentType[Signal2XH1State]()
var Signal2XH1FilamentStateComponent = ecs.NewComponentType[Signal2XH1FilamentState]()
var signal2XH1Query = ecs.NewQuery(filter.Contains(EntityIdentityComponent, Signal2XH1StateComponent))
// Update world 执行
func (me *Signal2XH1State) Update(w ecs.World) {
signal2XH1Query.Each(w, func(entry *ecs.Entry) {
state := Signal2XH1StateComponent.Get(entry)
filament := Signal2XH1FilamentStateComponent.Get(entry)
me.calculateL(state, filament)
me.calculateH(state, filament)
me.calculateDJ(w, entry, state, filament)
})
}
func (me *Signal2XH1State) calculateL(state *Signal2XH1State, filament *Signal2XH1FilamentState) {
isL := !state.DDJ && state.LXJ && filament.Lf
state.L = isL
}
func (me *Signal2XH1State) calculateH(state *Signal2XH1State, filament *Signal2XH1FilamentState) {
isH := !state.DDJ && !state.LXJ && filament.Hf
state.H = isH
}
func (me *Signal2XH1State) calculateDJ(w ecs.World, e *ecs.Entry, state *Signal2XH1State, filament *Signal2XH1FilamentState) {
isDJ := !state.DDJ && state.LXJ && filament.Lf || !state.DDJ && !state.LXJ && filament.Hf
//通知继电器进行动作
if state.DJ != isDJ {
signalModel := FindModelStorage(w).FindById(EntityIdentityComponent.Get(e).Id)
relayRole, _ := signalModel.(umi.IRelayCRole)
relayModel := relayRole.FindRelayModelByCRole(SIGNAL_2XH1, SIGNAL_DJ)
sysEvent.RelayNeedChangeEventBus.Publish(w, &sysEvent.RelayNeedChangeEvent{Id: relayModel.(umi.IDeviceModel).GetId(), Xh: isDJ})
}
}