rts-sim-module/entity/xcj.go

67 lines
2.0 KiB
Go
Raw Permalink Normal View History

package entity
import (
"errors"
"fmt"
"joylink.club/ecs"
"joylink.club/rtsssimulation/component"
"joylink.club/rtsssimulation/repository"
"strconv"
"strings"
"unsafe"
)
var XcjBaseComponentTypes = []ecs.IComponentType{component.UidType, component.XcjTag}
func LoadXcj(w ecs.World) error {
wd := GetWorldData(w)
for _, xcj := range wd.Repo.XcjList() {
//创建基础entry
entry := w.Entry(w.Create(XcjBaseComponentTypes...))
component.UidType.SetValue(entry, component.Uid{Id: xcj.Id()})
wd.EntityMap[xcj.Id()] = entry
//加载电路
if len(xcj.ComponentGroups()) != 0 {
circuit := &component.XcjCircuit{
TWJList: make([]*ecs.Entry, xcj.NumSegments),
CFJList: make([]*ecs.Entry, xcj.NumSegments),
}
entry.AddComponent(component.XcjCircuitType, unsafe.Pointer(circuit))
for _, group := range xcj.ComponentGroups() {
for _, ec := range group.Components() {
relay := ec.(*repository.Relay)
switch ec.Code() {
case "XQJ":
circuit.XQJ = NewRelayEntity(w, relay, wd.EntityMap)
case "TGQJ":
circuit.TGQJ = NewRelayEntity(w, relay, wd.EntityMap)
case "XCJXJ":
circuit.XCJXJ = NewRelayEntity(w, relay, wd.EntityMap)
case "XCYXJ":
circuit.XCYXJ = NewRelayEntity(w, relay, wd.EntityMap)
case "JTJ":
circuit.JTJ = NewRelayEntity(w, relay, wd.EntityMap)
case "TGYXJ":
circuit.TGYXJ = NewRelayEntity(w, relay, wd.EntityMap)
default:
if strings.Contains(ec.Code(), "TWJ") {
index, err := strconv.Atoi(ec.Code()[3:])
if err != nil {
return errors.New(fmt.Sprintf("洗车机继电器[%s]编号异常", ec.Code()))
}
circuit.TWJList[index-1] = NewRelayEntity(w, relay, wd.EntityMap)
} else if strings.Contains(ec.Code(), "CFJ") {
index, err := strconv.Atoi(ec.Code()[3:])
if err != nil {
return errors.New(fmt.Sprintf("洗车机继电器[%s]编号异常", ec.Code()))
}
circuit.CFJList[index-1] = NewRelayEntity(w, relay, wd.EntityMap)
}
}
}
}
}
}
return nil
}