diff --git a/component/axle_section.go b/component/axle_section.go index 13f1957..f8f4331 100644 --- a/component/axle_section.go +++ b/component/axle_section.go @@ -10,6 +10,10 @@ type AxleSectionState struct { Clr bool } +func NewAxleSectionState() *AxleSectionState { + return &AxleSectionState{Clr: true, Occ: false} +} + //计轴直接复位:计轴的轮对计数清零,区段转换为空闲状态 //计轴预复位:将计轴的轮对计数清零,但是区段不会立即变成空闲区段,而是处于一种“占用”状态,在压道车通过之后确认区段空闲且计轴正常后,区段转换为空闲状态 // @@ -32,6 +36,10 @@ type AxleSectionDevice struct { Pdrst bool } +func NewAxleSectionDevice() *AxleSectionDevice { + return &AxleSectionDevice{Rac: false, Rjo: false, Rjt: false, Drst: false, Pdrst: false} +} + // AxleSectionRuntime 计轴区段相关运算中间数据 type AxleSectionRuntime struct { //true-计轴系统正在执行直接预复位操作 @@ -46,8 +54,13 @@ type AxleSectionRuntime struct { CountLast int16 } +func NewAxleSectionRuntime() *AxleSectionRuntime { + return &AxleSectionRuntime{DoingPdrst: false, CountTrainInPulse: false, CountTrainOutPulse: false, Count: 0, CountLast: 0} +} + var ( AxleSectionStateType = ecs.NewComponentType[AxleSectionState]() AxleSectionDeviceType = ecs.NewComponentType[AxleSectionDevice]() AxleSectionRuntimeType = ecs.NewComponentType[AxleSectionRuntime]() + AxleSectionTag = ecs.NewTag() ) diff --git a/entity/init.go b/entity/init.go index 8083721..fe70050 100644 --- a/entity/init.go +++ b/entity/init.go @@ -34,5 +34,10 @@ func Load(w ecs.World, repo *repository.Repository) error { if err != nil { return err } + // 加载计轴区段相关实体 + err = LoadAxleSections(w) + if err != nil { + return err + } return err } diff --git a/entity/section.go b/entity/section.go new file mode 100644 index 0000000..32d32ec --- /dev/null +++ b/entity/section.go @@ -0,0 +1,32 @@ +package entity + +import ( + "joylink.club/ecs" + "joylink.club/rtsssimulation/component" + "joylink.club/rtsssimulation/repository" +) + +// LoadAxleSections 加载计轴区段 +func LoadAxleSections(w ecs.World) error { + data := GetWorldData(w) + sections := data.Repo.PhysicalSectionList() + for _, section := range sections { + if is, se := section.IsAxleSection(); se == nil && is { + newAxleSectionEntity(w, section, data) + } + } + return nil +} +func newAxleSectionEntity(w ecs.World, axleSection *repository.PhysicalSection, worldData *component.WorldData) *ecs.Entry { + uid := axleSection.Id() + entry, ok := worldData.EntityMap[uid] + if !ok { + entry = w.Entry(w.Create(component.UidType, component.AxleSectionTag, component.AxleSectionStateType, component.AxleSectionDeviceType, component.AxleSectionRuntimeType)) + component.UidType.SetValue(entry, component.Uid{Id: uid}) + component.AxleSectionStateType.Set(entry, component.NewAxleSectionState()) + component.AxleSectionDeviceType.Set(entry, component.NewAxleSectionDevice()) + component.AxleSectionRuntimeType.Set(entry, component.NewAxleSectionRuntime()) + worldData.EntityMap[uid] = entry + } + return entry +} diff --git a/repository/physical_section.go b/repository/physical_section.go index 8dd4ad4..c8125eb 100644 --- a/repository/physical_section.go +++ b/repository/physical_section.go @@ -77,6 +77,26 @@ func (s *PhysicalSection) BKilometer() *proto.Kilometer { return s.bKm } +// IsAxleSection 判断是否为计轴区段 +func (s *PhysicalSection) IsAxleSection() (bool, error) { + if len(s.checkPoints) > 0 { + axleCount := 0 + for _, cp := range s.checkPoints { + if cp.pointType == proto.CheckPointType_AxleCounter { + axleCount++ + } + } + if axleCount == len(s.checkPoints) { + return true, nil + } else if axleCount == 0 { + return false, nil + } else { //axleCount>0&&axleCount