package entity import ( "unsafe" "joylink.club/ecs" "joylink.club/rtsssimulation/component" "joylink.club/rtsssimulation/repository" "joylink.club/rtsssimulation/repository/model/proto" ) // 创建IBP实体 func NewIBPEntity(w ecs.World, ibp *repository.IBP, entityMap map[string]*ecs.Entry) { entry := w.Entry(w.Create(component.IBP)) loadSPKEntity(w, entry, ibp.Spk, entityMap) loadEMPEntity(w, entry, ibp.Emp, entityMap) } // 创建按钮实体 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(btnType, component.UidType, component.BitStateType)) component.UidType.SetValue(entry, component.Uid{Id: uid}) 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.UidType, component.AlarmDriveType, component.BitStateType)) component.UidType.SetValue(entry, component.Uid{Id: uid}) entityMap[uid] = entry } return entry } // 构建人员防护实体 func loadSPKEntity(w ecs.World, entry *ecs.Entry, spkData *repository.IBPRefMap, entityMap map[string]*ecs.Entry) error { spk := &component.SpkElectronic{} // 按钮组 for _, btn := range spkData.Buttons() { switch btn.Code() { 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) case "SDA": spk.SDA = NewButtonEntity(w, btn, entityMap) } } // 继电器组 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) } } // 灯组 spk.SPKSX1D = NewLightHEntity(w) spk.SPKSX3D = NewLightHEntity(w) spk.SPKSS2D = NewLightHEntity(w) spk.SPKSS4D = NewLightHEntity(w) spk.SPKSXPLAD = NewLightHEntity(w) spk.SPKSSPLAD = NewLightHEntity(w) // 设置默认值 setSpkDefault(spk) // 绑定组件 entry.AddComponent(component.SpkElectronicType, unsafe.Pointer(spk)) entry.AddComponent(component.SpkCollectStateType, unsafe.Pointer(&component.SpkCollectState{})) return nil } // 人员防护设置默认值 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) } // 构建紧急关闭实体 func loadEMPEntity(w ecs.World, entry *ecs.Entry, spkData *repository.IBPRefMap, entityMap map[string]*ecs.Entry) error { emp := &component.EmpElectronic{} // 按钮组 for _, btn := range spkData.Buttons() { switch btn.Code() { 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 "EMPX": emp.EMPX_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) case "EMPS": emp.EMPS_BTN = NewButtonEntity(w, btn, entityMap) case "XEMPFA": emp.XEMPFA_BTN = NewButtonEntity(w, btn, entityMap) case "SEMPFA": emp.SEMPFA_BTN = NewButtonEntity(w, btn, entityMap) case "QBA": emp.QBA = NewButtonEntity(w, btn, entityMap) case "SDA": emp.SDA = NewButtonEntity(w, btn, entityMap) } } // 继电器组 for _, relay := range spkData.Relays() { switch relay.Code() { case "XEMPJ": emp.XEMPJ = NewRelayEntity(w, relay, entityMap) case "SEMPJ": emp.SEMPJ = NewRelayEntity(w, relay, entityMap) } } // 灯组 emp.XEMPD = NewLightHEntity(w) emp.SEMPD = NewLightHEntity(w) // 蜂鸣器 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) { 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 }