rts-sim-module/sys/device_sys/section_detection.to.go

58 lines
1.5 KiB
Go
Raw Normal View History

2023-11-09 15:50:34 +08:00
package device_sys
import (
"github.com/yohamta/donburi"
"joylink.club/ecs"
"joylink.club/ecs/filter"
"joylink.club/rtsssimulation/component"
)
2023-11-10 10:39:41 +08:00
// SectionDetectSystem 区段检测系统
type SectionDetectSystem struct {
2023-11-09 15:50:34 +08:00
trainQuery *ecs.Query
axleSectionQuery *ecs.Query
}
2023-11-10 10:39:41 +08:00
func NewSectionDetectSystem() *SectionDetectSystem {
return &SectionDetectSystem{trainQuery: ecs.NewQuery(filter.Contains(component.UidType, component.TrainPositionInfoType)),
2023-11-09 15:50:34 +08:00
axleSectionQuery: ecs.NewQuery(filter.Contains(component.UidType, component.AxlePhysicalSectionType))}
}
2023-11-10 10:39:41 +08:00
func (s *SectionDetectSystem) Update(w ecs.World) {
2023-11-09 15:50:34 +08:00
//key-sectionId,统计区段上有车的情况
sectionTrainMap := make(map[string]*trainCount)
//所有列车
s.trainQuery.Each(w, func(entry *donburi.Entry) {
tp := component.TrainPositionInfoType.Get(entry)
for _, sectionId := range tp.SectionIds { //车所在区段
tc, find := sectionTrainMap[sectionId]
if !find {
tc = newTrainCount()
sectionTrainMap[sectionId] = tc
}
tc.add()
}
})
//计轴区段
s.axleSectionQuery.Each(w, func(entry *donburi.Entry) {
axleSectionId := component.UidType.Get(entry).Id
axleSection := component.AxlePhysicalSectionType.Get(entry)
tc, find := sectionTrainMap[axleSectionId]
if find {
axleSection.UpdateCount(int(tc.count))
} else {
axleSection.UpdateCount(0)
}
})
}
type trainCount struct {
count int8
}
func newTrainCount() *trainCount {
return &trainCount{count: 0}
}
func (c *trainCount) add() {
c.count++
}