rts-sim-module/entity/psd.go
2023-12-08 17:13:50 +08:00

139 lines
5.1 KiB
Go

package entity
import (
"joylink.club/ecs"
"joylink.club/rtsssimulation/component"
"joylink.club/rtsssimulation/repository"
"strconv"
)
var PsdBaseComponentTypeArr = []ecs.IComponentType{component.PsdTag, component.UidType,
component.PsdStateType, component.AsdListType, component.PscType}
var AsdBaseComponentTypeArr = []ecs.IComponentType{component.AsdTag, component.UidType,
component.AsdMotorStateType, component.TwoPositionTransformType, component.AsdStateType}
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)
}
pmcMap := make(map[string]*ecs.Entry)
for _, mkx := range data.Repo.MkxList() {
entry := NewMkxEntry(w, data, mkx)
loadPlatformMkxCircuit(w, data.EntityMap, pmcMap, mkx, entry, data.EntityMap[mkx.Psd().Id()])
}
return nil
}
func loadPlatformMkxCircuit(world ecs.World, entryMap map[string]*ecs.Entry, pmcMap map[string]*ecs.Entry,
mkx *repository.Mkx, mkxEntry *ecs.Entry, psdEntry *ecs.Entry) {
platformMkx, ok := pmcMap[mkx.Psd().Id()]
if !ok {
platformMkx = NewPlatformMkxEntry(world, entryMap, mkx)
pmcMap[mkx.Psd().Id()] = platformMkx
}
platformMkxCircuit := component.PlatformMkxCircuitType.Get(platformMkx)
platformMkxCircuit.MkxList = append(platformMkxCircuit.MkxList, mkxEntry)
psdEntry.AddComponent(component.PlatformMkxCircuitType)
component.PlatformMkxCircuitType.Set(psdEntry, platformMkxCircuit)
}
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{KMJMap: make(map[int32]*ecs.Entry)}
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.KMJMap[4] = NewRelayEntity(world, relay, entryMap)
case "8XKMJ", "8SKMJ":
circuit.KMJMap[8] = NewRelayEntity(world, relay, entryMap)
case "XMGJ", "SMGJ":
circuit.MGJ = NewRelayEntity(world, relay, entryMap)
case "XMPLJ", "SMPLJ":
circuit.MPLJ = NewRelayEntity(world, relay, entryMap)
case "SQDTCJ", "XQDTCJ":
circuit.QDTCJ = NewRelayEntity(world, relay, entryMap)
case "STZTCJ", "XTZTCJ":
circuit.TZTCJ = NewRelayEntity(world, relay, entryMap)
case "SZAWJ", "XZAWJ":
circuit.ZAWJ = NewRelayEntity(world, relay, entryMap)
case "SJXTCPLJ", "XJXTCPLJ":
circuit.JXTCPLJ = NewRelayEntity(world, relay, entryMap)
default:
circuit.UnusedJ = append(circuit.UnusedJ, NewRelayEntity(world, relay, entryMap))
}
}
}
entry.AddComponent(component.PsdCircuitType)
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(PsdBaseComponentTypeArr...))
component.UidType.SetValue(entry, component.Uid{Id: psd.Id()})
worldData.EntityMap[psd.Id()] = entry
asdList := &component.AsdList{List: make([]*ecs.Entry, psd.AsdAmount(), psd.AsdAmount())}
for i := int32(0); i < psd.AsdAmount(); i++ {
asdList.List[i] = NewAsdEntry(world, worldData, psd.Id(), int(i))
}
component.AsdListType.Set(entry, asdList)
component.PscType.Set(entry, &component.Psc{InterlockKmGroup: make(map[int32]bool)})
}
return entry
}
func NewAsdEntry(world ecs.World, worldData *component.WorldData, psdId string, asdIndex int) *ecs.Entry {
entry := world.Entry(world.Create(AsdBaseComponentTypeArr...))
uid := psdId + "_" + strconv.Itoa(asdIndex)
worldData.EntityMap[uid] = entry
component.UidType.SetValue(entry, component.Uid{Id: uid})
return entry
}
func NewMkxEntry(world ecs.World, worldData *component.WorldData, mkx *repository.Mkx) *ecs.Entry {
entry := world.Entry(world.Create(component.UidType, component.MkxType))
worldData.EntityMap[mkx.Id()] = entry
component.UidType.SetValue(entry, component.Uid{Id: mkx.Id()})
mkxComponent := &component.Mkx{}
component.MkxType.Set(entry, mkxComponent)
if pcb := mkx.Pcb(); pcb != nil {
mkxComponent.PCB = NewButtonEntity(world, pcb, worldData.EntityMap)
}
if pob := mkx.Pob(); pob != nil {
mkxComponent.POB = NewButtonEntity(world, pob, worldData.EntityMap)
}
if pab := mkx.Pab(); pab != nil {
mkxComponent.PAB = NewButtonEntity(world, pab, worldData.EntityMap)
}
if mpl := mkx.Mpl(); mpl != nil {
mkxComponent.MPL = NewButtonEntity(world, mpl, worldData.EntityMap)
}
return entry
}
func NewPlatformMkxEntry(world ecs.World, entryMap map[string]*ecs.Entry, mkx *repository.Mkx) *ecs.Entry {
entry := world.Entry(world.Create(component.PlatformMkxCircuitType))
circuit := &component.PlatformMkxCircuit{}
if pcbj := mkx.Pcbj(); pcbj != nil {
circuit.PCBJ = NewRelayEntity(world, pcbj, entryMap)
}
if pobj := mkx.Pobj(); pobj != nil {
circuit.POBJ = NewRelayEntity(world, pobj, entryMap)
}
if pabj := mkx.Pabj(); pabj != nil {
circuit.PABJ = NewRelayEntity(world, pabj, entryMap)
}
component.PlatformMkxCircuitType.Set(entry, circuit)
return entry
}