rts-sim-module/entity/ibp.go

210 lines
6.5 KiB
Go
Raw Normal View History

2023-10-12 13:47:57 +08:00
package entity
import (
2023-12-06 16:43:29 +08:00
"fmt"
"log/slog"
"strings"
2023-10-12 13:47:57 +08:00
"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:
2023-10-12 15:03:11 +08:00
btnType = component.KeyKnob
2023-10-12 13:47:57 +08:00
default:
btnType = component.UnknowBtn
}
2023-10-18 11:17:08 +08:00
entry = w.Entry(w.Create(component.ButtonTag, btnType, component.UidType, component.BitStateType))
2023-10-12 17:46:43 +08:00
component.UidType.SetValue(entry, component.Uid{Id: uid})
2023-10-18 11:17:08 +08:00
// 带灯按钮
2023-10-18 14:08:10 +08:00
if data.HasLight() {
2023-11-02 13:21:14 +08:00
entry.AddComponent(component.SingleLightType, unsafe.Pointer(&component.SingleLight{Light: NewLightEntity(w)}))
2023-10-18 11:17:08 +08:00
}
2023-10-12 13:47:57 +08:00
entityMap[uid] = entry
}
return entry
}
// 创建蜂鸣器实体
2023-10-12 17:46:43 +08:00
func NewAlarmEntity(w ecs.World, alarm *repository.Alarm, entityMap map[string]*ecs.Entry) *ecs.Entry {
uid := alarm.Id()
2023-10-12 13:47:57 +08:00
entry, ok := entityMap[uid]
if !ok {
2023-10-18 11:17:08 +08:00
entry = w.Entry(w.Create(component.AlarmTag, component.UidType, component.AlarmDriveType, component.BitStateType))
2023-10-12 17:46:43 +08:00
component.UidType.SetValue(entry, component.Uid{Id: uid})
2023-10-12 13:47:57 +08:00
entityMap[uid] = entry
}
return entry
}
2023-10-17 17:47:19 +08:00
// 创建IBP灯实体
func NewIBPLightEntity(w ecs.World, uid string, entityMap map[string]*ecs.Entry) *ecs.Entry {
entry, ok := entityMap[uid]
if !ok {
2023-10-19 16:38:46 +08:00
entry = w.Entry(w.Create(component.LightTag, component.UidType, component.LightDriveType, component.BitStateType))
2023-10-17 17:47:19 +08:00
component.UidType.SetValue(entry, component.Uid{Id: uid})
entityMap[uid] = entry
}
return entry
}
2023-10-20 13:13:44 +08:00
// 创建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
}
2023-10-12 13:47:57 +08:00
// 构建人员防护实体
2023-12-06 16:43:29 +08:00
func LoadSPKEntity(w ecs.World, entry *ecs.Entry, datas []repository.IGroupedElectronicComponent, worldData *component.WorldData) error {
if len(datas) == 0 {
return nil
}
2023-10-17 15:05:13 +08:00
entityMap := worldData.EntityMap
2023-10-12 13:47:57 +08:00
spk := &component.SpkElectronic{}
2023-12-06 16:43:29 +08:00
for _, c := range datas {
switch c.Code() {
// 按钮组
2023-11-20 16:09:28 +08:00
case "SPKSXPLA":
2023-12-06 16:43:29 +08:00
spk.SPKSXPLA_BTN = NewButtonEntity(w, c.(*repository.Button), entityMap)
2023-11-20 16:09:28 +08:00
case "SPKSSPLA":
2023-12-06 16:43:29 +08:00
spk.SPKSSPLA_BTN = NewButtonEntity(w, c.(*repository.Button), entityMap)
2023-11-20 16:09:28 +08:00
case "SDA":
2023-12-06 16:43:29 +08:00
spk.SDA = NewButtonEntity(w, c.(*repository.Button), entityMap)
// 钥匙组、继电器
2023-10-12 17:46:43 +08:00
case "SPKSX1J":
2023-12-06 16:43:29 +08:00
r, ok := c.(*repository.Relay)
if ok {
spk.SPKSX1J = NewRelayEntity(w, r, entityMap)
} else {
spk.SPKSX1J_KEY = NewIBPKeyEntity(w, c.Id(), entityMap)
}
2023-10-12 17:46:43 +08:00
case "SPKSX3J":
2023-12-06 16:43:29 +08:00
r, ok := c.(*repository.Relay)
if ok {
spk.SPKSX3J = NewRelayEntity(w, r, entityMap)
} else {
spk.SPKSX3J_KEY = NewIBPKeyEntity(w, c.Id(), entityMap)
}
2023-10-12 17:46:43 +08:00
case "SPKSS2J":
2023-12-06 16:43:29 +08:00
r, ok := c.(*repository.Relay)
if ok {
spk.SPKSS2J = NewRelayEntity(w, r, entityMap)
} else {
spk.SPKSS2J_KEY = NewIBPKeyEntity(w, c.Id(), entityMap)
}
2023-10-12 17:46:43 +08:00
case "SPKSS4J":
2023-12-06 16:43:29 +08:00
r, ok := c.(*repository.Relay)
if ok {
spk.SPKSS4J = NewRelayEntity(w, r, entityMap)
} else {
spk.SPKSS4J_KEY = NewIBPKeyEntity(w, c.Id(), entityMap)
}
2023-10-12 17:46:43 +08:00
case "SPKSXPLAJ":
2023-12-06 16:43:29 +08:00
spk.SPKSXPLAJ = NewRelayEntity(w, c.(*repository.Relay), entityMap)
2023-10-12 17:46:43 +08:00
case "SPKSSPLAJ":
2023-12-06 16:43:29 +08:00
spk.SPKSSPLAJ = NewRelayEntity(w, c.(*repository.Relay), entityMap)
// 灯组
2023-11-20 16:09:28 +08:00
case "SPKSX1D":
2023-12-06 16:43:29 +08:00
spk.SPKSX1D = NewIBPLightEntity(w, c.Id(), entityMap)
2023-11-20 16:09:28 +08:00
case "SPKSX3D":
2023-12-06 16:43:29 +08:00
spk.SPKSX3D = NewIBPLightEntity(w, c.Id(), entityMap)
2023-11-20 16:09:28 +08:00
case "SPKSS2D":
2023-12-06 16:43:29 +08:00
spk.SPKSS2D = NewIBPLightEntity(w, c.Id(), entityMap)
2023-11-20 16:09:28 +08:00
case "SPKSS4D":
2023-12-06 16:43:29 +08:00
spk.SPKSS4D = NewIBPLightEntity(w, c.Id(), entityMap)
2023-10-17 17:47:19 +08:00
}
}
2023-12-06 16:43:29 +08:00
2023-10-12 17:46:43 +08:00
// 绑定组件
2023-10-12 13:47:57 +08:00
entry.AddComponent(component.SpkElectronicType, unsafe.Pointer(spk))
return nil
}
// 构建紧急关闭实体
2023-12-06 16:43:29 +08:00
func LoadEMPEntity(w ecs.World, entry *ecs.Entry, datas []*repository.ElectronicComponentGroup, worldData *component.WorldData) error {
if len(datas) == 0 {
return nil
2023-10-12 17:46:43 +08:00
}
2023-12-06 16:43:29 +08:00
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 {
2023-12-07 10:54:55 +08:00
slog.Warn("未实现设备[id:%s]逻辑", c.Id())
2023-12-06 16:43:29 +08:00
}
}
}
} 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)
2023-12-07 14:07:53 +08:00
} else if ok && (strings.HasPrefix(c.Code(), "EMP") || strings.HasPrefix(c.Code(), "ESB")) {
2023-12-06 16:43:29 +08:00
d.EMP_BTNS = append(d.EMP_BTNS, NewButtonEntity(w, b, entityMap))
} else {
2023-12-07 10:54:55 +08:00
slog.Warn("未实现设备[id:%s]逻辑", c.Id())
2023-12-06 16:43:29 +08:00
}
}
emp.EMPJMap[g.Code()] = d
2023-10-12 17:46:43 +08:00
}
}
2023-12-06 16:43:29 +08:00
if emp.Alarm == nil || emp.QBA == nil || emp.SDA == nil {
2023-12-07 10:54:55 +08:00
return fmt.Errorf("EMP组合初始化出错,请检查IBP地图组合数据")
2023-10-17 17:47:19 +08:00
}
2023-12-06 16:43:29 +08:00
for code, e := range emp.EMPJMap {
if e.EMPFA_BTN == nil || e.EMPD == nil || len(e.EMP_BTNS) == 0 {
2023-12-07 10:54:55 +08:00
return fmt.Errorf("组合[%s]还原按钮未关联,请检查IBP地图组合数据", code)
2023-12-06 16:43:29 +08:00
} else if e.EMPJ == nil {
2023-12-07 10:54:55 +08:00
return fmt.Errorf("组合[%s]继电器未关联,请检查继电器地图组合数据", code)
2023-10-12 17:46:43 +08:00
}
}
2023-10-12 13:47:57 +08:00
entry.AddComponent(component.EmpElectronicType, unsafe.Pointer(emp))
return nil
}