package iscs_sys import ( "fmt" "joylink.club/ecs" "joylink.club/rtsssimulation/component" "joylink.club/rtsssimulation/entity" "joylink.club/rtsssimulation/repository" "log/slog" ) 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.initPipeFitting(wd, pf) && s.isPipeFittingEle(wd, pf) { //筛选出相对电源 pipePsMap := s.findEleSourcePipe(wd, pf) //管件连接的管线间电能传递 s.transEle(wd, pf, pipePsMap) } } } // 筛选出相对电源 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()] if pipeEntry.HasComponent(component.PipeElectricityType) { powerPipe := component.PipeElectricityType.Get(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 := component.PipeElectricityType.Get(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 (s *PipeFittingSystem) initPipeFitting(wd *component.WorldData, pf *repository.PipeFitting) bool { pipeMatter := component.PmtNon for _, pipePort := range pf.Ports() { pipeEntry := wd.EntityMap[pipePort.Device().Id()] pipe := component.PipeType.Get(pipeEntry) if !pipe.Matter.IsNon() { pipeMatter = pipe.Matter break } } // if pipeMatter.IsNon() { return false } // for _, pipePort := range pf.Ports() { pipeEntry := wd.EntityMap[pipePort.Device().Id()] pipe := component.PipeType.Get(pipeEntry) pipe.Matter = pipeMatter switch pipe.Matter { case component.PmtElectricity: { if !pipeEntry.HasComponent(component.PipeElectricityType) { pipeEntry.AddComponent(component.PipeElectricityType) component.PipeElectricityType.Set(pipeEntry, component.NewPipeElectricity()) } } case component.PmtAir: fallthrough case component.PmtOil: fallthrough case component.PmtSmoke: fallthrough case component.PmtWater: { if !pipeEntry.HasComponent(component.PipeFluidType) { pipeEntry.AddComponent(component.PipeFluidType) component.PipeFluidType.Set(pipeEntry, component.NewPipeFluid()) } } default: slog.Warn(fmt.Sprintf("PipeFittingSystem.initPipeFitting 未处理的管道物质[%d]", pipe.Matter)) } } // return true } // 从电线实体获取电线 func getPipeElectricity(pipeEntry *ecs.Entry) *component.PipeElectricity { if !pipeEntry.HasComponent(component.PipeElectricityType) { pipeEntry.AddComponent(component.PipeElectricityType) } return component.PipeElectricityType.Get(pipeEntry) }