Fix letting multiple mods editing one planet

This commit is contained in:
Nick 2022-05-10 18:44:36 -04:00
parent 9506c3dee5
commit 6f34dc71d5
3 changed files with 86 additions and 67 deletions

View File

@ -4,6 +4,7 @@ using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UnityEngine;
using Logger = NewHorizons.Utility.Logger;
namespace NewHorizons.Components.Orbital
{
@ -60,36 +61,44 @@ namespace NewHorizons.Components.Orbital
public override void Update()
{
AstroObject primary = _astroObject?.GetPrimaryBody();
try
{
AstroObject primary = _astroObject?.GetPrimaryBody();
// If it has nothing to orbit then why is this here
if (primary == null)
{
base.enabled = false;
return;
// If it has nothing to orbit then why is this here
if (primary == null)
{
base.enabled = false;
return;
}
Vector3 origin = primary.transform.position + SemiMajorAxis.normalized * _fociDistance;
float num = CalcProjectedAngleToCenter(origin, SemiMajorAxis, SemiMinorAxis, _astroObject.transform.position);
for (int i = 0; i < _numVerts; i++)
{
var stepSize = 2f * Mathf.PI / (float)(_numVerts - 1);
float f = num + stepSize * i;
_verts[i] = SemiMajorAxis * Mathf.Cos(f) + SemiMinorAxis * Mathf.Sin(f);
}
_lineRenderer.SetPositions(_verts);
transform.position = origin;
transform.rotation = Quaternion.Euler(0, 0, 0); //Quaternion.LookRotation(-SemiMajorAxis, _upAxis);
float num2 = DistanceToEllipticalOrbitLine(origin, SemiMajorAxis, SemiMinorAxis, _upAxis, Locator.GetActiveCamera().transform.position);
float widthMultiplier = Mathf.Min(num2 * (_lineWidth / 1000f), _maxLineWidth);
float num3 = _fade ? (1f - Mathf.Clamp01((num2 - _fadeStartDist) / (_fadeEndDist - _fadeStartDist))) : 1f;
_lineRenderer.widthMultiplier = widthMultiplier;
_lineRenderer.startColor = new Color(_color.r, _color.g, _color.b, num3 * num3);
}
Vector3 origin = primary.transform.position + SemiMajorAxis.normalized * _fociDistance;
float num = CalcProjectedAngleToCenter(origin, SemiMajorAxis, SemiMinorAxis, _astroObject.transform.position);
for (int i = 0; i < _numVerts; i++)
{
var stepSize = 2f * Mathf.PI / (float)(_numVerts - 1);
float f = num + stepSize * i;
_verts[i] = SemiMajorAxis * Mathf.Cos(f) + SemiMinorAxis * Mathf.Sin(f);
}
_lineRenderer.SetPositions(_verts);
transform.position = origin;
transform.rotation = Quaternion.Euler(0, 0, 0); //Quaternion.LookRotation(-SemiMajorAxis, _upAxis);
float num2 = DistanceToEllipticalOrbitLine(origin, SemiMajorAxis, SemiMinorAxis, _upAxis, Locator.GetActiveCamera().transform.position);
float widthMultiplier = Mathf.Min(num2 * (_lineWidth / 1000f), _maxLineWidth);
float num3 = _fade ? (1f - Mathf.Clamp01((num2 - _fadeStartDist) / (_fadeEndDist - _fadeStartDist))) : 1f;
_lineRenderer.widthMultiplier = widthMultiplier;
_lineRenderer.startColor = new Color(_color.r, _color.g, _color.b, num3 * num3);
catch(Exception ex)
{
Logger.LogError($"Exception in OrbitLine for [{_astroObject?.name}] : {ex.Message}, {ex.StackTrace}");
enabled = false;
}
}
private float CalcProjectedAngleToCenter(Vector3 foci, Vector3 semiMajorAxis, Vector3 semiMinorAxis, Vector3 point)

View File

@ -4,6 +4,7 @@ using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UnityEngine;
using Logger = NewHorizons.Utility.Logger;
namespace NewHorizons.Components.Orbital
{
@ -58,32 +59,40 @@ namespace NewHorizons.Components.Orbital
public override void Update()
{
var primary = _astroObject.GetPrimaryBody();
Vector3 origin = primary == null ? Locator.GetRootTransform().position : primary.transform.position;
_timer += Time.deltaTime;
var updateTime = (TrailTime / (float)_numVerts);
if (_timer > updateTime)
try
{
for (int i = _numVerts - 1; i > 0; i--)
var primary = _astroObject.GetPrimaryBody();
Vector3 origin = primary == null ? Locator.GetRootTransform().position : primary.transform.position;
_timer += Time.deltaTime;
var updateTime = (TrailTime / (float)_numVerts);
if (_timer > updateTime)
{
var v = _vertices[i - 1];
_vertices[i] = new Vector3(v.x, v.y, v.z);
for (int i = _numVerts - 1; i > 0; i--)
{
var v = _vertices[i - 1];
_vertices[i] = new Vector3(v.x, v.y, v.z);
}
_timer = 0;
}
_timer = 0;
_vertices[0] = transform.parent.position - origin;
_lineRenderer.SetPositions(_vertices);
base.transform.position = origin;
base.transform.rotation = Quaternion.AngleAxis(0f, Vector3.up);
float num2 = DistanceToTrackingOrbitLine(Locator.GetActiveCamera().transform.position);
float widthMultiplier = Mathf.Min(num2 * (_lineWidth / 1000f), _maxLineWidth);
float num3 = _fade ? (1f - Mathf.Clamp01((num2 - _fadeStartDist) / (_fadeEndDist - _fadeStartDist))) : 1f;
_lineRenderer.widthMultiplier = widthMultiplier;
_lineRenderer.startColor = new Color(_color.r, _color.g, _color.b, num3 * num3);
}
catch (Exception ex)
{
Logger.LogError($"Exception in OrbitLine for [{_astroObject?.name}] : {ex.Message}, {ex.StackTrace}");
enabled = false;
}
_vertices[0] = transform.parent.position - origin;
_lineRenderer.SetPositions(_vertices);
base.transform.position = origin;
base.transform.rotation = Quaternion.AngleAxis(0f, Vector3.up);
float num2 = DistanceToTrackingOrbitLine(Locator.GetActiveCamera().transform.position);
float widthMultiplier = Mathf.Min(num2 * (_lineWidth / 1000f), _maxLineWidth);
float num3 = _fade ? (1f - Mathf.Clamp01((num2 - _fadeStartDist) / (_fadeEndDist - _fadeStartDist))) : 1f;
_lineRenderer.widthMultiplier = widthMultiplier;
_lineRenderer.startColor = new Color(_color.r, _color.g, _color.b, num3 * num3);
}
private float DistanceToTrackingOrbitLine(Vector3 point)

View File

@ -83,14 +83,25 @@ namespace NewHorizons.Handlers
foreach (var node in planetGraph)
{
LoadBody(node.body);
toLoad.Remove(node.body);
if (node is PlanetGraphHandler.FocalPointNode focal)
{
LoadBody(focal.primary.body);
LoadBody(focal.secondary.body);
toLoad.Remove(focal.primary.body);
toLoad.Remove(focal.secondary.body);
}
}
}
// Are there more?
foreach(var body in toLoad)
{
LoadBody(body);
}
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
@ -169,27 +180,17 @@ namespace NewHorizons.Handlers
var sector = go.GetComponentInChildren<Sector>();
var rb = go.GetAttachedOWRigidbody();
// Did we already generate the rest of the body
var justUpdateOrbit = go.GetComponent<NHAstroObject>() != null && ExistingAOConfigs.ContainsKey(go.GetComponent<NHAstroObject>());
// Since orbits are always there just check if they set a semi major axis
if (body.Config.Orbit != null && body.Config.Orbit.SemiMajorAxis != 0f)
{
// If we aren't able to update the orbit wait until later
if (!UpdateBodyOrbit(body, go))
{
NextPassBodies.Add(body);
return null;
}
UpdateBodyOrbit(body, go);
}
if (justUpdateOrbit) return go;
if (body.Config.ChildrenToDestroy != null && body.Config.ChildrenToDestroy.Length > 0)
{
foreach (var child in body.Config.ChildrenToDestroy)
{
Main.Instance.ModHelper.Events.Unity.FireInNUpdates(() => GameObject.Find(go.name + "/" + child).SetActive(false), 2);
Main.Instance.ModHelper.Events.Unity.FireInNUpdates(() => GameObject.Find(go.name + "/" + child)?.SetActive(false), 2);
}
}
@ -401,7 +402,7 @@ namespace NewHorizons.Handlers
return go;
}
public static bool UpdateBodyOrbit(NewHorizonsBody body, GameObject go)
public static void UpdateBodyOrbit(NewHorizonsBody body, GameObject go)
{
Logger.Log($"Updating orbit of [{body.Config.Name}]");
@ -421,7 +422,7 @@ namespace NewHorizons.Handlers
{
// If we can't find the new one we want to try again later (return false)
primary = AstroObjectLocator.GetAstroObject(body.Config.Orbit.PrimaryBody);
if (primary == null) return false;
if (primary == null) return;
}
// Just destroy the existing AO after copying everything over
@ -505,10 +506,10 @@ namespace NewHorizons.Handlers
catch (Exception ex)
{
Logger.LogError($"Couldn't update orbit of [{body.Config.Name}]: {ex.Message}, {ex.StackTrace}");
// If it doesn't here there's no point trying again so we'll still return true
// If it doesn't work here there's no point trying again so we'll still return true
}
return true;
return;
}
private static void UpdatePosition(GameObject go, NewHorizonsBody body, AstroObject primaryBody, AstroObject secondaryBody)