Basic quantum planet setup

This commit is contained in:
Nick 2022-05-10 23:27:19 -04:00
parent 89ad5b5cfa
commit 923cd149fe
5 changed files with 185 additions and 26 deletions

View File

@ -73,12 +73,12 @@ namespace NewHorizons.Builder.Orbital
{
// It's a circumbinary moon/planet
var fakePrimaryBody = focalPoint.FakeMassBody.GetComponent<AstroObject>();
SetMotionFromPrimary(fakePrimaryBody, secondaryBody as NHAstroObject, initialMotion);
SetMotionFromPrimary(fakePrimaryBody, secondaryBody, secondaryBody as NHAstroObject, initialMotion);
}
}
else if (primaryBody.GetGravityVolume())
{
SetMotionFromPrimary(primaryBody, secondaryBody as NHAstroObject, initialMotion);
SetMotionFromPrimary(primaryBody, secondaryBody, secondaryBody as NHAstroObject, initialMotion);
}
else
{
@ -86,13 +86,13 @@ namespace NewHorizons.Builder.Orbital
}
}
private static void SetMotionFromPrimary(AstroObject primaryBody, NHAstroObject secondaryBody, InitialMotion initialMotion)
public static void SetMotionFromPrimary(AstroObject primaryBody, AstroObject secondaryBody, IOrbitalParameters orbit, InitialMotion initialMotion)
{
initialMotion.SetPrimaryBody(primaryBody.GetAttachedOWRigidbody());
var primaryGravity = new Gravity(primaryBody.GetGravityVolume());
var secondaryGravity = new Gravity(secondaryBody.GetGravityVolume());
var velocity = secondaryBody.GetOrbitalParameters(primaryGravity, secondaryGravity).InitialVelocity;
var velocity = orbit.GetOrbitalParameters(primaryGravity, secondaryGravity).InitialVelocity;
var parentVelocity = primaryBody?.GetComponent<InitialMotion>()?.GetInitVelocity() ?? Vector3.zero;
initialMotion._cachedInitVelocity = velocity + parentVelocity;

View File

@ -0,0 +1,109 @@
using NewHorizons.Builder.General;
using NewHorizons.Builder.Orbital;
using NewHorizons.Components.Orbital;
using NewHorizons.External;
using NewHorizons.Handlers;
using NewHorizons.Utility;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UnityEngine;
using Random = UnityEngine.Random;
using Logger = NewHorizons.Utility.Logger;
namespace NewHorizons.Components
{
public class QuantumPlanet : QuantumObject
{
public List<State> states = new List<State>();
private int _currentIndex;
private NHAstroObject _astroObject;
private ConstantForceDetector _detector;
private AlignWithTargetBody _alignment;
private OWRigidbody _rb;
public override void Awake()
{
base.Awake();
_astroObject = GetComponent<NHAstroObject>();
_detector = GetComponentInChildren<ConstantForceDetector>();
_alignment = GetComponent<AlignWithTargetBody>();
_rb = GetComponent<OWRigidbody>();
}
public override void Start()
{
base.Start();
foreach(var state in states)
{
state.sector?.gameObject?.SetActive(false);
}
ChangeQuantumState(true);
}
public override bool ChangeQuantumState(bool skipInstantVisibilityCheck)
{
Logger.Log($"Changing quantum state");
if (states.Count <= 1) return false;
var newIndex = _currentIndex;
while(newIndex == _currentIndex)
{
newIndex = Random.Range(0, states.Count);
}
var oldState = states[_currentIndex];
var newState = states[newIndex];
if (newState.sector != null) SetNewSector(oldState, newState);
if(newState.orbit != null) SetNewOrbit(newState);
_currentIndex = newIndex;
return true;
}
private void SetNewSector(State oldState, State newState)
{
oldState.sector.gameObject.SetActive(false);
newState.sector.gameObject.SetActive(true);
}
private void SetNewOrbit(State state)
{
var currentOrbit = state.orbit;
var primaryBody = AstroObjectLocator.GetAstroObject(currentOrbit.PrimaryBody);
_astroObject._primaryBody = primaryBody;
DetectorBuilder.SetDetector(primaryBody, _astroObject, _detector);
if (_alignment != null) _alignment.SetTargetBody(primaryBody.GetComponent<OWRigidbody>());
PlanetCreationHandler.UpdatePosition(gameObject, currentOrbit, primaryBody, _astroObject);
var primaryGravity = new Gravity(primaryBody.GetGravityVolume());
var secondaryGravity = new Gravity(_astroObject.GetGravityVolume());
_rb.SetVelocity(currentOrbit.GetOrbitalParameters(primaryGravity, secondaryGravity).InitialVelocity);
}
public class State
{
public Sector sector { get; set; }
public OrbitModule orbit { get; set; }
public State(Sector sector, OrbitModule orbit)
{
this.sector = sector;
this.orbit = orbit;
}
}
}
}

View File

@ -11,6 +11,7 @@ namespace NewHorizons.External.Configs
string[] ChildrenToDestroy { get; }
int BuildPriority { get; }
bool CanShowOnTitle { get; }
bool IsQuantumState { get; }
BaseModule Base { get; }
AtmosphereModule Atmosphere { get; }
OrbitModule Orbit { get; }

View File

@ -14,6 +14,7 @@ namespace NewHorizons.External.Configs
public string[] ChildrenToDestroy { get; set; }
public int BuildPriority { get; set; } = -1;
public bool CanShowOnTitle { get; set; } = true;
public bool IsQuantumState { get; set; }
public BaseModule Base { get; set; }
public AtmosphereModule Atmosphere { get; set; }
public OrbitModule Orbit { get; set; }

View File

@ -21,13 +21,18 @@ namespace NewHorizons.Handlers
public static class PlanetCreationHandler
{
public static List<NewHorizonsBody> NextPassBodies = new List<NewHorizonsBody>();
// I literally forget what this is for
private static Dictionary<AstroObject, NewHorizonsBody> ExistingAOConfigs;
private static Dictionary<NHAstroObject, NewHorizonsBody> _dict;
public static void Init(List<NewHorizonsBody> bodies)
{
Main.FurthestOrbit = 30000;
ExistingAOConfigs = new Dictionary<AstroObject, NewHorizonsBody>();
_dict = new Dictionary<NHAstroObject, NewHorizonsBody>();
// Set up stars
// Need to manage this when there are multiple stars
@ -128,7 +133,7 @@ namespace NewHorizons.Handlers
public static bool LoadBody(NewHorizonsBody body, bool defaultPrimaryToSun = false)
{
// I don't remember doing this why is it exceptions what am I doing
GameObject existingPlanet = null;
GameObject existingPlanet = null;
try
{
existingPlanet = AstroObjectLocator.GetAstroObject(body.Config.Name).gameObject;
@ -149,7 +154,40 @@ namespace NewHorizons.Handlers
if (ao != null) Main.Instance.ModHelper.Events.Unity.FireInNUpdates(() => PlanetDestructionHandler.RemoveBody(ao), 2);
else Main.Instance.ModHelper.Events.Unity.FireInNUpdates(() => existingPlanet.SetActive(false), 2);
}
else UpdateBody(body, existingPlanet);
else if (body.Config.IsQuantumState)
{
var quantumPlanet = existingPlanet.GetComponent<QuantumPlanet>();
if (quantumPlanet == null)
{
// Have to also add the root orbit and sector
quantumPlanet = existingPlanet.AddComponent<QuantumPlanet>();
var ao = quantumPlanet.GetComponent<NHAstroObject>();
var rootSector = quantumPlanet.GetComponentInChildren<Sector>();
var groundOrbit = _dict[ao].Config.Orbit;
quantumPlanet.states.Add(new QuantumPlanet.State(rootSector, groundOrbit));
}
var rb = existingPlanet.GetComponent<OWRigidbody>();
var sector = MakeSector.Make(existingPlanet, rb, GetSphereOfInfluence(body));
sector.name = $"Sector-{existingPlanet.GetComponents<Sector>().Count()}";
SharedGenerateBody(body, existingPlanet, sector, rb);
// If nothing was generated then forget the sector
if (sector.transform.childCount == 0) sector = null;
// If semimajor axis is 0 then forget the orbit
var orbit = body.Config.Orbit.SemiMajorAxis == 0 ? null : body.Config.Orbit;
quantumPlanet.states.Add(new QuantumPlanet.State(sector, orbit));
}
else
{
UpdateBody(body, existingPlanet);
}
}
catch (Exception e)
{
@ -159,16 +197,25 @@ namespace NewHorizons.Handlers
}
else
{
try
if(body.Config.IsQuantumState)
{
GameObject planetObject = GenerateBody(body, defaultPrimaryToSun);
if (planetObject == null) return false;
planetObject.SetActive(true);
// If the ground state object isn't made yet do it later
NextPassBodies.Add(body);
}
catch (Exception e)
else
{
Logger.LogError($"Couldn't generate body {body.Config?.Name}: {e.Message}, {e.StackTrace}");
return false;
try
{
GameObject planetObject = GenerateBody(body, defaultPrimaryToSun);
if (planetObject == null) return false;
planetObject.SetActive(true);
_dict.Add(planetObject.GetComponent<NHAstroObject>(), body);
}
catch (Exception e)
{
Logger.LogError($"Couldn't generate body {body.Config?.Name}: {e.Message}, {e.StackTrace}");
return false;
}
}
}
return true;
@ -234,13 +281,7 @@ namespace NewHorizons.Handlers
var owRigidBody = RigidBodyBuilder.Make(go, body.Config);
var ao = AstroObjectBuilder.Make(go, primaryBody, body.Config);
var atmoSize = body.Config.Atmosphere != null ? body.Config.Atmosphere.Size : 0f;
float sphereOfInfluence = Mathf.Max(Mathf.Max(atmoSize, 50), body.Config.Base.SurfaceSize * 2f);
var overrideSOI = body.Config.Base.SphereOfInfluence;
if (overrideSOI != 0)
{
sphereOfInfluence = overrideSOI;
}
var sphereOfInfluence = GetSphereOfInfluence(body);
var sector = MakeSector.Make(go, owRigidBody, sphereOfInfluence * 2f);
ao._rootSector = sector;
@ -298,7 +339,7 @@ namespace NewHorizons.Handlers
body.Object = go;
// Now that we're done move the planet into place
UpdatePosition(go, body, primaryBody, ao);
UpdatePosition(go, body.Config.Orbit, primaryBody, ao);
// Have to do this after setting position
var initialMotion = InitialMotionBuilder.Make(go, primaryBody, ao, owRigidBody, body.Config.Orbit);
@ -328,6 +369,15 @@ namespace NewHorizons.Handlers
return go;
}
private static float GetSphereOfInfluence(NewHorizonsBody body)
{
var atmoSize = body.Config.Atmosphere != null ? body.Config.Atmosphere.Size : 0f;
float sphereOfInfluence = Mathf.Max(Mathf.Max(atmoSize, 50), body.Config.Base.SurfaceSize * 2f);
var overrideSOI = body.Config.Base.SphereOfInfluence;
if (overrideSOI != 0) sphereOfInfluence = overrideSOI;
return sphereOfInfluence;
}
private static GameObject SharedGenerateBody(NewHorizonsBody body, GameObject go, Sector sector, OWRigidbody rb)
{
if (body.Config.Ring != null)
@ -476,7 +526,7 @@ namespace NewHorizons.Handlers
}
// Move the primary
UpdatePosition(go, body, primary, newAO);
UpdatePosition(go, body.Config.Orbit, primary, newAO);
for (int i = 0; i < children.Count(); i++)
{
@ -523,7 +573,7 @@ namespace NewHorizons.Handlers
return;
}
private static void UpdatePosition(GameObject go, NewHorizonsBody body, AstroObject primaryBody, AstroObject secondaryBody)
public static void UpdatePosition(GameObject go, IOrbitalParameters orbit, AstroObject primaryBody, AstroObject secondaryBody)
{
Logger.Log($"Placing [{secondaryBody?.name}] around [{primaryBody?.name}]");
@ -532,9 +582,7 @@ namespace NewHorizons.Handlers
if (primaryBody != null)
{
var primaryGravity = new Gravity(primaryBody.GetGravityVolume());
var secondaryGravity = new Gravity(secondaryBody.GetGravityVolume());
var orbit = body.Config.Orbit;
var secondaryGravity = new Gravity(secondaryBody.GetGravityVolume());;
go.transform.position = orbit.GetOrbitalParameters(primaryGravity, secondaryGravity).InitialPosition + primaryBody.transform.position;
}