rts-sim-module/system/psd_system.go

132 lines
3.9 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"
2023-08-22 11:00:14 +08:00
"joylink.club/rtsssimulation/cstate"
"joylink.club/rtsssimulation/memory"
"joylink.club/rtsssimulation/umi"
2023-08-16 18:13:18 +08:00
)
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-22 11:00:14 +08:00
cellsQuery map[cstate.EntityTag]*ecs.Query
2023-08-16 17:17:20 +08:00
}
func NewPsdSystem() *PsdSystem {
2023-08-22 11:00:14 +08:00
return &PsdSystem{cellsQuery: make(map[cstate.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 17:16:00 +08:00
psdCellQuery = ecs.NewQuery(filter.Contains(psdTag, components.PercentageDeviceComponent))
me.cellsQuery[psdTag] = psdCellQuery
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
// 车站单侧屏蔽门互锁解除操作
2023-08-18 16:45:05 +08:00
func FirePsdInterlockRelease(world ecs.World, psdId string, lockRelease bool) error {
psdEntry, err := findPsdEntry(world, psdId)
if nil != err {
return err
2023-08-17 14:25:18 +08:00
}
2023-08-18 10:06:17 +08:00
components.PsdStateComponent.Get(psdEntry).InterlockReleased = lockRelease
2023-08-17 14:25:18 +08:00
//
2023-08-18 16:45:05 +08:00
return nil
2023-08-17 14:25:18 +08:00
}
// 车站单侧屏蔽门操作
// 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))
2023-08-22 15:04:46 +08:00
psdCellEntry := queryEntityById(world, psdCellQuery, psdCellId)
2023-08-17 14:25:18 +08:00
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) {
2023-08-22 15:04:46 +08:00
psdEntry := queryEntityById(world, psdQuery, psdId)
2023-08-18 16:15:19 +08:00
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 {
2023-08-22 11:00:14 +08:00
psd := memory.DeviceModelStorage.FindModelById(psdId)
if nil != psd {
psdUmi := psd.(umi.IPsdModel)
return psdUmi.MovingTime()
}
//默认值
2023-08-18 16:15:19 +08:00
return 4000
2023-08-17 14:25:18 +08:00
}