rts-sim-module/entity/ibp.go

219 lines
6.5 KiB
Go
Raw Normal View History

2023-10-12 13:47:57 +08:00
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:
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-12 17:46:43 +08:00
entry = w.Entry(w.Create(btnType, component.UidType, component.BitStateType))
component.UidType.SetValue(entry, component.Uid{Id: uid})
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-12 17:46:43 +08:00
entry = w.Entry(w.Create(component.UidType, component.AlarmDriveType, component.BitStateType))
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 {
entry = w.Entry(w.Create(component.HdTag, component.UidType, component.LightDriveType, component.BitStateType))
component.UidType.SetValue(entry, component.Uid{Id: uid})
entityMap[uid] = entry
}
return entry
}
2023-10-12 13:47:57 +08:00
// 构建人员防护实体
2023-10-17 15:05:13 +08:00
func LoadSPKEntity(w ecs.World, entry *ecs.Entry, spkData *repository.IBPRefMap, worldData *component.WorldData) error {
entityMap := worldData.EntityMap
2023-10-12 13:47:57 +08:00
spk := &component.SpkElectronic{}
// 按钮组
2023-10-12 17:46:43 +08:00
for _, btn := range spkData.Buttons() {
2023-10-13 16:24:28 +08:00
switch btn.Code() {
2023-10-12 17:46:43 +08:00
case "SPKSXPLA":
spk.SPKSXPLA_BTN = NewButtonEntity(w, btn, entityMap)
case "SPKSSPLA":
spk.SPKSSPLA_BTN = NewButtonEntity(w, btn, entityMap)
case "SPKS1":
spk.SPKSX1J_KEY = NewButtonEntity(w, btn, entityMap)
case "SPKS3":
spk.SPKSX3J_KEY = NewButtonEntity(w, btn, entityMap)
case "SPKS2":
spk.SPKSS2J_KEY = NewButtonEntity(w, btn, entityMap)
case "SPKS4":
spk.SPKSS4J_KEY = NewButtonEntity(w, btn, entityMap)
2023-10-17 17:47:19 +08:00
case "人员防护试灯":
2023-10-12 17:46:43 +08:00
spk.SDA = NewButtonEntity(w, btn, entityMap)
}
}
2023-10-12 13:47:57 +08:00
// 继电器组
2023-10-12 17:46:43 +08:00
for _, relay := range spkData.Relays() {
switch relay.Code() {
case "SPKSX1J":
spk.SPKSX1J = NewRelayEntity(w, relay, entityMap)
case "SPKSX3J":
spk.SPKSX3J = NewRelayEntity(w, relay, entityMap)
case "SPKSS2J":
spk.SPKSS2J = NewRelayEntity(w, relay, entityMap)
case "SPKSS4J":
spk.SPKSS4J = NewRelayEntity(w, relay, entityMap)
case "SPKSXPLAJ":
spk.SPKSXPLAJ = NewRelayEntity(w, relay, entityMap)
case "SPKSSPLAJ":
spk.SPKSSPLAJ = NewRelayEntity(w, relay, entityMap)
}
}
2023-10-12 13:47:57 +08:00
// 灯组
2023-10-17 17:47:19 +08:00
for _, light := range spkData.Lights() {
switch light.Code() {
case "启用状态_SPKS1":
spk.SPKSX1D = NewIBPLightEntity(w, light.Id(), entityMap)
case "启用状态_SPKS3":
spk.SPKSX3D = NewIBPLightEntity(w, light.Id(), entityMap)
case "启用状态_SPKS2":
spk.SPKSS2D = NewIBPLightEntity(w, light.Id(), entityMap)
case "启用状态_SPKS4":
spk.SPKSS4D = NewIBPLightEntity(w, light.Id(), entityMap)
}
}
// 旁路灯组
2023-10-12 13:47:57 +08:00
spk.SPKSXPLAD = NewLightHEntity(w)
spk.SPKSSPLAD = NewLightHEntity(w)
2023-10-12 17:46:43 +08:00
// 设置默认值
setSpkDefault(spk)
// 绑定组件
2023-10-12 13:47:57 +08:00
entry.AddComponent(component.SpkElectronicType, unsafe.Pointer(spk))
entry.AddComponent(component.SpkCollectStateType, unsafe.Pointer(&component.SpkCollectState{}))
return nil
}
2023-10-12 17:46:43 +08:00
// 人员防护设置默认值
func setSpkDefault(spk *component.SpkElectronic) {
setBtnVal(spk.SPKSX1J_KEY, true)
setBtnVal(spk.SPKSX3J_KEY, true)
setBtnVal(spk.SPKSS2J_KEY, true)
setBtnVal(spk.SPKSS4J_KEY, true)
}
2023-10-12 13:47:57 +08:00
// 构建紧急关闭实体
2023-10-17 15:05:13 +08:00
func LoadEMPEntity(w ecs.World, entry *ecs.Entry, spkData *repository.IBPRefMap, worldData *component.WorldData) error {
entityMap := worldData.EntityMap
2023-10-12 13:47:57 +08:00
emp := &component.EmpElectronic{}
// 按钮组
2023-10-12 17:46:43 +08:00
for _, btn := range spkData.Buttons() {
2023-10-13 16:24:28 +08:00
switch btn.Code() {
2023-10-12 17:46:43 +08:00
case "EMP1":
emp.EMP1_BTN = NewButtonEntity(w, btn, entityMap)
case "EMP3":
emp.EMP3_BTN = NewButtonEntity(w, btn, entityMap)
case "EMP5":
emp.EMP5_BTN = NewButtonEntity(w, btn, entityMap)
case "EMP2":
emp.EMP2_BTN = NewButtonEntity(w, btn, entityMap)
case "EMP4":
emp.EMP4_BTN = NewButtonEntity(w, btn, entityMap)
case "EMP6":
emp.EMP6_BTN = NewButtonEntity(w, btn, entityMap)
2023-10-17 17:47:19 +08:00
case "X紧急关闭":
emp.EMPX_BTN = NewButtonEntity(w, btn, entityMap)
case "S紧急关闭":
2023-10-12 17:46:43 +08:00
emp.EMPS_BTN = NewButtonEntity(w, btn, entityMap)
2023-10-17 17:47:19 +08:00
case "X紧急关闭复原":
2023-10-12 17:46:43 +08:00
emp.XEMPFA_BTN = NewButtonEntity(w, btn, entityMap)
2023-10-17 17:47:19 +08:00
case "S紧急关闭复原":
2023-10-12 17:46:43 +08:00
emp.SEMPFA_BTN = NewButtonEntity(w, btn, entityMap)
2023-10-17 17:47:19 +08:00
case "切断警铃":
2023-10-12 17:46:43 +08:00
emp.QBA = NewButtonEntity(w, btn, entityMap)
2023-10-17 17:47:19 +08:00
case "信号试灯":
2023-10-12 17:46:43 +08:00
emp.SDA = NewButtonEntity(w, btn, entityMap)
}
}
2023-10-12 13:47:57 +08:00
// 继电器组
2023-10-12 17:46:43 +08:00
for _, relay := range spkData.Relays() {
switch relay.Code() {
case "XEMPJ":
emp.XEMPJ = NewRelayEntity(w, relay, entityMap)
case "SEMPJ":
emp.SEMPJ = NewRelayEntity(w, relay, entityMap)
}
}
2023-10-12 13:47:57 +08:00
// 灯组
2023-10-17 17:47:19 +08:00
for _, light := range spkData.Lights() {
switch light.Code() {
case "XEMPD":
emp.XEMPJ = NewIBPLightEntity(w, light.Id(), entityMap)
case "SEMPD":
emp.SEMPJ = NewIBPLightEntity(w, light.Id(), entityMap)
}
}
2023-10-12 13:47:57 +08:00
// 蜂鸣器
2023-10-12 17:46:43 +08:00
for _, alarm := range spkData.Alarms() {
switch alarm.Code() {
2023-10-17 17:47:19 +08:00
case "报警电铃":
2023-10-12 17:46:43 +08:00
emp.Alarm = NewAlarmEntity(w, alarm, entityMap)
}
}
// 设置默认值
setEmpDefault(emp)
2023-10-12 13:47:57 +08:00
entry.AddComponent(component.EmpElectronicType, unsafe.Pointer(emp))
entry.AddComponent(component.EmpCollectStateType, unsafe.Pointer(&component.EmpCollectState{}))
return nil
}
2023-10-12 17:46:43 +08:00
// 紧急关闭设置默认值
func setEmpDefault(emp *component.EmpElectronic) {
setBtnVal(emp.EMP1_BTN, true)
setBtnVal(emp.EMP3_BTN, true)
setBtnVal(emp.EMP5_BTN, true)
setBtnVal(emp.EMPX_BTN, true)
setBtnVal(emp.EMP2_BTN, true)
setBtnVal(emp.EMP4_BTN, true)
setBtnVal(emp.EMP6_BTN, true)
setBtnVal(emp.EMPS_BTN, true)
}
// 获取按钮状态
func setBtnVal(entry *ecs.Entry, val bool) {
if entry == nil {
return
}
btn := component.BitStateType.Get(entry)
btn.Val = val
}