diff --git a/source/Patches/ImpostorRoles/UnderdogMod/PatchKillTimer.cs b/source/Patches/ImpostorRoles/UnderdogMod/PatchKillTimer.cs new file mode 100644 index 000000000..a3bbee1b2 --- /dev/null +++ b/source/Patches/ImpostorRoles/UnderdogMod/PatchKillTimer.cs @@ -0,0 +1,20 @@ +using HarmonyLib; +using TownOfUs.Roles; +using UnityEngine; + +namespace TownOfUs.ImpostorRoles.UnderdogMod +{ + [HarmonyPatch(typeof(PlayerControl), nameof(PlayerControl.SetKillTimer))] + public static class PatchKillTimer + { + public static bool Prefix(PlayerControl __instance, [HarmonyArgument(0)] float time) + { + var role = Role.GetRole(__instance); + if (role?.RoleType != RoleEnum.Underdog) return true; + var maxTimer = ((Underdog)role).MaxTimer(); + __instance.killTimer = Mathf.Clamp(time, 0, maxTimer); + HudManager.Instance.KillButton.SetCoolDown(__instance.killTimer, maxTimer); + return false; + } + } +} diff --git a/source/Patches/ImpostorRoles/UnderdogMod/PerformKill.cs b/source/Patches/ImpostorRoles/UnderdogMod/PerformKill.cs index 433bad382..d8a943d85 100644 --- a/source/Patches/ImpostorRoles/UnderdogMod/PerformKill.cs +++ b/source/Patches/ImpostorRoles/UnderdogMod/PerformKill.cs @@ -1,5 +1,6 @@ -using System.Linq; +using System.Linq; using HarmonyLib; +using TownOfUs.Roles; namespace TownOfUs.ImpostorRoles.UnderdogMod { @@ -8,14 +9,15 @@ public class PerformKill { public static void Postfix(PlayerControl __instance, [HarmonyArgument(0)] PlayerControl target) { - if (__instance.Is(RoleEnum.Underdog)) - __instance.SetKillTimer(PlayerControl.GameOptions.KillCooldown * (LastImp() ? 0.5f : 1.5f)); + var role = Role.GetRole(__instance); + if (role?.RoleType == RoleEnum.Underdog) + ((Underdog)role).SetKillTimer(); } internal static bool LastImp() { - return PlayerControl.AllPlayerControls.ToArray().Count(x => x.Data.IsImpostor && !x.Data.IsDead) == - 1; + return PlayerControl.AllPlayerControls.ToArray() + .Count(x => x.Data.IsImpostor && !x.Data.IsDead) == 1; } } -} \ No newline at end of file +} diff --git a/source/Patches/ImpostorRoles/UnderdogMod/PostMeeting.cs b/source/Patches/ImpostorRoles/UnderdogMod/PostMeeting.cs index ea2e978cc..b0b55d5a5 100644 --- a/source/Patches/ImpostorRoles/UnderdogMod/PostMeeting.cs +++ b/source/Patches/ImpostorRoles/UnderdogMod/PostMeeting.cs @@ -1,4 +1,5 @@ using HarmonyLib; +using TownOfUs.Roles; using UnityEngine; namespace TownOfUs.ImpostorRoles.UnderdogMod @@ -8,11 +9,9 @@ public static class HUDClose { public static void Postfix() { - var localPlayer = PlayerControl.LocalPlayer; - if (localPlayer.Is(RoleEnum.Underdog)) - localPlayer.SetKillTimer(PlayerControl.GameOptions.KillCooldown * ( - PerformKill.LastImp() ? 0.5f : 1.5f - )); + var role = Role.GetRole(PlayerControl.LocalPlayer); + if (role?.RoleType == RoleEnum.Underdog) + ((Underdog)role).SetKillTimer(); } } } diff --git a/source/Patches/Roles/Underdog.cs b/source/Patches/Roles/Underdog.cs index 245c490ce..8574275ea 100644 --- a/source/Patches/Roles/Underdog.cs +++ b/source/Patches/Roles/Underdog.cs @@ -1,3 +1,5 @@ +using TownOfUs.ImpostorRoles.UnderdogMod; + namespace TownOfUs.Roles { public class Underdog : Role @@ -11,5 +13,14 @@ public Underdog(PlayerControl player) : base(player) RoleType = RoleEnum.Underdog; Faction = Faction.Impostors; } + + public float MaxTimer() => PlayerControl.GameOptions.KillCooldown * ( + PerformKill.LastImp() ? 0.5f : 1.5f + ); + + public void SetKillTimer() + { + Player.SetKillTimer(MaxTimer()); + } } -} \ No newline at end of file +}