rts-sim-module/examples/main.go
2023-08-16 17:17:20 +08:00

65 lines
1.9 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package main
import (
"fmt"
"time"
"joylink.club/ecs"
"joylink.club/rtsssimulation/components"
"joylink.club/rtsssimulation/memory"
"joylink.club/rtsssimulation/state"
"joylink.club/rtsssimulation/system"
)
func main() {
world := ecs.NewWorld(300)
worlTime := time.Now()
//外界与world交互的门面
face := memory.InitializeWorld(world, worlTime)
//
initDevicesStatus(world)
//
initSystems(world)
//
world.StartUp()
time.Sleep(2 * time.Second)
//外界与world交互
/*
reslult, _ := face.Call(func(w ecs.World) any {
fmt.Println("==>>1触发转动道岔 ...")
return system.FireSwitchTurn(w, "switch1", false)
})
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", true)
})
fmt.Println("==>>2触发转动道岔 。。。", reslult2)
*/
face.Call(func(w ecs.World) any {
return system.SetSignalDisplay(w, "siganl1", state.SignalAspect_B)
})
//
fmt.Println("==>>world 当前时间:", face.WorldTime().GoString())
//
time.Sleep(40 * time.Second)
world.Close()
}
func initDevicesStatus(world ecs.World) {
//当组件未显式set值时world会初始化组件的零值
switch1Entry := memory.CreateSwitchEntity(world)
components.ComDeviceIdentity.Set(switch1Entry, &state.DeviceIdentity{Id: "switch1"})
components.ComSwitchState.Set(switch1Entry, &state.SwitchState{NormalRelay: false, ReverseRelay: false, NormalTable: true, ReverseTable: false})
//
signal1Entry := memory.CreateSignalEntity(world)
components.ComDeviceIdentity.Set(signal1Entry, &state.DeviceIdentity{Id: "siganl1"})
components.ComSignalState.Set(signal1Entry, &state.SignalState{Display: state.SignalAspect_No})
}
func initSystems(world ecs.World) {
world.AddSystem(system.NewSwitchSystem())
world.AddSystem(system.NewSignalSystem())
}