Skip to content

Commit

Permalink
fix neutral killing issues and rename some properties in ICustomRole …
Browse files Browse the repository at this point in the history
…to clear confusion
  • Loading branch information
XtraCube committed Sep 2, 2024
1 parent f8bb634 commit f207849
Show file tree
Hide file tree
Showing 6 changed files with 107 additions and 17 deletions.
27 changes: 27 additions & 0 deletions MiraAPI.Example/Roles/NeutralKillerRole.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using MiraAPI.Roles;
using UnityEngine;

namespace MiraAPI.Example.Roles;

[RegisterCustomRole]
public class NeutralKillerRole : ImpostorRole, ICustomRole
{
public string RoleName => "Neutral Killer";
public string RoleDescription => "Neutral who can kill.";
public string RoleLongDescription => RoleDescription;
public Color RoleColor => Color.magenta;
public ModdedRoleTeams Team => ModdedRoleTeams.Neutral;
public bool CanUseKill => true;
public bool CanGetKilled => true;
public bool CanUseVent => true;

public override void SpawnTaskHeader(PlayerControl playerControl)
{
// remove existing task header.
}

public override bool DidWin(GameOverReason gameOverReason)
{
return GameManager.Instance.DidHumansWin(gameOverReason);
}
}
20 changes: 10 additions & 10 deletions MiraAPI/Networking/CustomMurderRpc.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
using AmongUs.GameOptions;
using System.Collections;
using System.Linq;
using AmongUs.GameOptions;
using Assets.CoreScripts;
using BepInEx.Unity.IL2CPP.Utils;
using Reactor.Networking.Attributes;
using Reactor.Utilities;
using Reactor.Utilities.Extensions;
using System.Collections;
using System.Linq;
using UnityEngine;

namespace MiraAPI.Networking;
Expand Down Expand Up @@ -71,7 +72,7 @@ public static void CustomMurder(
bool playKillSound = true)
{
source.isKilling = false;
Logger<MiraApiPlugin>.Debug($"{source.PlayerId} trying to murder {target.PlayerId}");
Logger<MiraApiPlugin>.Error($"{source.PlayerId} trying to murder {target.PlayerId}");
var data = target.Data;
if (resultFlags.HasFlag(MurderResultFlags.FailedError))
{
Expand Down Expand Up @@ -104,7 +105,7 @@ public static void CustomMurder(
target.RemoveProtection();
}

Logger<MiraApiPlugin>.Debug($"{source.PlayerId} failed to murder {target.PlayerId} due to guardian angel protection");
Logger<MiraApiPlugin>.Error($"{source.PlayerId} failed to murder {target.PlayerId} due to guardian angel protection");
return;
}

Expand Down Expand Up @@ -134,8 +135,7 @@ public static void CustomMurder(

if (resetKillTimer)
{
source.SetKillTimer(
GameOptionsManager.Instance.CurrentGameOptions.GetFloat(FloatOptionNames.KillCooldown));
source.SetKillTimer(GameOptionsManager.Instance.CurrentGameOptions.GetFloat(FloatOptionNames.KillCooldown));
}
}

Expand Down Expand Up @@ -172,8 +172,8 @@ public static void CustomMurder(
source.CurrentOutfitType == PlayerOutfitType.Shapeshifted,
source.shapeshiftTargetPlayerId,
target.PlayerId);
Coroutines.Start(source.KillAnimations.Random()?.CoPerformCustomKill(source, target, createDeadBody, teleportMurderer));
Logger<MiraApiPlugin>.Debug($"{source.PlayerId} succeeded in murdering {target.PlayerId}");
source.MyPhysics.StartCoroutine(source.KillAnimations.Random()?.CoPerformCustomKill(source, target, createDeadBody, teleportMurderer));
Logger<MiraApiPlugin>.Error($"{source.PlayerId} succeeded in murdering {target.PlayerId}");
}

