Tidied up AstroObjectLocator

Orbit updating breaks if you make an existing planet a moon of something and then you move that planet.
This commit is contained in:
Nick 2022-05-07 04:32:47 -04:00
parent 59109671c9
commit a41bd8c352
5 changed files with 207 additions and 214 deletions

View File

@ -68,7 +68,7 @@ namespace NewHorizons.Builder.General
if (ao.GetAstroObjectName() == AstroObject.Name.BrittleHollow) if (ao.GetAstroObjectName() == AstroObject.Name.BrittleHollow)
{ {
RemoveBody(AstroObjectLocator.GetAstroObject(AstroObject.Name.WhiteHole), delete, toDestroy); RemoveBody(AstroObjectLocator.GetAstroObject(AstroObject.Name.WhiteHole.ToString()), delete, toDestroy);
} }
// Check if any other objects depend on it and remove them too // Check if any other objects depend on it and remove them too
@ -77,12 +77,12 @@ namespace NewHorizons.Builder.General
{ {
if (obj?.gameObject == null || !obj.gameObject.activeInHierarchy) if (obj?.gameObject == null || !obj.gameObject.activeInHierarchy)
{ {
AstroObjectLocator.RemoveAstroObject(obj); AstroObjectLocator.DeregisterCustomAstroObject(obj);
continue; continue;
} }
if (ao.Equals(obj.GetPrimaryBody())) if (ao.Equals(obj.GetPrimaryBody()))
{ {
AstroObjectLocator.RemoveAstroObject(obj); AstroObjectLocator.DeregisterCustomAstroObject(obj);
RemoveBody(obj, delete, toDestroy); RemoveBody(obj, delete, toDestroy);
} }
} }
@ -149,6 +149,7 @@ namespace NewHorizons.Builder.General
// Just delete the children // Just delete the children
foreach (var child in AstroObjectLocator.GetChildren(ao)) foreach (var child in AstroObjectLocator.GetChildren(ao))
{ {
if (child.name == "Ship_Body") continue;
DisableBody(child, true); DisableBody(child, true);
} }
} }

View File

