2023-09-27 18:39:18 +08:00
|
|
|
package entity
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2023-09-28 14:34:00 +08:00
|
|
|
"strings"
|
|
|
|
"unsafe"
|
2023-09-27 18:39:18 +08:00
|
|
|
|
|
|
|
"joylink.club/ecs"
|
|
|
|
"joylink.club/rtsssimulation/component"
|
|
|
|
"joylink.club/rtsssimulation/repository"
|
|
|
|
"joylink.club/rtsssimulation/repository/model/proto"
|
|
|
|
)
|
|
|
|
|
|
|
|
// 加载道岔实体
|
|
|
|
func LoadTurnouts(w ecs.World) error {
|
|
|
|
data := GetWorldData(w)
|
|
|
|
turnouts := data.Repo.TurnoutList()
|
|
|
|
for _, turnout := range turnouts {
|
|
|
|
entry := NewTurnoutEntity(w, turnout.Id(), data)
|
|
|
|
var err error
|
|
|
|
switch turnout.SwitchMachineType() {
|
|
|
|
case proto.Turnout_ZDJ9_Single:
|
|
|
|
err = LoadTurnoutZdj9One(w, turnout, entry)
|
|
|
|
case proto.Turnout_ZDJ9_Double:
|
2023-09-28 14:34:00 +08:00
|
|
|
err = LoadTurnoutZdj9Two(w, turnout, entry, data.EntityMap)
|
2023-09-27 18:39:18 +08:00
|
|
|
default:
|
2023-09-28 16:36:25 +08:00
|
|
|
err = fmt.Errorf("id=%s的道岔没有转辙机型号", turnout.Id())
|
2023-09-27 18:39:18 +08:00
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// 加载道岔ZDJ9单机转辙机
|
|
|
|
func LoadTurnoutZdj9One(w ecs.World, turnout *repository.Turnout, entry *ecs.Entry) error {
|
|
|
|
panic("unimplemented")
|
|
|
|
}
|
|
|
|
|
|
|
|
// 加载道岔ZDJ9双机转辙机
|
2023-09-28 14:34:00 +08:00
|
|
|
func LoadTurnoutZdj9Two(w ecs.World, turnout *repository.Turnout, entry *ecs.Entry, entityMap map[string]*ecs.Entry) error {
|
|
|
|
// 加载转辙机
|
|
|
|
entrys := w.CreateMany(2, component.ZzjStateType, component.TwoPositionTransformType)
|
|
|
|
// 给道岔添加转辙机引用组件
|
|
|
|
entry.AddComponent(component.TurnoutZzjType, unsafe.Pointer(&component.TurnoutZzj{
|
|
|
|
ZzjList: entrys,
|
|
|
|
}))
|
|
|
|
|
|
|
|
// 继电器组合电路
|
|
|
|
groups := turnout.RelayGroups()
|
|
|
|
size := len(groups)
|
|
|
|
if size == 3 { // 有继电器组合,加载继电器实体和电路相关组件
|
|
|
|
zdj9TwoElectronic := &component.Zdj9TwoElectronic{}
|
|
|
|
for _, group := range groups {
|
|
|
|
elecs := group.Components()
|
|
|
|
if group.Code() == "TDC" {
|
|
|
|
for _, elec := range elecs {
|
|
|
|
relay := elec.(*repository.Relay)
|
|
|
|
if relay.Code() == "DCJ" {
|
|
|
|
zdj9TwoElectronic.TDC_DCJ = NewRelayEntity(w, relay, entityMap)
|
|
|
|
} else if relay.Code() == "FCJ" {
|
|
|
|
zdj9TwoElectronic.TDC_FCJ = NewRelayEntity(w, relay, entityMap)
|
|
|
|
} else if relay.Code() == "YCJ" {
|
|
|
|
zdj9TwoElectronic.TDC_YCJ = NewRelayEntity(w, relay, entityMap)
|
|
|
|
} else if relay.Code() == "ZDBJ" {
|
|
|
|
zdj9TwoElectronic.TDC_ZDBJ = NewRelayEntity(w, relay, entityMap)
|
|
|
|
} else if relay.Code() == "ZFBJ" {
|
|
|
|
zdj9TwoElectronic.TDC_ZFBJ = NewRelayEntity(w, relay, entityMap)
|
|
|
|
} else {
|
|
|
|
return fmt.Errorf("未知的道岔[%s]的组合[%s]继电器: %s", turnout.Id(), group.Code(), relay.Code())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if group.Code() == "TDFJ1" {
|
|
|
|
for _, elec := range elecs {
|
|
|
|
if elec.Code() == "1DQJ" {
|
|
|
|
zdj9TwoElectronic.TDFJ1_1DQJ = NewRelayEntity(w, elec.(*repository.Relay), entityMap)
|
|
|
|
} else if elec.Code() == "BHJ" {
|
|
|
|
zdj9TwoElectronic.TDFJ1_BHJ = NewRelayEntity(w, elec.(*repository.Relay), entityMap)
|
|
|
|
} else if elec.Code() == "2DQJ" {
|
|
|
|
zdj9TwoElectronic.TDFJ1_2DQJ = NewRelayEntity(w, elec.(*repository.Relay), entityMap)
|
|
|
|
} else if elec.Code() == "1DQJF" {
|
|
|
|
zdj9TwoElectronic.TDFJ1_1DQJF = NewRelayEntity(w, elec.(*repository.Relay), entityMap)
|
|
|
|
} else if elec.Code() == "DBQ" { // 断相保护器
|
|
|
|
zdj9TwoElectronic.TDFJ1_DBQ = NewDBQEntity(w, elec.Id(), entityMap)
|
|
|
|
} else if elec.Code() == "DBJ" {
|
|
|
|
zdj9TwoElectronic.TDFJ1_DBJ = NewRelayEntity(w, elec.(*repository.Relay), entityMap)
|
|
|
|
} else if elec.Code() == "FBJ" {
|
|
|
|
zdj9TwoElectronic.TDFJ1_FBJ = NewRelayEntity(w, elec.(*repository.Relay), entityMap)
|
|
|
|
} else if elec.Code() == "QDJ" {
|
|
|
|
zdj9TwoElectronic.TDFJ1_QDJ = NewRelayEntity(w, elec.(*repository.Relay), entityMap)
|
|
|
|
} else if elec.Code() == "ZBHJ" {
|
|
|
|
zdj9TwoElectronic.TDFJ1_ZBHJ = NewRelayEntity(w, elec.(*repository.Relay), entityMap)
|
|
|
|
} else {
|
|
|
|
return fmt.Errorf("未知的道岔[%s]的组合[%s]继电器: %s", turnout.Id(), group.Code(), elec.Code())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if group.Code() == "TDFJ2" {
|
|
|
|
for _, elec := range elecs {
|
|
|
|
if elec.Code() == "1DQJ" {
|
|
|
|
zdj9TwoElectronic.TDFJ2_1DQJ = NewRelayEntity(w, elec.(*repository.Relay), entityMap)
|
|
|
|
} else if elec.Code() == "BHJ" {
|
|
|
|
zdj9TwoElectronic.TDFJ2_BHJ = NewRelayEntity(w, elec.(*repository.Relay), entityMap)
|
|
|
|
} else if elec.Code() == "2DQJ" {
|
|
|
|
zdj9TwoElectronic.TDFJ2_2DQJ = NewRelayEntity(w, elec.(*repository.Relay), entityMap)
|
|
|
|
} else if elec.Code() == "1DQJF" {
|
|
|
|
zdj9TwoElectronic.TDFJ2_1DQJF = NewRelayEntity(w, elec.(*repository.Relay), entityMap)
|
|
|
|
} else if elec.Code() == "DBQ" { // 断相保护器
|
|
|
|
zdj9TwoElectronic.TDFJ2_DBQ = NewDBQEntity(w, elec.Id(), entityMap)
|
|
|
|
} else if elec.Code() == "DBJ" {
|
|
|
|
zdj9TwoElectronic.TDFJ2_DBJ = NewRelayEntity(w, elec.(*repository.Relay), entityMap)
|
|
|
|
} else if elec.Code() == "FBJ" {
|
|
|
|
zdj9TwoElectronic.TDFJ2_FBJ = NewRelayEntity(w, elec.(*repository.Relay), entityMap)
|
|
|
|
} else {
|
|
|
|
return fmt.Errorf("未知的道岔[%s]的组合[%s]继电器: %s", turnout.Id(), group.Code(), elec.Code())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return fmt.Errorf("未知的道岔[%s]组合类型:%s", turnout.Id(), group.Code())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
nils := zdj9TwoElectronic.CheckNilReference()
|
|
|
|
if len(nils) > 0 {
|
|
|
|
return fmt.Errorf("未知的道岔[%s]ZDJ9双机继电器组合数据异常,缺失:[%s]", turnout.Id(), strings.Join(nils, ","))
|
|
|
|
} else {
|
|
|
|
// 给道岔添加电路组件
|
|
|
|
entry.AddComponent(component.Zdj9TwoElectronicType, unsafe.Pointer(zdj9TwoElectronic))
|
|
|
|
}
|
|
|
|
} else if size > 0 && size < 3 {
|
|
|
|
fmt.Println("id=", turnout.Id(), "的道岔是ZDJ9双机牵引,但继电器组合类型少于3个(应为TDC、TDFJ1、TDFJ2三个组合)")
|
|
|
|
}
|
|
|
|
return nil
|
2023-09-27 18:39:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// 新建道岔实体
|
|
|
|
func NewTurnoutEntity(w ecs.World, uid string, worldData *component.WorldData) *ecs.Entry {
|
|
|
|
entry, ok := worldData.EntityMap[uid]
|
|
|
|
if !ok {
|
|
|
|
entry = w.Create(component.TurnoutTag, component.UidType, component.TurnoutPositionType)
|
|
|
|
component.UidType.Set(entry, &component.Uid{Id: uid})
|
|
|
|
worldData.EntityMap[uid] = entry
|
|
|
|
}
|
|
|
|
return entry
|
|
|
|
}
|