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 _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 (_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");
orbitGO.transform.parent = planetGO.transform;
orbitGO.transform.localPosition = Vector3.zero;
var astroObject = planetGO.GetComponent<NHAstroObject>();
var lineRenderer = orbitGO.AddComponent<LineRenderer>();
@ -47,7 +56,6 @@ namespace NewHorizons.Builder.Orbital
else
{
orbitLine = orbitGO.AddComponent<NHOrbitLine>();
(orbitLine as NHOrbitLine).SetFromParameters(astroObject);
}
@ -94,8 +102,6 @@ namespace NewHorizons.Builder.Orbital
orbitGO.SetActive(false);
};
}
return orbitLine;
}
}
}

View File

@ -87,6 +87,7 @@ namespace NewHorizons.Handlers
}
// Load all planets
_loadedBodies.Clear();
var toLoad = bodies.ToList();
var newPlanetGraph = new PlanetGraphHandler(toLoad);
@ -151,8 +152,18 @@ namespace NewHorizons.Handlers
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)
{
if (_loadedBodies.Contains(body)) return true;
body.LoadCache();
// I don't remember doing this why is it exceptions what am I doing
@ -306,6 +317,7 @@ namespace NewHorizons.Handlers
}
body.UnloadCache(true);
_loadedBodies.Add(body);
return true;
}
@ -506,7 +518,7 @@ namespace NewHorizons.Handlers
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);
@ -813,11 +825,18 @@ namespace NewHorizons.Handlers
if (referenceFrame != null) referenceFrame._attachedAstroObject = newAO;
// QM and stuff don't have orbit lines
var orbitLine = go.GetComponentInChildren<OrbitLine>()?.gameObject;
if (orbitLine != null) UnityEngine.Object.Destroy(orbitLine);
// Using the name as well since NH only creates the OrbitLine components next frame
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;
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>());