仿真系统

This commit is contained in:
xzb 2023-08-15 17:29:11 +08:00
parent 0f1e3258e4
commit a58338d128
4 changed files with 53 additions and 12 deletions

View File

@ -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())
}

View File

@ -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
}

View File

@ -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)
})
}

View File

@ -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("==>>完成转动道岔")
}
}
})