Small PlanetCreationHandler refactor

This commit is contained in:
Nick 2022-07-21 19:50:55 -04:00
parent cc9adcf732
commit db0997ecbb
2 changed files with 26 additions and 39 deletions

View File

@ -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) // 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] // 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 // Floyd Warshall

View File

@ -17,36 +17,24 @@ namespace NewHorizons.Handlers
{ {
public static class PlanetCreationHandler public static class PlanetCreationHandler
{ {
public static List<NewHorizonsBody> NextPassBodies = new List<NewHorizonsBody>(); public static List<NewHorizonsBody> Bodies { get; private set; }
// I literally forget what this is for private static List<NewHorizonsBody> _nextPassBodies = new List<NewHorizonsBody>();
private static Dictionary<AstroObject, NewHorizonsBody> ExistingAOConfigs;
private static Dictionary<NHAstroObject, NewHorizonsBody> _dict; // Stock bodies being updated
private static Dictionary<AstroObject, NewHorizonsBody> _dimensions; private static Dictionary<NHAstroObject, NewHorizonsBody> _existingBodyDict;
public static List<NewHorizonsBody> allBodies; // Custom bodies being created
private static Dictionary<NHAstroObject, NewHorizonsBody> _customBodyDict;
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];
}
public static void Init(List<NewHorizonsBody> bodies) public static void Init(List<NewHorizonsBody> bodies)
{ {
Main.FurthestOrbit = 30000; Main.FurthestOrbit = 30000;
ExistingAOConfigs = new Dictionary<AstroObject, NewHorizonsBody>(); _existingBodyDict = new();
_dict = new Dictionary<NHAstroObject, NewHorizonsBody>(); _customBodyDict = new();
_dimensions = new Dictionary<AstroObject, NewHorizonsBody>();
allBodies = bodies; Bodies = bodies;
// Set up stars // Set up stars
// Need to manage this when there are multiple stars // Need to manage this when there are multiple stars
@ -125,15 +113,15 @@ namespace NewHorizons.Handlers
Logger.Log("Loading Deferred Bodies"); 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 // 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(); toLoad = _nextPassBodies.Select(x => x).ToList();
while (NextPassBodies.Count != 0) while (_nextPassBodies.Count != 0)
{ {
foreach (var body in toLoad) foreach (var body in toLoad)
{ {
LoadBody(body, true); LoadBody(body, true);
} }
toLoad = NextPassBodies; toLoad = _nextPassBodies;
NextPassBodies = new List<NewHorizonsBody>(); _nextPassBodies = new List<NewHorizonsBody>();
} }
Logger.Log("Done loading bodies"); Logger.Log("Done loading bodies");
@ -179,7 +167,7 @@ namespace NewHorizons.Handlers
var ao = quantumPlanet.GetComponent<NHAstroObject>(); var ao = quantumPlanet.GetComponent<NHAstroObject>();
var rootSector = quantumPlanet.GetComponentInChildren<Sector>(); var rootSector = quantumPlanet.GetComponentInChildren<Sector>();
var groundOrbit = _dict[ao].Config.Orbit; var groundOrbit = _customBodyDict[ao].Config.Orbit;
quantumPlanet.groundState = new QuantumPlanet.State(rootSector, groundOrbit); quantumPlanet.groundState = new QuantumPlanet.State(rootSector, groundOrbit);
quantumPlanet.states.Add(quantumPlanet.groundState); quantumPlanet.states.Add(quantumPlanet.groundState);
@ -188,7 +176,7 @@ namespace NewHorizons.Handlers
visibilityTracker.transform.parent = existingPlanet.transform; visibilityTracker.transform.parent = existingPlanet.transform;
visibilityTracker.transform.localPosition = Vector3.zero; visibilityTracker.transform.localPosition = Vector3.zero;
var sphere = visibilityTracker.AddComponent<SphereShape>(); var sphere = visibilityTracker.AddComponent<SphereShape>();
sphere.radius = GetSphereOfInfluence(_dict[ao]); sphere.radius = GetSphereOfInfluence(_customBodyDict[ao]);
var tracker = visibilityTracker.AddComponent<ShapeVisibilityTracker>(); var tracker = visibilityTracker.AddComponent<ShapeVisibilityTracker>();
quantumPlanet._visibilityTrackers = new VisibilityTracker[] { tracker }; quantumPlanet._visibilityTrackers = new VisibilityTracker[] { tracker };
} }
@ -230,7 +218,7 @@ namespace NewHorizons.Handlers
if (body.Config.isQuantumState) if (body.Config.isQuantumState)
{ {
// If the ground state object isn't made yet do it later // If the ground state object isn't made yet do it later
NextPassBodies.Add(body); _nextPassBodies.Add(body);
} }
else else
{ {
@ -246,8 +234,7 @@ namespace NewHorizons.Handlers
var solarSystemRoot = SearchUtilities.Find("SolarSystemRoot").transform; var solarSystemRoot = SearchUtilities.Find("SolarSystemRoot").transform;
planetObject.GetComponent<OWRigidbody>()._origParent = ao.IsDimension ? solarSystemRoot.Find("Dimensions") : solarSystemRoot; planetObject.GetComponent<OWRigidbody>()._origParent = ao.IsDimension ? solarSystemRoot.Find("Dimensions") : solarSystemRoot;
if (!ao.IsDimension) _dict.Add(ao, body); _customBodyDict.Add(ao, body);
else _dimensions.Add(ao, body);
} }
catch (Exception e) catch (Exception e)
{ {
@ -332,8 +319,8 @@ namespace NewHorizons.Handlers
return go; return go;
} }
public static GameObject GenerateStandardBody(NewHorizonsBody body, bool defaultPrimaryToSun = false) public static GameObject GenerateStandardBody(NewHorizonsBody body, bool defaultPrimaryToSun = false)
{ {
// Focal points are weird // Focal points are weird
if (body.Config.FocalPoint != null) FocalPointBuilder.ValidateConfig(body.Config); if (body.Config.FocalPoint != null) FocalPointBuilder.ValidateConfig(body.Config);
@ -350,7 +337,7 @@ namespace NewHorizons.Handlers
} }
else else
{ {
NextPassBodies.Add(body); _nextPassBodies.Add(body);
return null; return null;
} }
} }
@ -488,7 +475,7 @@ namespace NewHorizons.Handlers
{ {
BrambleNodeBuilder.Make(go, sector, body.Config.Bramble.nodes, body.Mod); BrambleNodeBuilder.Make(go, sector, body.Config.Bramble.nodes, body.Mod);
} }
if (body.Config.Bramble.dimension != null) if (body.Config.Bramble.dimension != null)
{ {
BrambleNodeBuilder.FinishPairingNodesForDimension(body.Config.name, go.GetComponent<AstroObject>()); BrambleNodeBuilder.FinishPairingNodesForDimension(body.Config.name, go.GetComponent<AstroObject>());
@ -644,10 +631,10 @@ namespace NewHorizons.Handlers
var childAO = child.GetComponent<NHAstroObject>() ?? child.GetComponent<AstroObject>(); var childAO = child.GetComponent<NHAstroObject>() ?? child.GetComponent<AstroObject>();
if (childAO != null) 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 // 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 else
{ {
@ -671,7 +658,7 @@ namespace NewHorizons.Handlers
// Have to register this new AO to the locator // Have to register this new AO to the locator
Locator.RegisterAstroObject(newAO); Locator.RegisterAstroObject(newAO);
ExistingAOConfigs.Add(newAO, body); _existingBodyDict.Add(newAO, body);
} }
catch (Exception ex) catch (Exception ex)
{ {