More fixes (#954)

## Bug fixes

- Fixed configs getting loaded more than once if parent body is updated
again #921
This commit is contained in:
xen-42 2024-10-05 20:36:58 -04:00 committed by GitHub
commit f70706c6eb
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 38 additions and 13 deletions

View File

@ -10,16 +10,25 @@ namespace NewHorizons.Builder.Orbital
private static Material _dottedLineMaterial; private static Material _dottedLineMaterial;
private static Material _lineMaterial; private static Material _lineMaterial;
public static OrbitLine Make(GameObject planetGO, NHAstroObject astroObject, bool isMoon, PlanetConfig config) public static GameObject Make(GameObject planetGO, bool isMoon, PlanetConfig config)
{
var orbitGO = new GameObject("Orbit");
orbitGO.transform.parent = planetGO.transform;
orbitGO.transform.localPosition = Vector3.zero;
Delay.FireOnNextUpdate(() => PostMake(orbitGO, planetGO, isMoon, config));
return orbitGO;
}
private static void PostMake(GameObject orbitGO, GameObject planetGO, bool isMoon, PlanetConfig config)
{ {
if (_dottedLineMaterial == null) _dottedLineMaterial = SearchUtilities.FindResourceOfTypeAndName<Material>("Effects_SPA_OrbitLine_Dotted_mat"); if (_dottedLineMaterial == null) _dottedLineMaterial = SearchUtilities.FindResourceOfTypeAndName<Material>("Effects_SPA_OrbitLine_Dotted_mat");
if (_lineMaterial == null) _lineMaterial = SearchUtilities.FindResourceOfTypeAndName<Material>("Effects_SPA_OrbitLine_mat"); if (_lineMaterial == null) _lineMaterial = SearchUtilities.FindResourceOfTypeAndName<Material>("Effects_SPA_OrbitLine_mat");
if (_dottedLineMaterial == null || _lineMaterial == null) return null; // Might've been otherwise destroyed when updating
if (orbitGO == null) return;
var orbitGO = new GameObject("Orbit"); var astroObject = planetGO.GetComponent<NHAstroObject>();
orbitGO.transform.parent = planetGO.transform;
orbitGO.transform.localPosition = Vector3.zero;
var lineRenderer = orbitGO.AddComponent<LineRenderer>(); var lineRenderer = orbitGO.AddComponent<LineRenderer>();
@ -47,7 +56,6 @@ namespace NewHorizons.Builder.Orbital
else else
{ {
orbitLine = orbitGO.AddComponent<NHOrbitLine>(); orbitLine = orbitGO.AddComponent<NHOrbitLine>();
(orbitLine as NHOrbitLine).SetFromParameters(astroObject); (orbitLine as NHOrbitLine).SetFromParameters(astroObject);
} }
@ -94,8 +102,6 @@ namespace NewHorizons.Builder.Orbital
orbitGO.SetActive(false); orbitGO.SetActive(false);
}; };
} }
return orbitLine;
} }
} }
} }

View File

@ -87,6 +87,7 @@ namespace NewHorizons.Handlers
} }
// Load all planets // Load all planets
_loadedBodies.Clear();
var toLoad = bodies.ToList(); var toLoad = bodies.ToList();
var newPlanetGraph = new PlanetGraphHandler(toLoad); var newPlanetGraph = new PlanetGraphHandler(toLoad);
@ -151,8 +152,18 @@ namespace NewHorizons.Handlers
SingularityBuilder.PairAllSingularities(); SingularityBuilder.PairAllSingularities();
} }
private static List<NewHorizonsBody> _loadedBodies = new();
/// <summary>
/// Returns false if it failed
/// </summary>
/// <param name="body"></param>
/// <param name="defaultPrimaryToSun"></param>
/// <returns></returns>
public static bool LoadBody(NewHorizonsBody body, bool defaultPrimaryToSun = false) public static bool LoadBody(NewHorizonsBody body, bool defaultPrimaryToSun = false)
{ {
if (_loadedBodies.Contains(body)) return true;
body.LoadCache(); body.LoadCache();
// I don't remember doing this why is it exceptions what am I doing // I don't remember doing this why is it exceptions what am I doing
@ -306,6 +317,7 @@ namespace NewHorizons.Handlers
} }
body.UnloadCache(true); body.UnloadCache(true);
_loadedBodies.Add(body);
return true; return true;
} }
@ -506,7 +518,7 @@ namespace NewHorizons.Handlers
if (body.Config.Orbit.showOrbitLine && !body.Config.Orbit.isStatic) if (body.Config.Orbit.showOrbitLine && !body.Config.Orbit.isStatic)
{ {
Delay.FireOnNextUpdate(() => OrbitlineBuilder.Make(body.Object, ao, body.Config.Orbit.isMoon, body.Config)); OrbitlineBuilder.Make(body.Object, body.Config.Orbit.isMoon, body.Config);
} }
DetectorBuilder.Make(go, owRigidBody, primaryBody, ao, body.Config); DetectorBuilder.Make(go, owRigidBody, primaryBody, ao, body.Config);
@ -673,7 +685,7 @@ namespace NewHorizons.Handlers
SunOverrideBuilder.Make(go, sector, body.Config.Atmosphere, body.Config.Water, surfaceSize); SunOverrideBuilder.Make(go, sector, body.Config.Atmosphere, body.Config.Water, surfaceSize);
} }
} }
if (body.Config.Atmosphere.fogSize != 0) if (body.Config.Atmosphere.fogSize != 0)
{ {
fog = FogBuilder.Make(go, sector, body.Config.Atmosphere, body.Mod); fog = FogBuilder.Make(go, sector, body.Config.Atmosphere, body.Mod);
@ -813,11 +825,18 @@ namespace NewHorizons.Handlers
if (referenceFrame != null) referenceFrame._attachedAstroObject = newAO; if (referenceFrame != null) referenceFrame._attachedAstroObject = newAO;
// QM and stuff don't have orbit lines // QM and stuff don't have orbit lines
var orbitLine = go.GetComponentInChildren<OrbitLine>()?.gameObject; // Using the name as well since NH only creates the OrbitLine components next frame
if (orbitLine != null) UnityEngine.Object.Destroy(orbitLine); var orbitLine = go.GetComponentInChildren<OrbitLine>()?.gameObject ?? go.transform.Find("Orbit")?.gameObject;
if (orbitLine != null)
{
UnityEngine.Object.Destroy(orbitLine);
}
var isMoon = newAO.GetAstroObjectType() is AstroObject.Type.Moon or AstroObject.Type.Satellite or AstroObject.Type.SpaceStation; var isMoon = newAO.GetAstroObjectType() is AstroObject.Type.Moon or AstroObject.Type.Satellite or AstroObject.Type.SpaceStation;
if (body.Config.Orbit.showOrbitLine) OrbitlineBuilder.Make(go, newAO, isMoon, body.Config); if (body.Config.Orbit.showOrbitLine)
{
OrbitlineBuilder.Make(go, isMoon, body.Config);
}
DetectorBuilder.SetDetector(primary, newAO, go.GetComponentInChildren<ConstantForceDetector>()); DetectorBuilder.SetDetector(primary, newAO, go.GetComponentInChildren<ConstantForceDetector>());