122 lines
4.5 KiB
Go
122 lines
4.5 KiB
Go
package main
|
||
|
||
import (
|
||
"fmt"
|
||
"time"
|
||
|
||
"joylink.club/ecs"
|
||
"joylink.club/rtsssimulation/components"
|
||
"joylink.club/rtsssimulation/cstate"
|
||
"joylink.club/rtsssimulation/memory"
|
||
"joylink.club/rtsssimulation/memory/wdcreator"
|
||
"joylink.club/rtsssimulation/model"
|
||
"joylink.club/rtsssimulation/system"
|
||
)
|
||
|
||
func testModel() {
|
||
link1 := model.NewLinkModel("link1")
|
||
link2 := model.NewLinkModel("link2")
|
||
memory.DeviceModelStorage.AddModel(link1)
|
||
memory.DeviceModelStorage.AddModel(link2)
|
||
memory.DeviceModelStorage.ForEachModelsByType(cstate.DeviceType_Link, func(md memory.ModelData) {
|
||
link := md.(*model.LinkModel)
|
||
fmt.Println("==>>", link.Id)
|
||
})
|
||
}
|
||
func main() {
|
||
world := ecs.NewWorld(300)
|
||
worlTime := time.Now()
|
||
//外界与world交互的门面
|
||
face := wdcreator.InitializeWorld(world, worlTime)
|
||
//
|
||
initDevicesStatus(world)
|
||
//
|
||
initSystems(world)
|
||
//
|
||
world.StartUp()
|
||
time.Sleep(2 * time.Second)
|
||
//外界与world交互
|
||
//testSwitchTurn(world, face)
|
||
//testSignalOpt(world, face)
|
||
//testPsdCellOpt(world, face)
|
||
testPsdCellOpt(world, face)
|
||
//
|
||
fmt.Println("==>>world 当前时间:", face.WorldTime().GoString())
|
||
//
|
||
time.Sleep(40 * time.Second)
|
||
world.Close()
|
||
}
|
||
|
||
// /////////////////////////////////////////////////////////////////////////
|
||
func testPsdOpt(world ecs.World, face *system.FaceSystem) {
|
||
face.Call(func(w ecs.World) any {
|
||
return system.FirePsdOperation(w, "psd1", true)
|
||
})
|
||
time.Sleep(5 * time.Second)
|
||
face.Call(func(w ecs.World) any {
|
||
return system.FirePsdOperation(w, "psd1", false)
|
||
})
|
||
}
|
||
func testPsdCellOpt(world ecs.World, face *system.FaceSystem) {
|
||
face.Call(func(w ecs.World) any {
|
||
return system.FirePsdCellOperation(w, "psd1", "psd1Cell1", 0)
|
||
})
|
||
face.Call(func(w ecs.World) any {
|
||
return system.FirePsdCellOperation(w, "psd1", "psd1Cell2", 0)
|
||
})
|
||
time.Sleep(10 * time.Second)
|
||
face.Call(func(w ecs.World) any {
|
||
return system.FirePsdCellOperation(w, "psd1", "psd1Cell1", 100-20)
|
||
})
|
||
face.Call(func(w ecs.World) any {
|
||
return system.FirePsdCellOperation(w, "psd1", "psd1Cell2", 100)
|
||
})
|
||
}
|
||
func testSwitchTurn(world ecs.World, face *system.FaceSystem) {
|
||
reslult, _ := face.Call(func(w ecs.World) any {
|
||
fmt.Println("==>>1触发转动道岔 ...")
|
||
return system.FireSwitchTurn(w, "switch1", system.SwitchReverseRate-40)
|
||
})
|
||
fmt.Println("==>>1触发转动道岔 。。。", reslult)
|
||
time.Sleep(8 * time.Second)
|
||
reslult2, _ := face.Call(func(w ecs.World) any {
|
||
fmt.Println("==>>2触发转动道岔 ...")
|
||
return system.FireSwitchTurn(w, "switch1", system.SwitchNormalRate)
|
||
})
|
||
fmt.Println("==>>2触发转动道岔 。。。", reslult2)
|
||
}
|
||
func testSignalOpt(world ecs.World, face *system.FaceSystem) {
|
||
face.Call(func(w ecs.World) any {
|
||
return system.SetSignalDisplay(w, "siganl1", cstate.SignalAspect_B)
|
||
})
|
||
}
|
||
|
||
// ////////////////////////////////////////////////////////////////////////
|
||
func initDevicesStatus(world ecs.World) {
|
||
//当组件未显式set值时,world会初始化组件的零值
|
||
switch1Entry := wdcreator.CreateSwitchEntity(world)
|
||
components.DeviceIdentityComponent.Set(switch1Entry, &cstate.DeviceIdentity{Id: "switch1"})
|
||
components.SwitchRelayStateComponent.Set(switch1Entry, &cstate.SwitchRelayState{DcJ: false, FcJ: false, DbJ: true, FbJ: false})
|
||
components.PercentageDeviceComponent.Set(switch1Entry, &cstate.PercentageDeviceState{Rate: system.SwitchNormalRate})
|
||
//
|
||
signal1Entry := wdcreator.CreateSignalEntity(world)
|
||
components.DeviceIdentityComponent.Set(signal1Entry, &cstate.DeviceIdentity{Id: "siganl1"})
|
||
components.SignalStateComponent.Set(signal1Entry, &cstate.SignalState{Display: cstate.SignalAspect_No})
|
||
//psd1两个cell
|
||
psd1Entry := wdcreator.CreatePsdEntity(world)
|
||
components.DeviceIdentityComponent.Set(psd1Entry, &cstate.DeviceIdentity{Id: "psd1"})
|
||
components.PsdStateComponent.Set(psd1Entry, &cstate.PsdState{AllClosed: true, AllOpened: false, InterlockReleased: false})
|
||
psd1Cell1Entry := wdcreator.CreatePsdCellEntity(world, psd1Entry)
|
||
components.DeviceIdentityComponent.Set(psd1Cell1Entry, &cstate.DeviceIdentity{Id: "psd1Cell1"})
|
||
components.PercentageDeviceComponent.Set(psd1Cell1Entry, &cstate.PercentageDeviceState{Rate: system.PsdCellWholeCloseRate})
|
||
psd1Cell2Entry := wdcreator.CreatePsdCellEntity(world, psd1Entry)
|
||
components.DeviceIdentityComponent.Set(psd1Cell2Entry, &cstate.DeviceIdentity{Id: "psd1Cell2"})
|
||
components.PercentageDeviceComponent.Set(psd1Cell2Entry, &cstate.PercentageDeviceState{Rate: system.PsdCellWholeCloseRate})
|
||
}
|
||
|
||
func initSystems(world ecs.World) {
|
||
world.AddSystem(system.NewSwitchSystem())
|
||
world.AddSystem(system.NewSignalSystem())
|
||
world.AddSystem(system.NewPsdSystem())
|
||
}
|