From c58bcb36b0835a1d8c254cf202d21efb5823aba8 Mon Sep 17 00:00:00 2001 From: Nick Date: Sun, 15 May 2022 02:34:46 -0400 Subject: [PATCH] Set up star evolution controller --- NewHorizons/Builder/Body/StarBuilder.cs | 54 ++++++--- .../SizeControllers/SizeController.cs | 2 +- .../StarAtmosphereSizeController.cs | 35 ------ .../StarEvolutionController.cs | 111 ++++++++++++++++++ .../VariableSize/VariableSizeModule.cs | 7 +- 5 files changed, 154 insertions(+), 55 deletions(-) delete mode 100644 NewHorizons/Components/SizeControllers/StarAtmosphereSizeController.cs create mode 100644 NewHorizons/Components/SizeControllers/StarEvolutionController.cs diff --git a/NewHorizons/Builder/Body/StarBuilder.cs b/NewHorizons/Builder/Body/StarBuilder.cs index bd10bd3a..e5e731bb 100644 --- a/NewHorizons/Builder/Body/StarBuilder.cs +++ b/NewHorizons/Builder/Body/StarBuilder.cs @@ -33,9 +33,10 @@ namespace NewHorizons.Builder.Body sunAudio.name = "Audio_Star"; + GameObject sunAtmosphere = null; if (starModule.HasAtmosphere) { - var sunAtmosphere = GameObject.Instantiate(GameObject.Find("Sun_Body/Atmosphere_SUN"), planetGO.transform); + sunAtmosphere = GameObject.Instantiate(GameObject.Find("Sun_Body/Atmosphere_SUN"), planetGO.transform); sunAtmosphere.transform.position = planetGO.transform.position; sunAtmosphere.transform.localScale = Vector3.one; sunAtmosphere.name = "Atmosphere_Star"; @@ -54,12 +55,6 @@ namespace NewHorizons.Builder.Body fog.transform.localScale = Vector3.one; fog.fogRadius = starModule.Size * OuterRadiusRatio; fog.lodFadeDistance = fog.fogRadius * (StarBuilder.OuterRadiusRatio - 1f); - if (starModule.Curve != null) - { - var controller = sunAtmosphere.AddComponent(); - controller.scaleCurve = starModule.ToAnimationCurve(); - controller.initialSize = starModule.Size; - } } var ambientLightGO = GameObject.Instantiate(GameObject.Find("Sun_Body/AmbientLight_SUN"), starGO.transform); @@ -130,20 +125,31 @@ namespace NewHorizons.Builder.Body starController.SunColor = lightColour; } - if (starModule.Curve != null) - { - var levelController = starGO.AddComponent(); - var curve = new AnimationCurve(); - foreach (var pair in starModule.Curve) - { - curve.AddKey(new Keyframe(pair.Time, starModule.Size * pair.Value)); - } - levelController._scaleCurve = curve; - } + var supernova = MakeSupernova(starGO); + + var controller = starGO.AddComponent(); + if(starModule.Curve != null) controller.scaleCurve = starModule.ToAnimationCurve(); + controller.size = starModule.Size; + controller.atmosphere = sunAtmosphere; + controller.supernova = supernova; return starController; } + public static GameObject MakeStarProxy(GameObject planetGO, StarModule starModule) + { + MakeStarGraphics(planetGO, null, starModule); + + if (starModule.Curve != null) + { + var controller = planetGO.AddComponent(); + controller.scaleCurve = starModule.ToAnimationCurve(); + controller.size = starModule.Size; + } + + return planetGO; + } + public static GameObject MakeStarGraphics(GameObject rootObject, Sector sector, StarModule starModule) { if (_colorOverTime == null) _colorOverTime = ImageUtilities.GetTexture(Main.Instance, "AssetBundle/StarColorOverTime.png"); @@ -191,5 +197,19 @@ namespace NewHorizons.Builder.Body return starGO; } + + public static SupernovaEffectController MakeSupernova(GameObject starGO) + { + var supernovaGO = GameObject.Find("Sun_Body/Sector_SUN/Effects_SUN/Supernova").InstantiateInactive(); + supernovaGO.transform.SetParent(starGO.transform); + supernovaGO.transform.localPosition = Vector3.zero; + + var supernova = supernovaGO.GetComponent(); + supernova._surface = starGO.GetComponentInChildren(); + + supernovaGO.SetActive(true); + + return supernova; + } } } diff --git a/NewHorizons/Components/SizeControllers/SizeController.cs b/NewHorizons/Components/SizeControllers/SizeController.cs index 4a071a2f..35fbeee7 100644 --- a/NewHorizons/Components/SizeControllers/SizeController.cs +++ b/NewHorizons/Components/SizeControllers/SizeController.cs @@ -10,7 +10,7 @@ namespace NewHorizons.Components.SizeControllers public class SizeController : MonoBehaviour { public AnimationCurve scaleCurve; - public float CurrentScale { get; private set; } + public float CurrentScale { get; protected set; } public float size = 1f; protected void FixedUpdate() diff --git a/NewHorizons/Components/SizeControllers/StarAtmosphereSizeController.cs b/NewHorizons/Components/SizeControllers/StarAtmosphereSizeController.cs deleted file mode 100644 index be295f0b..00000000 --- a/NewHorizons/Components/SizeControllers/StarAtmosphereSizeController.cs +++ /dev/null @@ -1,35 +0,0 @@ -using NewHorizons.Builder.Body; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using UnityEngine; - -namespace NewHorizons.Components.SizeControllers -{ - public class StarAtmosphereSizeController : SizeController - { - private PlanetaryFogController fog; - public float initialSize; - private MeshRenderer[] atmosphereRenderers; - - void Awake() - { - fog = GetComponentInChildren(); - atmosphereRenderers = transform.Find("AtmoSphere").GetComponentsInChildren(); - } - - protected new void FixedUpdate() - { - base.FixedUpdate(); - fog.fogRadius = initialSize * CurrentScale * StarBuilder.OuterRadiusRatio; - fog.lodFadeDistance = initialSize * CurrentScale * (StarBuilder.OuterRadiusRatio - 1f); - foreach (var lod in atmosphereRenderers) - { - lod.material.SetFloat("_InnerRadius", initialSize * CurrentScale); - lod.material.SetFloat("_OuterRadius", initialSize * CurrentScale * StarBuilder.OuterRadiusRatio); - } - } - } -} diff --git a/NewHorizons/Components/SizeControllers/StarEvolutionController.cs b/NewHorizons/Components/SizeControllers/StarEvolutionController.cs new file mode 100644 index 00000000..07a86bb0 --- /dev/null +++ b/NewHorizons/Components/SizeControllers/StarEvolutionController.cs @@ -0,0 +1,111 @@ +using NewHorizons.Builder.Body; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using UnityEngine; + +namespace NewHorizons.Components.SizeControllers +{ + public class StarEvolutionController : SizeController + { + public GameObject atmosphere; + public SupernovaEffectController supernova; + + private PlanetaryFogController _fog; + private MeshRenderer[] _atmosphereRenderers; + + private bool _isCollapsing; + private float _collapseStartSize; + private float _collapseTimer; + + public float collapseTime = 5f; + + private bool _isSupernova; + private float _supernovaStartTime; + + private Material _collapseStartSurfaceMaterial; + private Material _collapseEndSurfaceMaterial; + + void Awake() + { + var sun = GameObject.FindObjectOfType(); + _collapseStartSurfaceMaterial = sun._collapseStartSurfaceMaterial; + _collapseEndSurfaceMaterial = sun._collapseEndSurfaceMaterial; + + if (atmosphere != null) + { + _fog = atmosphere.GetComponentInChildren(); + _atmosphereRenderers = atmosphere.transform.Find("AtmoSphere").GetComponentsInChildren(); + } + } + + public void Die() + { + _isCollapsing = true; + _collapseStartSize = CurrentScale; + _collapseTimer = 0f; + } + + protected new void FixedUpdate() + { + // If we've gone supernova and its been 30 seconds that means it has faded out and is gone + if (_isSupernova) + { + transform.localScale = Vector3.one; + if (Time.time > _supernovaStartTime + 30f) + { + base.gameObject.SetActive(false); + } + return; + } + + if (!_isCollapsing) + { + base.FixedUpdate(); + } + else + { + // When its collapsing we directly take over the scale + var t = _collapseTimer / collapseTime; + CurrentScale = Mathf.Lerp(_collapseStartSize, 0, t); + transform.localScale = Vector3.one * CurrentScale; + _collapseTimer += Time.deltaTime; + + supernova._surface._materials[0].Lerp(this._collapseStartSurfaceMaterial, this._collapseEndSurfaceMaterial, t); + + // After the collapse is done we go supernova + if (_collapseTimer > collapseTime) + { + supernova.enabled = true; + _isSupernova = true; + _supernovaStartTime = Time.time; + atmosphere.SetActive(false); + return; + } + } + + // This is just all the scales stuff for the atmosphere effects + if (atmosphere != null) + { + atmosphere.transform.localScale = CurrentScale * Vector3.one; + } + + if (_fog != null) + { + _fog.fogRadius = CurrentScale * StarBuilder.OuterRadiusRatio; + _fog.lodFadeDistance = CurrentScale * (StarBuilder.OuterRadiusRatio - 1f); + } + + if (_atmosphereRenderers.Count() > 0) + { + foreach (var lod in _atmosphereRenderers) + { + lod.material.SetFloat("_InnerRadius", CurrentScale); + lod.material.SetFloat("_OuterRadius", CurrentScale * StarBuilder.OuterRadiusRatio); + } + } + } + } +} diff --git a/NewHorizons/External/VariableSize/VariableSizeModule.cs b/NewHorizons/External/VariableSize/VariableSizeModule.cs index 1901c7c8..5fd02def 100644 --- a/NewHorizons/External/VariableSize/VariableSizeModule.cs +++ b/NewHorizons/External/VariableSize/VariableSizeModule.cs @@ -20,9 +20,12 @@ namespace NewHorizons.External.VariableSize public AnimationCurve ToAnimationCurve(float size = 1f) { var curve = new AnimationCurve(); - foreach (var pair in this.Curve) + if(Curve != null) { - curve.AddKey(new Keyframe(pair.Time, size * pair.Value)); + foreach (var pair in this.Curve) + { + curve.AddKey(new Keyframe(pair.Time, size * pair.Value)); + } } return curve; }