@ -19,10 +19,10 @@ namespace NewHorizons.Builder.Orbital
{ {
// Doing it like this so the planet orbit updater can just use an existing initial motion with the other method // Doing it like this so the planet orbit updater can just use an existing initial motion with the other method
InitialMotion initialMotion = body.AddComponent<InitialMotion>(); InitialMotion initialMotion = body.AddComponent<InitialMotion>();
return SetInitialMotion(initialMotion, primaryBody, secondaryBody, OWRB, orbit); return SetInitialMotionFromConfig(initialMotion, primaryBody, secondaryBody, orbit);
} }
public static InitialMotion SetInitialMotion(InitialMotion initialMotion, AstroObject primaryBody, AstroObject secondaryBody, OWRigidbody OWRB, OrbitModule orbit) public static InitialMotion SetInitialMotionFromConfig(InitialMotion initialMotion, AstroObject primaryBody, AstroObject secondaryBody, OrbitModule orbit)
{ {
// This bit makes the initial motion not try to calculate the orbit velocity itself for reasons // This bit makes the initial motion not try to calculate the orbit velocity itself for reasons
initialMotion._orbitImpulseScalar = 0f; initialMotion._orbitImpulseScalar = 0f;
@ -33,6 +33,19 @@ namespace NewHorizons.Builder.Orbital
secondaryBody.transform.rotation = Quaternion.FromToRotation(Vector3.up, rotationAxis); secondaryBody.transform.rotation = Quaternion.FromToRotation(Vector3.up, rotationAxis);
if (!orbit.IsStatic && primaryBody != null) if (!orbit.IsStatic && primaryBody != null)
{
SetInitialMotion(initialMotion, primaryBody, secondaryBody);
}
else
{
initialMotion._initLinearDirection = Vector3.forward;
initialMotion._initLinearSpeed = 0f;
}
return initialMotion;
}
public static void SetInitialMotion(InitialMotion initialMotion, AstroObject primaryBody, AstroObject secondaryBody)
{ {
var focalPoint = primaryBody.GetComponent<BinaryFocalPoint>(); var focalPoint = primaryBody.GetComponent<BinaryFocalPoint>();
if (focalPoint) if (focalPoint)
@ -72,14 +85,6 @@ namespace NewHorizons.Builder.Orbital
Logger.Log($"No primary gravity or focal point for {primaryBody}"); Logger.Log($"No primary gravity or focal point for {primaryBody}");
} }
} }
else
{
initialMotion._initLinearDirection = Vector3.forward;
initialMotion._initLinearSpeed = 0f;
}
return initialMotion;
}
private static void SetMotionFromPrimary(AstroObject primaryBody, NHAstroObject secondaryBody, InitialMotion initialMotion) private static void SetMotionFromPrimary(AstroObject primaryBody, NHAstroObject secondaryBody, InitialMotion initialMotion)
{ {

View File

@ -418,14 +418,18 @@ namespace NewHorizons.Handlers
newAO._moon = ao._moon; newAO._moon = ao._moon;
newAO._name = ao._name; newAO._name = ao._name;
newAO._owRigidbody = ao._owRigidbody; newAO._owRigidbody = ao._owRigidbody;
newAO._primaryBody = primary;
newAO._rootSector = ao._rootSector; newAO._rootSector = ao._rootSector;
newAO._sandLevelController = ao._sandLevelController; newAO._sandLevelController = ao._sandLevelController;
newAO._satellite = ao._satellite; newAO._satellite = ao._satellite;
newAO._type = ao._type; newAO._type = ao._type;
// We need these for later // We need these for later
var moons = AstroObjectLocator.GetMoons(ao); var children = AstroObjectLocator.GetChildren(ao).Concat(AstroObjectLocator.GetMoons(ao)).ToArray();
AstroObjectLocator.DeregisterCustomAstroObject(ao);
GameObject.Destroy(ao); GameObject.Destroy(ao);
AstroObjectLocator.RegisterCustomAstroObject(newAO);
newAO._primaryBody = primary;
var orbitLine = go.GetComponentInChildren<OrbitLine>(); var orbitLine = go.GetComponentInChildren<OrbitLine>();
var isMoon = newAO.GetAstroObjectType() == AstroObject.Type.Moon || newAO.GetAstroObjectType() == AstroObject.Type.Satellite; var isMoon = newAO.GetAstroObjectType() == AstroObject.Type.Moon || newAO.GetAstroObjectType() == AstroObject.Type.Satellite;
@ -434,18 +438,40 @@ namespace NewHorizons.Handlers
DetectorBuilder.SetDetector(primary, newAO, go.GetComponentInChildren<ConstantForceDetector>()); DetectorBuilder.SetDetector(primary, newAO, go.GetComponentInChildren<ConstantForceDetector>());
// Get ready to move all the satellites // Get ready to move all the satellites
var relativeMoonPositions = moons.Select(x => x.transform.position - go.transform.position).ToArray(); var relativeMoonPositions = children.Select(x => x.transform.position - go.transform.position).ToArray();
// If its tidally locked change the alignment
var alignment = go.GetComponent<AlignWithTargetBody>();
if (alignment != null)
{
alignment.SetTargetBody(primary.GetComponent<OWRigidbody>());
}
// Move the primary // Move the primary
UpdatePosition(go, body, primary, newAO); UpdatePosition(go, body, primary, newAO);
for(int i = 0; i < moons.Count(); i++) for (int i = 0; i < children.Count(); i++)
{ {
moons[i].transform.position = go.transform.position + relativeMoonPositions[i]; var child = children[i];
// If the child is an AO we do stuff too
var childAO = child.GetComponent<AstroObject>();
if (childAO != null)
{
foreach (var childChild in AstroObjectLocator.GetChildren(childAO))
{
var dPos = childChild.transform.position - child.transform.position;
childChild.transform.position = go.transform.position + relativeMoonPositions[i] + dPos;
}
// Make sure the moons get updated to the new AO
childAO._primaryBody = newAO;
}
child.transform.position = go.transform.position + relativeMoonPositions[i];
} }
// Have to do this after setting position // Have to do this after setting position
InitialMotionBuilder.SetInitialMotion(im, primary, newAO, owrb, body.Config.Orbit); InitialMotionBuilder.SetInitialMotion(im, primary, newAO);
// Have to register this new AO to the locator // Have to register this new AO to the locator
Locator.RegisterAstroObject(newAO); Locator.RegisterAstroObject(newAO);
@ -461,7 +487,7 @@ namespace NewHorizons.Handlers
private static void UpdatePosition(GameObject go, NewHorizonsBody body, AstroObject primaryBody, AstroObject secondaryBody) private static void UpdatePosition(GameObject go, NewHorizonsBody body, AstroObject primaryBody, AstroObject secondaryBody)
{ {
Logger.Log($"Placing [{secondaryBody.name}] around [{primaryBody.name}]"); Logger.Log($"Placing [{secondaryBody?.name}] around [{primaryBody?.name}]");
go.transform.parent = Locator.GetRootTransform(); go.transform.parent = Locator.GetRootTransform();

View File

@ -245,7 +245,7 @@ namespace NewHorizons
if (map != null) map._maxPanDistance = FurthestOrbit * 1.5f; if (map != null) map._maxPanDistance = FurthestOrbit * 1.5f;
// Fix the map satellite // Fix the map satellite
AstroObjectLocator.GetAstroObject(AstroObject.Name.MapSatellite).gameObject.AddComponent<MapSatelliteOrbitFix>(); GameObject.Find("HearthianMapSatellite_Body").AddComponent<MapSatelliteOrbitFix>();
} }
else else
{ {

View File

@ -9,28 +9,25 @@ namespace NewHorizons.Utility
{ {
public static class AstroObjectLocator public static class AstroObjectLocator
{ {
private static AstroObject _timberMoon;
private static AstroObject _volcanicMoon;
private static AstroObject _sunStation;
private static AstroObject _mapSatellite;
private static Dictionary<string, AstroObject> _customAstroObjectDictionary = new Dictionary<string, AstroObject>(); private static Dictionary<string, AstroObject> _customAstroObjectDictionary = new Dictionary<string, AstroObject>();
private static List<AstroObject> _list = new List<AstroObject>();
public static void Init() public static void Init()
{ {
_list = new List<AstroObject>();
_customAstroObjectDictionary = new Dictionary<string, AstroObject>(); _customAstroObjectDictionary = new Dictionary<string, AstroObject>();
foreach (AstroObject ao in GameObject.FindObjectsOfType<AstroObject>()) foreach (AstroObject ao in GameObject.FindObjectsOfType<AstroObject>())
{ {
AddAstroObject(ao); RegisterCustomAstroObject(ao);
} }
} }
public static AstroObject GetAstroObject(string name, bool flag = false) public static AstroObject GetAstroObject(string name, bool flag = false)
{ {
if (_customAstroObjectDictionary.ContainsKey(name)) return _customAstroObjectDictionary[name]; if (_customAstroObjectDictionary.ContainsKey(name))
{
return _customAstroObjectDictionary[name];
}
// Else check stock names
var stringID = name.ToUpper().Replace(" ", "_").Replace("'", ""); var stringID = name.ToUpper().Replace(" ", "_").Replace("'", "");
if (stringID.Equals("ATTLEROCK")) stringID = "TIMBER_MOON"; if (stringID.Equals("ATTLEROCK")) stringID = "TIMBER_MOON";
if (stringID.Equals("HOLLOWS_LANTERN")) stringID = "VOLCANIC_MOON"; if (stringID.Equals("HOLLOWS_LANTERN")) stringID = "VOLCANIC_MOON";
@ -38,11 +35,20 @@ namespace NewHorizons.Utility
if (stringID.Equals("EMBER_TWIN")) stringID = "CAVE_TWIN"; if (stringID.Equals("EMBER_TWIN")) stringID = "CAVE_TWIN";
if (stringID.Equals("INTERLOPER")) stringID = "COMET"; if (stringID.Equals("INTERLOPER")) stringID = "COMET";
string key;
if (stringID.ToUpper().Replace("_", "").Equals("MAPSATELLITE")) if (stringID.ToUpper().Replace("_", "").Equals("MAPSATELLITE"))
return GetAstroObject(AstroObject.Name.MapSatellite); {
key = AstroObject.Name.MapSatellite.ToString();
}
else
{
key = AstroObject.StringIDToAstroObjectName(stringID).ToString();
}
var aoName = AstroObject.StringIDToAstroObjectName(stringID); if (_customAstroObjectDictionary.ContainsKey(key))
if (aoName != AstroObject.Name.None && aoName != AstroObject.Name.CustomString) return GetAstroObject(aoName); {
return _customAstroObjectDictionary[key];
}
// Try again // Try again
if (!flag) return GetAstroObject(name.Replace(" ", ""), true); if (!flag) return GetAstroObject(name.Replace(" ", ""), true);
@ -50,82 +56,36 @@ namespace NewHorizons.Utility
return null; return null;
} }
public static AstroObject GetAstroObject(AstroObject.Name astroObjectName, string customName = null)
{
switch (astroObjectName)
{
case AstroObject.Name.Sun:
case AstroObject.Name.CaveTwin:
case AstroObject.Name.TowerTwin:
case AstroObject.Name.TimberHearth:
case AstroObject.Name.BrittleHollow:
case AstroObject.Name.GiantsDeep:
case AstroObject.Name.DarkBramble:
case AstroObject.Name.Comet:
case AstroObject.Name.WhiteHole:
case AstroObject.Name.WhiteHoleTarget:
case AstroObject.Name.QuantumMoon:
case AstroObject.Name.RingWorld:
case AstroObject.Name.ProbeCannon:
case AstroObject.Name.DreamWorld:
return Locator.GetAstroObject(astroObjectName);
case AstroObject.Name.TimberMoon:
if (_timberMoon == null) _timberMoon = GameObject.Find("Moon_Body")?.gameObject?.GetComponent<AstroObject>();
return _timberMoon;
case AstroObject.Name.VolcanicMoon:
if (_volcanicMoon == null) _volcanicMoon = GameObject.Find("VolcanicMoon_Body")?.gameObject?.GetComponent<AstroObject>();
return _volcanicMoon;
case AstroObject.Name.SunStation:
if (_sunStation == null) _sunStation = GameObject.Find("SunStation_Body")?.gameObject?.GetComponent<AstroObject>();
return _sunStation;
case AstroObject.Name.MapSatellite:
if (_mapSatellite == null) _mapSatellite = GameObject.Find("HearthianMapSatellite_Body")?.gameObject?.GetComponent<AstroObject>();
return _mapSatellite;
case AstroObject.Name.CustomString:
return _customAstroObjectDictionary[customName];
}
return null;
}
public static void RegisterCustomAstroObject(AstroObject ao) public static void RegisterCustomAstroObject(AstroObject ao)
{ {
if (ao.GetAstroObjectName() != AstroObject.Name.CustomString) var key = ao._name == AstroObject.Name.CustomString ? ao.GetCustomName() : ao._name.ToString();
{
Logger.Log($"Can't register {ao.name} as it's AstroObject.Name isn't CustomString.");
return;
}
var name = ao.GetCustomName(); if (_customAstroObjectDictionary.Keys.Contains(key))
if (_customAstroObjectDictionary.Keys.Contains(name)) {
_customAstroObjectDictionary[name] = ao; Logger.Log($"Registering duplicate [{ao.name}] as [{key}]");
_customAstroObjectDictionary[key] = ao;
}
else else
_customAstroObjectDictionary.Add(name, ao); {
Logger.Log($"Registering [{ao.name}] as [{key}]");
_customAstroObjectDictionary.Add(key, ao);
}
} }
public static void DeregisterCustomAstroObject(AstroObject ao) public static void DeregisterCustomAstroObject(AstroObject ao)
{ {
if (ao.GetAstroObjectName() != AstroObject.Name.CustomString) return; var key = ao._name == AstroObject.Name.CustomString ? ao.GetCustomName() : ao._name.ToString();
_customAstroObjectDictionary.Remove(ao.GetCustomName()); _customAstroObjectDictionary.Remove(key);
} }
public static AstroObject[] GetAllAstroObjects() public static AstroObject[] GetAllAstroObjects()
{ {
return _list.ToArray(); return _customAstroObjectDictionary.Values.ToArray();
}
public static void AddAstroObject(AstroObject ao)
{
_list.Add(ao);
}
public static void RemoveAstroObject(AstroObject ao)
{
_list.Remove(ao);
} }
public static GameObject[] GetMoons(AstroObject primary) public static GameObject[] GetMoons(AstroObject primary)
{ {
return _list.Where(x => x._primaryBody == primary).Select(x => x.gameObject).ToArray(); return _customAstroObjectDictionary.Values.Where(x => x._primaryBody == primary).Select(x => x.gameObject).ToArray();
} }
public static GameObject[] GetChildren(AstroObject primary) public static GameObject[] GetChildren(AstroObject primary)
@ -161,6 +121,7 @@ namespace NewHorizons.Utility
break; break;
case AstroObject.Name.TimberHearth: case AstroObject.Name.TimberHearth:
otherChildren.Add(GameObject.Find("MiningRig_Body")); otherChildren.Add(GameObject.Find("MiningRig_Body"));
otherChildren.Add(GameObject.Find("Ship_Body"));
break; break;
case AstroObject.Name.DreamWorld: case AstroObject.Name.DreamWorld:
otherChildren.Add(GameObject.Find("BackRaft_Body")); otherChildren.Add(GameObject.Find("BackRaft_Body"));