76 lines
2.6 KiB
Go
76 lines
2.6 KiB
Go
package entity
|
|
|
|
import (
|
|
"fmt"
|
|
"joylink.club/ecs"
|
|
"joylink.club/rtsssimulation/component"
|
|
"joylink.club/rtsssimulation/repository"
|
|
"strings"
|
|
"unsafe"
|
|
)
|
|
|
|
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
|
|
}
|
|
|
|
// 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 {
|
|
if len(strings.TrimSpace(section.CentralizedStation())) == 0 {
|
|
return fmt.Errorf("区段[%s]未设置所属集中站", section.Id())
|
|
}
|
|
amdEntry := FindAxleManageDevice(data, section.CentralizedStation())
|
|
if amdEntry == nil {
|
|
amdEntry = newAxleManageDevice(w, data, section.CentralizedStation())
|
|
}
|
|
amd := component.AxleManageDeviceType.Get(amdEntry)
|
|
//
|
|
createAxleSectionEntity(w, section, data)
|
|
//
|
|
amd.Adrs[section.Id()] = component.NewAxleDeviceRuntime()
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
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
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// 计轴区段实体
|
|
func createAxleSectionEntity(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.PhysicalSectionStateType, component.AxlePhysicalSectionType))
|
|
//
|
|
component.UidType.SetValue(entry, component.Uid{Id: uid})
|
|
component.PhysicalSectionStateType.Set(entry, &component.PhysicalSectionState{Occ: false})
|
|
component.AxlePhysicalSectionType.Set(entry, component.NewAxlePhysicalSection())
|
|
//
|
|
for _, group := range axleSection.ComponentGroups() {
|
|
for _, ec := range group.Components() {
|
|
if ec.Code() == "GJ" {
|
|
relay := ec.(*repository.Relay)
|
|
gjEntry := NewRelayEntity(w, relay, worldData.EntityMap)
|
|
circuit := &component.PhysicalSectionCircuit{GJ: gjEntry}
|
|
entry.AddComponent(component.PhysicalSectionCircuitType, unsafe.Pointer(circuit))
|
|
}
|
|
}
|
|
}
|
|
worldData.EntityMap[uid] = entry
|
|
}
|
|
return entry
|
|
}
|