diff --git a/NewHorizons/Builder/Body/SingularityBuilder.cs b/NewHorizons/Builder/Body/SingularityBuilder.cs index 1676879a..e6410e92 100644 --- a/NewHorizons/Builder/Body/SingularityBuilder.cs +++ b/NewHorizons/Builder/Body/SingularityBuilder.cs @@ -95,7 +95,7 @@ namespace NewHorizons.Builder.Body } public static GameObject MakeBlackHole(GameObject planetGO, Sector sector, Vector3 localPosition, float size, - bool hasDestructionVolume, string targetSolarSystem, VariableSizeModule.TimeValuePair[] curve, bool makeAudio = true) + bool hasDestructionVolume, string targetSolarSystem, VariableSizeModule.TimeValuePair[] curve = null, bool makeAudio = true) { var blackHole = new GameObject("BlackHole"); blackHole.SetActive(false); diff --git a/NewHorizons/Builder/Body/StellarRemnantBuilder.cs b/NewHorizons/Builder/Body/StellarRemnantBuilder.cs new file mode 100644 index 00000000..85ad7bea --- /dev/null +++ b/NewHorizons/Builder/Body/StellarRemnantBuilder.cs @@ -0,0 +1,134 @@ +using NewHorizons.Builder.General; +using NewHorizons.Components; +using NewHorizons.Components.SizeControllers; +using NewHorizons.External.Configs; +using NewHorizons.External.Modules.VariableSize; +using NewHorizons.Handlers; +using NewHorizons.Utility; +using OWML.Common; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using UnityEngine; +using Logger = NewHorizons.Utility.Logger; + +namespace NewHorizons.Builder.Body +{ + public static class StellarRemnantBuilder + { + public static void Make(GameObject go, OWRigidbody rb, PlanetConfig config, IModBehaviour mod, float sphereOfInfluence) + { + Logger.Log($"Creating stellar remnant for [{config.name}]"); + try + { + var starModule = config.Star; + var size = starModule.size; + var sector = SectorBuilder.Make(go, rb, sphereOfInfluence); + sector.name = "StellarRemnant"; + var ss = sector.GetComponent(); + + var stellarRemnantController = sector.gameObject.AddComponent(); + var starEvolutionController = go.GetComponentInChildren(true); + stellarRemnantController.SetStarEvolutionController(starEvolutionController); + starEvolutionController.SetStellarRemnantController(stellarRemnantController); + + sector.gameObject.SetActive(false); + + if (starModule.stellarRemnant != null) + { + var srConfig = starModule.stellarRemnant.ConvertToPlanetConfig(config); + var srBody = new NewHorizonsBody(srConfig, mod); + stellarRemnantController.SetRemnantType(StellarRemnantType.Custom); + stellarRemnantController.SetSurfaceSize(starModule.stellarRemnant.Base.surfaceSize); + stellarRemnantController.SetSurfaceGravity(starModule.stellarRemnant.Base.surfaceGravity); + stellarRemnantController.SetSiderealPeriod(starModule.stellarRemnant.siderealPeriod); + var srSphereOfInfluence = PlanetCreationHandler.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); + PlanetCreationHandler.SharedGenerateBody(srBody, go, sector, rb, true); + } + else + { + var remnantType = starModule.stellarRemnantType; + if (remnantType == StellarRemnantType.Default) + { + if (size > 4000) + remnantType = StellarRemnantType.BlackHole; + else if (2000 < size && size < 3000) + remnantType = StellarRemnantType.NeutronStar; + else + remnantType = StellarRemnantType.WhiteDwarf; + } + stellarRemnantController.SetRemnantType(starModule.stellarRemnantType); + switch (starModule.stellarRemnantType) + { + case StellarRemnantType.WhiteDwarf: + var wdSurfaceSize = size / 10; + stellarRemnantController.SetSurfaceSize(wdSurfaceSize); + stellarRemnantController.SetSurfaceGravity(config.Base.surfaceGravity * 1.4f); + stellarRemnantController.SetSphereOfInfluence(wdSurfaceSize * 2); + ss.radius = (wdSurfaceSize * 2) + 10; + stellarRemnantController.SetAlignmentRadius(wdSurfaceSize * 1.5f); + stellarRemnantController.SetSiderealPeriod(config.Orbit.siderealPeriod); + stellarRemnantController.SetStarController(StarBuilder.Make(go, sector, new StarModule + { + size = wdSurfaceSize, + tint = MColor.white, + endTint = MColor.black + }, mod, true)); + break; + case StellarRemnantType.NeutronStar: + var nsSurfaceSize = size / 50; + stellarRemnantController.SetSurfaceSize(nsSurfaceSize); + stellarRemnantController.SetSurfaceGravity(config.Base.surfaceGravity * 2); + stellarRemnantController.SetSphereOfInfluence(nsSurfaceSize * 2); + ss.radius = (nsSurfaceSize * 2) + 10; + stellarRemnantController.SetAlignmentRadius(nsSurfaceSize * 1.5f); + stellarRemnantController.SetSiderealPeriod(1); + stellarRemnantController.SetStarController(StarBuilder.Make(go, sector, new StarModule + { + size = nsSurfaceSize, + tint = MColor.cyan + }, mod, true)); + break; + case StellarRemnantType.Pulsar: + var psSurfaceSize = size / 50; + stellarRemnantController.SetSurfaceSize(psSurfaceSize); + stellarRemnantController.SetSurfaceGravity(config.Base.surfaceGravity * 2); + stellarRemnantController.SetSphereOfInfluence(psSurfaceSize * 2); + ss.radius = (psSurfaceSize * 2) + 10; + stellarRemnantController.SetAlignmentRadius(psSurfaceSize * 1.5f); + stellarRemnantController.SetSiderealPeriod(0.5f); + stellarRemnantController.SetStarController(StarBuilder.Make(go, sector, new StarModule + { + size = psSurfaceSize, + tint = MColor.cyan + }, mod, true)); + break; + case StellarRemnantType.BlackHole: + var bhSurfaceSize = size / 100; + stellarRemnantController.SetSurfaceSize(bhSurfaceSize); + stellarRemnantController.SetSurfaceGravity(config.Base.surfaceGravity * 4); + stellarRemnantController.SetSphereOfInfluence(bhSurfaceSize * 2); + stellarRemnantController.SetSiderealPeriod(0.25f); + ss.radius = (bhSurfaceSize * 2) + 10; + stellarRemnantController.SetAlignmentRadius(bhSurfaceSize * 1.5f); + SingularityBuilder.MakeBlackHole(go, sector, Vector3.zero, bhSurfaceSize, true, string.Empty); + break; + default: + break; + } + } + } + catch (Exception ex) + { + Logger.LogError($"Couldn't make stellar remnant for [{config.name}]:\n{ex}"); + } + } + } +} diff --git a/NewHorizons/Components/StellarRemnantController.cs b/NewHorizons/Components/StellarRemnantController.cs index 9f881952..7abc8659 100644 --- a/NewHorizons/Components/StellarRemnantController.cs +++ b/NewHorizons/Components/StellarRemnantController.cs @@ -1,4 +1,5 @@ using NewHorizons.Components.SizeControllers; +using NewHorizons.External.Modules.VariableSize; using System; using System.Collections.Generic; using System.Linq; @@ -8,7 +9,7 @@ namespace NewHorizons.Components { public class StellarRemnantController : MonoBehaviour { - private RemnantType _type = RemnantType.None; + private StellarRemnantType _type = StellarRemnantType.Default; private StarEvolutionController _starEvolutionController; @@ -22,8 +23,8 @@ namespace NewHorizons.Components private float _sphereOfInfluence = 0; private float _alignmentRadius = 0; - public RemnantType GetRemnantType() => _type; - public void SetRemnantType(RemnantType type) => _type = type; + public StellarRemnantType GetRemnantType() => _type; + public void SetRemnantType(StellarRemnantType type) => _type = type; public void SetSiderealPeriod(float siderealPeriod) => _siderealPeriod = siderealPeriod; public void SetSurfaceGravity(float surfaceGravity) => _surfaceGravity = surfaceGravity; @@ -68,14 +69,5 @@ namespace NewHorizons.Components if (_starController != null) StarLightController.AddStar(_starController); } - - public enum RemnantType - { - None, - BlackHole, - NeutronStar, - WhiteDwarf, - Custom - } } } diff --git a/NewHorizons/External/Configs/PlanetConfig.cs b/NewHorizons/External/Configs/PlanetConfig.cs index 0801d9e7..057aba8f 100644 --- a/NewHorizons/External/Configs/PlanetConfig.cs +++ b/NewHorizons/External/Configs/PlanetConfig.cs @@ -100,11 +100,6 @@ namespace NewHorizons.External.Configs /// public bool isQuantumState; - /// - /// Does this config describe a stellar remnant of a custom star defined in another file? - /// - public bool isStellarRemnant; - /// /// Add lava to this planet /// diff --git a/NewHorizons/External/Modules/VariableSize/StarModule.cs b/NewHorizons/External/Modules/VariableSize/StarModule.cs index 2c709162..2833a1c3 100644 --- a/NewHorizons/External/Modules/VariableSize/StarModule.cs +++ b/NewHorizons/External/Modules/VariableSize/StarModule.cs @@ -1,7 +1,10 @@ using System.ComponentModel; using System.ComponentModel.DataAnnotations; +using System.Runtime.Serialization; +using NewHorizons.External.Configs; using NewHorizons.Utility; using Newtonsoft.Json; +using Newtonsoft.Json.Converters; namespace NewHorizons.External.Modules.VariableSize { @@ -84,5 +87,141 @@ namespace NewHorizons.External.Modules.VariableSize /// [DefaultValue(50000f)] [Range(0f, double.MaxValue)] public float lightRadius = 50000f; + + /// + /// The type of death your star will have. + /// + [DefaultValue("default")] public StellarDeathType stellarDeathType = StellarDeathType.Default; + + /// + /// The type of stellar remnant your star will leave behind. + /// + [DefaultValue("default")] public StellarRemnantType stellarRemnantType = StellarRemnantType.Default; + + /// + /// If you want a custom stellar remnant, use this. + /// + public StellarRemnantModule stellarRemnant; + + public class StellarRemnantModule + { + /// + /// Describes this Body's atmosphere + /// + public AtmosphereModule Atmosphere; + + /// + /// Base Properties of this Body + /// + public BaseModule Base; + + /// + /// Add bramble nodes to this planet and/or make this planet a bramble dimension + /// + public BrambleModule Bramble; + + /// + /// Add a cloaking field to this planet + /// + public CloakModule Cloak; + + /// + /// Add funnel from this planet to another + /// + public FunnelModule Funnel; + + /// + /// Generate the surface of this planet using a heightmap + /// + public HeightMapModule HeightMap; + + /// + /// Add lava to this planet + /// + public LavaModule Lava; + + /// + /// Procedural Generation + /// + public ProcGenModule ProcGen; + + /// + /// Spawn various objects on this body + /// + public PropModule Props; + + /// + /// A list of paths to child GameObjects to destroy on this planet + /// + public string[] removeChildren; + + /// + /// Creates a ring around the planet + /// + public RingModule Ring; + + /// + /// Add sand to this planet + /// + public SandModule Sand; + + /// + /// Rotation period in minutes. + /// + public float siderealPeriod; + + /// + /// Make this body a star + /// + public StarModule Star; + + /// + /// Add water to this planet + /// + public WaterModule Water; + + public PlanetConfig ConvertToPlanetConfig(PlanetConfig star) + { + PlanetConfig planetConfig = new PlanetConfig(); + planetConfig.name = star.name; + planetConfig.starSystem = star.starSystem; + planetConfig.Atmosphere = Atmosphere; + planetConfig.Base = Base; + planetConfig.Bramble = Bramble; + planetConfig.Cloak = Cloak; + planetConfig.Funnel = Funnel; + planetConfig.HeightMap = HeightMap; + planetConfig.Lava = Lava; + planetConfig.Orbit = star.Orbit; + planetConfig.ProcGen = ProcGen; + planetConfig.Props = Props; + planetConfig.removeChildren = removeChildren; + planetConfig.Ring = Ring; + planetConfig.Sand = Sand; + planetConfig.Water = Water; + planetConfig.Validate(); + planetConfig.Migrate(); + return planetConfig; + } + } + } + + [JsonConverter(typeof(StringEnumConverter))] + public enum StellarDeathType + { + [EnumMember(Value = @"default")] Default, + [EnumMember(Value = @"planetaryNebula")] PlanetaryNebula, + [EnumMember(Value = @"supernova")] Supernova + } + + [JsonConverter(typeof(StringEnumConverter))] + public enum StellarRemnantType + { + [EnumMember(Value = @"default")] Default, + [EnumMember(Value = @"whiteDwarf")] WhiteDwarf, + [EnumMember(Value = @"neutronStar")] NeutronStar, + [EnumMember(Value = @"pulsar")] Pulsar, + [EnumMember(Value = @"blackHole")] BlackHole, + [EnumMember(Value = @"custom")] Custom } } diff --git a/NewHorizons/Handlers/PlanetCreationHandler.cs b/NewHorizons/Handlers/PlanetCreationHandler.cs index 604c3830..5cd1026c 100644 --- a/NewHorizons/Handlers/PlanetCreationHandler.cs +++ b/NewHorizons/Handlers/PlanetCreationHandler.cs @@ -200,10 +200,6 @@ namespace NewHorizons.Handlers return false; } } - else if (body.Config.isStellarRemnant) - { - //Skip - } else { UpdateBody(body, existingPlanet); @@ -222,10 +218,6 @@ namespace NewHorizons.Handlers // If the ground state object isn't made yet do it later _nextPassBodies.Add(body); } - else if (body.Config.isStellarRemnant) - { - //Skip - } else { try @@ -460,7 +452,7 @@ namespace NewHorizons.Handlers return go; } - private static float GetSphereOfInfluence(NewHorizonsBody body) + internal 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); @@ -470,7 +462,7 @@ namespace NewHorizons.Handlers } // What is called both on existing planets and new planets - private static GameObject SharedGenerateBody(NewHorizonsBody body, GameObject go, Sector sector, OWRigidbody rb) + internal static GameObject SharedGenerateBody(NewHorizonsBody body, GameObject go, Sector sector, OWRigidbody rb, bool isStellarRemnant = false) { var sphereOfInfluence = GetSphereOfInfluence(body); @@ -501,92 +493,14 @@ namespace NewHorizons.Handlers if (body.Config.Star != null) { - StarLightController.AddStar(StarBuilder.Make(go, sector, body.Config.Star, body.Mod, body.Config.isStellarRemnant)); - if (!body.Config.isStellarRemnant) + if (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); - stellarRemnantController.SetSiderealPeriod(srBody.Config.Orbit.siderealPeriod); - 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); - stellarRemnantController.SetSiderealPeriod(0.1f); - 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.SetSiderealPeriod(0.1f); - stellarRemnantController.SetStarController(StarBuilder.Make(go, srSector, new External.Modules.VariableSize.StarModule - { - size = nsSurfaceSize, - tint = MColor.cyan - }, 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.SetSiderealPeriod(body.Config.Orbit.siderealPeriod); - 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}"); - } + sector.GetComponent().SetStarController(StarBuilder.Make(go, sector, body.Config.Star, body.Mod, true)); + } + else + { + StarLightController.AddStar(StarBuilder.Make(go, sector, body.Config.Star, body.Mod, false)); + StellarRemnantBuilder.Make(go, rb, body.Config, body.Mod, sphereOfInfluence); } } diff --git a/NewHorizons/Schemas/body_schema.json b/NewHorizons/Schemas/body_schema.json index 0a49d399..daf498d4 100644 --- a/NewHorizons/Schemas/body_schema.json +++ b/NewHorizons/Schemas/body_schema.json @@ -69,10 +69,6 @@ "type": "boolean", "description": "Does this config describe a quantum state of a custom planet defined in another file?" }, - "isStellarRemnant": { - "type": "boolean", - "description": "Does this config describe a stellar remnant of a custom star defined in another file?" - }, "Lava": { "description": "Add lava to this planet", "$ref": "#/definitions/LavaModule"