rts-sim-module/system/signal_jdxh_system.go
2023-09-22 10:53:51 +08:00

131 lines
4.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"
sysEvent "joylink.club/rtsssimulation/system/event"
)
// SignalJDXHState 电路状态信号机JDXH(红-绿-黄) 进段信号机(三显示不封灯、无单黄显示、带引导)
type SignalJDXHState struct {
// 物理黄灯true-亮
U bool
// 物理绿灯true-亮
L bool
// 物理红灯true-亮
H bool
// 2DJ灯丝继电器true-吸合
EDJ bool
//灯丝继电器true-吸合
DJ bool
//列车信号继电器true-吸合
LXJ bool
//引导信号继电器true-吸合
YXJ bool
}
// SignalJDXHFilamentState 信号机3XH-2 灯丝状态
type SignalJDXHFilamentState struct {
// 物理黄灯true-灯丝正常
Uf bool
// 物理绿灯true-灯丝正常
Lf bool
// 物理红灯true-灯丝正常
Hf bool
}
type SignalJDXHSystem struct {
}
func NewSignalJDXHSystem() *SignalJDXHSystem {
return &SignalJDXHSystem{}
}
func NewSignalJDXHState() *SignalJDXHState {
return &SignalJDXHState{LXJ: false, YXJ: false, DJ: true, EDJ: false, H: true, U: false, L: false}
}
func NewSignalJDXHFilamentState() *SignalJDXHFilamentState {
return &SignalJDXHFilamentState{Uf: true, Lf: true, Hf: true}
}
var SignalJDXHStateComponent = ecs.NewComponentType[SignalJDXHState]()
var SignalJDXHFilamentStateComponent = ecs.NewComponentType[SignalJDXHFilamentState]()
var signalJDXHQuery = ecs.NewQuery(filter.Contains(EntityIdentityComponent, SignalJDXHStateComponent))
// Update world 执行
func (me *SignalJDXHSystem) Update(w ecs.World) {
signalJDXHQuery.Each(w, func(e *ecs.Entry) {
state := SignalJDXHStateComponent.Get(e)
filament := SignalJDXHFilamentStateComponent.Get(e)
//
me.calculateL(w, state, filament)
me.calculateU(w, state, filament)
me.calculateH(w, state, filament)
me.calculateDJ(w, e, state, filament)
me.calculate2DJ(w, e, state, filament)
})
}
// SignalJDXHRelayActionEventProcessor 继电器动作事件处理
var SignalJDXHRelayActionEventProcessor = func(w ecs.World, event sysEvent.RelayActionEvent) {
//根据event来更新SignalJDXHState中对应继电器的状态
signalJDXHQuery.Each(w, func(e *ecs.Entry) {
signalModel := FindModelStorage(w).FindById(EntityIdentityComponent.Get(e).Id)
roler, ok := signalModel.(IRelayCRole)
if ok {
if relayGroup, relayName, find := roler.FindCircuitRoleById(event.Id); find {
if relayGroup == SIGNAL_JDXH {
state := SignalJDXHStateComponent.Get(e)
switch relayName {
case SIGNAL_DJ:
state.DJ = event.Xh
case SIGNAL_2DJ:
state.EDJ = event.Xh
case SIGNAL_LXJ:
state.LXJ = event.Xh
case SIGNAL_YXJ:
state.YXJ = event.Xh
default:
panic(fmt.Sprintf("SignalJDXH的模型[%s]中继电器功能名称[%s]无法识别", signalModel.Id(), relayName))
}
} else {
panic(fmt.Sprintf("SignalJDXH的模型[%s]中继电器组合类型[%s]无法识别", signalModel.Id(), relayGroup))
}
}
} else {
panic("SignalJDXH的模型未实现接口umi.IRelayCRoler")
}
})
}
func (me *SignalJDXHSystem) calculateU(w ecs.World, state *SignalJDXHState, filament *SignalJDXHFilamentState) {
isU := filament.Uf && !state.LXJ && state.DJ && state.YXJ
state.U = isU
}
func (me *SignalJDXHSystem) calculateL(w ecs.World, state *SignalJDXHState, filament *SignalJDXHFilamentState) {
isL := filament.Lf && state.LXJ
state.L = isL
}
func (me *SignalJDXHSystem) calculateH(w ecs.World, state *SignalJDXHState, filament *SignalJDXHFilamentState) {
isH := filament.Hf && !state.LXJ
state.H = isH
}
func (me *SignalJDXHSystem) calculateDJ(w ecs.World, e *ecs.Entry, state *SignalJDXHState, filament *SignalJDXHFilamentState) {
isDJ := filament.Lf && state.LXJ || filament.Hf && !state.LXJ
if isDJ != state.DJ {
if event, ok := createRelayNeedChangeEvent(w, EntityIdentityComponent.Get(e).Id, SIGNAL_JDXH, SIGNAL_DJ, isDJ); ok {
sysEvent.RelayNeedChangeEventBus.Publish(w, event)
}
}
}
func (me *SignalJDXHSystem) calculate2DJ(w ecs.World, e *ecs.Entry, state *SignalJDXHState, filament *SignalJDXHFilamentState) {
is2DJ := filament.Uf && !state.LXJ && state.DJ && state.YXJ
if is2DJ != state.EDJ {
if event, ok := createRelayNeedChangeEvent(w, EntityIdentityComponent.Get(e).Id, SIGNAL_JDXH, SIGNAL_2DJ, is2DJ); ok {
sysEvent.RelayNeedChangeEventBus.Publish(w, event)
}
}
}