52 lines
1.5 KiB
Go
52 lines
1.5 KiB
Go
package entity
|
|
|
|
import (
|
|
"joylink.club/ecs"
|
|
"joylink.club/rtsssimulation/component"
|
|
"joylink.club/rtsssimulation/repository"
|
|
"unsafe"
|
|
)
|
|
|
|
var TrackCircuitBaseComponentTypeArr = []ecs.IComponentType{component.UidType, component.TrackCircuitType}
|
|
|
|
func LoadTrackCircuit(w ecs.World) error {
|
|
data := GetWorldData(w)
|
|
for _, trackCircuit := range data.Repo.PhysicalSectionList() {
|
|
isAxleSection, err := trackCircuit.IsAxleSection()
|
|
if isAxleSection || err != nil {
|
|
continue
|
|
}
|
|
trackCircuitEntry := newTrackCircuitEntry(w, trackCircuit, data)
|
|
addTrackCircuit(w, trackCircuit, trackCircuitEntry, data)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func newTrackCircuitEntry(w ecs.World, section *repository.PhysicalSection, data *component.WorldData) *ecs.Entry {
|
|
uid := section.Id()
|
|
entry, ok := data.EntityMap[uid]
|
|
if !ok {
|
|
entry = w.Entry(w.Create(TrackCircuitBaseComponentTypeArr...))
|
|
component.UidType.SetValue(entry, component.Uid{Id: uid})
|
|
data.EntityMap[uid] = entry
|
|
}
|
|
return entry
|
|
}
|
|
|
|
func addTrackCircuit(w ecs.World, circuit *repository.PhysicalSection, entry *ecs.Entry, data *component.WorldData) {
|
|
if len(circuit.ComponentGroups()) == 0 {
|
|
return
|
|
}
|
|
trackCircuit := component.TrackCircuit{}
|
|
entry.AddComponent(component.TrackCircuitType, unsafe.Pointer(&trackCircuit))
|
|
for _, group := range circuit.ComponentGroups() {
|
|
for _, ec := range group.Components() {
|
|
relay := ec.(*repository.Relay)
|
|
switch ec.Code() {
|
|
case "GJ":
|
|
trackCircuit.GJ = NewRelayEntity(w, relay, data.EntityMap)
|
|
}
|
|
}
|
|
}
|
|
}
|