Pair up singularities after all planets have been built

This commit is contained in:
Joshua Thome 2023-03-22 18:27:09 -05:00
parent 410f77add5
commit c7632aa70e
3 changed files with 46 additions and 45 deletions

View File

@ -32,6 +32,7 @@ namespace NewHorizons.Builder.Body
public static readonly int Color1 = Shader.PropertyToID("_Color"); public static readonly int Color1 = Shader.PropertyToID("_Color");
private static Dictionary<string, GameObject> _singularitiesByID; private static Dictionary<string, GameObject> _singularitiesByID;
private static List<(string, string)> _pairsToLink;
private static Mesh _blackHoleMesh; private static Mesh _blackHoleMesh;
private static GameObject _blackHoleAmbience; private static GameObject _blackHoleAmbience;
@ -45,14 +46,8 @@ namespace NewHorizons.Builder.Body
private static GameObject _whiteHoleRulesetVolume; private static GameObject _whiteHoleRulesetVolume;
private static GameObject _whiteHoleVolume; private static GameObject _whiteHoleVolume;
private static bool _isInit;
internal static void InitPrefabs() internal static void InitPrefabs()
{ {
if (_isInit) return;
_isInit = true;
if (_blackHoleProxyPrefab == null) _blackHoleProxyPrefab = SearchUtilities.Find(_blackHoleProxyPath).InstantiateInactive().Rename("BlackHoleSingularity").DontDestroyOnLoad(); if (_blackHoleProxyPrefab == null) _blackHoleProxyPrefab = SearchUtilities.Find(_blackHoleProxyPath).InstantiateInactive().Rename("BlackHoleSingularity").DontDestroyOnLoad();
if (_whiteHoleProxyPrefab == null) _whiteHoleProxyPrefab = SearchUtilities.Find(_whiteHoleProxyPath).InstantiateInactive().Rename("WhiteHoleSingularity").DontDestroyOnLoad(); if (_whiteHoleProxyPrefab == null) _whiteHoleProxyPrefab = SearchUtilities.Find(_whiteHoleProxyPath).InstantiateInactive().Rename("WhiteHoleSingularity").DontDestroyOnLoad();
@ -72,13 +67,14 @@ namespace NewHorizons.Builder.Body
if (_whiteHoleVolume == null) _whiteHoleVolume = SearchUtilities.Find("WhiteHole_Body/WhiteHoleVolume").InstantiateInactive().Rename("WhiteHoleVolume").DontDestroyOnLoad(); if (_whiteHoleVolume == null) _whiteHoleVolume = SearchUtilities.Find("WhiteHole_Body/WhiteHoleVolume").InstantiateInactive().Rename("WhiteHoleVolume").DontDestroyOnLoad();
} }
public static void Init()
{
_singularitiesByID = new Dictionary<string, GameObject>();
_pairsToLink = new List<(string, string)>();
}
public static void Make(GameObject go, Sector sector, OWRigidbody OWRB, PlanetConfig config, SingularityModule singularity) public static void Make(GameObject go, Sector sector, OWRigidbody OWRB, PlanetConfig config, SingularityModule singularity)
{ {
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<string, GameObject>();
var horizonRadius = singularity.horizonRadius; var horizonRadius = singularity.horizonRadius;
var distortRadius = singularity.distortRadius != 0f ? singularity.distortRadius : horizonRadius * 2.5f; var distortRadius = singularity.distortRadius != 0f ? singularity.distortRadius : horizonRadius * 2.5f;
var pairedSingularity = singularity.pairedSingularity; var pairedSingularity = singularity.pairedSingularity;
@ -95,51 +91,57 @@ namespace NewHorizons.Builder.Body
hasHazardVolume, singularity.targetStarSystem, singularity.curve, singularity.hasWarpEffects, singularity.renderQueueOverride, singularity.rename, singularity.parentPath, singularity.isRelativeToParent); 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; var uniqueID = string.IsNullOrEmpty(singularity.uniqueID) ? config.name : singularity.uniqueID;
_singularitiesByID.Add(uniqueID, newSingularity); _singularitiesByID.Add(uniqueID, newSingularity);
if (!string.IsNullOrEmpty(pairedSingularity))
// Try to pair them
if (!string.IsNullOrEmpty(pairedSingularity) && newSingularity != null)
{ {
if (_singularitiesByID.TryGetValue(pairedSingularity, out var pairedSingularityGO)) if (polarity)
{ {
switch (polarity) _pairsToLink.Add((uniqueID, pairedSingularity));
{ }
case true: else
PairSingularities(uniqueID, pairedSingularity, newSingularity, pairedSingularityGO); {
break; _pairsToLink.Add((pairedSingularity, uniqueID));
case false:
PairSingularities(pairedSingularity, uniqueID, pairedSingularityGO, newSingularity);
break;
}
} }
} }
} }
public static void PairSingularities(string blackHoleID, string whiteHoleID, GameObject blackHole, GameObject whiteHole) public static void PairAllSingularities()
{ {
InitPrefabs(); foreach (var pair in _pairsToLink)
if (blackHole == null || whiteHole == null) return;
Logger.LogVerbose($"Pairing singularities [{blackHoleID}], [{whiteHoleID}]");
var whiteHoleVolume = whiteHole.GetComponentInChildren<WhiteHoleVolume>();
var blackHoleVolume = blackHole.GetComponentInChildren<BlackHoleVolume>();
if (whiteHoleVolume == null || blackHoleVolume == null)
{ {
Logger.LogWarning($"[{blackHoleID}] and [{whiteHoleID}] do not have compatible polarities"); var (blackHoleID, whiteHoleID) = pair;
return; 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<WhiteHoleVolume>();
var blackHoleVolume = blackHole.GetComponentInChildren<BlackHoleVolume>();
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, 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) 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 // polarity true = black, false = white
var info = new SingularityModule var info = new SingularityModule
@ -272,8 +274,6 @@ namespace NewHorizons.Builder.Body
public static MeshRenderer MakeSingularityGraphics(GameObject singularity, bool polarity, float horizon, float distort, int queue = 2985) public static MeshRenderer MakeSingularityGraphics(GameObject singularity, bool polarity, float horizon, float distort, int queue = 2985)
{ {
InitPrefabs();
var singularityRenderer = new GameObject(polarity ? "BlackHoleRenderer" : "WhiteHoleRenderer"); var singularityRenderer = new GameObject(polarity ? "BlackHoleRenderer" : "WhiteHoleRenderer");
singularityRenderer.transform.parent = singularity.transform; singularityRenderer.transform.parent = singularity.transform;
singularityRenderer.transform.localPosition = Vector3.zero; singularityRenderer.transform.localPosition = Vector3.zero;
@ -296,8 +296,6 @@ namespace NewHorizons.Builder.Body
public static GameObject MakeSingularityProxy(GameObject rootObject, MVector3 position, bool polarity, float horizon, float distort, TimeValuePair[] curve = null, int queue = 2985) 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); var singularityRenderer = MakeSingularityGraphics(rootObject, polarity, horizon, distort, queue);
if (position != null) singularityRenderer.transform.localPosition = position; if (position != null) singularityRenderer.transform.localPosition = position;

View File

@ -131,6 +131,8 @@ namespace NewHorizons.Handlers
Logger.Log("Done loading bodies"); Logger.Log("Done loading bodies");
SingularityBuilder.PairAllSingularities();
// Events.FireOnNextUpdate(PlanetDestroyer.RemoveAllProxies); // Events.FireOnNextUpdate(PlanetDestroyer.RemoveAllProxies);
if (Main.SystemDict[Main.Instance.CurrentStarSystem].Config.destroyStockPlanets) PlanetDestructionHandler.RemoveStockPlanets(); if (Main.SystemDict[Main.Instance.CurrentStarSystem].Config.destroyStockPlanets) PlanetDestructionHandler.RemoveStockPlanets();

View File

@ -376,6 +376,7 @@ namespace NewHorizons
AudioTypeHandler.Init(); AudioTypeHandler.Init();
InterferenceHandler.Init(); InterferenceHandler.Init();
RemoteHandler.Init(); RemoteHandler.Init();
SingularityBuilder.Init();
AtmosphereBuilder.Init(); AtmosphereBuilder.Init();
BrambleNodeBuilder.Init(BodyDict[CurrentStarSystem].Select(x => x.Config).Where(x => x.Bramble?.dimension != null).ToArray()); BrambleNodeBuilder.Init(BodyDict[CurrentStarSystem].Select(x => x.Config).Where(x => x.Bramble?.dimension != null).ToArray());