rts-sim-module/system/psd_system.go
2023-08-23 16:01:01 +08:00

53 lines
1.5 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.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
})
}