rts-sim-module/entity/mkx.go

79 lines
2.6 KiB
Go

package entity
import (
"fmt"
"joylink.club/ecs"
"joylink.club/rtsssimulation/component"
"joylink.club/rtsssimulation/repository"
"joylink.club/rtsssimulation/repository/model/proto"
"strings"
)
func LoadMkx(w ecs.World) error {
data := GetWorldData(w)
mkxs := data.Repo.MkxList()
for _, mkx := range mkxs {
entry := NewMkxEntry(w, mkx, data)
loadMkxCircuit(w, entry, mkx, data.EntityMap)
}
return nil
}
func loadMkxCircuit(world ecs.World, entry *ecs.Entry, mkx *repository.Mkx, entryMap map[string]*ecs.Entry) {
if len(mkx.ComponentGroups()) == 0 {
return
}
circuit := &component.MkxCircuit{}
for _, group := range mkx.ComponentGroups() {
for _, ec := range group.Components() {
relay := ec.(*repository.Relay)
if strings.Contains(ec.Code(), "PCBJ") {
circuit.Pcbj = NewRelayEntity(world, relay, entryMap)
} else if strings.Contains(ec.Code(), "POBJ") {
circuit.Pobj = NewRelayEntity(world, relay, entryMap)
} else if strings.Contains(ec.Code(), "PABJ") {
circuit.Pabj = NewRelayEntity(world, relay, entryMap)
} else {
println(fmt.Sprintf("未知的门控箱继电器[%s]", ec.Id()))
}
}
}
for _, button := range mkx.PcbButtons() {
box := NewMkxBox(world, button, entryMap)
circuit.PcbList = append(circuit.PcbList, box)
}
for _, button := range mkx.PobButtons() {
box := NewMkxBox(world, button, entryMap)
circuit.PobList = append(circuit.PobList, box)
}
for _, button := range mkx.PabButtons() {
box := NewMkxBox(world, button, entryMap)
circuit.PabList = append(circuit.PabList, box)
}
component.MkxCircuitType.Set(entry, circuit)
}
func NewMkxEntry(world ecs.World, mkx *repository.Mkx, worldData *component.WorldData) *ecs.Entry {
entry, ok := worldData.EntityMap[mkx.Id()]
if !ok {
entry = world.Entry(world.Create(component.MkxTag, component.UidType, component.MkxInfoType, component.MkxCircuitType,
component.MkxCollectionCircuitType))
component.UidType.SetValue(entry, component.Uid{Id: mkx.Id()})
component.MkxInfoType.SetValue(entry, component.MkxInfo{PsdId: mkx.PsdId()})
worldData.EntityMap[mkx.Id()] = entry
}
return entry
}
func NewMkxBox(world ecs.World, btn *repository.Button, entryMap map[string]*ecs.Entry) *ecs.Entry {
mplButton := repository.NewButton(btn.Id()+"mpl", "mpl", proto.Button_NO_Reset_Press)
box := &component.MkxBox{
Btn: NewButtonEntity(world, btn, entryMap),
MkxplBtn: NewButtonEntity(world, mplButton, entryMap),
}
entry := world.Entry(world.Create(component.MkxBoxType))
component.MkxBoxType.Set(entry, box)
component.BitStateType.SetValue(box.MkxplBtn, component.BitState{Val: true})
return entry
}