rts-sim-module/entity/psd.go

51 lines
1.5 KiB
Go

package entity
import (
"joylink.club/ecs"
"joylink.club/rtsssimulation/component"
"joylink.club/rtsssimulation/repository"
)
func LoadPsd(w ecs.World) error {
data := GetWorldData(w)
psds := data.Repo.PsdList()
for _, psd := range psds {
entry := NewPsdEntry(w, psd.Id(), data)
loadPsdCircuit(w, entry, psd, data.EntityMap)
}
return nil
}
func loadPsdCircuit(world ecs.World, entry *ecs.Entry, psd *repository.Psd, entryMap map[string]*ecs.Entry) {
circuit := &component.PsdCircuit{}
for _, group := range psd.ComponentGroups() {
for _, ec := range group.Components() {
relay := ec.(*repository.Relay)
switch ec.Code() {
case "XGMJ", "SGMJ":
circuit.GMJ = NewRelayEntity(world, relay, entryMap)
case "4XKMJ", "4SKMJ":
circuit.KMJ4 = NewRelayEntity(world, relay, entryMap)
case "8XKMJ", "8SKMJ":
circuit.KMJ8 = NewRelayEntity(world, relay, entryMap)
case "XMGJ", "SMGJ":
circuit.MGJ = NewRelayEntity(world, relay, entryMap)
case "XMPLJ", "SMPLJ":
circuit.MPLJ = NewRelayEntity(world, relay, entryMap)
}
}
}
component.PsdCircuitType.Set(entry, circuit)
}
func NewPsdEntry(world ecs.World, uid string, worldData *component.WorldData) *ecs.Entry {
entry, ok := worldData.EntityMap[uid]
if !ok {
entry := world.Entry(world.Create(component.PsdTag, component.UidType, component.TwoPositionTransformType,
component.PsdCircuitType, component.PsdDriveCircuitType, component.PsdCollectionCircuitType))
component.UidType.SetValue(entry, component.Uid{Id: uid})
worldData.EntityMap[uid] = entry
}
return entry
}