rts-sim-module/system/psd_system.go
2023-08-29 15:00:56 +08:00

53 lines
1.6 KiB
Go

package system
import (
"github.com/yohamta/donburi/filter"
"joylink.club/ecs"
"joylink.club/rtsssimulation/components"
"joylink.club/rtsssimulation/components/cstate"
)
var PsdQuery = ecs.NewQuery(filter.Contains(components.DeviceIdentityComponent, components.PsdStateComponent, components.PsdTagHandlerComponent))
const (
//屏蔽门完全关闭
PsdCellWholeCloseValue = PercentageRateValueMax
//屏蔽门完全打开
PsdCellWholeOpenValue = PercentageRateValueMin
)
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.PsdTagHandlerComponent.Get(psdEntry).Tag
psdCellQuery, ok := me.cellsQuery[psdTag]
if !ok {
psdCellQuery = ecs.NewQuery(filter.Contains(psdTag, components.PercentageDeviceStateComponent, components.MovableDeviceStateComponent))
me.cellsQuery[psdTag] = psdCellQuery
}
//
var allCellClosed, allCellOpened bool = true, true
psdCellQuery.Each(world, func(psdCellEntry *ecs.Entry) {
psdCellRate := components.PercentageDeviceStateComponent.Get(psdCellEntry).Rate
if allCellClosed {
allCellClosed = psdCellRate == PsdCellWholeCloseValue
}
if allCellOpened {
allCellOpened = psdCellRate == PsdCellWholeOpenValue
}
})
//
psdState := components.PsdStateComponent.Get(psdEntry)
psdState.AllClosed = allCellClosed
psdState.AllOpened = allCellOpened
})
}