2023-12-21 16:14:02 +08:00
|
|
|
package iscs_sys
|
|
|
|
|
|
|
|
import (
|
|
|
|
"joylink.club/ecs"
|
|
|
|
"joylink.club/rtsssimulation/component"
|
|
|
|
"joylink.club/rtsssimulation/entity"
|
2024-01-05 11:30:39 +08:00
|
|
|
"joylink.club/rtsssimulation/repository"
|
2023-12-21 16:14:02 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
type PipeFittingSystem struct {
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewPipeFittingSystem() *PipeFittingSystem {
|
|
|
|
return &PipeFittingSystem{}
|
|
|
|
}
|
|
|
|
|
2024-01-05 11:30:39 +08:00
|
|
|
// Update in world
|
2023-12-21 16:14:02 +08:00
|
|
|
func (s *PipeFittingSystem) Update(w ecs.World) {
|
|
|
|
wd := entity.GetWorldData(w)
|
|
|
|
for _, pf := range wd.Repo.PipeFittingMap {
|
2024-01-05 15:21:29 +08:00
|
|
|
if s.isPipeFittingEle(wd, pf) { //管件为电力管件
|
2023-12-21 16:14:02 +08:00
|
|
|
//筛选出相对电源
|
2024-01-05 11:30:39 +08:00
|
|
|
pipePsMap := s.findEleSourcePipe(wd, pf)
|
|
|
|
//管件连接的管线间电能传递
|
|
|
|
s.transEle(wd, pf, pipePsMap)
|
2024-01-05 15:21:29 +08:00
|
|
|
} else { //流体管件
|
|
|
|
|
2024-01-05 11:30:39 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 筛选出相对电源
|
|
|
|
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()]
|
2024-01-05 15:21:29 +08:00
|
|
|
powerPipe := perfectPipeElectricity(pipeEntry)
|
|
|
|
for epId, ep := range powerPipe.Sources {
|
|
|
|
pipePs, ok := pipePsMap[epId]
|
|
|
|
if ok {
|
|
|
|
if ep.Fresh > pipePs.Fresh {
|
2024-01-05 11:30:39 +08:00
|
|
|
pipePsMap[epId] = ep
|
2023-12-21 16:14:02 +08:00
|
|
|
}
|
2024-01-05 15:21:29 +08:00
|
|
|
} else {
|
|
|
|
pipePsMap[epId] = ep
|
2023-12-21 16:14:02 +08:00
|
|
|
}
|
2024-01-05 11:30:39 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
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()]
|
2024-01-05 15:21:29 +08:00
|
|
|
powerPipe := perfectPipeElectricity(pipeEntry)
|
2024-01-05 11:30:39 +08:00
|
|
|
pipePortPs, ok := powerPipe.Sources[pipePsId]
|
|
|
|
if ok {
|
|
|
|
if pipePs.Fresh > pipePortPs.Fresh {
|
|
|
|
*powerPipe.Sources[pipePsId] = *pipePs
|
|
|
|
powerPipe.Sources[pipePsId].Fresh -= 1 //保证相对性
|
2023-12-21 16:14:02 +08:00
|
|
|
}
|
2024-01-05 11:30:39 +08:00
|
|
|
} else {
|
|
|
|
powerPipe.Sources[pipePsId] = &component.ElectricitySource{}
|
|
|
|
*powerPipe.Sources[pipePsId] = *pipePs
|
|
|
|
powerPipe.Sources[pipePsId].Fresh -= 1 //保证相对性
|
2023-12-21 16:14:02 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-01-05 11:30:39 +08:00
|
|
|
|
2024-01-05 15:21:29 +08:00
|
|
|
// 与管件连接的管线只要有一个为电线则其他的都为电线
|
2024-01-05 11:30:39 +08:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2024-01-05 15:21:29 +08:00
|
|
|
// 完善电线实体并获取电线状态
|
|
|
|
func perfectPipeElectricity(pipeEntry *ecs.Entry) *component.PipeElectricity {
|
|
|
|
component.PipeType.Get(pipeEntry).Matter = component.PmtElectricity
|
2024-01-05 11:30:39 +08:00
|
|
|
if !pipeEntry.HasComponent(component.PipeElectricityType) {
|
|
|
|
pipeEntry.AddComponent(component.PipeElectricityType)
|
|
|
|
}
|
|
|
|
return component.PipeElectricityType.Get(pipeEntry)
|
|
|
|
}
|