From c7632aa70ed3e28edf9b534fcb63824d1f6ca72b Mon Sep 17 00:00:00 2001 From: Joshua Thome Date: Wed, 22 Mar 2023 18:27:09 -0500 Subject: [PATCH] Pair up singularities after all planets have been built --- .../Builder/Body/SingularityBuilder.cs | 88 +++++++++---------- NewHorizons/Handlers/PlanetCreationHandler.cs | 2 + NewHorizons/Main.cs | 1 + 3 files changed, 46 insertions(+), 45 deletions(-) diff --git a/NewHorizons/Builder/Body/SingularityBuilder.cs b/NewHorizons/Builder/Body/SingularityBuilder.cs index 8f84e77fc..4ca159822 100644 --- a/NewHorizons/Builder/Body/SingularityBuilder.cs +++ b/NewHorizons/Builder/Body/SingularityBuilder.cs @@ -32,6 +32,7 @@ public static class SingularityBuilder public static readonly int Color1 = Shader.PropertyToID("_Color"); private static Dictionary _singularitiesByID; + private static List<(string, string)> _pairsToLink; private static Mesh _blackHoleMesh; private static GameObject _blackHoleAmbience; @@ -45,14 +46,8 @@ public static class SingularityBuilder private static GameObject _whiteHoleRulesetVolume; private static GameObject _whiteHoleVolume; - private static bool _isInit; - internal static void InitPrefabs() { - if (_isInit) return; - - _isInit = true; - if (_blackHoleProxyPrefab == null) _blackHoleProxyPrefab = SearchUtilities.Find(_blackHoleProxyPath).InstantiateInactive().Rename("BlackHoleSingularity").DontDestroyOnLoad(); if (_whiteHoleProxyPrefab == null) _whiteHoleProxyPrefab = SearchUtilities.Find(_whiteHoleProxyPath).InstantiateInactive().Rename("WhiteHoleSingularity").DontDestroyOnLoad(); @@ -72,13 +67,14 @@ internal static void InitPrefabs() if (_whiteHoleVolume == null) _whiteHoleVolume = SearchUtilities.Find("WhiteHole_Body/WhiteHoleVolume").InstantiateInactive().Rename("WhiteHoleVolume").DontDestroyOnLoad(); } - public static void Make(GameObject go, Sector sector, OWRigidbody OWRB, PlanetConfig config, SingularityModule singularity) + public static void Init() { - InitPrefabs(); - - // If we've reloaded the first one will now be null so we have to refresh the list - if (_singularitiesByID?.Values?.FirstOrDefault() == null) _singularitiesByID = new Dictionary(); + _singularitiesByID = new Dictionary(); + _pairsToLink = new List<(string, string)>(); + } + public static void Make(GameObject go, Sector sector, OWRigidbody OWRB, PlanetConfig config, SingularityModule singularity) + { var horizonRadius = singularity.horizonRadius; var distortRadius = singularity.distortRadius != 0f ? singularity.distortRadius : horizonRadius * 2.5f; var pairedSingularity = singularity.pairedSingularity; @@ -95,51 +91,57 @@ public static void Make(GameObject go, Sector sector, OWRigidbody OWRB, PlanetCo hasHazardVolume, singularity.targetStarSystem, singularity.curve, singularity.hasWarpEffects, singularity.renderQueueOverride, singularity.rename, singularity.parentPath, singularity.isRelativeToParent); var uniqueID = string.IsNullOrEmpty(singularity.uniqueID) ? config.name : singularity.uniqueID; + _singularitiesByID.Add(uniqueID, newSingularity); - - // Try to pair them - if (!string.IsNullOrEmpty(pairedSingularity) && newSingularity != null) + if (!string.IsNullOrEmpty(pairedSingularity)) { - if (_singularitiesByID.TryGetValue(pairedSingularity, out var pairedSingularityGO)) + if (polarity) { - switch (polarity) - { - case true: - PairSingularities(uniqueID, pairedSingularity, newSingularity, pairedSingularityGO); - break; - case false: - PairSingularities(pairedSingularity, uniqueID, pairedSingularityGO, newSingularity); - break; - } + _pairsToLink.Add((uniqueID, pairedSingularity)); + } + else + { + _pairsToLink.Add((pairedSingularity, uniqueID)); } } + } - public static void PairSingularities(string blackHoleID, string whiteHoleID, GameObject blackHole, GameObject whiteHole) + public static void PairAllSingularities() { - InitPrefabs(); - - if (blackHole == null || whiteHole == null) return; - - Logger.LogVerbose($"Pairing singularities [{blackHoleID}], [{whiteHoleID}]"); - - var whiteHoleVolume = whiteHole.GetComponentInChildren(); - var blackHoleVolume = blackHole.GetComponentInChildren(); - - if (whiteHoleVolume == null || blackHoleVolume == null) + foreach (var pair in _pairsToLink) { - Logger.LogWarning($"[{blackHoleID}] and [{whiteHoleID}] do not have compatible polarities"); - return; + var (blackHoleID, whiteHoleID) = pair; + if (!_singularitiesByID.TryGetValue(blackHoleID, out GameObject blackHole)) + { + Logger.LogWarning($"Black hole [{blackHoleID}] is missing."); + break; + } + if (!_singularitiesByID.TryGetValue(whiteHoleID, out GameObject whiteHole)) + { + Logger.LogWarning($"White hole [{whiteHoleID}] is missing."); + break; + } + var whiteHoleVolume = whiteHole.GetComponentInChildren(); + var blackHoleVolume = blackHole.GetComponentInChildren(); + if (whiteHoleVolume == null || blackHoleVolume == null) + { + Logger.LogWarning($"Singularities [{blackHoleID}] and [{whiteHoleID}] do not have compatible polarities."); + break; + } + if (blackHoleVolume._whiteHole != null && blackHoleVolume._whiteHole != whiteHoleVolume) + { + Logger.LogWarning($"Black hole [{blackHoleID}] has already been linked!"); + break; + } + Logger.LogVerbose($"Pairing singularities [{blackHoleID}], [{whiteHoleID}]"); + blackHoleVolume._whiteHole = whiteHoleVolume; } - - blackHoleVolume._whiteHole = whiteHoleVolume; } public static GameObject MakeSingularity(GameObject planetGO, Sector sector, Vector3 position, Vector3 rotation, bool polarity, float horizon, float distort, bool hasDestructionVolume, string targetStarSystem = null, TimeValuePair[] curve = null, bool warpEffects = true, int renderQueue = 2985, string rename = null, string parentPath = null, bool isRelativeToParent = false) { - InitPrefabs(); - // polarity true = black, false = white var info = new SingularityModule @@ -272,8 +274,6 @@ public static void PairSingularities(string blackHoleID, string whiteHoleID, Gam public static MeshRenderer MakeSingularityGraphics(GameObject singularity, bool polarity, float horizon, float distort, int queue = 2985) { - InitPrefabs(); - var singularityRenderer = new GameObject(polarity ? "BlackHoleRenderer" : "WhiteHoleRenderer"); singularityRenderer.transform.parent = singularity.transform; singularityRenderer.transform.localPosition = Vector3.zero; @@ -296,8 +296,6 @@ public static MeshRenderer MakeSingularityGraphics(GameObject singularity, bool public static GameObject MakeSingularityProxy(GameObject rootObject, MVector3 position, bool polarity, float horizon, float distort, TimeValuePair[] curve = null, int queue = 2985) { - InitPrefabs(); - var singularityRenderer = MakeSingularityGraphics(rootObject, polarity, horizon, distort, queue); if (position != null) singularityRenderer.transform.localPosition = position; diff --git a/NewHorizons/Handlers/PlanetCreationHandler.cs b/NewHorizons/Handlers/PlanetCreationHandler.cs index 7b7c1180d..c6dd25dc9 100644 --- a/NewHorizons/Handlers/PlanetCreationHandler.cs +++ b/NewHorizons/Handlers/PlanetCreationHandler.cs @@ -131,6 +131,8 @@ public static void Init(List bodies) Logger.Log("Done loading bodies"); + SingularityBuilder.PairAllSingularities(); + // Events.FireOnNextUpdate(PlanetDestroyer.RemoveAllProxies); if (Main.SystemDict[Main.Instance.CurrentStarSystem].Config.destroyStockPlanets) PlanetDestructionHandler.RemoveStockPlanets(); diff --git a/NewHorizons/Main.cs b/NewHorizons/Main.cs index 048525a8d..080bae406 100644 --- a/NewHorizons/Main.cs +++ b/NewHorizons/Main.cs @@ -376,6 +376,7 @@ private void OnSceneLoaded(Scene scene, LoadSceneMode mode) AudioTypeHandler.Init(); InterferenceHandler.Init(); RemoteHandler.Init(); + SingularityBuilder.Init(); AtmosphereBuilder.Init(); BrambleNodeBuilder.Init(BodyDict[CurrentStarSystem].Select(x => x.Config).Where(x => x.Bramble?.dimension != null).ToArray());