From b60b384a8a7b26a890c5a620794f6b16d6a64b6c Mon Sep 17 00:00:00 2001 From: Noah Pilarski Date: Mon, 8 Aug 2022 21:04:04 -0400 Subject: [PATCH] Add customizable loop duration --- NewHorizons/External/Configs/StarSystemConfig.cs | 6 ++++++ NewHorizons/Handlers/SystemCreationHandler.cs | 5 +++++ NewHorizons/Main.cs | 14 +++++++------- NewHorizons/Patches/NomaiCoordinatePatches.cs | 2 +- NewHorizons/Utility/DebugUtilities/DebugReload.cs | 2 +- NewHorizons/Utility/TimeLoopUtilities.cs | 15 +++++++++++++++ 6 files changed, 35 insertions(+), 9 deletions(-) create mode 100644 NewHorizons/Utility/TimeLoopUtilities.cs diff --git a/NewHorizons/External/Configs/StarSystemConfig.cs b/NewHorizons/External/Configs/StarSystemConfig.cs index aceadf96..3476f5a6 100644 --- a/NewHorizons/External/Configs/StarSystemConfig.cs +++ b/NewHorizons/External/Configs/StarSystemConfig.cs @@ -35,6 +35,11 @@ namespace NewHorizons.External.Configs /// public string factRequiredForWarp; + /// + /// The duration of the time loop. + /// + [DefaultValue(22f)] public float loopDuration = 22f; + /// /// Should the player not be able to view the map in this system? /// @@ -209,6 +214,7 @@ namespace NewHorizons.External.Configs canEnterViaWarpDrive = canEnterViaWarpDrive && otherConfig.canEnterViaWarpDrive; destroyStockPlanets = destroyStockPlanets && otherConfig.destroyStockPlanets; enableTimeLoop = enableTimeLoop && otherConfig.enableTimeLoop; + loopDuration = loopDuration == 22f ? otherConfig.loopDuration : loopDuration; // If current one is null take the other factRequiredForWarp = string.IsNullOrEmpty(factRequiredForWarp) ? otherConfig.factRequiredForWarp : factRequiredForWarp; diff --git a/NewHorizons/Handlers/SystemCreationHandler.cs b/NewHorizons/Handlers/SystemCreationHandler.cs index bd69d0cb..16df0230 100644 --- a/NewHorizons/Handlers/SystemCreationHandler.cs +++ b/NewHorizons/Handlers/SystemCreationHandler.cs @@ -31,6 +31,11 @@ namespace NewHorizons.Handlers timeLoopController.AddComponent(); } + if (system.Config.loopDuration != 22f) + { + TimeLoopUtilities.SetLoopDuration(system.Config.loopDuration); + } + if (!string.IsNullOrEmpty(system.Config.travelAudio)) { Delay.FireOnNextUpdate(() => AudioUtilities.SetAudioClip(Locator.GetGlobalMusicController()._travelSource, system.Config.travelAudio, system.Mod)); diff --git a/NewHorizons/Main.cs b/NewHorizons/Main.cs index c40df73c..5635bdd3 100644 --- a/NewHorizons/Main.cs +++ b/NewHorizons/Main.cs @@ -42,7 +42,7 @@ namespace NewHorizons public static Dictionary> BodyDict = new Dictionary>(); public static List MountedAddons = new List(); - public static float SecondsLeftInLoop = -1; + public static float SecondsElapsedInLoop = -1; public static bool IsSystemReady { get; private set; } public static float FurthestOrbit { get; set; } = 50000f; @@ -239,9 +239,9 @@ namespace NewHorizons } // Set time loop stuff if its enabled and if we're warping to a new place - if (IsChangingStarSystem && (SystemDict[_currentStarSystem].Config.enableTimeLoop || _currentStarSystem == "SolarSystem") && SecondsLeftInLoop > 0f) + if (IsChangingStarSystem && (SystemDict[_currentStarSystem].Config.enableTimeLoop || _currentStarSystem == "SolarSystem") && SecondsElapsedInLoop > 0f) { - TimeLoop.SetSecondsRemaining(SecondsLeftInLoop); + TimeLoopUtilities.SetSecondsElapsed(SecondsElapsedInLoop); // Prevent the OPC from firing var launchController = GameObject.FindObjectOfType(); if (launchController != null) @@ -258,7 +258,7 @@ namespace NewHorizons } // Reset this - SecondsLeftInLoop = -1; + SecondsElapsedInLoop = -1; IsChangingStarSystem = false; @@ -607,13 +607,13 @@ namespace NewHorizons if (newStarSystem == "EyeOfTheUniverse") { - PlayerData.SaveWarpedToTheEye(TimeLoop.GetSecondsRemaining()); + PlayerData.SaveWarpedToTheEye(TimeLoopUtilities.GetVanillaSecondsRemaining()); sceneToLoad = OWScene.EyeOfTheUniverse; } else { - if (SystemDict[_currentStarSystem].Config.enableTimeLoop) SecondsLeftInLoop = TimeLoop.GetSecondsRemaining(); - else SecondsLeftInLoop = -1; + if (SystemDict[_currentStarSystem].Config.enableTimeLoop) SecondsElapsedInLoop = TimeLoop.GetSecondsElapsed(); + else SecondsElapsedInLoop = -1; sceneToLoad = OWScene.SolarSystem; } diff --git a/NewHorizons/Patches/NomaiCoordinatePatches.cs b/NewHorizons/Patches/NomaiCoordinatePatches.cs index 1814b9a1..98f39af2 100644 --- a/NewHorizons/Patches/NomaiCoordinatePatches.cs +++ b/NewHorizons/Patches/NomaiCoordinatePatches.cs @@ -24,7 +24,7 @@ namespace NewHorizons.Patches VesselWarpController.s_playerWarpLocation = new RelativeLocationData(Locator.GetPlayerBody(), __instance.transform); VesselWarpController.s_relativeLocationSaved = !debugWarp; if (!Main.Instance.IsWarpingFromVessel) - PlayerData.SaveWarpedToTheEye(TimeLoop.GetSecondsRemaining()); + PlayerData.SaveWarpedToTheEye(TimeLoopUtilities.GetVanillaSecondsRemaining()); LoadManager.EnableAsyncLoadTransition(); return false; } diff --git a/NewHorizons/Utility/DebugUtilities/DebugReload.cs b/NewHorizons/Utility/DebugUtilities/DebugReload.cs index eaa09373..b8a513c9 100644 --- a/NewHorizons/Utility/DebugUtilities/DebugReload.cs +++ b/NewHorizons/Utility/DebugUtilities/DebugReload.cs @@ -49,7 +49,7 @@ namespace NewHorizons.Utility.DebugUtilities Main.Instance.ChangeCurrentStarSystem(Main.Instance.CurrentStarSystem); - Main.SecondsLeftInLoop = -1f; + Main.SecondsElapsedInLoop = -1f; } } } \ No newline at end of file diff --git a/NewHorizons/Utility/TimeLoopUtilities.cs b/NewHorizons/Utility/TimeLoopUtilities.cs new file mode 100644 index 00000000..9af60051 --- /dev/null +++ b/NewHorizons/Utility/TimeLoopUtilities.cs @@ -0,0 +1,15 @@ +using UnityEngine; + +namespace NewHorizons.Utility +{ + public static class TimeLoopUtilities + { + public const float LOOP_DURATION_IN_SECONDS = TimeLoop.LOOP_DURATION_IN_MINUTES * 60; + public static void SetLoopDuration(float minutes) => TimeLoop._loopDuration = minutes * 60f; + public static void SetSecondsElapsed(float secondsElapsed) => TimeLoop._timeOffset = secondsElapsed - Time.timeSinceLevelLoad; + public static float GetMinutesRemaining() => TimeLoop.GetSecondsRemaining() / 60f; + public static float GetVanillaSecondsRemaining() => LOOP_DURATION_IN_SECONDS - TimeLoop.GetSecondsElapsed(); + public static float GetVanillaMinutesRemaining() => GetVanillaSecondsRemaining() / 60f; + public static float GetVanillaFractionElapsed() => TimeLoop.GetSecondsElapsed() / LOOP_DURATION_IN_SECONDS; + } +}