system face根据id查找实体
This commit is contained in:
parent
a6d5623908
commit
0a288cf3d5
121
examples/main.go
121
examples/main.go
@ -1,121 +0,0 @@
|
||||
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())
|
||||
}
|
161
examples/test1/main.go
Normal file
161
examples/test1/main.go
Normal file
@ -0,0 +1,161 @@
|
||||
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"
|
||||
"joylink.club/rtsssimulation/umi"
|
||||
)
|
||||
|
||||
type Simulation struct {
|
||||
FaceSystem *system.FaceSystem
|
||||
World ecs.World
|
||||
}
|
||||
|
||||
func main() {
|
||||
var sim *Simulation = &Simulation{}
|
||||
//
|
||||
initModels()
|
||||
initSimWorld(sim, 300)
|
||||
//
|
||||
sim.World.StartUp()
|
||||
time.Sleep(2 * time.Second)
|
||||
se := sim.FaceSystem.FindEntity("signal1")
|
||||
fmt.Println("==>>fe : ", se)
|
||||
time.Sleep(60 * time.Second)
|
||||
//
|
||||
testSwitchTurn(sim.World, sim.FaceSystem)
|
||||
//testSignalOpt(world, face)
|
||||
//testPsdCellOpt(world, face)
|
||||
//testPsdCellOpt(sim.World, sim.FaceSystem)
|
||||
//
|
||||
time.Sleep(300 * time.Second)
|
||||
}
|
||||
|
||||
// 构建仿真world
|
||||
func initSimWorld(sim *Simulation, tick int) {
|
||||
sim.World = ecs.NewWorld(tick)
|
||||
sim.FaceSystem = wdcreator.InitializeWorld(sim.World, time.Now())
|
||||
//
|
||||
mds := memory.DeviceModelStorage
|
||||
//信号机相关组件
|
||||
mds.ForEachModelsByType(cstate.DeviceType_Signal, func(md memory.ModelData) {
|
||||
entry := wdcreator.CreateSignalEntity(sim.World)
|
||||
components.DeviceIdentityComponent.Set(entry, &cstate.DeviceIdentity{Id: md.GetId()})
|
||||
components.SignalStateComponent.Set(entry, &cstate.SignalState{Display: cstate.SignalAspect_No})
|
||||
})
|
||||
//道岔相关组件
|
||||
mds.ForEachModelsByType(cstate.DeviceType_Switch, func(md memory.ModelData) {
|
||||
entry := wdcreator.CreateSwitchEntity(sim.World)
|
||||
components.DeviceIdentityComponent.Set(entry, &cstate.DeviceIdentity{Id: md.GetId()})
|
||||
components.SwitchRelayStateComponent.Set(entry, &cstate.SwitchRelayState{DcJ: false, FcJ: false, DbJ: true, FbJ: false})
|
||||
components.PercentageDeviceComponent.Set(entry, &cstate.PercentageDeviceState{Rate: system.SwitchNormalRate})
|
||||
})
|
||||
//屏蔽门相关组件
|
||||
mds.ForEachModelsByType(cstate.DeviceType_Psd, func(md memory.ModelData) {
|
||||
psdEntry := wdcreator.CreatePsdEntity(sim.World)
|
||||
components.DeviceIdentityComponent.Set(psdEntry, &cstate.DeviceIdentity{Id: md.GetId()})
|
||||
components.PsdStateComponent.Set(psdEntry, &cstate.PsdState{AllClosed: true, AllOpened: false, InterlockReleased: false})
|
||||
//
|
||||
psd := md.(umi.IPsdModel)
|
||||
for _, psdCell := range psd.AllDeviceCells() {
|
||||
cellEntry := wdcreator.CreatePsdCellEntity(sim.World, psdEntry)
|
||||
components.DeviceIdentityComponent.Set(cellEntry, &cstate.DeviceIdentity{Id: psdCell.GetId()})
|
||||
components.PercentageDeviceComponent.Set(cellEntry, &cstate.PercentageDeviceState{Rate: system.PsdCellWholeCloseRate})
|
||||
}
|
||||
})
|
||||
//添加必要的系统
|
||||
sim.World.AddSystem(system.NewSwitchSystem())
|
||||
sim.World.AddSystem(system.NewSignalSystem())
|
||||
sim.World.AddSystem(system.NewPsdSystem())
|
||||
}
|
||||
|
||||
// 构建模型
|
||||
func initModels() {
|
||||
mds := memory.DeviceModelStorage
|
||||
//
|
||||
link1 := model.NewLinkModel("link1")
|
||||
link2 := model.NewLinkModel("link2")
|
||||
mds.AddModel(link1)
|
||||
mds.AddModel(link2)
|
||||
//
|
||||
signal1 := model.NewSignalModel("signal1")
|
||||
signal2 := model.NewSignalModel("signal2")
|
||||
mds.AddModel(signal1)
|
||||
mds.AddModel(signal2)
|
||||
//
|
||||
switch1 := model.NewSwitchModel("switch1")
|
||||
switch1.TurnTime = 3000
|
||||
switch2 := model.NewSwitchModel("switch2")
|
||||
switch2.TurnTime = 2500
|
||||
mds.AddModel(switch1)
|
||||
mds.AddModel(switch2)
|
||||
//
|
||||
psd1 := model.NewPsdModel("psd1")
|
||||
psd1.MoveTime = 5000
|
||||
psd1Cell1 := model.NewPsdCellModel("psd1Cell1", psd1)
|
||||
psd1Cell2 := model.NewPsdCellModel("psd1Cell2", psd1)
|
||||
psd2 := model.NewPsdModel("psd2")
|
||||
psd2.MoveTime = 6000
|
||||
psd2Cell1 := model.NewPsdCellModel("psd2Cell1", psd2)
|
||||
psd2Cell2 := model.NewPsdCellModel("psd2Cell2", psd2)
|
||||
mds.AddModel(psd1)
|
||||
mds.AddModel(psd1Cell1)
|
||||
mds.AddModel(psd1Cell2)
|
||||
mds.AddModel(psd2)
|
||||
mds.AddModel(psd2Cell1)
|
||||
mds.AddModel(psd2Cell2)
|
||||
//
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////
|
||||
|
||||
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)
|
||||
})
|
||||
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)
|
||||
})
|
||||
}
|
@ -17,12 +17,19 @@ type PsdModel struct {
|
||||
func NewPsdModel(id string) *PsdModel {
|
||||
return &PsdModel{DeviceModel: DeviceModel{Id: id, Type: cstate.DeviceType_Psd}, Cells: make([]*PsdCellModel, 0)}
|
||||
}
|
||||
func (me *PsdModel) AddCell(cell *PsdCellModel) {
|
||||
func (me *PsdModel) addCell(cell *PsdCellModel) {
|
||||
me.Cells = append(me.Cells, cell)
|
||||
}
|
||||
func (me *PsdModel) MovingTime() int64 {
|
||||
return me.MoveTime
|
||||
}
|
||||
func (me *PsdModel) AllDeviceCells() []umi.IDeviceModel {
|
||||
rt := make([]umi.IDeviceModel, 0, len(me.Cells))
|
||||
for _, cell := range me.Cells {
|
||||
rt = append(rt, cell)
|
||||
}
|
||||
return rt
|
||||
}
|
||||
|
||||
/////////////////////////////////////////////
|
||||
|
||||
@ -34,10 +41,12 @@ type PsdCellModel struct {
|
||||
}
|
||||
|
||||
func NewPsdCellModel(id string, psdModel *PsdModel) *PsdCellModel {
|
||||
return &PsdCellModel{
|
||||
cell := &PsdCellModel{
|
||||
DeviceModel: DeviceModel{Id: id, Type: cstate.DeviceType_PsdCell},
|
||||
psd: psdModel,
|
||||
}
|
||||
psdModel.addCell(cell)
|
||||
return cell
|
||||
}
|
||||
func (me *PsdCellModel) Psd() *PsdModel {
|
||||
return me.psd.(*PsdModel)
|
||||
|
@ -18,7 +18,9 @@ func NewDebugSystem() *DebugSystem {
|
||||
|
||||
// world 执行
|
||||
func (me *DebugSystem) Update(w ecs.World) {
|
||||
debugPsd(w)
|
||||
//debugPsd(w)
|
||||
debugSwitch(w)
|
||||
//debugSignal(w)
|
||||
}
|
||||
|
||||
// 屏蔽门状态
|
||||
|
@ -1,10 +1,14 @@
|
||||
package system
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/yohamta/donburi/filter"
|
||||
"joylink.club/ecs"
|
||||
"joylink.club/rtsssimulation/components"
|
||||
"joylink.club/rtsssimulation/util"
|
||||
)
|
||||
|
||||
// 外界与world交互请求定义
|
||||
@ -58,6 +62,23 @@ func (me *FaceSystem) Call(request FaceRequest) (any, bool) {
|
||||
return result.outcome, result.ok
|
||||
}
|
||||
|
||||
// 根据id获取实体
|
||||
func (me *FaceSystem) FindEntity(id string) *ecs.Entry {
|
||||
query := ecs.NewQuery(filter.Contains(components.DeviceIdentityComponent))
|
||||
var entry *ecs.Entry = nil
|
||||
func() {
|
||||
defer util.Recover()
|
||||
query.Each(me.world, func(e *ecs.Entry) {
|
||||
if id == components.DeviceIdentityComponent.Get(e).Id {
|
||||
entry = e
|
||||
panic(fmt.Sprintf("找到实体[%s],结束查找", id))
|
||||
}
|
||||
})
|
||||
}()
|
||||
//
|
||||
return entry
|
||||
}
|
||||
|
||||
// 获取world当前时间
|
||||
func (me *FaceSystem) WorldTime() *time.Time {
|
||||
now, ok := me.Call(func(w ecs.World) any {
|
||||
|
@ -24,4 +24,6 @@ type ISwitchModel interface {
|
||||
type IPsdModel interface {
|
||||
//屏蔽门移动从0-100耗时,单位ms
|
||||
MovingTime() int64
|
||||
//屏蔽门的所有单元门
|
||||
AllDeviceCells() []IDeviceModel
|
||||
}
|
||||
|
6
util/common_util.go
Normal file
6
util/common_util.go
Normal file
@ -0,0 +1,6 @@
|
||||
package util
|
||||
|
||||
// 捕获panic并恢复执行
|
||||
func Recover() {
|
||||
recover()
|
||||
}
|
Loading…
Reference in New Issue
Block a user