Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add CanUseSabotage Property to ICustomRole #13

Merged
merged 2 commits into from
Sep 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions MiraAPI/Patches/HudManagerPatches.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using HarmonyLib;
using MiraAPI.Hud;
using MiraAPI.Roles;
using Reactor.Utilities.Extensions;
using UnityEngine;
using Object = UnityEngine.Object;
Expand Down
3 changes: 2 additions & 1 deletion MiraAPI/Patches/Roles/HudManagerPatches.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,11 @@ public static void SetHudActivePostfix(
{
var flag = localPlayer.Data != null && localPlayer.Data.IsDead;

if (role is ICustomRole)
if (role is ICustomRole customRole)
{
__instance.KillButton.ToggleVisible(isActive && role.CanUseKillButton && !flag);
__instance.ImpostorVentButton.ToggleVisible(isActive && role.CanVent && !flag);
__instance.SabotageButton.gameObject.SetActive(isActive && customRole.CanUseSabotage);
}

if (_roleTab)
Expand Down
62 changes: 62 additions & 0 deletions MiraAPI/Patches/Roles/SabotageButtonPatch.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
using HarmonyLib;
using MiraAPI.Roles;

namespace MiraAPI.Patches.Roles;

[HarmonyPatch(typeof(SabotageButton))]
public class SabotageButtonPatch
{
/// <summary>
/// Patches the Sabotage button to check if the player's custom role can use sabotage.
/// </summary>
[HarmonyPatch(nameof(SabotageButton.DoClick))]
[HarmonyPrefix]
public static bool DoClickPrefix(SabotageButton __instance)
{
var player = PlayerControl.LocalPlayer;

if (player.Data.Role is not ICustomRole customRole)
{
return true;
}

if (!customRole.CanUseSabotage || PlayerControl.LocalPlayer.inVent || !GameManager.Instance.SabotagesEnabled())
{
return false;
}

DestroyableSingleton<HudManager>.Instance.ToggleMapVisible(
new MapOptions
{
Mode = MapOptions.Modes.Sabotage,
});
return false;
}

[HarmonyPrefix]
[HarmonyPatch(nameof(SabotageButton.Refresh))]
public static bool RefreshPrefix(SabotageButton __instance)
{
var player = PlayerControl.LocalPlayer;
if (GameManager.Instance == null || player == null)
{
__instance.ToggleVisible(false);
__instance.SetDisabled();
return false;
}

if (player.Data.Role is not ICustomRole customRole)
{
return true;
}

if (player.inVent || !GameManager.Instance.SabotagesEnabled() || player.petting)
{
__instance.ToggleVisible(customRole.CanUseSabotage && GameManager.Instance.SabotagesEnabled());
__instance.SetDisabled();
return false;
}
__instance.SetEnabled();
return false;
}
}
5 changes: 5 additions & 0 deletions MiraAPI/Roles/ICustomRole.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,11 @@ public interface ICustomRole
/// </summary>
bool CanUseVent => Team == ModdedRoleTeams.Impostor;

/// <summary>
/// Gets a value indicating whether the role can use the sabotage button.
/// </summary>
bool CanUseSabotage => Team == ModdedRoleTeams.Impostor;

/// <summary>
/// Gets a value indicating whether the role's tasks count towards task progress.
/// </summary>
Expand Down
Loading