mirror of
https://github.com/Outer-Wilds-New-Horizons/new-horizons.git
synced 2025-12-11 20:15:44 +01:00
Basic quantum planet setup
This commit is contained in:
parent
89ad5b5cfa
commit
923cd149fe
@ -73,12 +73,12 @@ namespace NewHorizons.Builder.Orbital
|
|||||||
{
|
{
|
||||||
// It's a circumbinary moon/planet
|
// It's a circumbinary moon/planet
|
||||||
var fakePrimaryBody = focalPoint.FakeMassBody.GetComponent<AstroObject>();
|
var fakePrimaryBody = focalPoint.FakeMassBody.GetComponent<AstroObject>();
|
||||||
SetMotionFromPrimary(fakePrimaryBody, secondaryBody as NHAstroObject, initialMotion);
|
SetMotionFromPrimary(fakePrimaryBody, secondaryBody, secondaryBody as NHAstroObject, initialMotion);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else if (primaryBody.GetGravityVolume())
|
else if (primaryBody.GetGravityVolume())
|
||||||
{
|
{
|
||||||
SetMotionFromPrimary(primaryBody, secondaryBody as NHAstroObject, initialMotion);
|
SetMotionFromPrimary(primaryBody, secondaryBody, secondaryBody as NHAstroObject, initialMotion);
|
||||||
}
|
}
|
||||||
else
|
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());
|
initialMotion.SetPrimaryBody(primaryBody.GetAttachedOWRigidbody());
|
||||||
|
|
||||||
var primaryGravity = new Gravity(primaryBody.GetGravityVolume());
|
var primaryGravity = new Gravity(primaryBody.GetGravityVolume());
|
||||||
var secondaryGravity = new Gravity(secondaryBody.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;
|
var parentVelocity = primaryBody?.GetComponent<InitialMotion>()?.GetInitVelocity() ?? Vector3.zero;
|
||||||
initialMotion._cachedInitVelocity = velocity + parentVelocity;
|
initialMotion._cachedInitVelocity = velocity + parentVelocity;
|
||||||
|
|||||||
109
NewHorizons/Components/QuantumPlanet.cs
Normal file
109
NewHorizons/Components/QuantumPlanet.cs
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -11,6 +11,7 @@ namespace NewHorizons.External.Configs
|
|||||||
string[] ChildrenToDestroy { get; }
|
string[] ChildrenToDestroy { get; }
|
||||||
int BuildPriority { get; }
|
int BuildPriority { get; }
|
||||||
bool CanShowOnTitle { get; }
|
bool CanShowOnTitle { get; }
|
||||||
|
bool IsQuantumState { get; }
|
||||||
BaseModule Base { get; }
|
BaseModule Base { get; }
|
||||||
AtmosphereModule Atmosphere { get; }
|
AtmosphereModule Atmosphere { get; }
|
||||||
OrbitModule Orbit { get; }
|
OrbitModule Orbit { get; }
|
||||||
|
|||||||
1
NewHorizons/External/Configs/PlanetConfig.cs
vendored
1
NewHorizons/External/Configs/PlanetConfig.cs
vendored
@ -14,6 +14,7 @@ namespace NewHorizons.External.Configs
|
|||||||
public string[] ChildrenToDestroy { get; set; }
|
public string[] ChildrenToDestroy { get; set; }
|
||||||
public int BuildPriority { get; set; } = -1;
|
public int BuildPriority { get; set; } = -1;
|
||||||
public bool CanShowOnTitle { get; set; } = true;
|
public bool CanShowOnTitle { get; set; } = true;
|
||||||
|
public bool IsQuantumState { get; set; }
|
||||||
public BaseModule Base { get; set; }
|
public BaseModule Base { get; set; }
|
||||||
public AtmosphereModule Atmosphere { get; set; }
|
public AtmosphereModule Atmosphere { get; set; }
|
||||||
public OrbitModule Orbit { get; set; }
|
public OrbitModule Orbit { get; set; }
|
||||||
|
|||||||
@ -21,13 +21,18 @@ namespace NewHorizons.Handlers
|
|||||||
public static class PlanetCreationHandler
|
public static class PlanetCreationHandler
|
||||||
{
|
{
|
||||||
public static List<NewHorizonsBody> NextPassBodies = new List<NewHorizonsBody>();
|
public static List<NewHorizonsBody> NextPassBodies = new List<NewHorizonsBody>();
|
||||||
|
|
||||||
|
// I literally forget what this is for
|
||||||
private static Dictionary<AstroObject, NewHorizonsBody> ExistingAOConfigs;
|
private static Dictionary<AstroObject, NewHorizonsBody> ExistingAOConfigs;
|
||||||
|
|
||||||
|
private static Dictionary<NHAstroObject, NewHorizonsBody> _dict;
|
||||||
|
|
||||||
public static void Init(List<NewHorizonsBody> bodies)
|
public static void Init(List<NewHorizonsBody> bodies)
|
||||||
{
|
{
|
||||||
Main.FurthestOrbit = 30000;
|
Main.FurthestOrbit = 30000;
|
||||||
|
|
||||||
ExistingAOConfigs = new Dictionary<AstroObject, NewHorizonsBody>();
|
ExistingAOConfigs = new Dictionary<AstroObject, NewHorizonsBody>();
|
||||||
|
_dict = new Dictionary<NHAstroObject, NewHorizonsBody>();
|
||||||
|
|
||||||
// Set up stars
|
// Set up stars
|
||||||
// Need to manage this when there are multiple stars
|
// Need to manage this when there are multiple stars
|
||||||
@ -149,7 +154,40 @@ namespace NewHorizons.Handlers
|
|||||||
if (ao != null) Main.Instance.ModHelper.Events.Unity.FireInNUpdates(() => PlanetDestructionHandler.RemoveBody(ao), 2);
|
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 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)
|
catch (Exception e)
|
||||||
{
|
{
|
||||||
@ -159,16 +197,25 @@ namespace NewHorizons.Handlers
|
|||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
try
|
if(body.Config.IsQuantumState)
|
||||||
{
|
{
|
||||||
GameObject planetObject = GenerateBody(body, defaultPrimaryToSun);
|
// If the ground state object isn't made yet do it later
|
||||||
if (planetObject == null) return false;
|
NextPassBodies.Add(body);
|
||||||
planetObject.SetActive(true);
|
|
||||||
}
|
}
|
||||||
catch (Exception e)
|
else
|
||||||
{
|
{
|
||||||
Logger.LogError($"Couldn't generate body {body.Config?.Name}: {e.Message}, {e.StackTrace}");
|
try
|
||||||
return false;
|
{
|
||||||
|
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;
|
return true;
|
||||||
@ -234,13 +281,7 @@ namespace NewHorizons.Handlers
|
|||||||
var owRigidBody = RigidBodyBuilder.Make(go, body.Config);
|
var owRigidBody = RigidBodyBuilder.Make(go, body.Config);
|
||||||
var ao = AstroObjectBuilder.Make(go, primaryBody, body.Config);
|
var ao = AstroObjectBuilder.Make(go, primaryBody, body.Config);
|
||||||
|
|
||||||
var atmoSize = body.Config.Atmosphere != null ? body.Config.Atmosphere.Size : 0f;
|
var sphereOfInfluence = GetSphereOfInfluence(body);
|
||||||
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 sector = MakeSector.Make(go, owRigidBody, sphereOfInfluence * 2f);
|
var sector = MakeSector.Make(go, owRigidBody, sphereOfInfluence * 2f);
|
||||||
ao._rootSector = sector;
|
ao._rootSector = sector;
|
||||||
@ -298,7 +339,7 @@ namespace NewHorizons.Handlers
|
|||||||
body.Object = go;
|
body.Object = go;
|
||||||
|
|
||||||
// Now that we're done move the planet into place
|
// 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
|
// Have to do this after setting position
|
||||||
var initialMotion = InitialMotionBuilder.Make(go, primaryBody, ao, owRigidBody, body.Config.Orbit);
|
var initialMotion = InitialMotionBuilder.Make(go, primaryBody, ao, owRigidBody, body.Config.Orbit);
|
||||||
@ -328,6 +369,15 @@ namespace NewHorizons.Handlers
|
|||||||
return go;
|
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)
|
private static GameObject SharedGenerateBody(NewHorizonsBody body, GameObject go, Sector sector, OWRigidbody rb)
|
||||||
{
|
{
|
||||||
if (body.Config.Ring != null)
|
if (body.Config.Ring != null)
|
||||||
@ -476,7 +526,7 @@ namespace NewHorizons.Handlers
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Move the primary
|
// Move the primary
|
||||||
UpdatePosition(go, body, primary, newAO);
|
UpdatePosition(go, body.Config.Orbit, primary, newAO);
|
||||||
|
|
||||||
for (int i = 0; i < children.Count(); i++)
|
for (int i = 0; i < children.Count(); i++)
|
||||||
{
|
{
|
||||||
@ -523,7 +573,7 @@ namespace NewHorizons.Handlers
|
|||||||
return;
|
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}]");
|
Logger.Log($"Placing [{secondaryBody?.name}] around [{primaryBody?.name}]");
|
||||||
|
|
||||||
@ -532,9 +582,7 @@ namespace NewHorizons.Handlers
|
|||||||
if (primaryBody != null)
|
if (primaryBody != null)
|
||||||
{
|
{
|
||||||
var primaryGravity = new Gravity(primaryBody.GetGravityVolume());
|
var primaryGravity = new Gravity(primaryBody.GetGravityVolume());
|
||||||
var secondaryGravity = new Gravity(secondaryBody.GetGravityVolume());
|
var secondaryGravity = new Gravity(secondaryBody.GetGravityVolume());;
|
||||||
|
|
||||||
var orbit = body.Config.Orbit;
|
|
||||||
|
|
||||||
go.transform.position = orbit.GetOrbitalParameters(primaryGravity, secondaryGravity).InitialPosition + primaryBody.transform.position;
|
go.transform.position = orbit.GetOrbitalParameters(primaryGravity, secondaryGravity).InitialPosition + primaryBody.transform.position;
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user