rts-sim-module/system/signal_jckxh_system.go
2023-09-21 16:23:03 +08:00

120 lines
3.9 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"
sysEvent "joylink.club/rtsssimulation/system/event"
"joylink.club/rtsssimulation/umi"
)
// SignalJCKXHState 电路状态信号机JCKXH(红-白-黄) 进/出库列车兼调车信号机(三显示不封灯、有单黄显示、无引导)
type SignalJCKXHState struct {
// 物理黄灯true-亮
U bool
// 物理白灯true-亮
B bool
// 物理红灯true-亮
H bool
//灯丝继电器true-吸合
DJ bool
//调车信号继电器true-吸合
DXJ bool
//列车信号继电器true-吸合
LXJ bool
}
// SignalJCKXHFilamentState 信号机3XH-2 灯丝状态
type SignalJCKXHFilamentState struct {
// 物理黄灯true-灯丝正常
Uf bool
// 物理白灯true-灯丝正常
Bf bool
// 物理红灯true-灯丝正常
Hf bool
}
type SignalJCKXHSystem struct {
}
func NewSignalJCKXHState() *SignalJCKXHState {
return &SignalJCKXHState{DJ: true, LXJ: false, DXJ: false, U: false, B: false, H: true}
}
func NewSignalJCKXHFilamentState() *SignalJCKXHFilamentState {
return &SignalJCKXHFilamentState{Uf: true, Bf: true, Hf: true}
}
func NewSignalJCKXHSystem() *SignalJCKXHSystem {
return &SignalJCKXHSystem{}
}
var SignalJCKXHStateComponent = ecs.NewComponentType[SignalJCKXHState]()
var SignalJCKXHFilamentStateComponent = ecs.NewComponentType[SignalJCKXHFilamentState]()
var signalJCKXHQuery = ecs.NewQuery(filter.Contains(EntityIdentityComponent, SignalJCKXHStateComponent))
// Update world 执行
func (me *SignalJCKXHSystem) Update(w ecs.World) {
signalJCKXHQuery.Each(w, func(e *ecs.Entry) {
state := SignalJCKXHStateComponent.Get(e)
filament := SignalJCKXHFilamentStateComponent.Get(e)
//
me.calculateU(w, state, filament)
me.calculateB(w, state, filament)
me.calculateH(w, state, filament)
me.calculateDJ(w, e, state, filament)
})
}
// SignalJCKXHRelayActionEventProcessor 继电器动作事件处理
var SignalJCKXHRelayActionEventProcessor = func(w ecs.World, event sysEvent.RelayActionEvent) {
//根据event来更新SignalJCKXHState中对应继电器的状态
signalJCKXHQuery.Each(w, func(e *ecs.Entry) {
signalModel := FindModelStorage(w).FindById(EntityIdentityComponent.Get(e).Id)
roler, ok := signalModel.(umi.IRelayCRole)
if ok {
if relayGroup, relayName, find := roler.FindCircuitRoleById(event.Id); find {
if relayGroup == SIGNAL_JCKXH {
state := SignalJCKXHStateComponent.Get(e)
switch relayName {
case SIGNAL_DJ:
state.DJ = event.Xh
case SIGNAL_DXJ:
state.DXJ = event.Xh
case SIGNAL_LXJ:
state.LXJ = event.Xh
default:
panic(fmt.Sprintf("SignalJCKXH的模型[%s]中继电器功能名称[%s]无法识别", signalModel.Id(), relayName))
}
} else {
panic(fmt.Sprintf("SignalJCKXH的模型[%s]中继电器组合类型[%s]无法识别", signalModel.Id(), relayGroup))
}
}
} else {
panic("SignalJCKXH的模型未实现接口umi.IRelayCRoler")
}
})
}
func (me *SignalJCKXHSystem) calculateU(w ecs.World, state *SignalJCKXHState, filament *SignalJCKXHFilamentState) {
isU := filament.Uf && state.LXJ
state.U = isU
}
func (me *SignalJCKXHSystem) calculateB(w ecs.World, state *SignalJCKXHState, filament *SignalJCKXHFilamentState) {
isB := filament.Bf && !state.LXJ && state.DXJ
state.B = isB
}
func (me *SignalJCKXHSystem) calculateH(w ecs.World, state *SignalJCKXHState, filament *SignalJCKXHFilamentState) {
isH := filament.Bf && !state.LXJ && state.DXJ
state.H = isH
}
func (me *SignalJCKXHSystem) calculateDJ(w ecs.World, e *ecs.Entry, state *SignalJCKXHState, filament *SignalJCKXHFilamentState) {
isDJ := filament.Uf && state.LXJ || filament.Bf && !state.LXJ && state.DXJ || filament.Bf && !state.LXJ && state.DXJ
if isDJ != state.DJ {
if event, ok := createRelayNeedChangeEvent(w, EntityIdentityComponent.Get(e).Id, SIGNAL_JCKXH, SIGNAL_DJ, isDJ); ok {
sysEvent.RelayNeedChangeEventBus.Publish(w, event)
}
}
}