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

105 lines
3.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"
)
// SignalDCXHState 电路状态信号机DCXH(蓝-白) 调车信号机
type SignalDCXHState struct {
// 物理白灯true-亮
B bool
// 物理蓝灯true-亮
A bool
//灯丝继电器true-吸合
DJ bool
//调车信号继电器true-吸合
DXJ bool
}
// SignalDCXHFilamentState 信号机3XH-2 灯丝状态
type SignalDCXHFilamentState struct {
// 物理白灯true-灯丝正常
Bf bool
// 物理蓝灯true-灯丝正常
Af bool
}
type SignalDCXHSystem struct {
}
func NewSignalDCXHSystem() *SignalDCXHSystem {
return &SignalDCXHSystem{}
}
func NewSignalDCXHState() *SignalDCXHState {
return &SignalDCXHState{DJ: true, DXJ: false, A: true, B: false}
}
func NewSignalDCXHFilamentState() *SignalDCXHFilamentState {
return &SignalDCXHFilamentState{Bf: true, Af: true}
}
var SignalDCXHStateComponent = ecs.NewComponentType[SignalDCXHState]()
var SignalDCXHFilamentStateComponent = ecs.NewComponentType[SignalDCXHFilamentState]()
var signalDCXHQuery = ecs.NewQuery(filter.Contains(EntityIdentityComponent, SignalDCXHStateComponent))
// Update world 执行
func (me *SignalDCXHSystem) Update(w ecs.World) {
signalDCXHQuery.Each(w, func(e *ecs.Entry) {
state := SignalDCXHStateComponent.Get(e)
filament := SignalDCXHFilamentStateComponent.Get(e)
//
me.calculateA(w, state, filament)
me.calculateB(w, state, filament)
me.calculateDJ(w, e, state, filament)
})
}
// SignalDCXHRelayActionEventProcessor 继电器动作事件处理
var SignalDCXHRelayActionEventProcessor = func(w ecs.World, event sysEvent.RelayActionEvent) {
//根据event来更新SignalDCXHState中对应继电器的状态
signalDCXHQuery.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_DCXH {
state := SignalDCXHStateComponent.Get(e)
switch relayName {
case SIGNAL_DJ:
state.DJ = event.Xh
case SIGNAL_DXJ:
state.DXJ = event.Xh
default:
panic(fmt.Sprintf("SignalDCXH的模型[%s]中继电器功能名称[%s]无法识别", signalModel.Id(), relayName))
}
} else {
panic(fmt.Sprintf("SignalDCXH的模型[%s]中继电器组合类型[%s]无法识别", signalModel.Id(), relayGroup))
}
}
} else {
panic("SignalDCXH的模型未实现接口umi.IRelayCRoler")
}
})
}
func (me *SignalDCXHSystem) calculateB(w ecs.World, state *SignalDCXHState, filament *SignalDCXHFilamentState) {
isB := filament.Bf && state.DXJ
state.B = isB
}
func (me *SignalDCXHSystem) calculateA(w ecs.World, state *SignalDCXHState, filament *SignalDCXHFilamentState) {
isA := filament.Af && !state.DXJ
state.A = isA
}
func (me *SignalDCXHSystem) calculateDJ(w ecs.World, e *ecs.Entry, state *SignalDCXHState, filament *SignalDCXHFilamentState) {
isDJ := filament.Bf && state.DXJ || filament.Af && !state.DXJ
if isDJ != state.DJ {
if event, ok := createRelayNeedChangeEvent(w, EntityIdentityComponent.Get(e).Id, SIGNAL_DCXH, SIGNAL_DJ, isDJ); ok {
sysEvent.RelayNeedChangeEventBus.Publish(w, event)
}
}
}