From f17089f4355de4140736836ebc76a1d77f1efee8 Mon Sep 17 00:00:00 2001 From: XtraCube <72575280+XtraCube@users.noreply.github.com> Date: Sun, 1 Sep 2024 16:14:30 -0400 Subject: [PATCH] split HudManager patches --- MiraAPI/Patches/HudManagerPatches.cs | 68 +------------------- MiraAPI/Patches/Roles/HudManagerPatches.cs | 74 ++++++++++++++++++++++ 2 files changed, 75 insertions(+), 67 deletions(-) create mode 100644 MiraAPI/Patches/Roles/HudManagerPatches.cs diff --git a/MiraAPI/Patches/HudManagerPatches.cs b/MiraAPI/Patches/HudManagerPatches.cs index c3ee9e8..45bd114 100644 --- a/MiraAPI/Patches/HudManagerPatches.cs +++ b/MiraAPI/Patches/HudManagerPatches.cs @@ -17,62 +17,6 @@ public static class HudManagerPatches // Custom buttons parent. private static GameObject? _bottomLeft; - // Custom role tab. - private static TaskPanelBehaviour? _roleTab; - - /// - /// Update custom role tab and custom role hud elements. - /// - /// The HudManager instance. - [HarmonyPostfix] - [HarmonyPatch(nameof(HudManager.Update))] - public static void UpdatePostfix(HudManager __instance) - { - var local = PlayerControl.LocalPlayer; - - if (AmongUsClient.Instance.GameState != InnerNetClient.GameStates.Started && !ShipStatus.Instance) - { - return; - } - - // CustomGameModeManager.ActiveMode?.HudUpdate(__instance); - - switch (local?.Data?.Role) - { - case null: - return; - - case ICustomRole customRole: - { - customRole.HudUpdate(__instance); - - if (customRole.RoleHintType != RoleHintType.RoleTab) - { - _roleTab?.gameObject.Destroy(); - return; - } - - if (_roleTab == null) - { - _roleTab = CustomRoleManager.CreateRoleTab(customRole); - } - else - { - CustomRoleManager.UpdateRoleTab(_roleTab, customRole); - } - - break; - } - - default: - if (_roleTab != null) - { - _roleTab.gameObject.Destroy(); - } - break; - } - } - /* /// /// Trigger hudstart on current custom gamemode @@ -136,7 +80,7 @@ public static class HudManagerPatches } /// - /// Set the custom role tab and custom buttons active when the hud is active. + /// Set the custom buttons active when the hud is active. /// /// HudManager instance. /// The local PlayerControl. @@ -151,19 +95,9 @@ public static class HudManagerPatches return; } - if (_roleTab) - { - _roleTab?.gameObject.SetActive(isActive); - } - foreach (var button in CustomButtonManager.CustomButtons) { button.SetActive(isActive, role); } - - if (role is ICustomRole customRole) - { - __instance.ImpostorVentButton.gameObject.SetActive(isActive && customRole.CanUseVent); - } } } diff --git a/MiraAPI/Patches/Roles/HudManagerPatches.cs b/MiraAPI/Patches/Roles/HudManagerPatches.cs new file mode 100644 index 0000000..34f0bb9 --- /dev/null +++ b/MiraAPI/Patches/Roles/HudManagerPatches.cs @@ -0,0 +1,74 @@ +using HarmonyLib; +using InnerNet; +using MiraAPI.Roles; +using Reactor.Utilities.Extensions; + +namespace MiraAPI.Patches.Roles; + +/// +/// HudManager patches for roles. +/// +[HarmonyPatch(typeof(HudManager))] +public static class HudManagerPatches +{ + // Custom role tab. + private static TaskPanelBehaviour? _roleTab; + + /// + /// Fixes Kill Button not showing for Neutral killing role. + /// + [HarmonyPostfix] + [HarmonyPatch(nameof(HudManager.SetHudActive), typeof(PlayerControl), typeof(RoleBehaviour), typeof(bool))] + public static void SetHudActivePostfix( + HudManager __instance, + PlayerControl localPlayer, + RoleBehaviour role, + bool isActive) + { + var flag = localPlayer.Data != null && localPlayer.Data.IsDead; + + if (role is ICustomRole) + { + __instance.KillButton.ToggleVisible(isActive && role.CanUseKillButton && !flag); + __instance.ImpostorVentButton.ToggleVisible(isActive && role.CanVent && !flag); + } + + if (_roleTab) + { + _roleTab?.gameObject.SetActive(isActive); + } + } + + /// + /// Update custom role tab and custom role hud elements. + /// + [HarmonyPostfix] + [HarmonyPatch(nameof(HudManager.Update))] + public static void UpdatePostfix(HudManager __instance) + { + var local = PlayerControl.LocalPlayer; + + if (AmongUsClient.Instance.GameState != InnerNetClient.GameStates.Started && !ShipStatus.Instance) + { + return; + } + + var role = local?.Data?.Role; + + if (role is ICustomRole { RoleHintType: RoleHintType.RoleTab } customRole) + { + customRole.HudUpdate(__instance); + + if (_roleTab == null) + { + _roleTab = CustomRoleManager.CreateRoleTab(customRole); + } + + CustomRoleManager.UpdateRoleTab(_roleTab, customRole); + } + else if (_roleTab) + { + _roleTab?.gameObject.Destroy(); + } + } +}