rts-sim-module/entity/signal.go
2023-10-20 10:21:35 +08:00

102 lines
3.5 KiB
Go

package entity
import (
"fmt"
"joylink.club/ecs"
"joylink.club/rtsssimulation/component"
"joylink.club/rtsssimulation/consts"
"joylink.club/rtsssimulation/repository/model/proto"
"log/slog"
)
// LoadSignals 加载信号机实体
func LoadSignals(w ecs.World) error {
data := GetWorldData(w)
signals := data.Repo.SignalList()
for _, signal := range signals {
groups := signal.RelayGroups()
signalEntry := newSignalEntity(w, signal.Id(), data)
if len(groups) == 1 { //带电路的信号机,有且只有一个组合类型,组合类型与电路有关
group := groups[0]
elecs := group.Components()
//
switch group.Code() {
case consts.SIGNAL_2XH1:
if le := loadSignal2xh1(w, signal, signalEntry, elecs, data.EntityMap); le != nil {
return le
}
case consts.SIGNAL_3XH1:
if le := loadSignal3xh1(w, signal, signalEntry, elecs, data.EntityMap); le != nil {
return le
}
case consts.SIGNAL_3XH2:
if le := loadSignal3xh2(w, signal, signalEntry, elecs, data.EntityMap); le != nil {
return le
}
case consts.SIGNAL_3XH3:
if le := loadSignal3xh3(w, signal, signalEntry, elecs, data.EntityMap); le != nil {
return le
}
case consts.SIGNAL_3XH4:
if le := loadSignal3xh4(w, signal, signalEntry, elecs, data.EntityMap); le != nil {
return le
}
case consts.SIGNAL_JDXH:
if le := loadSignalJdxh(w, signal, signalEntry, elecs, data.EntityMap); le != nil {
return le
}
case consts.SIGNAL_DCXH:
if le := loadSignalDcxh(w, signal, signalEntry, elecs, data.EntityMap); le != nil {
return le
}
case consts.SIGNAL_JCKXH:
if le := loadSignalJckxh(w, signal, signalEntry, elecs, data.EntityMap); le != nil {
return le
}
default:
slog.Warn(fmt.Sprintf("id=[%s]的信号机,无效组合类型[%s]", signal.Id(), group.Code()))
}
} else { //不带电路的信号机
signalEntry.AddComponent(component.SignalLightsType)
component.SignalLightsType.Set(signalEntry, newSignalLightsByModel(w, signal.Model()))
}
}
return nil
}
// 新建信号机实体
func newSignalEntity(w ecs.World, uid string, worldData *component.WorldData) *ecs.Entry {
entry, ok := worldData.EntityMap[uid]
if !ok {
entry = w.Entry(w.Create(component.UidType, component.SignalTag))
component.UidType.SetValue(entry, component.Uid{Id: uid})
worldData.EntityMap[uid] = entry
}
return entry
}
// 新建灯位列表
func newSignalLights(w ecs.World, lightTags ...component.DsTag) *component.SignalLights {
return &component.SignalLights{Lights: NewLights(w, lightTags...)}
}
// 新建灯位列表
func newSignalLightsByModel(w ecs.World, sigMt proto.Signal_Model) *component.SignalLights {
switch sigMt {
case proto.Signal_HLU:
return newSignalLights(w, component.HdTag, component.LdTag, component.UdTag)
case proto.Signal_HL:
return newSignalLights(w, component.HdTag, component.LdTag)
case proto.Signal_HLU_FU:
return &component.SignalLights{Lights: []*ecs.Entry{NewLight(w, component.HdTag), NewLight(w, component.LdTag), NewLight(w, component.UdTag, component.FdTag)}}
case proto.Signal_HLU_FL:
return &component.SignalLights{Lights: []*ecs.Entry{NewLight(w, component.HdTag), NewLight(w, component.LdTag, component.FdTag), NewLight(w, component.UdTag)}}
case proto.Signal_AB:
return newSignalLights(w, component.AdTag, component.BdTag)
case proto.Signal_HBU:
return newSignalLights(w, component.HdTag, component.BdTag, component.UdTag)
default:
panic(fmt.Sprintf("根据信号机模型来创建灯位列表,暂不支持该信号机模型[%d]", sigMt))
}
}