rts-sim-module/examples/main.go

122 lines
4.5 KiB
Go
Raw Normal View History

2023-08-15 16:50:24 +08:00
package main
import (
"fmt"
"time"
"joylink.club/ecs"
"joylink.club/rtsssimulation/components"
2023-08-22 11:00:14 +08:00
"joylink.club/rtsssimulation/cstate"
2023-08-15 16:50:24 +08:00
"joylink.club/rtsssimulation/memory"
2023-08-22 11:00:14 +08:00
"joylink.club/rtsssimulation/memory/wdcreator"
2023-08-21 15:12:57 +08:00
"joylink.club/rtsssimulation/model"
2023-08-15 16:50:24 +08:00
"joylink.club/rtsssimulation/system"
)
2023-08-21 15:12:57 +08:00
func testModel() {
link1 := model.NewLinkModel("link1")
link2 := model.NewLinkModel("link2")
2023-08-22 11:00:14 +08:00
memory.DeviceModelStorage.AddModel(link1)
memory.DeviceModelStorage.AddModel(link2)
memory.DeviceModelStorage.ForEachModelsByType(cstate.DeviceType_Link, func(md memory.ModelData) {
2023-08-21 15:12:57 +08:00
link := md.(*model.LinkModel)
fmt.Println("==>>", link.Id)
})
}
2023-08-15 16:50:24 +08:00
func main() {
world := ecs.NewWorld(300)
worlTime := time.Now()
//外界与world交互的门面
2023-08-22 11:00:14 +08:00
face := wdcreator.InitializeWorld(world, worlTime)
2023-08-15 17:29:11 +08:00
//
initDevicesStatus(world)
//
initSystems(world)
2023-08-15 16:50:24 +08:00
//
world.StartUp()
time.Sleep(2 * time.Second)
2023-08-15 17:29:11 +08:00
//外界与world交互
2023-08-18 16:15:19 +08:00
//testSwitchTurn(world, face)
2023-08-17 14:25:18 +08:00
//testSignalOpt(world, face)
//testPsdCellOpt(world, face)
2023-08-18 16:15:19 +08:00
testPsdCellOpt(world, face)
2023-08-15 16:50:24 +08:00
//
fmt.Println("==>>world 当前时间:", face.WorldTime().GoString())
2023-08-15 17:29:11 +08:00
//
time.Sleep(40 * time.Second)
2023-08-15 16:50:24 +08:00
world.Close()
}
2023-08-15 17:29:11 +08:00
2023-08-17 14:25:18 +08:00
// /////////////////////////////////////////////////////////////////////////
func testPsdOpt(world ecs.World, face *system.FaceSystem) {
face.Call(func(w ecs.World) any {
2023-08-18 16:15:19 +08:00
return system.FirePsdOperation(w, "psd1", true)
2023-08-17 14:25:18 +08:00
})
time.Sleep(5 * time.Second)
face.Call(func(w ecs.World) any {
2023-08-18 16:15:19 +08:00
return system.FirePsdOperation(w, "psd1", false)
2023-08-17 14:25:18 +08:00
})
}
func testPsdCellOpt(world ecs.World, face *system.FaceSystem) {
face.Call(func(w ecs.World) any {
2023-08-18 16:15:19 +08:00
return system.FirePsdCellOperation(w, "psd1", "psd1Cell1", 0)
2023-08-17 14:25:18 +08:00
})
face.Call(func(w ecs.World) any {
2023-08-18 16:15:19 +08:00
return system.FirePsdCellOperation(w, "psd1", "psd1Cell2", 0)
2023-08-17 14:25:18 +08:00
})
2023-08-18 16:28:23 +08:00
time.Sleep(10 * time.Second)
2023-08-17 14:25:18 +08:00
face.Call(func(w ecs.World) any {
2023-08-18 16:15:19 +08:00
return system.FirePsdCellOperation(w, "psd1", "psd1Cell1", 100-20)
2023-08-17 14:25:18 +08:00
})
face.Call(func(w ecs.World) any {
2023-08-18 16:15:19 +08:00
return system.FirePsdCellOperation(w, "psd1", "psd1Cell2", 100)
2023-08-17 14:25:18 +08:00
})
}
func testSwitchTurn(world ecs.World, face *system.FaceSystem) {
reslult, _ := face.Call(func(w ecs.World) any {
fmt.Println("==>>1触发转动道岔 ...")
2023-08-18 14:40:13 +08:00
return system.FireSwitchTurn(w, "switch1", system.SwitchReverseRate-40)
2023-08-17 14:25:18 +08:00
})
fmt.Println("==>>1触发转动道岔 。。。", reslult)
time.Sleep(8 * time.Second)
reslult2, _ := face.Call(func(w ecs.World) any {
fmt.Println("==>>2触发转动道岔 ...")
2023-08-18 14:40:13 +08:00
return system.FireSwitchTurn(w, "switch1", system.SwitchNormalRate)
2023-08-17 14:25:18 +08:00
})
fmt.Println("==>>2触发转动道岔 。。。", reslult2)
}
func testSignalOpt(world ecs.World, face *system.FaceSystem) {
face.Call(func(w ecs.World) any {
2023-08-22 11:00:14 +08:00
return system.SetSignalDisplay(w, "siganl1", cstate.SignalAspect_B)
2023-08-17 14:25:18 +08:00
})
}
// ////////////////////////////////////////////////////////////////////////
2023-08-15 17:29:11 +08:00
func initDevicesStatus(world ecs.World) {
//当组件未显式set值时world会初始化组件的零值
2023-08-22 11:00:14 +08:00
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})
2023-08-16 15:00:24 +08:00
//
2023-08-22 11:00:14 +08:00
signal1Entry := wdcreator.CreateSignalEntity(world)
components.DeviceIdentityComponent.Set(signal1Entry, &cstate.DeviceIdentity{Id: "siganl1"})
components.SignalStateComponent.Set(signal1Entry, &cstate.SignalState{Display: cstate.SignalAspect_No})
2023-08-17 14:25:18 +08:00
//psd1两个cell
2023-08-22 11:00:14 +08:00
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})
2023-08-15 17:29:11 +08:00
}
func initSystems(world ecs.World) {
world.AddSystem(system.NewSwitchSystem())
2023-08-16 15:00:24 +08:00
world.AddSystem(system.NewSignalSystem())
2023-08-17 14:25:18 +08:00
world.AddSystem(system.NewPsdSystem())
2023-08-15 17:29:11 +08:00
}