diff --git a/NewHorizons/Handlers/InvulnerabilityHandler.cs b/NewHorizons/Handlers/InvulnerabilityHandler.cs index 00df816e..cb48fe22 100644 --- a/NewHorizons/Handlers/InvulnerabilityHandler.cs +++ b/NewHorizons/Handlers/InvulnerabilityHandler.cs @@ -1,14 +1,21 @@ +using HarmonyLib; using NewHorizons.Utility.OWML; using UnityEngine; +using UnityEngine.SceneManagement; namespace NewHorizons.Handlers { + [HarmonyPatch] internal class InvulnerabilityHandler { + private static bool _invulnerableOverride; + public static void MakeInvulnerable(bool invulnerable) { NHLogger.Log($"Toggling immortality: {invulnerable}"); + // We're setting our own override because we want to ensure that no other mod that can set _invincible can break this for us + _invulnerableOverride = invulnerable; var deathManager = GetDeathManager(); var resources = GetPlayerResouces(); @@ -23,7 +30,24 @@ namespace NewHorizons.Handlers } } + [HarmonyPrefix] + [HarmonyPatch(typeof(DeathManager), nameof(DeathManager.KillPlayer))] + [HarmonyPatch(typeof(PlayerResources), nameof(PlayerResources.ApplyInstantDamage))] + [HarmonyPatch(typeof(PlayerImpactAudio), nameof(PlayerImpactAudio.OnImpact))] + public static bool DeathManager_KillPlayer_Prefix() + { + // Base game _invincible is still overriden by high speed impacts + // We also are avoiding playing impact related effects by just skipping these methods + return !_invulnerableOverride; + } + private static DeathManager GetDeathManager() => GameObject.FindObjectOfType(); private static PlayerResources GetPlayerResouces() => GameObject.FindObjectOfType(); + + static InvulnerabilityHandler() + { + // If the scene unloads when _invulnerableOverride is on it might not get turned off + SceneManager.sceneUnloaded += (_) => _invulnerableOverride = false; + } } } diff --git a/NewHorizons/Patches/InvincibilityPatches.cs b/NewHorizons/Patches/InvincibilityPatches.cs deleted file mode 100644 index 0a7f58e9..00000000 --- a/NewHorizons/Patches/InvincibilityPatches.cs +++ /dev/null @@ -1,19 +0,0 @@ -using HarmonyLib; - -namespace NewHorizons.Patches -{ - [HarmonyPatch] - public static class InvincibilityPatches - { - [HarmonyPrefix] - [HarmonyPatch(typeof(DeathManager), nameof(DeathManager.KillPlayer))] - [HarmonyPatch(typeof(PlayerResources), nameof(PlayerResources.ApplyInstantDamage))] - [HarmonyPatch(typeof(PlayerImpactAudio), nameof(PlayerImpactAudio.OnImpact))] - public static bool DeathManager_KillPlayer_Prefix() - { - // Base game _invincible is still overriden by high speed impacts - // We also are avoiding playing impact related effects by just skipping these methods - return !(Locator.GetDeathManager()?._invincible ?? false); - } - } -}