From 3b25c5cf2030aef17a8f8deba1880391888a1c6e Mon Sep 17 00:00:00 2001 From: FreezeDriedMangoes Date: Thu, 30 Jun 2022 14:00:27 -0400 Subject: [PATCH] added exit pairing --- .../Builder/Body/BrambleDimensionBuilder.cs | 41 ++++++++++- .../Builder/Props/BrambleNodeBuilder.cs | 69 ++++++++++++++----- 2 files changed, 91 insertions(+), 19 deletions(-) diff --git a/NewHorizons/Builder/Body/BrambleDimensionBuilder.cs b/NewHorizons/Builder/Body/BrambleDimensionBuilder.cs index 8b5eec37..83a22345 100644 --- a/NewHorizons/Builder/Body/BrambleDimensionBuilder.cs +++ b/NewHorizons/Builder/Body/BrambleDimensionBuilder.cs @@ -1,3 +1,4 @@ +using NewHorizons.Builder.Props; using NewHorizons.Utility; using System; using System.Collections.Generic; @@ -6,6 +7,7 @@ using System.Text; using System.Threading.Tasks; using UnityEngine; using static NewHorizons.External.Modules.BrambleModule; +using Logger = NewHorizons.Utility.Logger; namespace NewHorizons.Builder.Body { @@ -26,6 +28,12 @@ namespace NewHorizons.Builder.Body private static int DIMENSION_COUNTER = 0; + + // keys are all node names that have been referenced as an exit by at least one dimension but do not (yet) exist + // values are all dimensions' warp controllers that link to a given dimension + // unpairedNodes[name of node that doesn't exist yet] => List{warp controller for dimension that exits to that node, ...} + private static Dictionary> unpairedDimensions = new(); + public static GameObject Make(NewHorizonsBody body) { var config = body.Config.Bramble.dimension; @@ -69,10 +77,41 @@ namespace NewHorizons.Builder.Body var outerFogWarpVolume = exitWarps.GetComponent(); outerFogWarpVolume._senderWarps.Clear(); outerFogWarpVolume._linkedInnerWarpVolume = null; + outerFogWarpVolume._name = OuterFogWarpVolume.Name.None; + //outerFogWarpVolume._sector = dimensionSector.GetComponent(); + //outerFogWarpVolume.Awake(); // I can't spawn this game object disabled, but Awake needs to run after _sector is set. That means I need to call Awake myself - // TODO MAYBE: set "exitWarps/ExitPoint", "exitWarp/ExitPoint (1)", ... "exitWarp/ExitPoint (5)" + // TODO if I need to: set "exitWarps/ExitPoint", "exitWarp/ExitPoint (1)", ... "exitWarp/ExitPoint (5)" + + PairExit(config.linksTo, outerFogWarpVolume); return dimension; } + + public static void PairExit(string exitName, OuterFogWarpVolume warpController) + { + if (!BrambleNodeBuilder.namedNodes.ContainsKey(exitName)) + { + if (!unpairedDimensions.ContainsKey(exitName)) unpairedDimensions[exitName] = new(); + unpairedDimensions[exitName].Add(warpController); + return; + } + + warpController._linkedInnerWarpVolume = BrambleNodeBuilder.namedNodes[exitName]; + } + + public static void FinishPairingDimensionsForExitNode(string nodeName) + { + if (!unpairedDimensions.ContainsKey(nodeName)) return; + + var warpControllers = unpairedDimensions[nodeName].ToList(); + foreach (var dimensionWarpController in warpControllers) + { + PairExit(nodeName, dimensionWarpController); + } + + unpairedDimensions.Remove(nodeName); + } + } } diff --git a/NewHorizons/Builder/Props/BrambleNodeBuilder.cs b/NewHorizons/Builder/Props/BrambleNodeBuilder.cs index 7aa9a27e..055bfd6f 100644 --- a/NewHorizons/Builder/Props/BrambleNodeBuilder.cs +++ b/NewHorizons/Builder/Props/BrambleNodeBuilder.cs @@ -1,4 +1,5 @@ using HarmonyLib; +using NewHorizons.Builder.Body; using NewHorizons.Utility; using System; using System.Collections.Generic; @@ -16,6 +17,45 @@ namespace NewHorizons.Builder.Props [HarmonyPatch] public static class FogDebuggingPatches { + + + [HarmonyPrefix] + [HarmonyPatch(typeof(FogWarpVolume), nameof(FogWarpVolume.WarpDetector))] + public static bool FogWarpVolume_WarpDetector(FogWarpVolume __instance, FogWarpDetector detector, FogWarpVolume linkedWarpVolume) + { + bool flag = detector.CompareName(FogWarpDetector.Name.Player); + bool flag2 = detector.CompareName(FogWarpDetector.Name.Ship); + if (!flag || !PlayerState.IsInsideShip()) + { + OWRigidbody oWRigidbody = detector.GetOWRigidbody(); + if (flag && PlayerState.IsAttached()) + { + oWRigidbody = detector.GetOWRigidbody().transform.parent.GetComponentInParent(); + MonoBehaviour.print("body to warp: " + oWRigidbody.name); + } + Vector3 localRelVelocity = __instance.transform.InverseTransformDirection(oWRigidbody.GetVelocity() - __instance._attachedBody.GetVelocity()); + Vector3 localPos = __instance.transform.InverseTransformPoint(oWRigidbody.transform.position); + Quaternion localRot = Quaternion.Inverse(__instance.transform.rotation) * oWRigidbody.transform.rotation; + if (flag2 && PlayerState.IsInsideShip()) + { + __instance._sector.GetTriggerVolume().RemoveObjectFromVolume(Locator.GetPlayerDetector()); + __instance._sector.GetTriggerVolume().RemoveObjectFromVolume(Locator.GetPlayerCameraDetector()); + } + if (flag || (flag2 && PlayerState.IsInsideShip())) + { + GlobalMessenger.FireEvent("PlayerFogWarp"); + } + __instance._sector.GetTriggerVolume().RemoveObjectFromVolume(detector.gameObject); + linkedWarpVolume.ReceiveWarpedDetector(detector, localRelVelocity, localPos, localRot); + //if (__instance.OnWarpDetector != null) + //{ + // __instance.OnWarpDetector(detector); + //} + } + + return false; + } + [HarmonyPrefix] [HarmonyPatch(typeof(FogWarpVolume), nameof(FogWarpVolume.OnOccupantEnterSector))] @@ -79,28 +119,16 @@ namespace NewHorizons.Builder.Props public static Dictionary namedNodes = new(); - public static void FinishPairingNodesForDimension(string dimensionName, AstroObject dimensionAO = null, BrambleDimensionInfo dimensionInfo = null) + public static void FinishPairingNodesForDimension(string dimensionName, AstroObject dimensionAO = null) { - Logger.Log("Pairing for " + dimensionName); - // TODO: I might need to call this on Make: InnerFogWarpVolume.OnOccupantEnterSector + if (!unpairedNodes.ContainsKey(dimensionName)) return; - // pair node->dimension (entrances) - if (unpairedNodes.ContainsKey(dimensionName)) + foreach (var nodeWarpController in unpairedNodes[dimensionName]) { - foreach (var nodeWarpController in unpairedNodes[dimensionName]) - { - Pair(nodeWarpController, dimensionName, dimensionAO); - } - - unpairedNodes.Remove(dimensionName); + Pair(nodeWarpController, dimensionName, dimensionAO); } - // pair dimension->node (exit) - if (dimensionInfo != null && dimensionAO != null && namedNodes.ContainsKey(dimensionInfo.linksTo)) - { - var dimensionWarpController = dimensionAO.GetComponentInChildren(); - dimensionWarpController._linkedInnerWarpVolume = namedNodes[dimensionInfo.linksTo]; - } + unpairedNodes.Remove(dimensionName); } private static void RecordUnpairedNode(InnerFogWarpVolume warpVolume, string linksTo) @@ -131,7 +159,7 @@ namespace NewHorizons.Builder.Props if (destination == null) return false; nodeWarp._linkedOuterWarpVolume = destination; - destination._senderWarps.Add(nodeWarp); + destination.RegisterSenderWarp(nodeWarp); return true; } @@ -207,6 +235,11 @@ namespace NewHorizons.Builder.Props // TODO: support adding signals to these nodes // + // + // Cleanup for dimension exits + // + if (config.name != null) BrambleDimensionBuilder.FinishPairingDimensionsForExitNode(config.name); + // Done! return brambleNode; }