mirror of
https://github.com/Outer-Wilds-New-Horizons/new-horizons.git
synced 2025-12-11 20:15:44 +01:00
Can move existing planet but not change primary
This commit is contained in:
parent
d4d0ea32a9
commit
5c705f12c8
@ -9,35 +9,10 @@ using NewHorizons.Components.Orbital;
|
|||||||
|
|
||||||
namespace NewHorizons.Builder.General
|
namespace NewHorizons.Builder.General
|
||||||
{
|
{
|
||||||
static class BaseBuilder
|
static class AstroObjectBuilder
|
||||||
{
|
{
|
||||||
public static Tuple<AstroObject, OWRigidbody> Make(GameObject body, AstroObject primaryBody, IPlanetConfig config)
|
public static NHAstroObject Make(GameObject body, AstroObject primaryBody, IPlanetConfig config)
|
||||||
{
|
{
|
||||||
body.AddComponent<ProxyShadowCasterSuperGroup>();
|
|
||||||
|
|
||||||
Rigidbody rigidBody = body.AddComponent<Rigidbody>();
|
|
||||||
rigidBody.mass = 10000;
|
|
||||||
rigidBody.drag = 0f;
|
|
||||||
rigidBody.angularDrag = 0f;
|
|
||||||
rigidBody.useGravity = false;
|
|
||||||
rigidBody.isKinematic = true;
|
|
||||||
rigidBody.interpolation = RigidbodyInterpolation.None;
|
|
||||||
rigidBody.collisionDetectionMode = CollisionDetectionMode.Discrete;
|
|
||||||
|
|
||||||
KinematicRigidbody kinematicRigidBody = body.AddComponent<KinematicRigidbody>();
|
|
||||||
kinematicRigidBody.centerOfMass = Vector3.zero;
|
|
||||||
|
|
||||||
OWRigidbody owRigidBody = body.AddComponent<OWRigidbody>();
|
|
||||||
owRigidBody._kinematicSimulation = true;
|
|
||||||
owRigidBody._autoGenerateCenterOfMass = true;
|
|
||||||
owRigidBody.SetIsTargetable(true);
|
|
||||||
owRigidBody._maintainOriginalCenterOfMass = true;
|
|
||||||
owRigidBody._rigidbody = rigidBody;
|
|
||||||
owRigidBody._kinematicRigidbody = kinematicRigidBody;
|
|
||||||
owRigidBody._origParent = GameObject.Find("SolarSystemRoot").transform;
|
|
||||||
owRigidBody.EnableKinematicSimulation();
|
|
||||||
owRigidBody.MakeKinematic();
|
|
||||||
|
|
||||||
NHAstroObject astroObject = body.AddComponent<NHAstroObject>();
|
NHAstroObject astroObject = body.AddComponent<NHAstroObject>();
|
||||||
astroObject.HideDisplayName = !config.Base.HasMapMarker;
|
astroObject.HideDisplayName = !config.Base.HasMapMarker;
|
||||||
|
|
||||||
@ -75,16 +50,16 @@ namespace NewHorizons.Builder.General
|
|||||||
{
|
{
|
||||||
alignment._localAlignmentAxis = config.Orbit.AlignmentAxis;
|
alignment._localAlignmentAxis = config.Orbit.AlignmentAxis;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (config.Base.CenterOfSolarSystem)
|
if (config.Base.CenterOfSolarSystem)
|
||||||
{
|
{
|
||||||
Logger.Log($"Setting center of universe to {config.Name}");
|
Logger.Log($"Setting center of universe to {config.Name}");
|
||||||
Main.Instance.ModHelper.Events.Unity.FireInNUpdates(() => Locator.GetCenterOfTheUniverse()._staticReferenceFrame = owRigidBody, 2);
|
// By the time it runs we'll be able to get the OWRB with the method
|
||||||
|
Main.Instance.ModHelper.Events.Unity.FireInNUpdates(() => Locator.GetCenterOfTheUniverse()._staticReferenceFrame = astroObject.GetAttachedOWRigidbody(), 2);
|
||||||
}
|
}
|
||||||
|
|
||||||
return new Tuple<AstroObject, OWRigidbody>(astroObject, owRigidBody);
|
return astroObject;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -42,6 +42,14 @@ namespace NewHorizons.Builder.General
|
|||||||
// Could copy the splash from the interloper as well some day
|
// Could copy the splash from the interloper as well some day
|
||||||
}
|
}
|
||||||
|
|
||||||
|
SetDetector(primaryBody, astroObject, forceDetector);
|
||||||
|
|
||||||
|
detectorGO.SetActive(true);
|
||||||
|
return detectorGO;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void SetDetector(AstroObject primaryBody, AstroObject astroObject, ConstantForceDetector forceDetector)
|
||||||
|
{
|
||||||
GravityVolume parentGravityVolume = primaryBody?.GetAttachedOWRigidbody()?.GetAttachedGravityVolume();
|
GravityVolume parentGravityVolume = primaryBody?.GetAttachedOWRigidbody()?.GetAttachedGravityVolume();
|
||||||
if (parentGravityVolume != null)
|
if (parentGravityVolume != null)
|
||||||
{
|
{
|
||||||
@ -51,9 +59,10 @@ namespace NewHorizons.Builder.General
|
|||||||
{
|
{
|
||||||
// It's probably a focal point (or its just broken)
|
// It's probably a focal point (or its just broken)
|
||||||
var binaryFocalPoint = primaryBody?.gameObject?.GetComponent<BinaryFocalPoint>();
|
var binaryFocalPoint = primaryBody?.gameObject?.GetComponent<BinaryFocalPoint>();
|
||||||
if(binaryFocalPoint != null)
|
if (binaryFocalPoint != null)
|
||||||
{
|
{
|
||||||
if(astroObject.GetCustomName().Equals(binaryFocalPoint.PrimaryName)) {
|
if (astroObject.GetCustomName().Equals(binaryFocalPoint.PrimaryName))
|
||||||
|
{
|
||||||
binaryFocalPoint.Primary = astroObject;
|
binaryFocalPoint.Primary = astroObject;
|
||||||
if (binaryFocalPoint.Secondary != null)
|
if (binaryFocalPoint.Secondary != null)
|
||||||
{
|
{
|
||||||
@ -73,7 +82,7 @@ namespace NewHorizons.Builder.General
|
|||||||
else
|
else
|
||||||
{
|
{
|
||||||
// It's a planet
|
// It's a planet
|
||||||
if(binaryFocalPoint.Primary != null && binaryFocalPoint.Secondary != null)
|
if (binaryFocalPoint.Primary != null && binaryFocalPoint.Secondary != null)
|
||||||
{
|
{
|
||||||
var fakeBarycenterGravityVolume = binaryFocalPoint.FakeMassBody.GetComponent<AstroObject>().GetGravityVolume();
|
var fakeBarycenterGravityVolume = binaryFocalPoint.FakeMassBody.GetComponent<AstroObject>().GetGravityVolume();
|
||||||
forceDetector._detectableFields = new ForceVolume[] { fakeBarycenterGravityVolume };
|
forceDetector._detectableFields = new ForceVolume[] { fakeBarycenterGravityVolume };
|
||||||
@ -81,9 +90,6 @@ namespace NewHorizons.Builder.General
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
detectorGO.SetActive(true);
|
|
||||||
return detectorGO;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void SetBinaryForceDetectableFields(BinaryFocalPoint point, ConstantForceDetector primaryCFD, ConstantForceDetector secondaryCFD)
|
private static void SetBinaryForceDetectableFields(BinaryFocalPoint point, ConstantForceDetector primaryCFD, ConstantForceDetector secondaryCFD)
|
||||||
|
|||||||
43
NewHorizons/Builder/General/RigidBodyBuilder.cs
Normal file
43
NewHorizons/Builder/General/RigidBodyBuilder.cs
Normal file
@ -0,0 +1,43 @@
|
|||||||
|
using NewHorizons.External.Configs;
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using UnityEngine;
|
||||||
|
|
||||||
|
namespace NewHorizons.Builder.General
|
||||||
|
{
|
||||||
|
public static class RigidBodyBuilder
|
||||||
|
{
|
||||||
|
public static OWRigidbody Make(GameObject body, IPlanetConfig config)
|
||||||
|
{
|
||||||
|
body.AddComponent<ProxyShadowCasterSuperGroup>();
|
||||||
|
|
||||||
|
Rigidbody rigidBody = body.AddComponent<Rigidbody>();
|
||||||
|
rigidBody.mass = 10000;
|
||||||
|
rigidBody.drag = 0f;
|
||||||
|
rigidBody.angularDrag = 0f;
|
||||||
|
rigidBody.useGravity = false;
|
||||||
|
rigidBody.isKinematic = true;
|
||||||
|
rigidBody.interpolation = RigidbodyInterpolation.None;
|
||||||
|
rigidBody.collisionDetectionMode = CollisionDetectionMode.Discrete;
|
||||||
|
|
||||||
|
KinematicRigidbody kinematicRigidBody = body.AddComponent<KinematicRigidbody>();
|
||||||
|
kinematicRigidBody.centerOfMass = Vector3.zero;
|
||||||
|
|
||||||
|
OWRigidbody owRigidBody = body.AddComponent<OWRigidbody>();
|
||||||
|
owRigidBody._kinematicSimulation = true;
|
||||||
|
owRigidBody._autoGenerateCenterOfMass = true;
|
||||||
|
owRigidBody.SetIsTargetable(true);
|
||||||
|
owRigidBody._maintainOriginalCenterOfMass = true;
|
||||||
|
owRigidBody._rigidbody = rigidBody;
|
||||||
|
owRigidBody._kinematicRigidbody = kinematicRigidBody;
|
||||||
|
owRigidBody._origParent = GameObject.Find("SolarSystemRoot").transform;
|
||||||
|
owRigidBody.EnableKinematicSimulation();
|
||||||
|
owRigidBody.MakeKinematic();
|
||||||
|
|
||||||
|
return owRigidBody;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -17,8 +17,13 @@ namespace NewHorizons.Builder.Orbital
|
|||||||
{
|
{
|
||||||
public static InitialMotion Make(GameObject body, AstroObject primaryBody, AstroObject secondaryBody, OWRigidbody OWRB, OrbitModule orbit)
|
public static InitialMotion Make(GameObject body, AstroObject primaryBody, AstroObject secondaryBody, OWRigidbody OWRB, OrbitModule orbit)
|
||||||
{
|
{
|
||||||
|
// 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);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static InitialMotion SetInitialMotion(InitialMotion initialMotion, AstroObject primaryBody, AstroObject secondaryBody, OWRigidbody OWRB, 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;
|
||||||
|
|
||||||
|
|||||||
@ -11,7 +11,7 @@ namespace NewHorizons.Builder.Orbital
|
|||||||
{
|
{
|
||||||
static class OrbitlineBuilder
|
static class OrbitlineBuilder
|
||||||
{
|
{
|
||||||
public static void Make(GameObject body, NHAstroObject astroObject, bool isMoon, IPlanetConfig config)
|
public static OrbitLine Make(GameObject body, NHAstroObject astroObject, bool isMoon, IPlanetConfig config)
|
||||||
{
|
{
|
||||||
GameObject orbitGO = new GameObject("Orbit");
|
GameObject orbitGO = new GameObject("Orbit");
|
||||||
orbitGO.transform.parent = body.transform;
|
orbitGO.transform.parent = body.transform;
|
||||||
@ -75,6 +75,8 @@ namespace NewHorizons.Builder.Orbital
|
|||||||
orbitLine._numVerts = (int)Mathf.Clamp(config.Orbit.SemiMajorAxis / 1000f, numVerts, 4096);
|
orbitLine._numVerts = (int)Mathf.Clamp(config.Orbit.SemiMajorAxis / 1000f, numVerts, 4096);
|
||||||
|
|
||||||
Main.Instance.ModHelper.Events.Unity.FireOnNextUpdate(orbitLine.InitializeLineRenderer);
|
Main.Instance.ModHelper.Events.Unity.FireOnNextUpdate(orbitLine.InitializeLineRenderer);
|
||||||
|
|
||||||
|
return orbitLine;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,20 +0,0 @@
|
|||||||
using NewHorizons.Builder.Orbital;
|
|
||||||
using NewHorizons.Utility;
|
|
||||||
using System;
|
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.Linq;
|
|
||||||
using System.Text;
|
|
||||||
using System.Threading.Tasks;
|
|
||||||
using UnityEngine;
|
|
||||||
using Logger = NewHorizons.Utility.Logger;
|
|
||||||
|
|
||||||
namespace NewHorizons.Builder.Updater
|
|
||||||
{
|
|
||||||
public static class OrbitUpdater
|
|
||||||
{
|
|
||||||
public static void Update(NewHorizonsBody body, GameObject go)
|
|
||||||
{
|
|
||||||
Logger.Log("OrbitUpdater does not work currently");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ -3,7 +3,6 @@ using NewHorizons.Builder.Body;
|
|||||||
using NewHorizons.Builder.General;
|
using NewHorizons.Builder.General;
|
||||||
using NewHorizons.Builder.Orbital;
|
using NewHorizons.Builder.Orbital;
|
||||||
using NewHorizons.Builder.Props;
|
using NewHorizons.Builder.Props;
|
||||||
using NewHorizons.Builder.Updater;
|
|
||||||
using NewHorizons.Components;
|
using NewHorizons.Components;
|
||||||
using NewHorizons.Components.Orbital;
|
using NewHorizons.Components.Orbital;
|
||||||
using NewHorizons.External.VariableSize;
|
using NewHorizons.External.VariableSize;
|
||||||
@ -163,6 +162,17 @@ namespace NewHorizons.Handlers
|
|||||||
var sector = go.GetComponentInChildren<Sector>();
|
var sector = go.GetComponentInChildren<Sector>();
|
||||||
var rb = go.GetAttachedOWRigidbody();
|
var rb = go.GetAttachedOWRigidbody();
|
||||||
|
|
||||||
|
// 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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (body.Config.ChildrenToDestroy != null && body.Config.ChildrenToDestroy.Length > 0)
|
if (body.Config.ChildrenToDestroy != null && body.Config.ChildrenToDestroy.Length > 0)
|
||||||
{
|
{
|
||||||
foreach (var child in body.Config.ChildrenToDestroy)
|
foreach (var child in body.Config.ChildrenToDestroy)
|
||||||
@ -174,12 +184,6 @@ namespace NewHorizons.Handlers
|
|||||||
// Do stuff that's shared between generating new planets and updating old ones
|
// Do stuff that's shared between generating new planets and updating old ones
|
||||||
go = SharedGenerateBody(body, go, sector, rb);
|
go = SharedGenerateBody(body, go, sector, rb);
|
||||||
|
|
||||||
// Since orbits are always there just check if they set a semi major axis
|
|
||||||
if (body.Config.Orbit != null && body.Config.Orbit.SemiMajorAxis != 0f)
|
|
||||||
{
|
|
||||||
OrbitUpdater.Update(body, go);
|
|
||||||
}
|
|
||||||
|
|
||||||
return go;
|
return go;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -226,9 +230,8 @@ namespace NewHorizons.Handlers
|
|||||||
sphereOfInfluence = overrideSOI;
|
sphereOfInfluence = overrideSOI;
|
||||||
}
|
}
|
||||||
|
|
||||||
var outputTuple = BaseBuilder.Make(go, primaryBody, body.Config);
|
var owRigidBody = RigidBodyBuilder.Make(go, body.Config);
|
||||||
var ao = outputTuple.Item1;
|
var ao = AstroObjectBuilder.Make(go, primaryBody, body.Config);
|
||||||
var owRigidBody = outputTuple.Item2;
|
|
||||||
|
|
||||||
if (body.Config.Base.SurfaceGravity != 0)
|
if (body.Config.Base.SurfaceGravity != 0)
|
||||||
{
|
{
|
||||||
@ -386,6 +389,76 @@ namespace NewHorizons.Handlers
|
|||||||
return go;
|
return go;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static bool UpdateBodyOrbit(NewHorizonsBody body, GameObject go)
|
||||||
|
{
|
||||||
|
Logger.Log($"Updating orbit of [{body.Config.Name}] to [{body.Config.Orbit}]");
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var ao = go.GetComponent<AstroObject>();
|
||||||
|
var aoName = ao.GetAstroObjectName();
|
||||||
|
var aoType = ao.GetAstroObjectType();
|
||||||
|
|
||||||
|
var owrb = go.GetComponent<OWRigidbody>();
|
||||||
|
|
||||||
|
var im = go.GetComponent<InitialMotion>();
|
||||||
|
|
||||||
|
// By default keep it with the same primary body else update to the new one
|
||||||
|
var primary = ao._primaryBody;
|
||||||
|
if (!string.IsNullOrEmpty(body.Config.Orbit.PrimaryBody))
|
||||||
|
{
|
||||||
|
// 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;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Just destroy the existing AO after copying everything over
|
||||||
|
var newAO = AstroObjectBuilder.Make(go, primary, body.Config);
|
||||||
|
newAO._gravityVolume = ao._gravityVolume;
|
||||||
|
newAO._moon = ao._moon;
|
||||||
|
newAO._name = ao._name;
|
||||||
|
newAO._owRigidbody = ao._owRigidbody;
|
||||||
|
newAO._primaryBody = ao._primaryBody;
|
||||||
|
newAO._rootSector = ao._rootSector;
|
||||||
|
newAO._sandLevelController = ao._sandLevelController;
|
||||||
|
newAO._satellite = ao._satellite;
|
||||||
|
newAO._type = ao._type;
|
||||||
|
// We need these for later
|
||||||
|
var moons = AstroObjectLocator.GetMoons(ao);
|
||||||
|
GameObject.Destroy(ao);
|
||||||
|
|
||||||
|
var orbitLine = go.GetComponentInChildren<OrbitLine>();
|
||||||
|
var isMoon = newAO.GetAstroObjectType() == AstroObject.Type.Moon || newAO.GetAstroObjectType() == AstroObject.Type.Satellite;
|
||||||
|
var newOrbitLine = OrbitlineBuilder.Make(go, newAO, isMoon, body.Config);
|
||||||
|
|
||||||
|
DetectorBuilder.SetDetector(primary, newAO, go.GetComponentInChildren<ConstantForceDetector>());
|
||||||
|
|
||||||
|
// Get ready to move all the satellites
|
||||||
|
var relativeMoonPositions = moons.Select(x => x.transform.position - go.transform.position).ToArray();
|
||||||
|
|
||||||
|
// Move the primary
|
||||||
|
UpdatePosition(go, body, primary, newAO);
|
||||||
|
|
||||||
|
for(int i = 0; i < moons.Count(); i++)
|
||||||
|
{
|
||||||
|
moons[i].transform.position = go.transform.position + relativeMoonPositions[i];
|
||||||
|
}
|
||||||
|
|
||||||
|
// Have to do this after setting position
|
||||||
|
InitialMotionBuilder.SetInitialMotion(im, primary, newAO, owrb, body.Config.Orbit);
|
||||||
|
|
||||||
|
// Have to register this new AO to the locator
|
||||||
|
Locator.RegisterAstroObject(newAO);
|
||||||
|
}
|
||||||
|
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
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
//private static int j = 0;
|
//private static int j = 0;
|
||||||
private static void UpdatePosition(GameObject go, NewHorizonsBody body, AstroObject primaryBody, AstroObject secondaryBody)
|
private static void UpdatePosition(GameObject go, NewHorizonsBody body, AstroObject primaryBody, AstroObject secondaryBody)
|
||||||
{
|
{
|
||||||
|
|||||||
@ -3,7 +3,6 @@ using NewHorizons.Builder.General;
|
|||||||
using NewHorizons.Builder.Orbital;
|
using NewHorizons.Builder.Orbital;
|
||||||
using NewHorizons.Builder.Props;
|
using NewHorizons.Builder.Props;
|
||||||
using NewHorizons.Builder.ShipLog;
|
using NewHorizons.Builder.ShipLog;
|
||||||
using NewHorizons.Builder.Updater;
|
|
||||||
using NewHorizons.Components;
|
using NewHorizons.Components;
|
||||||
using NewHorizons.External;
|
using NewHorizons.External;
|
||||||
using NewHorizons.External.Configs;
|
using NewHorizons.External.Configs;
|
||||||
|
|||||||
@ -122,5 +122,10 @@ namespace NewHorizons.Utility
|
|||||||
{
|
{
|
||||||
_list.Remove(ao);
|
_list.Remove(ao);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static AstroObject[] GetMoons(AstroObject primary)
|
||||||
|
{
|
||||||
|
return _list.Where(x => x._primaryBody == primary).ToArray();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user