210 lines
5.3 KiB
Go
210 lines
5.3 KiB
Go
package entity
|
|
|
|
import (
|
|
"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, spkData *repository.IBPRefMap, worldData *component.WorldData) error {
|
|
entityMap := worldData.EntityMap
|
|
spk := &component.SpkElectronic{}
|
|
// 按钮组
|
|
for _, btn := range spkData.Buttons() {
|
|
e := NewButtonEntity(w, btn, entityMap)
|
|
switch btn.Code() {
|
|
case "SPKSXPLA":
|
|
spk.SPKSXPLA_BTN = e
|
|
case "SPKSSPLA":
|
|
spk.SPKSSPLA_BTN = e
|
|
case "SDA":
|
|
spk.SDA = e
|
|
}
|
|
}
|
|
// 钥匙组
|
|
for _, key := range spkData.Keys() {
|
|
e := NewIBPKeyEntity(w, key.Id(), entityMap)
|
|
switch key.Code() {
|
|
case "SPKSX1J":
|
|
spk.SPKSX1J_KEY = e
|
|
case "SPKSX3J":
|
|
spk.SPKSX3J_KEY = e
|
|
case "SPKSS2J":
|
|
spk.SPKSS2J_KEY = e
|
|
case "SPKSS4J":
|
|
spk.SPKSS4J_KEY = e
|
|
}
|
|
}
|
|
// 继电器组
|
|
for _, relay := range spkData.Relays() {
|
|
e := NewRelayEntity(w, relay, entityMap)
|
|
switch relay.Code() {
|
|
case "SPKSX1J":
|
|
spk.SPKSX1J = e
|
|
case "SPKSX3J":
|
|
spk.SPKSX3J = e
|
|
case "SPKSS2J":
|
|
spk.SPKSS2J = e
|
|
case "SPKSS4J":
|
|
spk.SPKSS4J = e
|
|
case "SPKSXPLAJ":
|
|
spk.SPKSXPLAJ = e
|
|
case "SPKSSPLAJ":
|
|
spk.SPKSSPLAJ = e
|
|
}
|
|
}
|
|
// 灯组
|
|
for _, light := range spkData.Lights() {
|
|
e := NewIBPLightEntity(w, light.Id(), entityMap)
|
|
switch light.Code() {
|
|
case "SPKSX1D":
|
|
spk.SPKSX1D = e
|
|
case "SPKSX3D":
|
|
spk.SPKSX3D = e
|
|
case "SPKSS2D":
|
|
spk.SPKSS2D = e
|
|
case "SPKSS4D":
|
|
spk.SPKSS4D = e
|
|
}
|
|
}
|
|
// 绑定组件
|
|
entry.AddComponent(component.SpkElectronicType, unsafe.Pointer(spk))
|
|
entry.AddComponent(component.SpkCollectStateType, unsafe.Pointer(&component.SpkCollectState{}))
|
|
return nil
|
|
}
|
|
|
|
// 构建紧急关闭实体
|
|
func LoadEMPEntity(w ecs.World, entry *ecs.Entry, spkData *repository.IBPRefMap, worldData *component.WorldData) error {
|
|
entityMap := worldData.EntityMap
|
|
emp := &component.EmpElectronic{}
|
|
// 按钮组
|
|
for _, btn := range spkData.Buttons() {
|
|
e := NewButtonEntity(w, btn, entityMap)
|
|
switch btn.Code() {
|
|
case "EMP1":
|
|
emp.EMP1_BTN = e
|
|
case "EMP3":
|
|
emp.EMP3_BTN = e
|
|
case "EMP5":
|
|
emp.EMP5_BTN = e
|
|
case "EMP2":
|
|
emp.EMP2_BTN = e
|
|
case "EMP4":
|
|
emp.EMP4_BTN = e
|
|
case "EMP6":
|
|
emp.EMP6_BTN = e
|
|
case "EMP-X":
|
|
emp.EMPX_BTN = e
|
|
case "EMP-S":
|
|
emp.EMPS_BTN = e
|
|
case "XEMPFA":
|
|
emp.XEMPFA_BTN = e
|
|
case "SEMPFA":
|
|
emp.SEMPFA_BTN = e
|
|
case "QBA":
|
|
emp.QBA = e
|
|
case "SDA":
|
|
emp.SDA = e
|
|
}
|
|
}
|
|
// 继电器组
|
|
for _, relay := range spkData.Relays() {
|
|
e := NewRelayEntity(w, relay, entityMap)
|
|
switch relay.Code() {
|
|
case "XEMPJ":
|
|
emp.XEMPJ = e
|
|
case "SEMPJ":
|
|
emp.SEMPJ = e
|
|
}
|
|
}
|
|
// 灯组
|
|
for _, light := range spkData.Lights() {
|
|
e := NewIBPLightEntity(w, light.Id(), entityMap)
|
|
switch light.Code() {
|
|
case "XEMPD":
|
|
emp.XEMPD = e
|
|
case "SEMPD":
|
|
emp.SEMPD = e
|
|
}
|
|
}
|
|
// 蜂鸣器
|
|
for _, alarm := range spkData.Alarms() {
|
|
switch alarm.Code() {
|
|
case "蜂鸣器":
|
|
emp.Alarm = NewAlarmEntity(w, alarm, entityMap)
|
|
}
|
|
}
|
|
entry.AddComponent(component.EmpElectronicType, unsafe.Pointer(emp))
|
|
entry.AddComponent(component.EmpCollectStateType, unsafe.Pointer(&component.EmpCollectState{}))
|
|
return nil
|
|
}
|