rts-sim-module/entity/axle_section.go

65 lines
2.2 KiB
Go
Raw Normal View History

2023-10-31 11:30:17 +08:00
package entity
import (
"fmt"
"joylink.club/ecs"
"joylink.club/rtsssimulation/component"
"joylink.club/rtsssimulation/repository"
2023-10-31 17:39:10 +08:00
"strings"
2023-10-31 11:30:17 +08:00
)
2023-11-06 14:35:32 +08:00
func newAxleManageDevice(w ecs.World, data *component.WorldData, centralizedStation string) *ecs.Entry {
entry := w.Entry(w.Create(component.AxleManageDeviceType))
component.AxleManageDeviceType.Set(entry, component.NewAxleManageDevice(centralizedStation))
data.AxleManageDeviceEntities = append(data.AxleManageDeviceEntities, entry)
return entry
}
2023-10-31 15:55:16 +08:00
// LoadAxlePhysicalSections 加载计轴区段
func LoadAxlePhysicalSections(w ecs.World) error {
data := GetWorldData(w)
sections := data.Repo.PhysicalSectionList()
for _, section := range sections {
if is, se := section.IsAxleSection(); se == nil && is {
2023-10-31 17:39:10 +08:00
if len(strings.TrimSpace(section.CentralizedStation())) == 0 {
return fmt.Errorf("区段[%s]未设置所属集中站", section.Id())
}
2023-11-06 15:30:57 +08:00
amdEntry := FindAxleManageDevice(data, section.CentralizedStation())
if amdEntry == nil {
amdEntry = newAxleManageDevice(w, data, section.CentralizedStation())
}
2023-11-06 14:35:32 +08:00
amd := component.AxleManageDeviceType.Get(amdEntry)
//
createAxleSectionEntity(w, section, data)
//
amd.Adrs[section.Id()] = component.NewAxleDeviceRuntime()
2023-10-31 15:55:16 +08:00
}
}
return nil
}
2023-11-06 14:35:32 +08:00
func FindAxleManageDevice(data *component.WorldData, centralizedStation string) *ecs.Entry {
for _, entry := range data.AxleManageDeviceEntities {
amd := component.AxleManageDeviceType.Get(entry)
if amd != nil && amd.CentralizedStation == centralizedStation {
return entry
}
2023-10-31 11:30:17 +08:00
}
2023-11-06 14:35:32 +08:00
return nil
2023-10-31 11:30:17 +08:00
}
2023-10-31 15:55:16 +08:00
// 计轴区段实体
func createAxleSectionEntity(w ecs.World, axleSection *repository.PhysicalSection, worldData *component.WorldData) *ecs.Entry {
uid := axleSection.Id()
2023-10-31 11:30:17 +08:00
entry, ok := worldData.EntityMap[uid]
if !ok {
2023-11-06 14:35:32 +08:00
entry = w.Entry(w.Create(component.UidType, component.PhysicalSectionStateType, component.AxlePhysicalSectionType))
2023-10-31 11:30:17 +08:00
//
component.UidType.SetValue(entry, component.Uid{Id: uid})
2023-11-06 14:35:32 +08:00
component.PhysicalSectionStateType.Set(entry, &component.PhysicalSectionState{Occ: false})
component.AxlePhysicalSectionType.Set(entry, component.NewAxlePhysicalSection())
2023-10-31 11:30:17 +08:00
//
worldData.EntityMap[uid] = entry
}
return entry
}