jl-ecs/examples/rtss-cg/main.go
2023-08-08 15:47:43 +08:00

128 lines
5.0 KiB
Go

package main
import (
"log"
"time"
"github.com/yohamta/donburi/filter"
"joylink.club/ecs"
"joylink.club/rtss/iwayside"
"joylink.club/rtss/mwayside"
"joylink.club/rtss/simulation"
"joylink.club/rtss/state"
"joylink.club/rtss/system"
)
func init() {
log.SetFlags(log.Lshortfile | log.Lmicroseconds | log.Ldate)
}
// 1 world 须增加attach , 绑定与该world相关的信息
// 2 事件的订阅、发布、处理是独立调用执行的,事件处理可以集中处理也可以只处理某类事件
func main() {
//
simWorld := ecs.NewWorld(400)
//
initMemoryModel(simWorld)
//道岔
initSwitchState(simWorld)
//轨道
initLinkState(simWorld)
//添加列车
initTrainState(simWorld, "train1", iwayside.LinkLocation{LinkId: "link4", LinkOffset: 3000 * 100 * 10}, false)
//
simWorld.AddSystem(system.NewSwitchSystem())
simWorld.AddSystem(system.NewTrainSystem())
simWorld.AddSystem(system.NewCiSystem())
//
simWorld.StartUp()
//
time.Sleep(2 * time.Second)
//
operateSwitch(simWorld, "dc1")
operateSwitch(simWorld, "dc2")
time.Sleep(200 * time.Second)
}
// 根据道岔id来搬动道岔
func operateSwitch(world ecs.World, dcId string) {
query := ecs.NewQuery(filter.Contains(simulation.ComSwitchState, simulation.ComSwitchOperating))
query.Each(world, func(e *ecs.Entry) {
dcState := simulation.ComSwitchState.Get(e)
if dcState.Id == dcId {
simulation.ComSwitchOperating.SetValue(e, state.SwitchOperating{EnableStart: true, StartTime: time.Now()})
}
})
}
// 初始化memory
func initMemoryModel(world ecs.World) {
memoryEntry := world.Create(simulation.ComSimMemory)
//simulation.ComSimMemory.SetValue(memoryEntry, *simulation.NewSimMemory())
simMemory := simulation.NewSimMemory()
simulation.ComSimMemory.Set(memoryEntry, simMemory)
createModelsRelation(simMemory)
}
// 初始化道岔状态
func initSwitchState(world ecs.World) {
simMemory := simulation.GetMemoryModel(world)
for _, switchModel := range simMemory.Switchs {
dc1 := &state.SwitchState{Id: switchModel.GetId(), Normal: true, Reverse: false}
dc1Entry := world.Create(simulation.ComSwitchState, simulation.ComSwitchOperating)
simulation.ComSwitchState.Set(dc1Entry, dc1)
}
}
// 初始化轨道状态
func initLinkState(world ecs.World) {
simMemory := simulation.GetMemoryModel(world)
for _, linkModel := range simMemory.Links {
linkState := &state.LinkState{Id: linkModel.GetId(), Occupied: false}
linkEntry := world.Create(simulation.ComLinkState)
simulation.ComLinkState.Set(linkEntry, linkState)
}
}
// 添加列车并初始化
func initTrainState(world ecs.World, trainId string, linkLocation iwayside.LinkLocation, up bool) {
train := &state.TrainState{Id: trainId, LinkId: linkLocation.LinkId, LinkOffset: linkLocation.LinkOffset, Up: up}
trainEntry := world.Create(simulation.ComTrainState)
simulation.ComTrainState.Set(trainEntry, train)
}
// 构建模型关系
func createModelsRelation(memory *simulation.SimMemory) {
//
link1Model := &mwayside.LinkModel{DeviceModel: mwayside.DeviceModel{Id: "link1", Type: iwayside.Link}, Len: 3000 * 100 * 10}
link2Model := &mwayside.LinkModel{DeviceModel: mwayside.DeviceModel{Id: "link2", Type: iwayside.Link}, Len: 3070 * 100 * 10}
link3Model := &mwayside.LinkModel{DeviceModel: mwayside.DeviceModel{Id: "link3", Type: iwayside.Link}, Len: 3070 * 100 * 10}
link4Model := &mwayside.LinkModel{DeviceModel: mwayside.DeviceModel{Id: "link4", Type: iwayside.Link}, Len: 3000 * 100 * 10}
link5Model := &mwayside.LinkModel{DeviceModel: mwayside.DeviceModel{Id: "link5", Type: iwayside.Link}, Len: 120 * 100 * 10}
memory.Links[link1Model.Id] = link1Model
memory.Links[link2Model.Id] = link2Model
memory.Links[link3Model.Id] = link3Model
memory.Links[link4Model.Id] = link4Model
memory.Links[link5Model.Id] = link5Model
//
switch1Model := &mwayside.SwitchModel{DeviceModel: mwayside.DeviceModel{Id: "dc1", Type: iwayside.Switch}}
switch2Model := &mwayside.SwitchModel{DeviceModel: mwayside.DeviceModel{Id: "dc2", Type: iwayside.Switch}}
memory.Switchs[switch1Model.Id] = switch1Model
memory.Switchs[switch2Model.Id] = switch2Model
//
link1Model.PortB = &mwayside.SwitchRef{Port: iwayside.A, Switch: switch1Model}
link2Model.PortA = &mwayside.SwitchRef{Port: iwayside.B, Switch: switch1Model}
link3Model.PortB = &mwayside.SwitchRef{Port: iwayside.B, Switch: switch2Model}
link4Model.PortA = &mwayside.SwitchRef{Port: iwayside.A, Switch: switch2Model}
link5Model.PortA = &mwayside.SwitchRef{Port: iwayside.C, Switch: switch1Model}
link5Model.PortB = &mwayside.SwitchRef{Port: iwayside.C, Switch: switch2Model}
//
switch1Model.PortA = &mwayside.LinkRef{Port: iwayside.B, Link: link1Model}
switch1Model.PortB = &mwayside.LinkRef{Port: iwayside.A, Link: link2Model}
switch1Model.PortC = &mwayside.LinkRef{Port: iwayside.A, Link: link5Model}
//
switch2Model.PortA = &mwayside.LinkRef{Port: iwayside.A, Link: link4Model}
switch2Model.PortB = &mwayside.LinkRef{Port: iwayside.B, Link: link3Model}
switch2Model.PortC = &mwayside.LinkRef{Port: iwayside.B, Link: link5Model}
}