diff --git a/entities/common_entity.go b/entities/common_entity.go index aee674e..2c93013 100644 --- a/entities/common_entity.go +++ b/entities/common_entity.go @@ -15,14 +15,6 @@ func CreateModelStorageEntity(w ecs.World, modelManager umi.IModelManager) *ecs. return e } -// 创建继电器实体 -func CreateRelayEntity(w ecs.World, relayId string) *ecs.Entry { - e := w.Create(system.EntityIdentityComponent, system.RelayStateComponent) - system.EntityIdentityComponent.Set(e, &system.EntityIdentity{Id: relayId}) - system.RelayStateComponent.Set(e, system.NewRelayState()) - return e -} - // 创建系统时钟实体 func CreateSystemTimerEntity(w ecs.World, systemTime time.Time) *ecs.Entry { e := w.Create(system.SystemTimerComponent) diff --git a/entities/relay_entity.go b/entities/relay_entity.go new file mode 100644 index 0000000..9b65a19 --- /dev/null +++ b/entities/relay_entity.go @@ -0,0 +1,14 @@ +package entities + +import ( + "joylink.club/ecs" + "joylink.club/rtsssimulation/system" +) + +// 创建继电器实体 +func CreateRelayEntity(w ecs.World, relayId string) *ecs.Entry { + e := w.Create(system.EntityIdentityComponent, system.RelayStateComponent) + system.EntityIdentityComponent.Set(e, &system.EntityIdentity{Id: relayId}) + system.RelayStateComponent.Set(e, system.NewRelayState()) + return e +} diff --git a/examples/test1/main.go b/examples/test1/main.go index 3247efa..970f6c6 100644 --- a/examples/test1/main.go +++ b/examples/test1/main.go @@ -2,6 +2,7 @@ package main import ( "fmt" + sysEvent "joylink.club/rtsssimulation/system/event" "time" "github.com/yohamta/donburi/filter" @@ -23,7 +24,7 @@ func main() { // worldConfig := simulation.WorldConfig{ ModelManager: worldStorage, - Systems: []ecs.ISystem{system.NewTimerSystem(), system.NewSwitch2jZdj9System(), system.NewDebugSystem()}, + Systems: []ecs.ISystem{system.NewTimerSystem(), system.NewSwitch2jZdj9System(), system.NewRelaySystem(), system.NewMovableSystem(), system.NewDebugSystem()}, Tick: 100, InitTime: time.Now(), } @@ -31,9 +32,18 @@ func main() { addEntities(world, modelStorage) initComponents(world) // - world.StartUp() + //ecs.EventsDebugEnable() // - time.Sleep(5 * time.Second) + system.Switch2jzdj9AddListeners(world) + system.RelayAddListeners(world) + // + world.StartUp() + time.Sleep(1 * time.Second) + //反操 + sysEvent.RelayNeedChangeEventBus.PublishOutWorld(world, &sysEvent.RelayNeedChangeEvent{Id: "sw1-sjzdj9-ycj", Xh: true}) + sysEvent.RelayNeedChangeEventBus.PublishOutWorld(world, &sysEvent.RelayNeedChangeEvent{Id: "sw1-sjzdj9-fcj", Xh: true}) + // + time.Sleep(10 * time.Second) world.Close() } diff --git a/jl-ecs-go b/jl-ecs-go index c3b9d96..747a81e 160000 --- a/jl-ecs-go +++ b/jl-ecs-go @@ -1 +1 @@ -Subproject commit c3b9d965c607a2f29e0bdc586aba6851d4f29f13 +Subproject commit 747a81e44a4318a0390422156de44b7c3afddb06 diff --git a/system/dubug_system.go b/system/dubug_system.go index 7bce7f9..680f046 100644 --- a/system/dubug_system.go +++ b/system/dubug_system.go @@ -18,6 +18,8 @@ func (debug *DebugSystem) Update(w ecs.World) { zdj9Query.Each(w, func(entry *ecs.Entry) { swId := EntityIdentityComponent.Get(entry).Id swState := Switch2jZdj9StateComponent.Get(entry) - log.Println(fmt.Sprintf("==>>2JZDJ9=%s j1-2DQJ=%t", swId, swState.J1_2DQJ)) + //log.Println(fmt.Sprintf("==>>2JZDJ9=%s QDJ=%t j1-2DQJ=%t j2-2DQJ=%t", swId, swState.J1_QDJ, swState.J1_2DQJ, swState.J2_2DQJ)) + //log.Println(fmt.Sprintf("==>>2JZDJ9=%s j1-1DQJ=%t j2-1DQJ=%t", swId, swState.J1_1DQJ, swState.J2_1DQJ)) + log.Println(fmt.Sprintf("==>>2JZDJ9=%s YCJ=%t DCJ=%t FCJ=%t ZDBJ=%t ZFBJ=%t", swId, swState.YCJ, swState.DCJ, swState.FCJ, swState.ZDBJ, swState.ZFBJ)) }) } diff --git a/system/move_system.go b/system/move_system.go index 9fc4174..382ba54 100644 --- a/system/move_system.go +++ b/system/move_system.go @@ -21,18 +21,18 @@ var MovableObjectComponent = ecs.NewComponentType[MovableObject]() var MovableObject1Component = ecs.NewComponentType[MovableObject]() var MovableObject2Component = ecs.NewComponentType[MovableObject]() -type TimeMovableSystem struct { +type MovableSystem struct { query *ecs.Query } -func NewTimeMovableSystem() *TimeMovableSystem { - return &TimeMovableSystem{ +func NewMovableSystem() *MovableSystem { + return &MovableSystem{ query: ecs.NewQuery(filter.Or(filter.Contains(MovableObjectComponent), filter.Contains(MovableObject1Component), filter.Contains(MovableObject2Component))), } } // world 执行 -func (me *TimeMovableSystem) Update(w ecs.World) { +func (me *MovableSystem) Update(w ecs.World) { me.query.Each(w, func(e *ecs.Entry) { if e.HasComponent(MovableObjectComponent) { diff --git a/system/relay_system.go b/system/relay_system.go index fa9ae6d..f3771a8 100644 --- a/system/relay_system.go +++ b/system/relay_system.go @@ -4,6 +4,7 @@ import ( "github.com/yohamta/donburi/filter" "joylink.club/ecs" sysEvent "joylink.club/rtsssimulation/system/event" + "log" ) // 继电器有两个线圈1-2,3-4 @@ -51,8 +52,13 @@ func NewRelaySystem() *RelaySystem { } } +func RelayAddListeners(w ecs.World) { + sysEvent.RelayNeedChangeEventBus.Subscribe(w, RelayNeedChangeEventProcessor) +} + // 继电器须要改变事件处理 var RelayNeedChangeEventProcessor = func(w ecs.World, event sysEvent.RelayNeedChangeEvent) { + log.Println("==>>收到继电器须改变事件:id = ", event.Id, " xh = ", event.Xh) relayEntry := FindEntityById(w, event.Id) relayState := RelayStateComponent.Get(relayEntry) relayState.needXh = event.Xh diff --git a/system/switch_2jzdj9_system.go b/system/switch_2jzdj9_system.go index 50d53fb..2eddac6 100644 --- a/system/switch_2jzdj9_system.go +++ b/system/switch_2jzdj9_system.go @@ -2,6 +2,7 @@ package system import ( "fmt" + "log" "github.com/yohamta/donburi/filter" "joylink.club/ecs" @@ -276,8 +277,13 @@ const ( SJZDJ9_ZFBJ = "ZFBJ" ) +func Switch2jzdj9AddListeners(w ecs.World) { + sysEvent.RelayActionEventBus.Subscribe(w, Switch2jzdj9RelayActionEventProcessor) +} + // 接收继电器动作事件 var Switch2jzdj9RelayActionEventProcessor = func(w ecs.World, event sysEvent.RelayActionEvent) { + log.Println("==>>收到继电器动作事件:id = ", event.Id, " xh = ", event.Xh) zdj9Query.Each(w, func(e *ecs.Entry) { switchModel := FindModelStorage(w).FindById(EntityIdentityComponent.Get(e).Id) roler, ok := switchModel.(umi.IRelayCRole)