道岔系统

This commit is contained in:
xzb 2023-08-18 11:23:52 +08:00
parent 8d8791cdd6
commit a4745d7a67
6 changed files with 226 additions and 552 deletions

View File

@ -18,14 +18,8 @@ var EntityTagHandlerComponent = ecs.NewComponentType[state.EntityTagHandler]()
var PercentageDeviceComponent = ecs.NewComponentType[state.PercentageDeviceState]()
var PercentageDeviceOperatingComponent = ecs.NewComponentType[state.PercentageDeviceOperating]()
// 道岔状态组件
var SwitchStateComponent = ecs.NewComponentType[state.SwitchState]()
// 道岔定反操继电器动作组件
var SwitchNRRelayOperatingComponent = ecs.NewComponentType[state.SwitchNRRelayOperating]()
// 道岔正常转动组件
var SwitchTurnOperatingComponent = ecs.NewComponentType[state.SwitchTurnOperating]()
// 道岔继电器状态组件
var SwitchRelayStateComponent = ecs.NewComponentType[state.SwitchRelayState]()
// 物理区段状态组件
var PhysicalSectionStateComponent = ecs.NewComponentType[state.PhysicalSectionState]()

View File

@ -26,7 +26,7 @@ func InitializeWorld(w ecs.World, worldTime time.Time) *system.FaceSystem {
// 创建道岔实体
func CreateSwitchEntity(w ecs.World) *ecs.Entry {
return w.Create(components.DeviceIdentityComponent, components.SwitchStateComponent)
return w.Create(components.DeviceIdentityComponent, components.SwitchRelayStateComponent, components.PercentageDeviceComponent)
}
// 创建信号机实体

View File

@ -10,41 +10,21 @@ message DeviceIdentity{
/////////////////////////////////////////////////////////
//
message SwitchState{
//
message SwitchRelayState{
//
//true-
bool normalRelay = 1;
//true-
bool dcJ = 1;
//
//true-
bool reverseRelay = 2;
//true-
bool fcJ = 2;
//
//true-
bool normalTable = 3;
bool dbJ = 3;
//
//true-
bool reverseTable = 4;
}
//
message SwitchNRRelayOperating{
//true-false-
bool normal = 1;
//
bool start = 2;
//ms
int64 operateTime = 3;
//true-false-
bool close = 4;
}
//
message SwitchTurnOperating{
//,true-
bool start = 1;
//ms
int64 operateTime = 2;
//,true-false-
bool turnNormal = 3;
}
bool fbJ = 4;
}
///////////////////////////////////////////////////////////

File diff suppressed because it is too large Load Diff

View File

@ -62,23 +62,6 @@ func debugSignal(w ecs.World) {
// 显示道岔状态
func debugSwitch(w ecs.World) {
switchesQuery := ecs.NewQuery(filter.Contains(components.DeviceIdentityComponent, components.SwitchStateComponent))
switchesQuery.Each(w, func(e *ecs.Entry) {
id := components.DeviceIdentityComponent.Get(e).Id
state := components.SwitchStateComponent.Get(e)
fmt.Printf("==>>道岔[%s] ,定操Relay=%t ,反操Relay=%t ,定表Relay=%t ,反表Relay=%t ,", id, state.NormalRelay, state.ReverseRelay, state.NormalTable, state.ReverseTable)
if e.HasComponent(components.SwitchNRRelayOperatingComponent) {
nrOperation := components.SwitchNRRelayOperatingComponent.Get(e)
if nrOperation.Normal {
fmt.Printf("定操动作Start=%t ,剩余时间=%d ,吸合动作=%t ,", nrOperation.Start, nrOperation.OperateTime, nrOperation.Close)
} else {
fmt.Printf("反操动作Start=%t ,剩余时间=%d ,吸合动作=%t ,", nrOperation.Start, nrOperation.OperateTime, nrOperation.Close)
}
}
if e.HasComponent(components.SwitchTurnOperatingComponent) {
turnOperation := components.SwitchTurnOperatingComponent.Get(e)
fmt.Printf("转动操作Start=%t ,转动到定位=%t ,剩余时间=%d", turnOperation.Start, turnOperation.TurnNormal, turnOperation.OperateTime)
}
fmt.Println()
})
//switchesQuery := ecs.NewQuery(filter.Contains(components.DeviceIdentityComponent, components.SwitchStateComponent))
}

View File

@ -1,21 +1,9 @@
package system
import (
"github.com/yohamta/donburi/filter"
"joylink.club/ecs"
"joylink.club/rtsssimulation/components"
"joylink.club/rtsssimulation/state"
)
// 定反操继电器查询
var nrRelayQuery *ecs.Query = ecs.NewQuery(filter.Contains(components.SwitchNRRelayOperatingComponent))
// 道岔转动查询
var turningQuery *ecs.Query = ecs.NewQuery(filter.Contains(components.SwitchTurnOperatingComponent))
// 道岔状态查询
var switchStateQuery *ecs.Query = ecs.NewQuery(filter.Contains(components.DeviceIdentityComponent, components.SwitchStateComponent))
// 道岔系统操作
type SwitchSystem struct {
}
@ -29,96 +17,13 @@ func NewSwitchSystem() *SwitchSystem {
// switchId: 道岔id
// turnNormal: true-道岔转动到定位false-道岔转动到反位
func FireSwitchTurn(w ecs.World, switchId string, turnNormal bool) bool {
var switchEntry *ecs.Entry = nil
switchStateQuery.Each(w, func(e *ecs.Entry) {
if id := components.DeviceIdentityComponent.Get(e).Id; id == switchId {
switchEntry = e
}
})
//
if nil == switchEntry {
return false
}
//
switchState := components.SwitchStateComponent.Get(switchEntry)
if turnNormal == switchState.NormalTable && !turnNormal == switchState.ReverseTable {
return true
}
//
nrRelayTime := getSwitchNRDelayTime(switchId)
if !switchEntry.HasComponent(components.SwitchNRRelayOperatingComponent) {
switchEntry.AddComponent(components.SwitchNRRelayOperatingComponent)
}
*(components.SwitchNRRelayOperatingComponent.Get(switchEntry)) = state.SwitchNRRelayOperating{Start: true, OperateTime: nrRelayTime, Close: true, Normal: turnNormal}
//
return true
}
// world 执行
func (me *SwitchSystem) Update(w ecs.World) {
updateNormalReverseRelay(w)
updateTurnOperating(w)
}
// 道岔转动动作
func updateTurnOperating(w ecs.World) {
turningQuery.Each(w, func(e *ecs.Entry) {
if !e.HasComponent(components.SwitchNRRelayOperatingComponent) {
operation := components.SwitchTurnOperatingComponent.Get(e)
switchState := components.SwitchStateComponent.Get(e)
if operation.Start {
if operation.OperateTime <= 0 {
operation.Start = false
switchState.NormalTable = operation.TurnNormal
switchState.ReverseTable = !operation.TurnNormal
switchState.NormalRelay = false
switchState.ReverseRelay = false
} else { //此时道岔失表
operation.OperateTime -= int64(w.Tick())
switchState.NormalTable = false
switchState.ReverseTable = false
}
}
if !operation.Start {
e.RemoveComponent(components.SwitchTurnOperatingComponent)
}
} else {
e.RemoveComponent(components.SwitchTurnOperatingComponent)
}
})
}
// 定反操继电器操作
func updateNormalReverseRelay(w ecs.World) {
nrRelayQuery.Each(w, func(e *ecs.Entry) {
operation := components.SwitchNRRelayOperatingComponent.Get(e)
if operation.Start {
if operation.OperateTime <= 0 {
operation.Start = false
//
switchState := components.SwitchStateComponent.Get(e)
if operation.Normal {
switchState.NormalRelay = operation.Close
} else {
switchState.ReverseRelay = operation.Close
}
//
if operation.Close { //触发道岔转动操作
if !e.HasComponent(components.SwitchTurnOperatingComponent) {
e.AddComponent(components.SwitchTurnOperatingComponent)
}
switchId := components.DeviceIdentityComponent.Get(e).Id
turnTime := getSwitchTurnTime(switchId)
*(components.SwitchTurnOperatingComponent.Get(e)) = state.SwitchTurnOperating{Start: true, OperateTime: turnTime, TurnNormal: operation.Normal}
}
} else {
operation.OperateTime -= int64(w.Tick())
}
}
if !operation.Start {
e.RemoveComponent(components.SwitchNRRelayOperatingComponent)
}
})
}
// 获取道岔转动耗时ms