diff --git a/NewHorizons/Builder/Body/StarBuilder.cs b/NewHorizons/Builder/Body/StarBuilder.cs index cc505a73..b77a072b 100644 --- a/NewHorizons/Builder/Body/StarBuilder.cs +++ b/NewHorizons/Builder/Body/StarBuilder.cs @@ -21,7 +21,7 @@ namespace NewHorizons.Builder.Body private static readonly int InnerRadius = Shader.PropertyToID("_InnerRadius"); private static readonly int OuterRadius = Shader.PropertyToID("_OuterRadius"); - public static StarController Make(GameObject planetGO, Sector sector, StarModule starModule, IModBehaviour mod) + public static StarController Make(GameObject planetGO, Sector sector, StarModule starModule, IModBehaviour mod, bool isStellarRemnant) { var starGO = MakeStarGraphics(planetGO, sector, starModule, mod); var ramp = starGO.GetComponentInChildren().sharedMaterial.GetTexture(ColorRamp); @@ -32,6 +32,7 @@ namespace NewHorizons.Builder.Body sunAudio.transform.Find("SurfaceAudio_Sun").GetComponent().maxDistance = starModule.size * 2f; var sunSurfaceAudio = sunAudio.GetComponentInChildren(); var surfaceAudio = sunSurfaceAudio.gameObject.AddComponent(); + surfaceAudio._size = starModule.size; GameObject.Destroy(sunSurfaceAudio); surfaceAudio.SetSector(sector); @@ -143,44 +144,48 @@ namespace NewHorizons.Builder.Body starController.ProxyShadowLight = proxyShadowLight; starController.Intensity = starModule.solarLuminosity; starController.SunColor = lightColour; + starController.IsStellarRemnant = isStellarRemnant; } - var supernova = MakeSupernova(starGO, starModule); - - starGO.SetActive(false); - var controller = starGO.AddComponent(); - if (starModule.curve != null) controller.SetScaleCurve(starModule.curve); - controller.size = starModule.size; - controller.atmosphere = sunAtmosphere; - controller.controller = starController; - controller.supernova = supernova; - controller.StartColour = starModule.tint; - controller.EndColour = starModule.endTint; - controller.SupernovaColour = starModule.supernovaTint; - controller.WillExplode = starModule.goSupernova; - controller.lifespan = starModule.lifespan; - controller.normalRamp = !string.IsNullOrEmpty(starModule.starRampTexture) ? ImageUtilities.GetTexture(mod, starModule.starRampTexture) : ramp; - controller._heatVolume = heatVolume.GetComponent(); - controller._destructionVolume = deathVolume.GetComponent(); - controller._planetDestructionVolume = planetDestructionVolume.GetComponent(); - controller._starFluidVolume = starFluidVolume; - starFluidVolume.SetStarEvolutionController(controller); - if (!string.IsNullOrEmpty(starModule.starCollapseRampTexture)) + if (!isStellarRemnant) { - controller.collapseRamp = ImageUtilities.GetTexture(mod, starModule.starCollapseRampTexture); - } - surfaceAudio.SetStarEvolutionController(controller); - starGO.SetActive(true); + var supernova = MakeSupernova(starGO, starModule); - // It fucking insists on this existing and its really annoying - var supernovaVolume = new GameObject("SupernovaVolumePlaceholder"); - supernovaVolume.transform.SetParent(starGO.transform); - supernovaVolume.layer = LayerMask.NameToLayer("BasicEffectVolume"); - var sphere = supernovaVolume.AddComponent(); - sphere.radius = 0f; - sphere.isTrigger = true; - supernovaVolume.AddComponent(); - supernova._supernovaVolume = supernovaVolume.AddComponent(); + starGO.SetActive(false); + var controller = starGO.AddComponent(); + if (starModule.curve != null) controller.SetScaleCurve(starModule.curve); + controller.size = starModule.size; + controller.atmosphere = sunAtmosphere; + controller.controller = starController; + controller.supernova = supernova; + controller.StartColour = starModule.tint; + controller.EndColour = starModule.endTint; + controller.SupernovaColour = starModule.supernovaTint; + controller.WillExplode = starModule.goSupernova; + controller.lifespan = starModule.lifespan; + controller.normalRamp = !string.IsNullOrEmpty(starModule.starRampTexture) ? ImageUtilities.GetTexture(mod, starModule.starRampTexture) : ramp; + controller._heatVolume = heatVolume.GetComponent(); + controller._destructionVolume = deathVolume.GetComponent(); + controller._planetDestructionVolume = planetDestructionVolume.GetComponent(); + controller._starFluidVolume = starFluidVolume; + starFluidVolume.SetStarEvolutionController(controller); + if (!string.IsNullOrEmpty(starModule.starCollapseRampTexture)) + { + controller.collapseRamp = ImageUtilities.GetTexture(mod, starModule.starCollapseRampTexture); + } + surfaceAudio.SetStarEvolutionController(controller); + starGO.SetActive(true); + + // It fucking insists on this existing and its really annoying + var supernovaVolume = new GameObject("SupernovaVolumePlaceholder"); + supernovaVolume.transform.SetParent(starGO.transform); + supernovaVolume.layer = LayerMask.NameToLayer("BasicEffectVolume"); + var sphere = supernovaVolume.AddComponent(); + sphere.radius = 0f; + sphere.isTrigger = true; + supernovaVolume.AddComponent(); + supernova._supernovaVolume = supernovaVolume.AddComponent(); + } var shockLayerRuleset = sector.gameObject.AddComponent(); shockLayerRuleset._type = ShockLayerRuleset.ShockType.Radial; @@ -309,7 +314,7 @@ namespace NewHorizons.Builder.Body var supernova = supernovaGO.GetComponent(); supernova._surface = starGO.GetComponentInChildren(); supernova._supernovaScale = new AnimationCurve(new Keyframe(0, 200, 0, 0, 1f / 3f, 1f / 3f), new Keyframe(45, starModule.supernovaSize, 1758.508f, 1758.508f, 1f / 3f, 1f / 3f)); - supernova._supernovaAlpha = new AnimationCurve(new Keyframe(5, 1, 0, 0, 1f / 3f, 1f / 3f), new Keyframe(15, 1.0002f, 0, 0, 1f / 3f, 1f / 3f), new Keyframe(50, 0, -0.0578f, 1 / 3f, -0.0578f, 1 / 3f) ); + supernova._supernovaAlpha = new AnimationCurve(new Keyframe(5, 1, 0, 0, 1f / 3f, 1f / 3f), new Keyframe(15, 1.0002f, 0, 0, 1f / 3f, 1f / 3f), new Keyframe(50, 0, -0.0578f, 1 / 3f, -0.0578f, 1 / 3f)); supernova._supernovaVolume = null; if (starModule.supernovaTint != null) diff --git a/NewHorizons/Components/SizeControllers/StarEvolutionController.cs b/NewHorizons/Components/SizeControllers/StarEvolutionController.cs index 0029ac06..aff59bfe 100644 --- a/NewHorizons/Components/SizeControllers/StarEvolutionController.cs +++ b/NewHorizons/Components/SizeControllers/StarEvolutionController.cs @@ -247,6 +247,8 @@ namespace NewHorizons.Components.SizeControllers _planetDestructionVolume.GetComponent().radius = Mathf.Lerp(0.8f, 1, t); } + if (_stellarRemnantController != null && Time.time > _supernovaStartTime + 15) _stellarRemnantController.PartiallyActivate(); + if (Time.time > _supernovaStartTime + supernovaTime) { if (_destructionVolume != null && _destructionVolume._shrinkingBodies.Count > 0) return; @@ -260,9 +262,9 @@ namespace NewHorizons.Components.SizeControllers if (controller != null) StarLightController.RemoveStar(controller); // Just turn off the star entirely - base.gameObject.SetActive(false); + transform.parent.gameObject.SetActive(false); - if (_stellarRemnantController != null) _stellarRemnantController.gameObject.SetActive(true); + if (_stellarRemnantController != null) _stellarRemnantController.FullyActivate(); if (start && _planetDestructionVolume != null) { diff --git a/NewHorizons/Components/StarController.cs b/NewHorizons/Components/StarController.cs index 4090a0f9..8f69f085 100644 --- a/NewHorizons/Components/StarController.cs +++ b/NewHorizons/Components/StarController.cs @@ -10,6 +10,7 @@ namespace NewHorizons.Components public ProxyShadowLight ProxyShadowLight; public float Intensity; public Color SunColor; + public bool IsStellarRemnant; public void Awake() { diff --git a/NewHorizons/Components/StarSurfaceAudioController.cs b/NewHorizons/Components/StarSurfaceAudioController.cs index e5833e4b..aab8d3ce 100644 --- a/NewHorizons/Components/StarSurfaceAudioController.cs +++ b/NewHorizons/Components/StarSurfaceAudioController.cs @@ -10,6 +10,7 @@ namespace NewHorizons.Components private StarEvolutionController _starEvolutionController; private OWAudioSource _audioSource; private float _fade; + public float _size; public void Start() { @@ -33,7 +34,7 @@ namespace NewHorizons.Components public void Update() { _fade = Mathf.MoveTowards(_fade, 1, Time.deltaTime * 0.2f); - float value = Mathf.Max(0.0f, Vector3.Distance(Locator.GetPlayerCamera().transform.position, this.transform.position) - _starEvolutionController.CurrentScale); + float value = Mathf.Max(0.0f, Vector3.Distance(Locator.GetPlayerCamera().transform.position, this.transform.position) - (_starEvolutionController != null ? _starEvolutionController.CurrentScale : _size)); float num = Mathf.InverseLerp(1600f, 100f, value); _audioSource.SetLocalVolume(num * num * _fade); } diff --git a/NewHorizons/Components/StellarRemnantController.cs b/NewHorizons/Components/StellarRemnantController.cs index 6cca270f..39a9e24f 100644 --- a/NewHorizons/Components/StellarRemnantController.cs +++ b/NewHorizons/Components/StellarRemnantController.cs @@ -8,12 +8,65 @@ namespace NewHorizons.Components { public class StellarRemnantController : MonoBehaviour { + private RemnantType _type = RemnantType.None; + private StarEvolutionController _starEvolutionController; private StellarRemnantController _proxy; + private StarController _starController; + + private float _surfaceGravity = 0; + private float _surfaceSize = 0; + private float _sphereOfInfluence = 0; + private float _alignmentRadius = 0; + + public RemnantType GetRemnantType() => _type; + public void SetRemnantType(RemnantType type) => _type = type; + + public void SetSurfaceGravity(float surfaceGravity) => _surfaceGravity = surfaceGravity; + public void SetSurfaceSize(float surfaceSize) => _surfaceSize = surfaceSize; + public void SetAlignmentRadius(float alignmentRadius) => _alignmentRadius = alignmentRadius; + public void SetSphereOfInfluence(float sphereOfInfluence) => _sphereOfInfluence = sphereOfInfluence; + public void SetStarController(StarController starController) => _starController = starController; + public void SetProxy(StellarRemnantController proxy) => _proxy = proxy; public void SetStarEvolutionController(StarEvolutionController controller) => _starEvolutionController = controller; + + public void PartiallyActivate() + { + if (!gameObject.activeSelf) gameObject.SetActive(true); + } + + public void FullyActivate() + { + if (!gameObject.activeSelf) gameObject.SetActive(true); + + var gravityVolume = this.GetAttachedOWRigidbody().GetAttachedGravityVolume(); + if (gravityVolume != null) + { + gravityVolume._alignmentRadius = _alignmentRadius; + gravityVolume._upperSurfaceRadius = _surfaceSize; + gravityVolume._surfaceAcceleration = _surfaceGravity; + } + var referenceFrameVolume = this.GetAttachedOWRigidbody()._attachedRFVolume; + if (referenceFrameVolume != null) + { + referenceFrameVolume.GetComponent().radius = _sphereOfInfluence * 2; + referenceFrameVolume._maxColliderRadius = _sphereOfInfluence * 2; + } + + if (_starController != null) StarLightController.AddStar(_starController); + } + + public enum RemnantType + { + None, + BlackHole, + NeutronStar, + WhiteDwarf, + Custom + } } } diff --git a/NewHorizons/Handlers/PlanetCreationHandler.cs b/NewHorizons/Handlers/PlanetCreationHandler.cs index 47f13001..dee59132 100644 --- a/NewHorizons/Handlers/PlanetCreationHandler.cs +++ b/NewHorizons/Handlers/PlanetCreationHandler.cs @@ -202,28 +202,7 @@ namespace NewHorizons.Handlers } else if (body.Config.isStellarRemnant) { - Logger.Log($"Creating stellar remnant for [{body.Config.name}]"); - try - { - var rb = existingPlanet.GetComponent(); - - var sector = SectorBuilder.Make(existingPlanet, rb, GetSphereOfInfluence(body)); - sector.name = $"StellarRemnant"; - - var stellarRemnantController = sector.gameObject.AddComponent(); - var starEvolutionController = existingPlanet.GetComponentInChildren(true); - stellarRemnantController.SetStarEvolutionController(starEvolutionController); - starEvolutionController.SetStellarRemnantController(stellarRemnantController); - - sector.gameObject.SetActive(false); - - SharedGenerateBody(body, existingPlanet, sector, rb); - } - catch (Exception ex) - { - Logger.LogError($"Couldn't make stellar remnant for [{body.Config.name}]:\n{ex}"); - return false; - } + //Skip } else { @@ -245,8 +224,7 @@ namespace NewHorizons.Handlers } else if (body.Config.isStellarRemnant) { - // If the star object isn't made yet do it later - _nextPassBodies.Add(body); + //Skip } else { @@ -523,7 +501,89 @@ namespace NewHorizons.Handlers if (body.Config.Star != null) { - StarLightController.AddStar(StarBuilder.Make(go, sector, body.Config.Star, body.Mod)); + StarLightController.AddStar(StarBuilder.Make(go, sector, body.Config.Star, body.Mod, body.Config.isStellarRemnant)); + if (!body.Config.isStellarRemnant) + { + Logger.Log($"Creating stellar remnant for [{body.Config.name}]"); + try + { + var size = body.Config.Star.size; + var srBody = Main.BodyDict[body.Config.starSystem].Where(x => x.Config.name == body.Config.name && body.Config.isStellarRemnant).FirstOrDefault(); + var srSector = SectorBuilder.Make(go, rb, GetSphereOfInfluence(body)); + srSector.name = "StellarRemnant"; + var ss = srSector.GetComponent(); + + var stellarRemnantController = srSector.gameObject.AddComponent(); + var starEvolutionController = go.GetComponentInChildren(true); + stellarRemnantController.SetStarEvolutionController(starEvolutionController); + starEvolutionController.SetStellarRemnantController(stellarRemnantController); + + srSector.gameObject.SetActive(false); + + if (srBody != null) + { + stellarRemnantController.SetRemnantType(StellarRemnantController.RemnantType.Custom); + stellarRemnantController.SetSurfaceSize(srBody.Config.Base.surfaceSize); + stellarRemnantController.SetSurfaceGravity(srBody.Config.Base.surfaceGravity); + var srSphereOfInfluence = GetSphereOfInfluence(srBody); + stellarRemnantController.SetSphereOfInfluence(srSphereOfInfluence); + ss.radius = srSphereOfInfluence + 10; + var alignmentRadius = srBody.Config.Atmosphere?.clouds?.outerCloudRadius ?? 1.5f * srBody.Config.Base.surfaceSize; + if (srBody.Config.Base.surfaceGravity == 0) alignmentRadius = 0; + stellarRemnantController.SetAlignmentRadius(alignmentRadius); + SharedGenerateBody(srBody, go, srSector, rb); + } + // Black Hole + else if (size > 4000) + { + stellarRemnantController.SetRemnantType(StellarRemnantController.RemnantType.BlackHole); + var bhSurfaceSize = size * 0.015f; + stellarRemnantController.SetSurfaceSize(bhSurfaceSize); + stellarRemnantController.SetSurfaceGravity(body.Config.Base.surfaceGravity * 4); + stellarRemnantController.SetSphereOfInfluence(bhSurfaceSize * 2); + ss.radius = (bhSurfaceSize * 2) + 10; + stellarRemnantController.SetAlignmentRadius(bhSurfaceSize * 1.5f); + SingularityBuilder.MakeBlackHole(go, srSector, Vector3.zero, bhSurfaceSize, true, string.Empty, new External.Modules.VariableSize.VariableSizeModule.TimeValuePair[0]); + } + // Neutron Star + else if (2000 < size && size < 3000) + { + stellarRemnantController.SetRemnantType(StellarRemnantController.RemnantType.NeutronStar); + var nsSurfaceSize = size * 0.03f; + stellarRemnantController.SetSurfaceSize(nsSurfaceSize); + stellarRemnantController.SetSurfaceGravity(body.Config.Base.surfaceGravity * 2); + stellarRemnantController.SetSphereOfInfluence(nsSurfaceSize * 2); + ss.radius = (nsSurfaceSize * 2) + 10; + stellarRemnantController.SetAlignmentRadius(nsSurfaceSize * 1.5f); + stellarRemnantController.SetStarController(StarBuilder.Make(go, srSector, new External.Modules.VariableSize.StarModule + { + size = nsSurfaceSize, + tint = MColor.white + }, body.Mod, true)); + } + // White Dwarf + else + { + stellarRemnantController.SetRemnantType(StellarRemnantController.RemnantType.WhiteDwarf); + var wdSurfaceSize = size * 0.15f; + stellarRemnantController.SetSurfaceSize(wdSurfaceSize); + stellarRemnantController.SetSurfaceGravity(body.Config.Base.surfaceGravity * 1.4f); + stellarRemnantController.SetSphereOfInfluence(wdSurfaceSize * 2); + ss.radius = (wdSurfaceSize * 2) + 10; + stellarRemnantController.SetAlignmentRadius(wdSurfaceSize * 1.5f); + stellarRemnantController.SetStarController(StarBuilder.Make(go, srSector, new External.Modules.VariableSize.StarModule + { + size = wdSurfaceSize, + tint = MColor.white, + endTint = MColor.black + }, body.Mod, true)); + } + } + catch (Exception ex) + { + Logger.LogError($"Couldn't make stellar remnant for [{body.Config.name}]:\n{ex}"); + } + } } if (body.Config?.Bramble != null)