rts-sim-module/entity/psd.go

57 lines
1.8 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, 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) {
if len(psd.ComponentGroups()) == 0 {
return
}
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, psd *repository.Psd, worldData *component.WorldData) *ecs.Entry {
entry, ok := worldData.EntityMap[psd.Id()]
if !ok {
entry = world.Entry(world.Create(component.PsdTag, component.UidType, component.PsdInfoType, component.PsdStateType,
component.PsdDriveCircuitType, component.PsdCollectionCircuitType, component.PsdMotorType))
component.UidType.SetValue(entry, component.Uid{Id: psd.Id()})
component.PsdInfoType.SetValue(entry, component.PsdInfo{PlatformId: psd.PlatformId()})
psdMotor := world.Entry(world.Create(component.PsdMotorStateType))
component.PsdMotorType.Set(entry, psdMotor)
worldData.EntityMap[psd.Id()] = entry
}
return entry
}