rts-sim-module/entity/mkx.go

71 lines
2.4 KiB
Go

package entity
import (
"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) {
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)
}
}
}
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.UidType.SetValue(entry, component.Uid{Id: mkx.Id()})
component.MkxInfoType.SetValue(entry, component.MkxInfo{PlatformId: mkx.PlatformId()})
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_Up)
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)
return entry
}