2023-08-23 16:52:01 +08:00
|
|
|
package creator
|
|
|
|
|
|
|
|
import (
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"joylink.club/ecs"
|
|
|
|
"joylink.club/rtsssimulation/components"
|
|
|
|
"joylink.club/rtsssimulation/components/cstate"
|
|
|
|
"joylink.club/rtsssimulation/system"
|
2023-08-23 18:06:09 +08:00
|
|
|
"joylink.club/rtsssimulation/umi"
|
2023-08-23 16:52:01 +08:00
|
|
|
)
|
|
|
|
|
2023-08-24 10:27:50 +08:00
|
|
|
// 仿真world配置
|
2023-08-23 18:06:09 +08:00
|
|
|
type WorldConfig struct {
|
|
|
|
//模型管理器,接收模型仓库管理器实例的指针
|
|
|
|
ModelManager umi.IModelManager
|
|
|
|
//world 系统
|
|
|
|
Systems []ecs.ISystem
|
|
|
|
//world tick
|
|
|
|
Tick int
|
|
|
|
//world 起始时间
|
|
|
|
InitTime time.Time
|
|
|
|
}
|
|
|
|
|
2023-08-24 10:27:50 +08:00
|
|
|
// 初始化仿真world
|
2023-08-23 18:06:09 +08:00
|
|
|
func InitializeWorld(config *WorldConfig) ecs.World {
|
|
|
|
world := ecs.NewWorld(config.Tick)
|
|
|
|
// 初始化系统
|
|
|
|
for _, sys := range config.Systems {
|
|
|
|
world.AddSystem(sys)
|
|
|
|
}
|
|
|
|
// 初始化实体
|
|
|
|
initEntites(config, world)
|
|
|
|
//
|
|
|
|
return world
|
|
|
|
}
|
|
|
|
|
|
|
|
// 初始化实体
|
|
|
|
func initEntites(config *WorldConfig, world ecs.World) {
|
|
|
|
modelStorageRefEntry := CreateModelStorageRefEntity(world)
|
|
|
|
components.ModelStorageRefComponent.Set(modelStorageRefEntry, &cstate.ModelStorageRef{ModelManager: config.ModelManager})
|
|
|
|
//
|
|
|
|
foreachModels(config.ModelManager, umi.Switch, func(md umi.IDeviceModel) {
|
|
|
|
entry := CreateSwitchEntity(world)
|
|
|
|
components.DeviceIdentityComponent.Set(entry, &cstate.DeviceIdentity{Id: md.GetId()})
|
|
|
|
components.SwitchRelayStateComponent.Set(entry, &cstate.SwitchRelayState{DcJ: false, FcJ: false, DbJ: true, FbJ: false})
|
|
|
|
components.PercentageDeviceComponent.Set(entry, &cstate.PercentageDeviceState{Rate: system.SwitchNormalRate})
|
|
|
|
})
|
2023-08-23 16:52:01 +08:00
|
|
|
//
|
2023-08-23 18:06:09 +08:00
|
|
|
foreachModels(config.ModelManager, umi.Signal, func(md umi.IDeviceModel) {
|
|
|
|
entry := CreateSignalEntity(world)
|
|
|
|
components.DeviceIdentityComponent.Set(entry, &cstate.DeviceIdentity{Id: md.GetId()})
|
|
|
|
components.SignalStateComponent.Set(entry, &cstate.SignalState{Display: cstate.SignalAspect_No})
|
|
|
|
})
|
|
|
|
//
|
|
|
|
foreachModels(config.ModelManager, umi.Psd, func(md umi.IDeviceModel) {
|
|
|
|
psdEntry := CreatePsdEntity(world)
|
|
|
|
components.DeviceIdentityComponent.Set(psdEntry, &cstate.DeviceIdentity{Id: md.GetId()})
|
|
|
|
components.PsdStateComponent.Set(psdEntry, &cstate.PsdState{AllClosed: true, AllOpened: false, InterlockReleased: false})
|
|
|
|
//
|
|
|
|
psd := md.(umi.IPsdModel)
|
|
|
|
for _, psdCell := range psd.AllDeviceCells() {
|
|
|
|
cellEntry := CreatePsdCellEntity(world, psdEntry)
|
|
|
|
components.DeviceIdentityComponent.Set(cellEntry, &cstate.DeviceIdentity{Id: psdCell.GetId()})
|
|
|
|
components.PercentageDeviceComponent.Set(cellEntry, &cstate.PercentageDeviceState{Rate: system.PsdCellWholeCloseRate})
|
|
|
|
}
|
|
|
|
})
|
|
|
|
//
|
|
|
|
foreachModels(config.ModelManager, umi.TowPosButton, func(md umi.IDeviceModel) {
|
|
|
|
entry := CreateTowPosButtonEntity(world)
|
|
|
|
components.DeviceIdentityComponent.Set(entry, &cstate.DeviceIdentity{Id: md.GetId()})
|
|
|
|
components.TowPositionButtonStateComponent.Set(entry, &cstate.TowPositionButtonState{Pos1: false, Pos2: true})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
func foreachModels(modelManager umi.IModelManager, deviceType umi.DeviceType, eachFunc func(deviceModel umi.IDeviceModel)) {
|
|
|
|
for _, model := range modelManager.FindByType(deviceType) {
|
|
|
|
eachFunc(model)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
// 创建模型仓库引用实体
|
|
|
|
func CreateModelStorageRefEntity(w ecs.World) *ecs.Entry {
|
|
|
|
return w.Create(components.ModelStorageRefComponent)
|
2023-08-23 16:52:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// 创建道岔实体
|
|
|
|
func CreateSwitchEntity(w ecs.World) *ecs.Entry {
|
|
|
|
return w.Create(components.DeviceIdentityComponent, components.SwitchRelayStateComponent, components.PercentageDeviceComponent)
|
|
|
|
}
|
|
|
|
|
|
|
|
// 创建信号机实体
|
|
|
|
func CreateSignalEntity(w ecs.World) *ecs.Entry {
|
|
|
|
return w.Create(components.DeviceIdentityComponent, components.SignalStateComponent)
|
|
|
|
}
|
|
|
|
|
|
|
|
// 创建物理区段实体
|
|
|
|
func CreatePhysicalSectionEntity(w ecs.World) *ecs.Entry {
|
|
|
|
return w.Create(components.DeviceIdentityComponent, components.PhysicalSectionStateComponent)
|
|
|
|
}
|
|
|
|
|
|
|
|
// 创建站台单侧屏蔽门实体
|
|
|
|
func CreatePsdEntity(w ecs.World) *ecs.Entry {
|
|
|
|
e := w.Create(components.DeviceIdentityComponent, components.PsdStateComponent, components.EntityTagHandlerComponent)
|
|
|
|
components.EntityTagHandlerComponent.Set(e, &cstate.EntityTagHandler{Tag: ecs.NewComponentType[struct{}]()})
|
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
|
|
|
// 创建屏蔽门cell实体
|
|
|
|
func CreatePsdCellEntity(w ecs.World, psdEntry *ecs.Entry) *ecs.Entry {
|
|
|
|
return w.Create(components.DeviceIdentityComponent, components.PercentageDeviceComponent, components.EntityTagHandlerComponent.Get(psdEntry).Tag)
|
|
|
|
}
|
|
|
|
|
|
|
|
// 创建应答器实体
|
|
|
|
func CreateBaliseEntity(w ecs.World) *ecs.Entry {
|
|
|
|
return w.Create(components.DeviceIdentityComponent, components.BaliseStateComponent)
|
|
|
|
}
|
|
|
|
|
|
|
|
// 创建列车实体
|
|
|
|
func CreateTrainEntity(w ecs.World) *ecs.Entry {
|
|
|
|
return w.Create(components.DeviceIdentityComponent, components.TrainStateComponent)
|
|
|
|
}
|
|
|
|
|
|
|
|
// 创建两档位按钮实体
|
|
|
|
func CreateTowPosButtonEntity(w ecs.World) *ecs.Entry {
|
|
|
|
return w.Create(components.DeviceIdentityComponent, components.TowPositionButtonStateComponent)
|
|
|
|
}
|