rts-sim-module/system/psd_system.go

140 lines
4.0 KiB
Go
Raw Normal View History

2023-08-16 17:17:20 +08:00
package system
2023-08-16 18:13:18 +08:00
import (
2023-08-18 16:15:19 +08:00
"fmt"
2023-08-16 18:13:18 +08:00
"github.com/yohamta/donburi/filter"
"joylink.club/ecs"
"joylink.club/rtsssimulation/components"
"joylink.club/rtsssimulation/state"
)
2023-08-16 17:17:20 +08:00
2023-08-18 10:06:17 +08:00
var psdQuery = ecs.NewQuery(filter.Contains(components.DeviceIdentityComponent, components.PsdStateComponent, components.EntityTagHandlerComponent))
2023-08-17 14:25:18 +08:00
2023-08-18 16:15:19 +08:00
const (
//屏蔽门完全关闭
PsdCellWholeCloseRate = 100
//屏蔽门完全打开
PsdCellWholeOpenRate = 0
)
2023-08-16 17:17:20 +08:00
type PsdSystem struct {
2023-08-18 16:15:19 +08:00
cellsQuery map[state.EntityTag]*ecs.Query
2023-08-16 17:17:20 +08:00
}
func NewPsdSystem() *PsdSystem {
2023-08-18 16:15:19 +08:00
return &PsdSystem{cellsQuery: make(map[state.EntityTag]*ecs.Query)}
2023-08-16 17:17:20 +08:00
}
// world 执行
func (me *PsdSystem) Update(world ecs.World) {
2023-08-18 16:15:19 +08:00
psdQuery.Each(world, func(psdEntry *ecs.Entry) {
psdTag := components.EntityTagHandlerComponent.Get(psdEntry).Tag
psdCellQuery, ok := me.cellsQuery[psdTag]
2023-08-16 18:13:18 +08:00
if !ok {
2023-08-18 16:15:19 +08:00
me.cellsQuery[psdTag] = ecs.NewQuery(filter.Contains(psdTag, components.PercentageDeviceComponent))
psdCellQuery = me.cellsQuery[psdTag]
2023-08-16 18:13:18 +08:00
}
2023-08-18 16:15:19 +08:00
//
var allCellClosed, allCellOpened bool = true, true
psdCellQuery.Each(world, func(psdCellEntry *ecs.Entry) {
psdCellRate := components.PercentageDeviceComponent.Get(psdCellEntry).Rate
if allCellClosed {
allCellClosed = psdCellRate == PsdCellWholeCloseRate
2023-08-16 18:13:18 +08:00
}
2023-08-18 16:15:19 +08:00
if allCellOpened {
allCellOpened = psdCellRate == PsdCellWholeOpenRate
2023-08-16 18:13:18 +08:00
}
})
//
2023-08-18 16:15:19 +08:00
psdState := components.PsdStateComponent.Get(psdEntry)
psdState.AllClosed = allCellClosed
psdState.AllOpened = allCellOpened
2023-08-16 18:13:18 +08:00
})
}
2023-08-17 14:25:18 +08:00
// 车站单侧屏蔽门互锁解除操作
func FirePsdInterlockRelease(world ecs.World, psdId string, lockRelease bool) bool {
var psdEntry *ecs.Entry = nil
psdQuery.Each(world, func(e *ecs.Entry) {
2023-08-18 10:06:17 +08:00
if psdId == components.DeviceIdentityComponent.Get(e).Id {
2023-08-17 14:25:18 +08:00
psdEntry = e
}
})
//
if psdEntry == nil {
return false
}
2023-08-18 10:06:17 +08:00
components.PsdStateComponent.Get(psdEntry).InterlockReleased = lockRelease
2023-08-17 14:25:18 +08:00
//
return true
}
// 车站单侧屏蔽门操作
// closeOpt true-关门操作false-开门操作
2023-08-18 16:15:19 +08:00
func FirePsdOperation(world ecs.World, psdId string, closeOpt bool) error {
psdEntry, err := findPsdEntry(world, psdId)
if nil != err {
return err
2023-08-17 14:25:18 +08:00
}
2023-08-18 16:15:19 +08:00
//屏蔽门标签
2023-08-18 10:06:17 +08:00
psdTag := components.EntityTagHandlerComponent.Get(psdEntry).Tag
2023-08-17 14:25:18 +08:00
psdCellQuery := ecs.NewQuery(filter.Contains(psdTag))
2023-08-18 16:15:19 +08:00
psdCellQuery.Each(world, func(psdCellEntry *ecs.Entry) {
if closeOpt {
FirePercentageDeviceOperation(world, psdCellEntry, findPsdWholeMoveTime(psdId), PsdCellWholeCloseRate)
} else {
FirePercentageDeviceOperation(world, psdCellEntry, findPsdWholeMoveTime(psdId), PsdCellWholeOpenRate)
}
2023-08-17 14:25:18 +08:00
})
//
2023-08-18 16:15:19 +08:00
return nil
2023-08-17 14:25:18 +08:00
}
2023-08-18 16:15:19 +08:00
// 车站单侧屏蔽门单元cell操作
// closeRate cell最终关上的百分比100-完全关上0-完全未关即完全打开,(0,100)内即半关闭
func FirePsdCellOperation(world ecs.World, psdId string, psdCellId string, closeRate int32) error {
if closeRate < 0 || closeRate > 100 {
return fmt.Errorf("屏蔽门单元操作closeRate(%d)不在[0,100]内", closeRate)
2023-08-17 14:25:18 +08:00
}
//
2023-08-18 16:15:19 +08:00
psdEntry, err := findPsdEntry(world, psdId)
if nil != err {
return err
}
//屏蔽门标签
2023-08-18 10:06:17 +08:00
psdTag := components.EntityTagHandlerComponent.Get(psdEntry).Tag
2023-08-17 14:25:18 +08:00
psdCellQuery := ecs.NewQuery(filter.Contains(psdTag))
var psdCellEntry *ecs.Entry = nil
psdCellQuery.Each(world, func(e *ecs.Entry) {
2023-08-18 10:06:17 +08:00
if psdCellId == components.DeviceIdentityComponent.Get(e).Id {
2023-08-17 14:25:18 +08:00
psdCellEntry = e
}
})
if psdCellEntry == nil {
2023-08-18 16:15:19 +08:00
return fmt.Errorf("屏蔽门[%s]的单元门[%s]的实体不存在", psdId, psdCellId)
2023-08-17 14:25:18 +08:00
}
//
2023-08-18 16:15:19 +08:00
return FirePercentageDeviceOperation(world, psdCellEntry, findPsdWholeMoveTime(psdId), closeRate)
}
// 获取屏蔽门实体
func findPsdEntry(world ecs.World, psdId string) (*ecs.Entry, error) {
var psdEntry *ecs.Entry = nil
psdQuery.Each(world, func(e *ecs.Entry) {
if psdId == components.DeviceIdentityComponent.Get(e).Id {
psdEntry = e
}
})
if psdEntry == nil {
return nil, fmt.Errorf("屏蔽门[%s]实体不存在", psdId)
2023-08-17 14:25:18 +08:00
} else {
2023-08-18 16:15:19 +08:00
return psdEntry, nil
2023-08-17 14:25:18 +08:00
}
2023-08-18 16:15:19 +08:00
}
// 获取屏蔽门从完全开到完全关所需时间单位ms
func findPsdWholeMoveTime(psdId string) int64 {
return 4000
2023-08-17 14:25:18 +08:00
}