rts-sim-module/sys/iscs_sys/iscs_pscada_pipe_fitting.go
2024-01-05 15:21:29 +08:00

94 lines
2.8 KiB
Go

package iscs_sys
import (
"joylink.club/ecs"
"joylink.club/rtsssimulation/component"
"joylink.club/rtsssimulation/entity"
"joylink.club/rtsssimulation/repository"
)
type PipeFittingSystem struct {
}
func NewPipeFittingSystem() *PipeFittingSystem {
return &PipeFittingSystem{}
}
// Update in world
func (s *PipeFittingSystem) Update(w ecs.World) {
wd := entity.GetWorldData(w)
for _, pf := range wd.Repo.PipeFittingMap {
if s.isPipeFittingEle(wd, pf) { //管件为电力管件
//筛选出相对电源
pipePsMap := s.findEleSourcePipe(wd, pf)
//管件连接的管线间电能传递
s.transEle(wd, pf, pipePsMap)
} else { //流体管件
}
}
}
// 筛选出相对电源
func (s *PipeFittingSystem) findEleSourcePipe(wd *component.WorldData, pf *repository.PipeFitting) map[string]*component.ElectricitySource {
//筛选出相对电源
pipePsMap := make(map[string]*component.ElectricitySource)
for _, pipePort := range pf.Ports() {
pipeEntry := wd.EntityMap[pipePort.Device().Id()]
powerPipe := perfectPipeElectricity(pipeEntry)
for epId, ep := range powerPipe.Sources {
pipePs, ok := pipePsMap[epId]
if ok {
if ep.Fresh > pipePs.Fresh {
pipePsMap[epId] = ep
}
} else {
pipePsMap[epId] = ep
}
}
}
return pipePsMap
}
// 管件连接的管线间电能传递
func (s *PipeFittingSystem) transEle(wd *component.WorldData, pf *repository.PipeFitting, pipePsMap map[string]*component.ElectricitySource) {
for _, pipePort := range pf.Ports() {
for pipePsId, pipePs := range pipePsMap { //相对电源
pipeEntry := wd.EntityMap[pipePort.Device().Id()]
powerPipe := perfectPipeElectricity(pipeEntry)
pipePortPs, ok := powerPipe.Sources[pipePsId]
if ok {
if pipePs.Fresh > pipePortPs.Fresh {
*powerPipe.Sources[pipePsId] = *pipePs
powerPipe.Sources[pipePsId].Fresh -= 1 //保证相对性
}
} else {
powerPipe.Sources[pipePsId] = &component.ElectricitySource{}
*powerPipe.Sources[pipePsId] = *pipePs
powerPipe.Sources[pipePsId].Fresh -= 1 //保证相对性
}
}
}
}
// 与管件连接的管线只要有一个为电线则其他的都为电线
func (s *PipeFittingSystem) isPipeFittingEle(wd *component.WorldData, pf *repository.PipeFitting) bool {
for _, pipePort := range pf.Ports() {
pipeEntry := wd.EntityMap[pipePort.Device().Id()]
pipe := component.PipeType.Get(pipeEntry)
if pipe.Matter.IsEle() {
return true
}
}
return false
}
// 完善电线实体并获取电线状态
func perfectPipeElectricity(pipeEntry *ecs.Entry) *component.PipeElectricity {
component.PipeType.Get(pipeEntry).Matter = component.PmtElectricity
if !pipeEntry.HasComponent(component.PipeElectricityType) {
pipeEntry.AddComponent(component.PipeElectricityType)
}
return component.PipeElectricityType.Get(pipeEntry)
}