From 406cfe6c0a8f2df9410c2d5ebd58ae1a2994e4e9 Mon Sep 17 00:00:00 2001 From: xzb <223@qq.com> Date: Thu, 12 Oct 2023 11:00:56 +0800 Subject: [PATCH] =?UTF-8?q?signal=202xh1=20=E9=A9=B1=E9=87=87?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- component/signal.go | 23 +++++++++++++++---- entity/light.go | 11 +++++++++ entity/signal.go | 5 ++++ entity/signal_2xh1.go | 4 +++- examples/signal_2xh1/sigSys/debug_sys.go | 7 ++++-- sys/circuit_sys/signal_2xh1.go | 29 +++++++++++++++--------- 6 files changed, 61 insertions(+), 18 deletions(-) diff --git a/component/signal.go b/component/signal.go index 4f07f8e..ef1c894 100644 --- a/component/signal.go +++ b/component/signal.go @@ -2,6 +2,25 @@ package component import "joylink.club/ecs" +// SignalLights 信号机灯位列表 +type SignalLights struct { + Lights []*ecs.Entry +} + +var SignalLightsType = ecs.NewComponentType[SignalLights]() + +// GetLightByTag 根据标签获取灯位列表中的某个灯 +func (sl *SignalLights) GetLightByTag(dTag ecs.IComponentType) *ecs.Entry { + for _, light := range sl.Lights { + if light.HasComponent(dTag) { + return light + } + } + return nil +} + +/////////////////////////////////////////////////// + // Signal2XH1Electronic 电路状态:**信号机2XH-1(红-绿) 出段(场)信号机 或 **出站区间阻挡信号机 type Signal2XH1Electronic struct { // 点灯继电器,true-吸合,常态落下表示逻辑点灯 @@ -10,10 +29,6 @@ type Signal2XH1Electronic struct { Z2XH1_DJ *ecs.Entry //列车信号继电器,true-吸合 Z2XH1_LXJ *ecs.Entry - //物理绿灯,true-亮 - Z2XH1_L *ecs.Entry - // 物理红灯,true-亮 - Z2XH1_H *ecs.Entry } // Signal2XH1Lsq 联锁驱 diff --git a/entity/light.go b/entity/light.go index d8319e9..af433e5 100644 --- a/entity/light.go +++ b/entity/light.go @@ -44,3 +44,14 @@ func NewLightAEntity(w ecs.World) *ecs.Entry { e.AddComponent(component.AdTag) return e } + +// NewLights 创建灯位列表(灯位顺序与lightTags一致) +func NewLights(w ecs.World, lightTags ...ecs.IComponentType) []*ecs.Entry { + ls := make([]*ecs.Entry, 0, len(lightTags)) + for _, lightTag := range lightTags { + lightEntry := NewLightEntity(w) + lightEntry.AddComponent(lightTag) + ls = append(ls, lightEntry) + } + return ls +} diff --git a/entity/signal.go b/entity/signal.go index 6d63138..5acc291 100644 --- a/entity/signal.go +++ b/entity/signal.go @@ -72,3 +72,8 @@ func newSignalEntity(w ecs.World, uid string, worldData *component.WorldData) *e } return entry } + +// 新建灯位列表 +func newSignalLights(w ecs.World, lightTags ...ecs.IComponentType) *component.SignalLights { + return &component.SignalLights{Lights: NewLights(w, lightTags...)} +} diff --git a/entity/signal_2xh1.go b/entity/signal_2xh1.go index f79d961..ea5cdee 100644 --- a/entity/signal_2xh1.go +++ b/entity/signal_2xh1.go @@ -14,8 +14,9 @@ func loadSignal2xh1(w ecs.World, signal *repository.Signal, signalEntry *ecs.Ent signalEntry.AddComponent(component.Signal2XH1ElectronicType) signalEntry.AddComponent(component.Signal2XH1LsqType) signalEntry.AddComponent(component.Signal2XH1LscType) + signalEntry.AddComponent(component.SignalLightsType) // - elecState := &component.Signal2XH1Electronic{Z2XH1_L: NewLightLEntity(w), Z2XH1_H: NewLightHEntity(w)} + elecState := &component.Signal2XH1Electronic{} for _, elec := range elecs { switch elec.Code() { case consts.SIGNAL_DDJ: @@ -32,6 +33,7 @@ func loadSignal2xh1(w ecs.World, signal *repository.Signal, signalEntry *ecs.Ent component.Signal2XH1ElectronicType.Set(signalEntry, elecState) component.Signal2XH1LsqType.Set(signalEntry, &component.Signal2XH1Lsq{}) component.Signal2XH1LscType.Set(signalEntry, &component.Signal2XH1Lsc{}) + component.SignalLightsType.Set(signalEntry, newSignalLights(w, component.HdTag, component.LdTag)) } else { return fmt.Errorf("id=[%s]的信号机2xh1,电子元器件数量须为3", signal.Id()) } diff --git a/examples/signal_2xh1/sigSys/debug_sys.go b/examples/signal_2xh1/sigSys/debug_sys.go index d5128a1..8004820 100644 --- a/examples/signal_2xh1/sigSys/debug_sys.go +++ b/examples/signal_2xh1/sigSys/debug_sys.go @@ -20,8 +20,11 @@ func (s *SignalDebugSystem) Update(w ecs.World) { s.query.Each(w, func(entry *ecs.Entry) { uid := component.UidType.Get(entry) state := component.Signal2XH1ElectronicType.Get(entry) - ld := component.BitStateType.Get(state.Z2XH1_L) - hd := component.BitStateType.Get(state.Z2XH1_H) + lights := component.SignalLightsType.Get(entry) + Z2XH1_L := lights.GetLightByTag(component.LdTag) + Z2XH1_H := lights.GetLightByTag(component.HdTag) + ld := component.BitStateType.Get(Z2XH1_L) + hd := component.BitStateType.Get(Z2XH1_H) // ddj := component.BitStateType.Get(state.Z2XH1_DDJ) lxj := component.BitStateType.Get(state.Z2XH1_LXJ) diff --git a/sys/circuit_sys/signal_2xh1.go b/sys/circuit_sys/signal_2xh1.go index a0d0586..730457d 100644 --- a/sys/circuit_sys/signal_2xh1.go +++ b/sys/circuit_sys/signal_2xh1.go @@ -11,7 +11,11 @@ type Signal2XH1System struct { } func NewSignal2XH1System() *Signal2XH1System { - return &Signal2XH1System{query: ecs.NewQuery(filter.Contains(component.Signal2XH1ElectronicType))} + return &Signal2XH1System{query: ecs.NewQuery(filter.Contains( + component.Signal2XH1ElectronicType, + component.Signal2XH1LsqType, + component.Signal2XH1LscType, + component.SignalLightsType))} } // Update world 执行 @@ -20,10 +24,13 @@ func (s *Signal2XH1System) Update(w ecs.World) { state := component.Signal2XH1ElectronicType.Get(entry) lsq := component.Signal2XH1LsqType.Get(entry) lsc := component.Signal2XH1LscType.Get(entry) + lights := component.SignalLightsType.Get(entry) + Z2XH1_L := lights.GetLightByTag(component.LdTag) + Z2XH1_H := lights.GetLightByTag(component.HdTag) s.calculateLsq(state, lsq) - s.calculateL(state) - s.calculateH(state) - s.calculateDJ(state) + s.calculateL(state, Z2XH1_L) + s.calculateH(state, Z2XH1_H) + s.calculateDJ(state, Z2XH1_L, Z2XH1_H) s.calculateLsc(state, lsc) }) } @@ -52,25 +59,25 @@ func (s *Signal2XH1System) calculateLsc(state *component.Signal2XH1Electronic, l lsc.Z2XH1_DJ_Xq = dj.Val lsc.Z2XH1_LXJ_Xq = lxj.Val } -func (s *Signal2XH1System) calculateL(state *component.Signal2XH1Electronic) { - driveL := component.LightDriveType.Get(state.Z2XH1_L) +func (s *Signal2XH1System) calculateL(state *component.Signal2XH1Electronic, Z2XH1_L *ecs.Entry) { + driveL := component.LightDriveType.Get(Z2XH1_L) ddj := component.BitStateType.Get(state.Z2XH1_DDJ) lxj := component.BitStateType.Get(state.Z2XH1_LXJ) isL := !ddj.Val && lxj.Val driveL.Td = isL } -func (s *Signal2XH1System) calculateH(state *component.Signal2XH1Electronic) { - driveH := component.LightDriveType.Get(state.Z2XH1_H) +func (s *Signal2XH1System) calculateH(state *component.Signal2XH1Electronic, Z2XH1_H *ecs.Entry) { + driveH := component.LightDriveType.Get(Z2XH1_H) ddj := component.BitStateType.Get(state.Z2XH1_DDJ) lxj := component.BitStateType.Get(state.Z2XH1_LXJ) isH := !ddj.Val && !lxj.Val driveH.Td = isH } -func (s *Signal2XH1System) calculateDJ(state *component.Signal2XH1Electronic) { +func (s *Signal2XH1System) calculateDJ(state *component.Signal2XH1Electronic, Z2XH1_L *ecs.Entry, Z2XH1_H *ecs.Entry) { ddj := component.BitStateType.Get(state.Z2XH1_DDJ) lxj := component.BitStateType.Get(state.Z2XH1_LXJ) - ld := component.BitStateType.Get(state.Z2XH1_L) - hd := component.BitStateType.Get(state.Z2XH1_H) + ld := component.BitStateType.Get(Z2XH1_L) + hd := component.BitStateType.Get(Z2XH1_H) isDJ := !ddj.Val && lxj.Val && ld.Val || !ddj.Val && !lxj.Val && hd.Val //通知继电器进行动作 drive := component.RelayDriveType.Get(state.Z2XH1_DJ)