2023-10-08 13:11:34 +08:00
|
|
|
package entity
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"joylink.club/ecs"
|
|
|
|
"joylink.club/rtsssimulation/component"
|
|
|
|
"joylink.club/rtsssimulation/consts"
|
2023-10-20 10:21:35 +08:00
|
|
|
"joylink.club/rtsssimulation/repository/model/proto"
|
2023-10-16 17:59:35 +08:00
|
|
|
"log/slog"
|
2023-10-08 13:11:34 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
// LoadSignals 加载信号机实体
|
|
|
|
func LoadSignals(w ecs.World) error {
|
|
|
|
data := GetWorldData(w)
|
|
|
|
signals := data.Repo.SignalList()
|
|
|
|
for _, signal := range signals {
|
|
|
|
groups := signal.RelayGroups()
|
2023-10-20 10:21:35 +08:00
|
|
|
signalEntry := newSignalEntity(w, signal.Id(), data)
|
|
|
|
if len(groups) == 1 { //带电路的信号机,有且只有一个组合类型,组合类型与电路有关
|
2023-10-08 13:11:34 +08:00
|
|
|
group := groups[0]
|
|
|
|
elecs := group.Components()
|
|
|
|
//
|
|
|
|
switch group.Code() {
|
|
|
|
case consts.SIGNAL_2XH1:
|
2023-10-08 16:00:05 +08:00
|
|
|
if le := loadSignal2xh1(w, signal, signalEntry, elecs, data.EntityMap); le != nil {
|
|
|
|
return le
|
|
|
|
}
|
2023-10-08 13:11:34 +08:00
|
|
|
case consts.SIGNAL_3XH1:
|
2023-10-08 16:00:05 +08:00
|
|
|
if le := loadSignal3xh1(w, signal, signalEntry, elecs, data.EntityMap); le != nil {
|
|
|
|
return le
|
|
|
|
}
|
2023-10-08 13:11:34 +08:00
|
|
|
case consts.SIGNAL_3XH2:
|
2023-10-08 16:48:10 +08:00
|
|
|
if le := loadSignal3xh2(w, signal, signalEntry, elecs, data.EntityMap); le != nil {
|
|
|
|
return le
|
|
|
|
}
|
2023-10-08 13:11:34 +08:00
|
|
|
case consts.SIGNAL_3XH3:
|
2023-10-08 17:19:50 +08:00
|
|
|
if le := loadSignal3xh3(w, signal, signalEntry, elecs, data.EntityMap); le != nil {
|
|
|
|
return le
|
|
|
|
}
|
2023-10-08 13:11:34 +08:00
|
|
|
case consts.SIGNAL_3XH4:
|
2023-10-08 18:03:07 +08:00
|
|
|
if le := loadSignal3xh4(w, signal, signalEntry, elecs, data.EntityMap); le != nil {
|
|
|
|
return le
|
|
|
|
}
|
2023-10-08 13:11:34 +08:00
|
|
|
case consts.SIGNAL_JDXH:
|
2023-10-09 11:09:29 +08:00
|
|
|
if le := loadSignalJdxh(w, signal, signalEntry, elecs, data.EntityMap); le != nil {
|
|
|
|
return le
|
|
|
|
}
|
2023-10-08 13:11:34 +08:00
|
|
|
case consts.SIGNAL_DCXH:
|
2023-10-09 10:06:01 +08:00
|
|
|
if le := loadSignalDcxh(w, signal, signalEntry, elecs, data.EntityMap); le != nil {
|
|
|
|
return le
|
|
|
|
}
|
2023-10-08 13:11:34 +08:00
|
|
|
case consts.SIGNAL_JCKXH:
|
2023-10-09 10:30:44 +08:00
|
|
|
if le := loadSignalJckxh(w, signal, signalEntry, elecs, data.EntityMap); le != nil {
|
|
|
|
return le
|
|
|
|
}
|
2023-10-08 13:11:34 +08:00
|
|
|
default:
|
2023-10-16 17:59:35 +08:00
|
|
|
slog.Warn(fmt.Sprintf("id=[%s]的信号机,无效组合类型[%s]", signal.Id(), group.Code()))
|
2023-10-08 13:11:34 +08:00
|
|
|
}
|
2023-10-20 10:21:35 +08:00
|
|
|
} else { //不带电路的信号机
|
|
|
|
signalEntry.AddComponent(component.SignalLightsType)
|
|
|
|
component.SignalLightsType.Set(signalEntry, newSignalLightsByModel(w, signal.Model()))
|
2023-10-08 13:11:34 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// 新建信号机实体
|
|
|
|
func newSignalEntity(w ecs.World, uid string, worldData *component.WorldData) *ecs.Entry {
|
|
|
|
entry, ok := worldData.EntityMap[uid]
|
|
|
|
if !ok {
|
2023-10-20 09:46:49 +08:00
|
|
|
entry = w.Entry(w.Create(component.UidType, component.SignalTag))
|
2023-10-08 13:11:34 +08:00
|
|
|
component.UidType.SetValue(entry, component.Uid{Id: uid})
|
|
|
|
worldData.EntityMap[uid] = entry
|
|
|
|
}
|
|
|
|
return entry
|
|
|
|
}
|
2023-10-12 11:00:56 +08:00
|
|
|
|
|
|
|
// 新建灯位列表
|
2023-10-12 11:06:20 +08:00
|
|
|
func newSignalLights(w ecs.World, lightTags ...component.DsTag) *component.SignalLights {
|
2023-10-12 11:00:56 +08:00
|
|
|
return &component.SignalLights{Lights: NewLights(w, lightTags...)}
|
|
|
|
}
|
2023-10-20 10:21:35 +08:00
|
|
|
|
|
|
|
// 新建灯位列表
|
|
|
|
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))
|
|
|
|
}
|
|
|
|
}
|