From ebe8bd9c1ce1e06610ace7c850209d932ed6182b Mon Sep 17 00:00:00 2001 From: Nick Date: Mon, 16 May 2022 18:41:03 -0400 Subject: [PATCH] Supernovas work locally Proxies are broken Star atmospheres are broken --- NewHorizons/Builder/Body/ProxyBuilder.cs | 4 +- NewHorizons/Builder/Body/StarBuilder.cs | 29 +++-- .../StarEvolutionController.cs | 105 ++++++++++++++++-- .../External/VariableSize/StarModule.cs | 1 + NewHorizons/schema.json | 4 + 5 files changed, 119 insertions(+), 24 deletions(-) diff --git a/NewHorizons/Builder/Body/ProxyBuilder.cs b/NewHorizons/Builder/Body/ProxyBuilder.cs index 96c7f8e1..52e39c11 100644 --- a/NewHorizons/Builder/Body/ProxyBuilder.cs +++ b/NewHorizons/Builder/Body/ProxyBuilder.cs @@ -22,7 +22,7 @@ namespace NewHorizons.Builder.Body private static readonly string _whiteHolePath = "TowerTwin_Body/Sector_TowerTwin/Sector_Tower_HGT/Interactables_Tower_HGT/Interactables_Tower_CT/Prefab_NOM_WarpTransmitter/WhiteHole/WhiteHoleSingularity"; - public static void Make(GameObject gameObject, NewHorizonsBody body) + public static void Make(GameObject planetGO, NewHorizonsBody body) { if (lavaMaterial == null) lavaMaterial = SearchUtilities.FindObjectOfTypeAndName("VolcanicMoon_Body").transform.Find("LavaSphere").GetComponent().material; @@ -57,7 +57,7 @@ namespace NewHorizons.Builder.Body } if (body.Config.Star != null) { - var starGO = StarBuilder.MakeStarGraphics(newProxy, null, body.Config.Star); + var starGO = StarBuilder.MakeStarProxy(planetGO, newProxy, body.Config.Star); if (body.Config.Star.Curve != null) AddSizeController(starGO, body.Config.Star.Curve, body.Config.Star.Size); diff --git a/NewHorizons/Builder/Body/StarBuilder.cs b/NewHorizons/Builder/Body/StarBuilder.cs index e5e731bb..cdbca3a9 100644 --- a/NewHorizons/Builder/Body/StarBuilder.cs +++ b/NewHorizons/Builder/Body/StarBuilder.cs @@ -36,7 +36,7 @@ namespace NewHorizons.Builder.Body GameObject sunAtmosphere = null; if (starModule.HasAtmosphere) { - sunAtmosphere = GameObject.Instantiate(GameObject.Find("Sun_Body/Atmosphere_SUN"), planetGO.transform); + sunAtmosphere = GameObject.Instantiate(GameObject.Find("Sun_Body/Atmosphere_SUN"), starGO.transform); sunAtmosphere.transform.position = planetGO.transform.position; sunAtmosphere.transform.localScale = Vector3.one; sunAtmosphere.name = "Atmosphere_Star"; @@ -132,22 +132,30 @@ namespace NewHorizons.Builder.Body controller.size = starModule.Size; controller.atmosphere = sunAtmosphere; controller.supernova = supernova; + controller.startColour = starModule.Tint; + controller.startColour = starModule.EndTint; return starController; } - public static GameObject MakeStarProxy(GameObject planetGO, StarModule starModule) + public static GameObject MakeStarProxy(GameObject planet, GameObject proxyGO, StarModule starModule) { - MakeStarGraphics(planetGO, null, starModule); + var starGO = new GameObject("Star"); + starGO.transform.parent = proxyGO.transform; + starGO.transform.localPosition = Vector3.zero; - if (starModule.Curve != null) - { - var controller = planetGO.AddComponent(); - controller.scaleCurve = starModule.ToAnimationCurve(); - controller.size = starModule.Size; - } + MakeStarGraphics(proxyGO, null, starModule); - return planetGO; + var supernova = MakeSupernova(starGO); + + var controller = starGO.AddComponent(); + if (starModule.Curve != null) controller.scaleCurve = starModule.ToAnimationCurve(); + controller.size = starModule.Size; + controller.supernova = supernova; + + planet.GetComponentInChildren().SetProxy(controller); + + return proxyGO; } public static GameObject MakeStarGraphics(GameObject rootObject, Sector sector, StarModule starModule) @@ -206,6 +214,7 @@ namespace NewHorizons.Builder.Body var supernova = supernovaGO.GetComponent(); supernova._surface = starGO.GetComponentInChildren(); + supernova._supernovaVolume = null; supernovaGO.SetActive(true); diff --git a/NewHorizons/Components/SizeControllers/StarEvolutionController.cs b/NewHorizons/Components/SizeControllers/StarEvolutionController.cs index 58953a9f..e59e9480 100644 --- a/NewHorizons/Components/SizeControllers/StarEvolutionController.cs +++ b/NewHorizons/Components/SizeControllers/StarEvolutionController.cs @@ -1,4 +1,5 @@ using NewHorizons.Builder.Body; +using NewHorizons.Utility; using System; using System.Collections.Generic; using System.Linq; @@ -13,32 +14,91 @@ namespace NewHorizons.Components.SizeControllers public GameObject atmosphere; public SupernovaEffectController supernova; + public MColor startColour; + public MColor endColour; + + private Color _startColour; + private Color _endColour; + private PlanetaryFogController _fog; private MeshRenderer[] _atmosphereRenderers; + private HeatHazardVolume _heatVolume; + private DestructionVolume _destructionVolume; private bool _isCollapsing; private float _collapseStartSize; private float _collapseTimer; - public float collapseTime = 5f; + public float collapseTime = 5f; // seconds + public float lifespan = 22f; // minutes + private float _age; private bool _isSupernova; private float _supernovaStartTime; private Material _collapseStartSurfaceMaterial; private Material _collapseEndSurfaceMaterial; + private Material _startSurfaceMaterial; + private Material _endSurfaceMaterial; + + private StarEvolutionController _proxy; void Awake() { var sun = GameObject.FindObjectOfType(); - _collapseStartSurfaceMaterial = sun._collapseStartSurfaceMaterial; - _collapseEndSurfaceMaterial = sun._collapseEndSurfaceMaterial; + _collapseStartSurfaceMaterial = new Material(sun._collapseStartSurfaceMaterial); + _collapseEndSurfaceMaterial = new Material(sun._collapseEndSurfaceMaterial); + _startSurfaceMaterial = new Material(sun._startSurfaceMaterial); + _endSurfaceMaterial = new Material(sun._endSurfaceMaterial); + + // Copy over the material that was set in star builder + _collapseStartSurfaceMaterial.SetTexture("_ColorRamp", supernova._surface.sharedMaterial.GetTexture("_ColorRamp")); + _collapseEndSurfaceMaterial.SetTexture("_ColorRamp", supernova._surface.sharedMaterial.GetTexture("_ColorRamp")); + _startSurfaceMaterial.SetTexture("_ColorRamp", supernova._surface.sharedMaterial.GetTexture("_ColorRamp")); + _endSurfaceMaterial.SetTexture("_ColorRamp", supernova._surface.sharedMaterial.GetTexture("_ColorRamp")); + + if (startColour == null) + { + _startColour = _startSurfaceMaterial.color; + } + else + { + _startColour = startColour.ToColor(); + _startSurfaceMaterial.color = _startColour; + } + + if (endColour == null) + { + _endColour = _endSurfaceMaterial.color; + } + else + { + _endColour = endColour.ToColor(); + _endSurfaceMaterial.color = _endColour; + } + + + _heatVolume = GetComponentInChildren(); + _destructionVolume = GetComponentInChildren(); if (atmosphere != null) { _fog = atmosphere.GetComponentInChildren(); _atmosphereRenderers = atmosphere.transform.Find("AtmoSphere").GetComponentsInChildren(); } + + GlobalMessenger.AddListener("TriggerSupernova", Die); + } + + public void OnDestroy() + { + GlobalMessenger.RemoveListener("TriggerSupernova", Die); + } + + public void SetProxy(StarEvolutionController proxy) + { + _proxy = proxy; + _proxy.supernova.SetIsProxy(true); } public void Die() @@ -46,24 +106,44 @@ namespace NewHorizons.Components.SizeControllers _isCollapsing = true; _collapseStartSize = CurrentScale; _collapseTimer = 0f; + + if (_proxy != null) _proxy.Die(); } protected new void FixedUpdate() { - // If we've gone supernova and its been 60 seconds that means it has faded out and is gone + _age += Time.deltaTime; + + var ageValue = _age / (lifespan * 60f); + + // 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 if (_isSupernova) { + // Reset the scale back to normal bc now its just the supernova scaling itself + destruction and heat volumes transform.localScale = Vector3.one; - if (Time.time > _supernovaStartTime + 60f) + + // Make the destruction volume scale slightly smaller so you really have to be in the supernova to die + if(_destructionVolume != null) _destructionVolume.transform.localScale = Vector3.one * supernova.GetSupernovaRadius() * 0.9f; + if(_heatVolume != null) _heatVolume.transform.localScale = Vector3.one * supernova.GetSupernovaRadius(); + + if (Time.time > _supernovaStartTime + 45f) { + // Just turn off the star entirely base.gameObject.SetActive(false); } return; - } + } + + Color currentColour; if (!_isCollapsing) { base.FixedUpdate(); + + currentColour = Color.Lerp(_startColour, _endColour, ageValue); + + supernova._surface._materials[0].Lerp(_startSurfaceMaterial, _endSurfaceMaterial, ageValue); } else { @@ -73,7 +153,9 @@ namespace NewHorizons.Components.SizeControllers transform.localScale = Vector3.one * CurrentScale; _collapseTimer += Time.deltaTime; - supernova._surface._materials[0].Lerp(this._collapseStartSurfaceMaterial, this._collapseEndSurfaceMaterial, t); + currentColour = Color.Lerp(_endColour, Color.white, t); + + supernova._surface._materials[0].Lerp(_collapseStartSurfaceMaterial, _collapseEndSurfaceMaterial, t); // After the collapse is done we go supernova if (_collapseTimer > collapseTime) @@ -82,20 +164,18 @@ namespace NewHorizons.Components.SizeControllers _isSupernova = true; _supernovaStartTime = Time.time; atmosphere.SetActive(false); + _destructionVolume._deathType = DeathType.Supernova; 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); + + _fog.fogTint = currentColour; } if (_atmosphereRenderers.Count() > 0) @@ -104,6 +184,7 @@ namespace NewHorizons.Components.SizeControllers { lod.material.SetFloat("_InnerRadius", CurrentScale); lod.material.SetFloat("_OuterRadius", CurrentScale * StarBuilder.OuterRadiusRatio); + lod.material.SetColor("_SkyColor", currentColour); } } } diff --git a/NewHorizons/External/VariableSize/StarModule.cs b/NewHorizons/External/VariableSize/StarModule.cs index 7808f880..1f3ee87a 100644 --- a/NewHorizons/External/VariableSize/StarModule.cs +++ b/NewHorizons/External/VariableSize/StarModule.cs @@ -11,6 +11,7 @@ namespace NewHorizons.External.VariableSize { public float Size { get; set; } = 2000f; public MColor Tint { get; set; } + public MColor EndTint { get; set; } public MColor SolarFlareTint { get; set; } public MColor LightTint { get; set; } public float SolarLuminosity { get; set; } = 1f; diff --git a/NewHorizons/schema.json b/NewHorizons/schema.json index ad9ab075..1a9db790 100644 --- a/NewHorizons/schema.json +++ b/NewHorizons/schema.json @@ -596,6 +596,10 @@ "$ref": "#/$defs/color", "description": "Colour of the star." }, + "endTint": { + "$ref": "#/$defs/color", + "description": "Colour of the star at the end of its life." + }, "solarFlareTint": { "$ref": "#/$defs/color", "description": "Colour of the solar flares. The shader is a bit weird so the value you put won't exactly reflect what you see. Try experimenting with different colours to see what works."