signal 2xh1 驱采

This commit is contained in:
xzb 2023-10-12 11:00:56 +08:00
parent b6bdcc8748
commit 406cfe6c0a
6 changed files with 61 additions and 18 deletions

View File

@ -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 联锁驱

View File

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

View File

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

View File

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

View File

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

View File

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