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(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 "X旁路": spk.SPKSXPLA_BTN = e case "S旁路": spk.SPKSSPLA_BTN = e case "人员防护试灯": spk.SDA = e } } // 钥匙组 for _, key := range spkData.Keys() { e := NewIBPKeyEntity(w, key.Id(), entityMap) switch key.Code() { case "SPKS1": spk.SPKSX1J_KEY = e case "SPKS3": spk.SPKSX3J_KEY = e case "SPKS2": spk.SPKSS2J_KEY = e case "SPKS4": 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 "启用状态_SPKS1": spk.SPKSX1D = e case "启用状态_SPKS3": spk.SPKSX3D = e case "启用状态_SPKS2": spk.SPKSS2D = e case "启用状态_SPKS4": spk.SPKSS4D = e } } // 设置默认值 setSpkDefault(spk) // 绑定组件 entry.AddComponent(component.SpkElectronicType, unsafe.Pointer(spk)) entry.AddComponent(component.SpkCollectStateType, unsafe.Pointer(&component.SpkCollectState{})) return nil } // 人员防护设置默认值 func setSpkDefault(spk *component.SpkElectronic) { setIbpKeyGearVal(spk.SPKSX1J_KEY, 1) setIbpKeyGearVal(spk.SPKSX3J_KEY, 1) setIbpKeyGearVal(spk.SPKSS2J_KEY, 1) setIbpKeyGearVal(spk.SPKSS4J_KEY, 1) } // 设置IBP钥匙状态 func setIbpKeyGearVal(entry *ecs.Entry, val int32) { if entry == nil { return } btn := component.GearStateType.Get(entry) btn.Val = val } // 构建紧急关闭实体 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 "X紧急关闭": emp.EMPX_BTN = e case "S紧急关闭": emp.EMPS_BTN = e case "X紧急关闭复原": emp.XEMPFA_BTN = e case "S紧急关闭复原": emp.SEMPFA_BTN = e case "切断警铃": emp.QBA = e case "信号试灯": 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 "X紧急关闭": emp.XEMPD = e case "S紧急关闭": emp.SEMPD = e } } // 蜂鸣器 for _, alarm := range spkData.Alarms() { switch alarm.Code() { case "报警电铃": emp.Alarm = NewAlarmEntity(w, alarm, entityMap) } } // 设置默认值 setEmpDefault(emp) entry.AddComponent(component.EmpElectronicType, unsafe.Pointer(emp)) entry.AddComponent(component.EmpCollectStateType, unsafe.Pointer(&component.EmpCollectState{})) return nil } // 紧急关闭设置默认值 func setEmpDefault(emp *component.EmpElectronic) { setIbpBtnVal(emp.EMP1_BTN, true) setIbpBtnVal(emp.EMP3_BTN, true) setIbpBtnVal(emp.EMP5_BTN, true) setIbpBtnVal(emp.EMPX_BTN, true) setIbpBtnVal(emp.EMP2_BTN, true) setIbpBtnVal(emp.EMP4_BTN, true) setIbpBtnVal(emp.EMP6_BTN, true) setIbpBtnVal(emp.EMPS_BTN, true) } // 获取按钮状态 func setIbpBtnVal(entry *ecs.Entry, val bool) { if entry == nil { return } btn := component.BitStateType.Get(entry) btn.Val = val }