rts-sim-module/entity/signal.go
2023-11-16 17:21:03 +08:00

120 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 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...)}
}
/*
enum Model{
HL = 0; //2XH-1 红绿
HLU_FU = 1; //2XH-1 红绿黄,封黄灯,无引导
HLU_DU_YY = 2; //3XH-1 红绿黄,不封灯,有单黄,带引导
HLU_YY = 3; //3XH-2或JDXH 红绿黄,不封灯,无单黄,带引导
HLU_FL_DU_YY = 4;//3XH-3 红绿黄,封绿灯,有单黄,带引导
HLU_DU = 5; //3XH-4 红绿黄,不封灯,有单黄,无引导
AB = 6; //DXCH 蓝白
HBU_DU = 7; //JCKXH 红白黄,不封灯,有单黄,无引导
}
*/
// 新建灯位列表
func newSignalLightsByModel(w ecs.World, sigMt proto.Signal_Model) *component.SignalLights {
switch sigMt {
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_DU_YY:
return &component.SignalLights{Lights: []*ecs.Entry{NewLight(w, component.HdTag), NewLight(w, component.LdTag, component.FdTag), NewLight(w, component.UdTag)}}
case proto.Signal_HLU_DU_YY:
fallthrough
case proto.Signal_HLU_YY:
fallthrough
case proto.Signal_HLU_DU:
return newSignalLights(w, component.HdTag, component.LdTag, component.UdTag)
case proto.Signal_AB:
return newSignalLights(w, component.AdTag, component.BdTag)
case proto.Signal_HBU_DU:
return newSignalLights(w, component.HdTag, component.BdTag, component.UdTag)
default:
panic(fmt.Sprintf("根据信号机模型来创建灯位列表,暂不支持该信号机模型[%d]", sigMt))
}
}