Add star lifespan

This commit is contained in:
Noah Pilarski 2022-08-08 18:56:21 -04:00
parent fc7b1ae498
commit 6a790273fe
3 changed files with 23 additions and 9 deletions

View File

@ -141,6 +141,7 @@ namespace NewHorizons.Builder.Body
controller.StartColour = starModule.tint; controller.StartColour = starModule.tint;
controller.EndColour = starModule.endTint; controller.EndColour = starModule.endTint;
controller.WillExplode = starModule.goSupernova; controller.WillExplode = starModule.goSupernova;
controller.lifespan = starModule.lifespan;
controller.normalRamp = !string.IsNullOrEmpty(starModule.starRampTexture) ? ImageUtilities.GetTexture(mod, starModule.starRampTexture) : ramp; controller.normalRamp = !string.IsNullOrEmpty(starModule.starRampTexture) ? ImageUtilities.GetTexture(mod, starModule.starRampTexture) : ramp;
if (!string.IsNullOrEmpty(starModule.starCollapseRampTexture)) if (!string.IsNullOrEmpty(starModule.starCollapseRampTexture))
{ {

View File

@ -36,7 +36,6 @@ namespace NewHorizons.Components.SizeControllers
public float collapseTime = 10f; // seconds public float collapseTime = 10f; // seconds
public float lifespan = 22f; // minutes public float lifespan = 22f; // minutes
private float _age;
private bool _isSupernova; private bool _isSupernova;
private float _supernovaStartTime; private float _supernovaStartTime;
@ -122,8 +121,6 @@ namespace NewHorizons.Components.SizeControllers
_atmosphereRenderers = atmosphere?.transform?.Find("AtmoSphere")?.GetComponentsInChildren<MeshRenderer>(); _atmosphereRenderers = atmosphere?.transform?.Find("AtmoSphere")?.GetComponentsInChildren<MeshRenderer>();
} }
if (WillExplode) GlobalMessenger.AddListener("TriggerSupernova", StartCollapse);
if (scaleCurve != null) if (scaleCurve != null)
{ {
maxScale = scaleCurve.keys.Select(x => x.value).Max() * size; maxScale = scaleCurve.keys.Select(x => x.value).Max() * size;
@ -143,7 +140,6 @@ namespace NewHorizons.Components.SizeControllers
public void OnDestroy() public void OnDestroy()
{ {
if (WillExplode) GlobalMessenger.RemoveListener("TriggerSupernova", StartCollapse);
} }
public void SetProxy(StarEvolutionController proxy) public void SetProxy(StarEvolutionController proxy)
@ -157,8 +153,8 @@ namespace NewHorizons.Components.SizeControllers
// Only do colour transition stuff if they set an end colour // Only do colour transition stuff if they set an end colour
if (EndColour != null) if (EndColour != null)
{ {
// Use the age if theres no resizing happening, else make it get redder the larger it is or wtv // Use minutes elapsed if theres no resizing happening, else make it get redder the larger it is or wtv
var t = _age / (lifespan * 60f); var t = TimeLoop.GetMinutesElapsed() / lifespan;
if (maxScale != minScale) t = Mathf.InverseLerp(minScale, maxScale, CurrentScale); if (maxScale != minScale) t = Mathf.InverseLerp(minScale, maxScale, CurrentScale);
if (t < 1f) if (t < 1f)
@ -218,6 +214,8 @@ namespace NewHorizons.Components.SizeControllers
public void StartCollapse() public void StartCollapse()
{ {
if (_isCollapsing) return;
Logger.LogVerbose($"{gameObject.transform.root.name} started collapse"); Logger.LogVerbose($"{gameObject.transform.root.name} started collapse");
_isCollapsing = true; _isCollapsing = true;
@ -230,6 +228,8 @@ namespace NewHorizons.Components.SizeControllers
public void StopCollapse() public void StopCollapse()
{ {
if (!_isCollapsing) return;
Logger.LogVerbose($"{gameObject.transform.root.name} stopped collapse"); Logger.LogVerbose($"{gameObject.transform.root.name} stopped collapse");
_isCollapsing = false; _isCollapsing = false;
@ -240,6 +240,8 @@ namespace NewHorizons.Components.SizeControllers
public void StartSupernova() public void StartSupernova()
{ {
if (_isSupernova) return;
Logger.LogVerbose($"{gameObject.transform.root.name} started supernova"); Logger.LogVerbose($"{gameObject.transform.root.name} started supernova");
SupernovaStart.Invoke(); SupernovaStart.Invoke();
@ -248,10 +250,14 @@ namespace NewHorizons.Components.SizeControllers
_supernovaStartTime = Time.time; _supernovaStartTime = Time.time;
if (atmosphere != null) atmosphere.SetActive(false); if (atmosphere != null) atmosphere.SetActive(false);
if (_destructionVolume != null) _destructionVolume._deathType = DeathType.Supernova; if (_destructionVolume != null) _destructionVolume._deathType = DeathType.Supernova;
if (_proxy != null) _proxy.StartSupernova();
} }
public void StopSupernova() public void StopSupernova()
{ {
if (!_isSupernova) return;
Logger.LogVerbose($"{gameObject.transform.root.name} stopped supernova"); Logger.LogVerbose($"{gameObject.transform.root.name} stopped supernova");
supernova.enabled = false; supernova.enabled = false;
@ -267,12 +273,12 @@ namespace NewHorizons.Components.SizeControllers
transform.localScale = Vector3.one; transform.localScale = Vector3.one;
supernova._surface._materials[0] = _surfaceMaterial; supernova._surface._materials[0] = _surfaceMaterial;
supernova._surface.transform.localScale = Vector3.one; supernova._surface.transform.localScale = Vector3.one;
if (_proxy != null) _proxy.StopSupernova();
} }
protected new void FixedUpdate() protected new void FixedUpdate()
{ {
_age += Time.deltaTime;
// If we've gone supernova and its been 45 seconds that means it has faded out and is gone // If we've gone supernova and its been 45 seconds that means it has faded out and is gone
// The 45 is from the animation curve used for the supernova alpha // The 45 is from the animation curve used for the supernova alpha
if (_isSupernova) if (_isSupernova)
@ -285,6 +291,7 @@ namespace NewHorizons.Components.SizeControllers
{ {
base.FixedUpdate(); base.FixedUpdate();
UpdateMainSequence(); UpdateMainSequence();
if (WillExplode && (TimeLoop.GetMinutesElapsed() / lifespan) >= 1) StartCollapse();
} }
else else
{ {

View File

@ -17,7 +17,13 @@ namespace NewHorizons.External.Modules.VariableSize
/// Should this star explode after 22 minutes? /// Should this star explode after 22 minutes?
/// </summary> /// </summary>
[DefaultValue(true)] public bool goSupernova = true; [DefaultValue(true)] public bool goSupernova = true;
/// <summary>
/// How long this star will last until it supernovas.
/// </summary>
[DefaultValue(22f)]
public float lifespan = 22f;
/// <summary> /// <summary>
/// Should we add a star controller to this body? If you want clouds to work on a binary brown dwarf system, set this to false. /// Should we add a star controller to this body? If you want clouds to work on a binary brown dwarf system, set this to false.
/// </summary> /// </summary>