finalize custom gameover

This commit is contained in:
XtraCube committed 2025-02-27 20:11:24 -05:00
1 parent 530cb79981
commit 90ed9b2a98
8 files changed
+103 -22

No files matched your search

@@ -0,0 +1,24 @@
using MiraAPI.Example.GameOver;
using MiraAPI.Example.Roles;
using MiraAPI.GameEnd;
using MiraAPI.Hud;
using MiraAPI.Utilities.Assets;
using UnityEngine;
namespace MiraAPI.Example.Buttons;
public class NeutralKillerButton : CustomActionButton
{
public override string Name => "Win Game";
public override float Cooldown => 0f;
public override LoadableAsset<Sprite> Sprite => ExampleAssets.ExampleButton;
protected override void OnClick()
{
CustomGameOver.Trigger<NeutralKillerGameOver>([PlayerControl.LocalPlayer.Data]);
}
public override bool Enabled(RoleBehaviour? role)
{
return role is NeutralKillerRole;
}
}
@@ -0,0 +1,15 @@
using MiraAPI.GameEnd;
using MiraAPI.Utilities;
using UnityEngine;
namespace MiraAPI.Example.GameOver;
public class NeutralKillerGameOver : CustomGameOver
{
public override void AfterEndGameSetup(EndGameManager endGameManager)
{
endGameManager.WinText.text = "Outcast Killer Wins!";
endGameManager.WinText.color = Color.magenta;
endGameManager.BackgroundBar.material.SetColor(ShaderID.Color, Color.magenta);
}
}
+5 -3
View File
@@ -1,4 +1,6 @@
using MiraAPI.Roles;
using MiraAPI.Example.GameOver;
using MiraAPI.GameEnd;
using MiraAPI.Roles;
using UnityEngine;
namespace MiraAPI.Example.Roles;
@@ -22,7 +24,7 @@ public class NeutralKillerRole : ImpostorRole, ICustomRole
public TeamIntroConfiguration? IntroConfiguration { get; } = new(
Color.gray,
"NEUTRAL",
"OUTCAST",
"You are an Outcast. You do not have a team.");
public override void SpawnTaskHeader(PlayerControl playerControl)
@@ -32,6 +34,6 @@ public class NeutralKillerRole : ImpostorRole, ICustomRole
public override bool DidWin(GameOverReason gameOverReason)
{
return GameManager.Instance.DidHumansWin(gameOverReason);
return gameOverReason == CustomGameOver.GameOverReason<NeutralKillerGameOver>();
}
}
+16 -6
View File
@@ -16,29 +16,39 @@ public abstract class CustomGameOver
public static CustomGameOver? Instance { get; internal set; }
/// <summary>
/// Run before setting up the game over.
/// Runs before the base game calls EndGameManager.SetEverythingUp.
/// </summary>
/// <param name="endGameManager">The EndGameManager instance.</param>
/// <returns>Return True to use the default implementation, return False to skip it.</returns>
public virtual bool BeforeSetEverythingUp(EndGameManager endGameManager)
/// <returns>Return True to use the run the original method, return False to skip it.</returns>
public virtual bool BeforeEndGameSetup(EndGameManager endGameManager)
{
return true;
}
/// <summary>
/// Run after setting up the game over.
/// Runs after the base game calls EndGameManager.SetEverythingUp.
/// </summary>
/// <param name="endGameManager">The EndGameManager instance.</param>
public virtual void AfterSetEverythingUp(EndGameManager endGameManager)
public virtual void AfterEndGameSetup(EndGameManager endGameManager)
{
}
/// <summary>
/// Get the GameOverReason associated with this CustomGameOver.
/// </summary>
/// <typeparam name="T">Type of the custom game over.</typeparam>
/// <returns>The GameOverReason associated with the custom game over.</returns>
public static GameOverReason GameOverReason<T>() where T : CustomGameOver
{
return (GameOverReason)GameOverManager.GetGameOverId<T>();
}
/// <summary>
/// Send a custom game over.
/// </summary>
/// <param name="winners">A collection of winners.</param>
/// <typeparam name="T">Type of the custom game over.</typeparam>
public static void Send<T>(IEnumerable<NetworkedPlayerInfo> winners) where T : CustomGameOver
public static void Trigger<T>(IEnumerable<NetworkedPlayerInfo> winners) where T : CustomGameOver
{
if (!AmongUsClient.Instance.AmHost)
{
+15 -4
View File
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using Reactor.Utilities;
namespace MiraAPI.GameEnd;
@@ -18,20 +19,30 @@ public static class GameOverManager
/// </summary>
/// <param name="gameOverType">Type of the custom game over.</param>
/// <exception cref="ArgumentException">Thrown when the type is not a subclass of CustomGameOver, is abstract, or does not have a parameterless constructor.</exception>
public static void RegisterGameOver(Type gameOverType)
/// <returns>>True if the game over was registered successfully, false otherwise.</returns>
public static bool RegisterGameOver(Type gameOverType)
{
if (!typeof(CustomGameOver).IsAssignableFrom(gameOverType))
throw new ArgumentException("The type must be a subclass of CustomGameOver.");
{
return false;
}
if (gameOverType.IsAbstract)
throw new ArgumentException("The type must not be abstract.");
{
Logger<MiraApiPlugin>.Error("The type must not be abstract.");
return false;
}
if (gameOverType.GetConstructor(Type.EmptyTypes) == null)
throw new ArgumentException("The type must have a parameterless constructor.");
{
Logger<MiraApiPlugin>.Error("The type must have a parameterless constructor.");
return false;
}
GameOverIds.Add(gameOverType, _nextId);
GameOverTypes.Add(_nextId, gameOverType);
_nextId++;
return true;
}
/// <summary>
+1 -1
View File
@@ -33,7 +33,7 @@ public class CustomGameOverRpc(MiraApiPlugin plugin, uint id) : PlayerCustomRpc<
public override GameOverData Read(MessageReader reader)
{
var reason = reader.ReadPackedInt32();
var count = reader.ReadInt32();
var count = reader.ReadPackedInt32();
var data = new List<NetworkedPlayerInfo>(count);
for (var i = 0; i < count; i++)
{
+2 -2
View File
@@ -15,7 +15,7 @@ internal static class EndGamePatches
return true;
}
var result = gameOver.BeforeSetEverythingUp(__instance);
var result = gameOver.BeforeEndGameSetup(__instance);
return result;
}
@@ -23,7 +23,7 @@ internal static class EndGamePatches
{
if (CustomGameOver.Instance is { } gameOver)
{
gameOver.AfterSetEverythingUp(__instance);
gameOver.AfterEndGameSetup(__instance);
CustomGameOver.Instance = null;
}
+25 -6
View File
@@ -1,6 +1,12 @@
using BepInEx.Unity.IL2CPP;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Reflection;
using BepInEx.Unity.IL2CPP;
using MiraAPI.Colors;
using MiraAPI.Events;
using MiraAPI.GameEnd;
using MiraAPI.GameOptions;
using MiraAPI.GameOptions.Attributes;
using MiraAPI.Hud;
@@ -9,11 +15,6 @@ using MiraAPI.Roles;
using MiraAPI.Utilities;
using Reactor.Networking;
using Reactor.Utilities;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Reflection;
namespace MiraAPI.PluginLoading;
@@ -69,6 +70,11 @@ public sealed class MiraPluginManager
continue;
}
if (RegisterGameOver(type))
{
continue;
}
RegisterColorClasses(type);
}
@@ -96,6 +102,19 @@ public sealed class MiraPluginManager
return Instance._registeredPlugins.Values.FirstOrDefault(plugin => plugin.PluginId == pluginId);
}
private static bool RegisterGameOver(Type type)
{
try
{
return GameOverManager.RegisterGameOver(type);
}
catch (Exception e)
{
Logger<MiraApiPlugin>.Error($"Failed to register game over {type.Name}: {e}");
return false;
}
}
private static bool RegisterOptions(Type type, MiraPluginInfo pluginInfo)
{
try