package entity import ( "fmt" "joylink.club/ecs" "joylink.club/rtsssimulation/component" "joylink.club/rtsssimulation/consts" "joylink.club/rtsssimulation/repository" "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, 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 { slog.Debug(fmt.Sprintf("id=[%s]的信号机,没有组合类型或组合类型个数不为1", signal.Id())) } } return nil } // 新建信号机实体 func newSignalEntity(w ecs.World, signal *repository.Signal, worldData *component.WorldData) *ecs.Entry { uid := signal.Id() entry, ok := worldData.EntityMap[uid] if !ok { entry = w.Entry(w.Create(component.UidType, component.SignalTag, component.SignalLightsType)) component.UidType.SetValue(entry, component.Uid{Id: uid}) component.SignalLightsType.Set(entry, newSignalLightsByModel(w, signal.Model())) 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)) } }