From a58338d128ea4bee1fcf0b1f182a2f5b77a9ad18 Mon Sep 17 00:00:00 2001 From: xzb <223@qq.com> Date: Tue, 15 Aug 2023 17:29:11 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BB=BF=E7=9C=9F=E7=B3=BB=E7=BB=9F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- simulation/examples/main.go | 26 +++++++++++++++-------- simulation/memory/init_world.go | 2 ++ simulation/system/debug_system.go | 33 ++++++++++++++++++++++++++++++ simulation/system/switch_system.go | 4 ---- 4 files changed, 53 insertions(+), 12 deletions(-) create mode 100644 simulation/system/debug_system.go diff --git a/simulation/examples/main.go b/simulation/examples/main.go index a3266ee..6667f72 100644 --- a/simulation/examples/main.go +++ b/simulation/examples/main.go @@ -16,16 +16,14 @@ func main() { worlTime := time.Now() //外界与world交互的门面 face := memory.InitializeWorld(world, worlTime) - //当组件为显示set值时,world会初始化组件的零值 - switch1Entry := world.Create(components.ComDeviceIdentity, components.ComSwitchState, components.ComSwitchTurnOperating) - components.ComDeviceIdentity.Set(switch1Entry, &state.DeviceIdentity{Id: "switch1"}) - components.ComSwitchState.Set(switch1Entry, &state.SwitchState{Normal: true, Reverse: false}) - world.AddSystem(system.NewSwitchSystem()) + // + initDevicesStatus(world) + // + initSystems(world) // world.StartUp() time.Sleep(2 * time.Second) - // - + //外界与world交互 reslult, _ := face.Call(func(w ecs.World) any { fmt.Println("==>>3触发转动道岔 ...") return system.FireSwitchTurn(w, "switch1", false, 3000) @@ -33,6 +31,18 @@ func main() { fmt.Println("==>>3触发转动道岔 。。。", reslult) // fmt.Println("==>>world 当前时间:", face.WorldTime().GoString()) - time.Sleep(4 * time.Second) + // + time.Sleep(40 * time.Second) world.Close() } + +func initDevicesStatus(world ecs.World) { + //当组件未显式set值时,world会初始化组件的零值 + switch1Entry := world.Create(components.ComDeviceIdentity, components.ComSwitchState, components.ComSwitchTurnOperating) + components.ComDeviceIdentity.Set(switch1Entry, &state.DeviceIdentity{Id: "switch1"}) + components.ComSwitchState.Set(switch1Entry, &state.SwitchState{Normal: true, Reverse: false}) +} + +func initSystems(world ecs.World) { + world.AddSystem(system.NewSwitchSystem()) +} diff --git a/simulation/memory/init_world.go b/simulation/memory/init_world.go index bc8492b..7faebca 100644 --- a/simulation/memory/init_world.go +++ b/simulation/memory/init_world.go @@ -18,6 +18,8 @@ func InitializeWorld(w ecs.World, worldTime time.Time) *system.FaceSystem { //初始化world与外界交互的门面 faceSystem := system.NewFaceSystem(w) w.AddSystem(faceSystem) + //添加调试系统 + w.AddSystem(system.NewDebugSystem()) // return faceSystem } diff --git a/simulation/system/debug_system.go b/simulation/system/debug_system.go new file mode 100644 index 0000000..26e4cd8 --- /dev/null +++ b/simulation/system/debug_system.go @@ -0,0 +1,33 @@ +package system + +import ( + "fmt" + + "github.com/yohamta/donburi/filter" + "joylink.club/ecs" + "joylink.club/rtsssimulation/components" +) + +// 调试时显示一些信息 +type DebugSystem struct { +} + +func NewDebugSystem() *DebugSystem { + return &DebugSystem{} +} + +// world 执行 +func (me *DebugSystem) Update(w ecs.World) { + debugSwitch(w) +} + +// 显示道岔状态 +func debugSwitch(w ecs.World) { + switchesQuery := ecs.NewQuery(filter.Contains(components.ComDeviceIdentity, components.ComSwitchState, components.ComSwitchTurnOperating)) + switchesQuery.Each(w, func(e *ecs.Entry) { + id := components.ComDeviceIdentity.Get(e).Id + state := components.ComSwitchState.Get(e) + operating := components.ComSwitchTurnOperating.Get(e) + fmt.Println("==>>道岔[", id, "]", ", 定位=", state.Normal, ",反位=", state.Reverse, " , 正在转换道岔=", operating.TurningTime > 0) + }) +} diff --git a/simulation/system/switch_system.go b/simulation/system/switch_system.go index d5688eb..79bf075 100644 --- a/simulation/system/switch_system.go +++ b/simulation/system/switch_system.go @@ -1,8 +1,6 @@ package system import ( - "fmt" - "github.com/yohamta/donburi/filter" "joylink.club/ecs" "joylink.club/rtsssimulation/components" @@ -53,7 +51,6 @@ func (me *SwitchSystem) Update(w ecs.World) { if turnOperting := components.ComSwitchTurnOperating.Get(e); turnOperting.StartTurn { if turnOperting.TurningTime > 0 { //正在转动 turnOperting.TurningTime -= int64(w.Tick()) - fmt.Println("==>>正在转动道岔 ...") } else { //转动完成 turnOperting.StartTurn = false state := components.ComSwitchState.Get(e) @@ -64,7 +61,6 @@ func (me *SwitchSystem) Update(w ecs.World) { state.Normal = false state.Reverse = true } - fmt.Println("==>>完成转动道岔") } } })