/// <summary>
Expand Down Expand Up @@ -262,7 +262,7 @@ public static IEnumerator CoPerformCustomKill(

if (cam != null)
{
cam.Locked = true;
cam.Locked = false;
}

PlayerControl.LocalPlayer.isKilling = false;
Expand Down
2 changes: 0 additions & 2 deletions MiraAPI/Patches/HudManagerPatches.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
using HarmonyLib;
using InnerNet;
using MiraAPI.Hud;
using MiraAPI.Roles;
using Reactor.Utilities.Extensions;
using UnityEngine;
using Object = UnityEngine.Object;
Expand Down
65 changes: 65 additions & 0 deletions MiraAPI/Patches/Roles/KillButtonPatches.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
using HarmonyLib;
using Il2CppSystem;
using MiraAPI.Networking;
using MiraAPI.Roles;
using UnityEngine;

namespace MiraAPI.Patches.Roles;

/// <summary>
/// Fix kill button issues for neutral killers.
/// </summary>
[HarmonyPatch(typeof(KillButton))]
public static class KillButtonPatches
{
[HarmonyPrefix]
[HarmonyPatch(nameof(KillButton.SetTarget))]
public static bool SetTargetPrefix(KillButton __instance, PlayerControl target)
{
if (!PlayerControl.LocalPlayer || PlayerControl.LocalPlayer.Data == null || !PlayerControl.LocalPlayer.Data.Role)
{
return false;
}

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

if (__instance.currentTarget && __instance.currentTarget != target)
{
__instance.currentTarget.cosmetics.SetOutline(false, new Nullable<Color>(Color.clear));
}
__instance.currentTarget = target;
if (__instance.currentTarget)
{
__instance.currentTarget.cosmetics.SetOutline(true, new Nullable<Color>(customRole.RoleColor));
__instance.SetEnabled();
return false;
}
__instance.SetDisabled();

return false;
}

[HarmonyPrefix]
[HarmonyPatch(nameof(KillButton.DoClick))]
public static bool DoClickPrefix(KillButton __instance)
{
if (PlayerControl.LocalPlayer.Data.Role is not ICustomRole)
{
return true;
}

if (!__instance.isActiveAndEnabled || !__instance.currentTarget || __instance.isCoolingDown ||
PlayerControl.LocalPlayer.Data.IsDead || !PlayerControl.LocalPlayer.CanMove)
{
return false;
}

PlayerControl.LocalPlayer.RpcCustomMurder(__instance.currentTarget);
__instance.SetTarget(null);

return false;
}
}
2 changes: 1 addition & 1 deletion MiraAPI/Roles/CustomRoleManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ internal static void RegisterRoleTypes(List<Type> roles, MiraPluginInfo pluginIn
roleBehaviour.StringName = CustomStringName.CreateAndRegister(customRole.RoleName);
roleBehaviour.BlurbName = CustomStringName.CreateAndRegister(customRole.RoleDescription);
roleBehaviour.BlurbNameLong = CustomStringName.CreateAndRegister(customRole.RoleLongDescription);
roleBehaviour.AffectedByLightAffectors = customRole.AffectedByLight;
roleBehaviour.AffectedByLightAffectors = customRole.AffectedByLightOnAirship;
roleBehaviour.CanBeKilled = customRole.CanGetKilled;
roleBehaviour.CanUseKillButton = customRole.CanUseKill;
roleBehaviour.TasksCountTowardProgress = customRole.TasksCountForProgress;
Expand Down
8 changes: 4 additions & 4 deletions MiraAPI/Roles/ICustomRole.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,9 @@ public interface ICustomRole
ConfigDefinition ChanceConfigDefinition => new("Roles", $"Chance {GetType().FullName}");

/// <summary>
/// Gets a value indicating whether the role is affected by light sabotages.
/// Gets a value indicating whether the role is affected by light affectors on Airship.
/// </summary>
bool AffectedByLight => Team == ModdedRoleTeams.Crewmate;
bool AffectedByLightOnAirship => Team == ModdedRoleTeams.Crewmate;

/// <summary>
/// Gets a value indicating whether the role can be killed by others.
Expand All @@ -80,7 +80,7 @@ public interface ICustomRole
/// <summary>
/// Gets a value indicating whether the role can kill others.
/// </summary>
bool CanKill => Team == ModdedRoleTeams.Impostor;
bool CanUseKill => Team == ModdedRoleTeams.Impostor;

/// <summary>
/// Gets a value indicating whether the role can use vents.
Expand All @@ -90,7 +90,7 @@ public interface ICustomRole
/// <summary>
/// Gets a value indicating whether the role's tasks count towards task progress.
/// </summary>
bool TasksCount => Team == ModdedRoleTeams.Crewmate;
bool TasksCountForProgress => Team == ModdedRoleTeams.Crewmate;

/// <summary>
/// Gets a value indicating whether the role is a Ghost.
Expand Down

0 comments on commit f207849

Please sign in to comment.