rts-sim-module/entity/ibp.go
2023-12-07 10:54:55 +08:00

210 lines
6.5 KiB
Go

package entity
import (
"fmt"
"log/slog"
"strings"
"unsafe"
"joylink.club/ecs"
"joylink.club/rtsssimulation/component"
"joylink.club/rtsssimulation/repository"
"joylink.club/rtsssimulation/repository/model/proto"
)
// 创建按钮实体
func NewButtonEntity(w ecs.World, data *repository.Button, entityMap map[string]*ecs.Entry) *ecs.Entry {
uid := data.Id()
entry, ok := entityMap[uid]
if !ok {
var btnType ecs.IComponentType
switch data.GetBtnType() {
case proto.Button_NO_Reset_Press:
btnType = component.NoResetPressBtn
case proto.Button_NO_Reset_Up:
btnType = component.NoResetUpBtn
case proto.Button_Reset_Press:
btnType = component.ResetPressBtn
case proto.Button_Reset_Up:
btnType = component.ResetUpBtn
case proto.Button_Key_Knob:
btnType = component.KeyKnob
default:
btnType = component.UnknowBtn
}
entry = w.Entry(w.Create(component.ButtonTag, btnType, component.UidType, component.BitStateType))
component.UidType.SetValue(entry, component.Uid{Id: uid})
// 带灯按钮
if data.HasLight() {
entry.AddComponent(component.SingleLightType, unsafe.Pointer(&component.SingleLight{Light: NewLightEntity(w)}))
}
entityMap[uid] = entry
}
return entry
}
// 创建蜂鸣器实体
func NewAlarmEntity(w ecs.World, alarm *repository.Alarm, entityMap map[string]*ecs.Entry) *ecs.Entry {
uid := alarm.Id()
entry, ok := entityMap[uid]
if !ok {
entry = w.Entry(w.Create(component.AlarmTag, component.UidType, component.AlarmDriveType, component.BitStateType))
component.UidType.SetValue(entry, component.Uid{Id: uid})
entityMap[uid] = entry
}
return entry
}
// 创建IBP灯实体
func NewIBPLightEntity(w ecs.World, uid string, entityMap map[string]*ecs.Entry) *ecs.Entry {
entry, ok := entityMap[uid]
if !ok {
entry = w.Entry(w.Create(component.LightTag, component.UidType, component.LightDriveType, component.BitStateType))
component.UidType.SetValue(entry, component.Uid{Id: uid})
entityMap[uid] = entry
}
return entry
}
// 创建IBP钥匙实体
func NewIBPKeyEntity(w ecs.World, uid string, entityMap map[string]*ecs.Entry) *ecs.Entry {
entry, ok := entityMap[uid]
if !ok {
entry = w.Entry(w.Create(component.KeyTag, component.UidType, component.GearStateType))
component.UidType.SetValue(entry, component.Uid{Id: uid})
entityMap[uid] = entry
}
return entry
}
// 构建人员防护实体
func LoadSPKEntity(w ecs.World, entry *ecs.Entry, datas []repository.IGroupedElectronicComponent, worldData *component.WorldData) error {
if len(datas) == 0 {
return nil
}
entityMap := worldData.EntityMap
spk := &component.SpkElectronic{}
for _, c := range datas {
switch c.Code() {
// 按钮组
case "SPKSXPLA":
spk.SPKSXPLA_BTN = NewButtonEntity(w, c.(*repository.Button), entityMap)
case "SPKSSPLA":
spk.SPKSSPLA_BTN = NewButtonEntity(w, c.(*repository.Button), entityMap)
case "SDA":
spk.SDA = NewButtonEntity(w, c.(*repository.Button), entityMap)
// 钥匙组、继电器
case "SPKSX1J":
r, ok := c.(*repository.Relay)
if ok {
spk.SPKSX1J = NewRelayEntity(w, r, entityMap)
} else {
spk.SPKSX1J_KEY = NewIBPKeyEntity(w, c.Id(), entityMap)
}
case "SPKSX3J":
r, ok := c.(*repository.Relay)
if ok {
spk.SPKSX3J = NewRelayEntity(w, r, entityMap)
} else {
spk.SPKSX3J_KEY = NewIBPKeyEntity(w, c.Id(), entityMap)
}
case "SPKSS2J":
r, ok := c.(*repository.Relay)
if ok {
spk.SPKSS2J = NewRelayEntity(w, r, entityMap)
} else {
spk.SPKSS2J_KEY = NewIBPKeyEntity(w, c.Id(), entityMap)
}
case "SPKSS4J":
r, ok := c.(*repository.Relay)
if ok {
spk.SPKSS4J = NewRelayEntity(w, r, entityMap)
} else {
spk.SPKSS4J_KEY = NewIBPKeyEntity(w, c.Id(), entityMap)
}
case "SPKSXPLAJ":
spk.SPKSXPLAJ = NewRelayEntity(w, c.(*repository.Relay), entityMap)
case "SPKSSPLAJ":
spk.SPKSSPLAJ = NewRelayEntity(w, c.(*repository.Relay), entityMap)
// 灯组
case "SPKSX1D":
spk.SPKSX1D = NewIBPLightEntity(w, c.Id(), entityMap)
case "SPKSX3D":
spk.SPKSX3D = NewIBPLightEntity(w, c.Id(), entityMap)
case "SPKSS2D":
spk.SPKSS2D = NewIBPLightEntity(w, c.Id(), entityMap)
case "SPKSS4D":
spk.SPKSS4D = NewIBPLightEntity(w, c.Id(), entityMap)
}
}
// 绑定组件
entry.AddComponent(component.SpkElectronicType, unsafe.Pointer(spk))
return nil
}
// 构建紧急关闭实体
func LoadEMPEntity(w ecs.World, entry *ecs.Entry, datas []*repository.ElectronicComponentGroup, worldData *component.WorldData) error {
if len(datas) == 0 {
return nil
}
entityMap := worldData.EntityMap
emp := &component.EmpElectronic{EMPJMap: make(map[string]*component.EmpDeviceElectronic)}
for _, g := range datas {
if g.Code() == "EMP" { // 独立设备
for _, c := range g.Components() {
switch c.Code() {
case "QBA":
emp.QBA = NewButtonEntity(w, c.(*repository.Button), entityMap)
case "SDA":
emp.SDA = NewButtonEntity(w, c.(*repository.Button), entityMap)
case "蜂鸣器":
emp.Alarm = NewAlarmEntity(w, c.(*repository.Alarm), entityMap)
default:
r, ok := c.(*repository.Relay)
if ok { // 如果是继电器
d := emp.EMPJMap[r.Code()]
if d == nil {
d = &component.EmpDeviceElectronic{}
}
d.EMPJ = NewRelayEntity(w, r, entityMap)
emp.EMPJMap[r.Code()] = d
} else {
slog.Warn("未实现设备[id:%s]逻辑", c.Id())
}
}
}
} else { // 组合设备
d := emp.EMPJMap[g.Code()]
if d == nil {
d = &component.EmpDeviceElectronic{}
}
for _, c := range g.Components() {
b, ok := c.(*repository.Button)
if strings.HasSuffix(c.Code(), "EMPD") {
d.EMPD = NewIBPLightEntity(w, c.Id(), entityMap)
} else if ok && strings.HasSuffix(c.Code(), "EMPFA") {
d.EMPFA_BTN = NewButtonEntity(w, b, entityMap)
} else if ok && strings.HasPrefix(c.Code(), "EMP") {
d.EMP_BTNS = append(d.EMP_BTNS, NewButtonEntity(w, b, entityMap))
} else {
slog.Warn("未实现设备[id:%s]逻辑", c.Id())
}
}
emp.EMPJMap[g.Code()] = d
}
}
if emp.Alarm == nil || emp.QBA == nil || emp.SDA == nil {
return fmt.Errorf("EMP组合初始化出错,请检查IBP地图组合数据")
}
for code, e := range emp.EMPJMap {
if e.EMPFA_BTN == nil || e.EMPD == nil || len(e.EMP_BTNS) == 0 {
return fmt.Errorf("组合[%s]还原按钮未关联,请检查IBP地图组合数据", code)
} else if e.EMPJ == nil {
return fmt.Errorf("组合[%s]继电器未关联,请检查继电器地图组合数据", code)
}
}
entry.AddComponent(component.EmpElectronicType, unsafe.Pointer(emp))
return nil
}