rts-sim-module/system/psd_system.go

53 lines
1.6 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 (
"github.com/yohamta/donburi/filter"
"joylink.club/ecs"
"joylink.club/rtsssimulation/components"
2023-08-23 15:13:06 +08:00
"joylink.club/rtsssimulation/components/cstate"
2023-08-16 18:13:18 +08:00
)
2023-08-16 17:17:20 +08:00
2023-08-29 15:00:56 +08:00
var PsdQuery = ecs.NewQuery(filter.Contains(components.DeviceIdentityComponent, components.PsdStateComponent, components.PsdTagHandlerComponent))
2023-08-17 14:25:18 +08:00
2023-08-18 16:15:19 +08:00
const (
//屏蔽门完全关闭
2023-08-25 10:50:39 +08:00
PsdCellWholeCloseValue = PercentageRateValueMax
2023-08-18 16:15:19 +08:00
//屏蔽门完全打开
2023-08-25 10:50:39 +08:00
PsdCellWholeOpenValue = PercentageRateValueMin
2023-08-18 16:15:19 +08:00
)
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-23 16:01:01 +08:00
PsdQuery.Each(world, func(psdEntry *ecs.Entry) {
2023-08-29 15:00:56 +08:00
psdTag := components.PsdTagHandlerComponent.Get(psdEntry).Tag
2023-08-18 16:15:19 +08:00
psdCellQuery, ok := me.cellsQuery[psdTag]
2023-08-16 18:13:18 +08:00
if !ok {
2023-08-25 10:50:39 +08:00
psdCellQuery = ecs.NewQuery(filter.Contains(psdTag, components.PercentageDeviceStateComponent, components.MovableDeviceStateComponent))
2023-08-18 17:16:00 +08:00
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) {
2023-08-25 10:50:39 +08:00
psdCellRate := components.PercentageDeviceStateComponent.Get(psdCellEntry).Rate
2023-08-18 16:15:19 +08:00
if allCellClosed {
2023-08-25 10:50:39 +08:00
allCellClosed = psdCellRate == PsdCellWholeCloseValue
2023-08-16 18:13:18 +08:00
}
2023-08-18 16:15:19 +08:00
if allCellOpened {
2023-08-25 10:50:39 +08:00
allCellOpened = psdCellRate == PsdCellWholeOpenValue
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
})
}