132 lines
3.9 KiB
Go
132 lines
3.9 KiB
Go
package system
|
||
|
||
import (
|
||
"fmt"
|
||
|
||
"github.com/yohamta/donburi/filter"
|
||
"joylink.club/ecs"
|
||
"joylink.club/rtsssimulation/components"
|
||
"joylink.club/rtsssimulation/cstate"
|
||
"joylink.club/rtsssimulation/memory"
|
||
"joylink.club/rtsssimulation/umi"
|
||
)
|
||
|
||
var psdQuery = ecs.NewQuery(filter.Contains(components.DeviceIdentityComponent, components.PsdStateComponent, components.EntityTagHandlerComponent))
|
||
|
||
const (
|
||
//屏蔽门完全关闭
|
||
PsdCellWholeCloseRate = 100
|
||
//屏蔽门完全打开
|
||
PsdCellWholeOpenRate = 0
|
||
)
|
||
|
||
type PsdSystem struct {
|
||
cellsQuery map[cstate.EntityTag]*ecs.Query
|
||
}
|
||
|
||
func NewPsdSystem() *PsdSystem {
|
||
return &PsdSystem{cellsQuery: make(map[cstate.EntityTag]*ecs.Query)}
|
||
}
|
||
|
||
// world 执行
|
||
func (me *PsdSystem) Update(world ecs.World) {
|
||
psdQuery.Each(world, func(psdEntry *ecs.Entry) {
|
||
psdTag := components.EntityTagHandlerComponent.Get(psdEntry).Tag
|
||
psdCellQuery, ok := me.cellsQuery[psdTag]
|
||
if !ok {
|
||
psdCellQuery = ecs.NewQuery(filter.Contains(psdTag, components.PercentageDeviceComponent))
|
||
me.cellsQuery[psdTag] = psdCellQuery
|
||
}
|
||
//
|
||
var allCellClosed, allCellOpened bool = true, true
|
||
psdCellQuery.Each(world, func(psdCellEntry *ecs.Entry) {
|
||
psdCellRate := components.PercentageDeviceComponent.Get(psdCellEntry).Rate
|
||
if allCellClosed {
|
||
allCellClosed = psdCellRate == PsdCellWholeCloseRate
|
||
}
|
||
if allCellOpened {
|
||
allCellOpened = psdCellRate == PsdCellWholeOpenRate
|
||
}
|
||
})
|
||
//
|
||
psdState := components.PsdStateComponent.Get(psdEntry)
|
||
psdState.AllClosed = allCellClosed
|
||
psdState.AllOpened = allCellOpened
|
||
})
|
||
}
|
||
|
||
// 车站单侧屏蔽门互锁解除操作
|
||
func FirePsdInterlockRelease(world ecs.World, psdId string, lockRelease bool) error {
|
||
psdEntry, err := findPsdEntry(world, psdId)
|
||
if nil != err {
|
||
return err
|
||
}
|
||
components.PsdStateComponent.Get(psdEntry).InterlockReleased = lockRelease
|
||
//
|
||
return nil
|
||
}
|
||
|
||
// 车站单侧屏蔽门操作
|
||
// closeOpt true-关门操作,false-开门操作
|
||
func FirePsdOperation(world ecs.World, psdId string, closeOpt bool) error {
|
||
psdEntry, err := findPsdEntry(world, psdId)
|
||
if nil != err {
|
||
return err
|
||
}
|
||
//屏蔽门标签
|
||
psdTag := components.EntityTagHandlerComponent.Get(psdEntry).Tag
|
||
psdCellQuery := ecs.NewQuery(filter.Contains(psdTag))
|
||
psdCellQuery.Each(world, func(psdCellEntry *ecs.Entry) {
|
||
if closeOpt {
|
||
FirePercentageDeviceOperation(world, psdCellEntry, findPsdWholeMoveTime(psdId), PsdCellWholeCloseRate)
|
||
} else {
|
||
FirePercentageDeviceOperation(world, psdCellEntry, findPsdWholeMoveTime(psdId), PsdCellWholeOpenRate)
|
||
}
|
||
})
|
||
//
|
||
return nil
|
||
}
|
||
|
||
// 车站单侧屏蔽门单元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)
|
||
}
|
||
//
|
||
psdEntry, err := findPsdEntry(world, psdId)
|
||
if nil != err {
|
||
return err
|
||
}
|
||
//屏蔽门标签
|
||
psdTag := components.EntityTagHandlerComponent.Get(psdEntry).Tag
|
||
psdCellQuery := ecs.NewQuery(filter.Contains(psdTag))
|
||
psdCellEntry := queryEntityById(world, psdCellQuery, psdCellId)
|
||
if psdCellEntry == nil {
|
||
return fmt.Errorf("屏蔽门[%s]的单元门[%s]的实体不存在", psdId, psdCellId)
|
||
}
|
||
//
|
||
return FirePercentageDeviceOperation(world, psdCellEntry, findPsdWholeMoveTime(psdId), closeRate)
|
||
}
|
||
|
||
// 获取屏蔽门实体
|
||
func findPsdEntry(world ecs.World, psdId string) (*ecs.Entry, error) {
|
||
psdEntry := queryEntityById(world, psdQuery, psdId)
|
||
if psdEntry == nil {
|
||
return nil, fmt.Errorf("屏蔽门[%s]实体不存在", psdId)
|
||
} else {
|
||
return psdEntry, nil
|
||
}
|
||
}
|
||
|
||
// 获取屏蔽门从完全开到完全关所需时间,单位ms
|
||
func findPsdWholeMoveTime(psdId string) int64 {
|
||
psd := memory.DeviceModelStorage.FindModelById(psdId)
|
||
if nil != psd {
|
||
psdUmi := psd.(umi.IPsdModel)
|
||
return psdUmi.MovingTime()
|
||
}
|
||
//默认值
|
||
return 4000
|
||
}
|