From db0997ecbb946a5eb6c28b2ae47f24f2da64fe3e Mon Sep 17 00:00:00 2001 From: Nick Date: Thu, 21 Jul 2022 19:50:55 -0400 Subject: [PATCH] Small PlanetCreationHandler refactor --- .../Builder/Props/BrambleNodeBuilder.cs | 2 +- NewHorizons/Handlers/PlanetCreationHandler.cs | 63 ++++++++----------- 2 files changed, 26 insertions(+), 39 deletions(-) diff --git a/NewHorizons/Builder/Props/BrambleNodeBuilder.cs b/NewHorizons/Builder/Props/BrambleNodeBuilder.cs index 15e4f668..88547852 100644 --- a/NewHorizons/Builder/Props/BrambleNodeBuilder.cs +++ b/NewHorizons/Builder/Props/BrambleNodeBuilder.cs @@ -90,7 +90,7 @@ namespace NewHorizons.Builder.Props // 1) Run Floyd-Warshall on the dimensions (where each dimension is a vertex and each node is an edge) // 2) For each dimension A, if it's possible to reach dimension B, add dimension B's signals to the list propagatedSignals[A] - var allDimensions = PlanetCreationHandler.allBodies.Where(body => body?.Config?.Bramble?.dimension != null).Select(body => body.Config).ToList(); + var allDimensions = PlanetCreationHandler.Bodies.Where(body => body?.Config?.Bramble?.dimension != null).Select(body => body.Config).ToList(); // // Floyd Warshall diff --git a/NewHorizons/Handlers/PlanetCreationHandler.cs b/NewHorizons/Handlers/PlanetCreationHandler.cs index 2bfb3532..3d4e58e9 100644 --- a/NewHorizons/Handlers/PlanetCreationHandler.cs +++ b/NewHorizons/Handlers/PlanetCreationHandler.cs @@ -17,36 +17,24 @@ namespace NewHorizons.Handlers { public static class PlanetCreationHandler { - public static List NextPassBodies = new List(); + public static List Bodies { get; private set; } - // I literally forget what this is for - private static Dictionary ExistingAOConfigs; + private static List _nextPassBodies = new List(); - private static Dictionary _dict; - private static Dictionary _dimensions; + // Stock bodies being updated + private static Dictionary _existingBodyDict; - public static List allBodies; - - public static NewHorizonsBody GetNewHorizonsBody(AstroObject ao) - { - if (ao is NHAstroObject nhAO) - { - if (_dict.ContainsKey(nhAO)) return _dict[nhAO]; - } - - if (!_dimensions.ContainsKey(ao)) return null; - - return _dimensions[ao]; - } + // Custom bodies being created + private static Dictionary _customBodyDict; public static void Init(List bodies) { Main.FurthestOrbit = 30000; - ExistingAOConfigs = new Dictionary(); - _dict = new Dictionary(); - _dimensions = new Dictionary(); - allBodies = bodies; + _existingBodyDict = new(); + _customBodyDict = new(); + + Bodies = bodies; // Set up stars // Need to manage this when there are multiple stars @@ -125,15 +113,15 @@ namespace NewHorizons.Handlers Logger.Log("Loading Deferred Bodies"); // Make a copy of the next pass of bodies so that the array can be edited while we load them - toLoad = NextPassBodies.Select(x => x).ToList(); - while (NextPassBodies.Count != 0) + toLoad = _nextPassBodies.Select(x => x).ToList(); + while (_nextPassBodies.Count != 0) { foreach (var body in toLoad) { LoadBody(body, true); } - toLoad = NextPassBodies; - NextPassBodies = new List(); + toLoad = _nextPassBodies; + _nextPassBodies = new List(); } Logger.Log("Done loading bodies"); @@ -179,7 +167,7 @@ namespace NewHorizons.Handlers var ao = quantumPlanet.GetComponent(); var rootSector = quantumPlanet.GetComponentInChildren(); - var groundOrbit = _dict[ao].Config.Orbit; + var groundOrbit = _customBodyDict[ao].Config.Orbit; quantumPlanet.groundState = new QuantumPlanet.State(rootSector, groundOrbit); quantumPlanet.states.Add(quantumPlanet.groundState); @@ -188,7 +176,7 @@ namespace NewHorizons.Handlers visibilityTracker.transform.parent = existingPlanet.transform; visibilityTracker.transform.localPosition = Vector3.zero; var sphere = visibilityTracker.AddComponent(); - sphere.radius = GetSphereOfInfluence(_dict[ao]); + sphere.radius = GetSphereOfInfluence(_customBodyDict[ao]); var tracker = visibilityTracker.AddComponent(); quantumPlanet._visibilityTrackers = new VisibilityTracker[] { tracker }; } @@ -230,7 +218,7 @@ namespace NewHorizons.Handlers if (body.Config.isQuantumState) { // If the ground state object isn't made yet do it later - NextPassBodies.Add(body); + _nextPassBodies.Add(body); } else { @@ -246,8 +234,7 @@ namespace NewHorizons.Handlers var solarSystemRoot = SearchUtilities.Find("SolarSystemRoot").transform; planetObject.GetComponent()._origParent = ao.IsDimension ? solarSystemRoot.Find("Dimensions") : solarSystemRoot; - if (!ao.IsDimension) _dict.Add(ao, body); - else _dimensions.Add(ao, body); + _customBodyDict.Add(ao, body); } catch (Exception e) { @@ -332,8 +319,8 @@ namespace NewHorizons.Handlers return go; } - public static GameObject GenerateStandardBody(NewHorizonsBody body, bool defaultPrimaryToSun = false) - { + public static GameObject GenerateStandardBody(NewHorizonsBody body, bool defaultPrimaryToSun = false) + { // Focal points are weird if (body.Config.FocalPoint != null) FocalPointBuilder.ValidateConfig(body.Config); @@ -350,7 +337,7 @@ namespace NewHorizons.Handlers } else { - NextPassBodies.Add(body); + _nextPassBodies.Add(body); return null; } } @@ -488,7 +475,7 @@ namespace NewHorizons.Handlers { BrambleNodeBuilder.Make(go, sector, body.Config.Bramble.nodes, body.Mod); } - + if (body.Config.Bramble.dimension != null) { BrambleNodeBuilder.FinishPairingNodesForDimension(body.Config.name, go.GetComponent()); @@ -644,10 +631,10 @@ namespace NewHorizons.Handlers var childAO = child.GetComponent() ?? child.GetComponent(); if (childAO != null) { - if (childAO is NHAstroObject && ExistingAOConfigs.ContainsKey(childAO)) + if (childAO is NHAstroObject childNHAO && _existingBodyDict.ContainsKey(childNHAO)) { // If it's already an NH object we repeat the whole process else it doesn't work idk - NextPassBodies.Add(ExistingAOConfigs[childAO]); + _nextPassBodies.Add(_existingBodyDict[childNHAO]); } else { @@ -671,7 +658,7 @@ namespace NewHorizons.Handlers // Have to register this new AO to the locator Locator.RegisterAstroObject(newAO); - ExistingAOConfigs.Add(newAO, body); + _existingBodyDict.Add(newAO, body); } catch (Exception ex) {