rts-sim-module/entity/psd.go

181 lines
6.6 KiB
Go

package entity
import (
"joylink.club/ecs"
"joylink.club/rtsssimulation/component"
"joylink.club/rtsssimulation/repository"
"regexp"
"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.FixedPositionTransformType, 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.PsdPsl, 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)}
pattern := regexp.MustCompile("(\\d*).*KMJ") //用来匹配开门继电器的正则表达式
for _, group := range psd.ComponentGroups() {
for _, ec := range group.Components() {
relay := ec.(*repository.Relay)
matches := pattern.FindStringSubmatch(relay.Code())
if len(matches) != 0 { //匹配上了
if matches[1] == "" { //SKMJ/XKMJ
circuit.KMJMap[0] = NewRelayEntity(world, relay, entryMap)
} else {
num, err := strconv.Atoi(matches[1])
if err != nil {
panic(err)
}
circuit.KMJMap[int32(num)] = NewRelayEntity(world, relay, entryMap)
}
continue
}
switch ec.Code() {
case "XGMJ", "SGMJ":
circuit.GMJ = 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.PsdPsl) *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 pcbpl := mkx.Pcbpl(); pcbpl != nil {
mkxComponent.PCBPL = NewButtonEntity(world, pcbpl, worldData.EntityMap)
}
if pob := mkx.Pob(); pob != nil {
mkxComponent.POB = NewButtonEntity(world, pob, worldData.EntityMap)
}
if pobpl := mkx.Pobpl(); pobpl != nil {
mkxComponent.POBPL = NewButtonEntity(world, pobpl, worldData.EntityMap)
}
if pab := mkx.Pab(); pab != nil {
mkxComponent.PAB = NewButtonEntity(world, pab, worldData.EntityMap)
}
if pabpl := mkx.Pabpl(); pabpl != nil {
mkxComponent.PABPL = NewButtonEntity(world, pabpl, worldData.EntityMap)
}
if wrzf := mkx.Wrzf(); wrzf != nil {
mkxComponent.WRZF = NewButtonEntity(world, wrzf, worldData.EntityMap)
}
if wrzfpl := mkx.Wrzfpl(); wrzfpl != nil {
mkxComponent.WRZFPL = NewButtonEntity(world, wrzfpl, worldData.EntityMap)
}
if qkqr := mkx.Qkqr(); qkqr != nil {
mkxComponent.QKQR = NewButtonEntity(world, qkqr, worldData.EntityMap)
}
if qkqrpl := mkx.Qkqrpl(); qkqrpl != nil {
mkxComponent.QKQRPL = NewButtonEntity(world, qkqrpl, worldData.EntityMap)
}
if mpl := mkx.Mpl(); mpl != nil {
mkxComponent.MPL = NewButtonEntity(world, mpl, worldData.EntityMap)
}
if jxtcpl := mkx.Jxtcpl(); jxtcpl != nil {
mkxComponent.JXTCPL = NewButtonEntity(world, jxtcpl, worldData.EntityMap)
}
return entry
}
func NewPlatformMkxEntry(world ecs.World, entryMap map[string]*ecs.Entry, mkx *repository.PsdPsl) *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)
}
if wrzfj := mkx.Wrzfj(); wrzfj != nil {
circuit.WRZFJ = NewRelayEntity(world, wrzfj, entryMap)
}
if qkqrj := mkx.Qkqrj(); qkqrj != nil {
circuit.QKQRJ = NewRelayEntity(world, qkqrj, entryMap)
}
component.PlatformMkxCircuitType.Set(entry, circuit)
return entry
}