Role singleton

This commit is contained in:
ang-xd committed 2024-09-14 15:47:02 -04:00
1 parent fd75c99850
commit dd6d57343e
4 files changed
+53 -12

No files matched your search

-1
View File
@@ -15,7 +15,6 @@ public class ChameloenRole : CrewmateRole, ICustomRole
public CustomRoleConfiguration Configuration => new CustomRoleConfiguration(this)
{
MaxRoleCount = 0,
OptionsScreenshot = ExampleAssets.Banner,
};
+19
View File
@@ -0,0 +1,19 @@
using System.Linq;
namespace MiraAPI.Roles;
/// <summary>
/// A utility class to get the instance of a custom role.
/// </summary>
/// <typeparam name="T">The role you are trying to access.</typeparam>
public static class CustomRoleSingleton<T> where T : ICustomRole
{
private static ICustomRole? _instance;
/// <summary>
/// Gets the instance of the role.
/// </summary>
#pragma warning disable CA1000
public static ICustomRole Instance => _instance ??= CustomRoleManager.CustomRoles.Values.OfType<T>().Single();
#pragma warning restore CA1000
}
+33 -10
View File
@@ -37,18 +37,11 @@ public interface ICustomRole
/// </summary>
ModdedRoleTeams Team { get; }
/// <summary>
/// Configure advanced settings of the role.
/// </summary>
CustomRoleConfiguration Configuration { get; }
/// <summary>
/// Gets the BepInEx ConfigDefinition for the amount of players that can have this role.
/// </summary>
ConfigDefinition NumConfigDefinition => new("Roles", $"Num {GetType().FullName}");
/// <summary>
/// Gets the BepInEx ConfigDefinition for the chance of this role being selected.
/// </summary>
ConfigDefinition ChanceConfigDefinition => new("Roles", $"Chance {GetType().FullName}");
/// <summary>
/// Gets the parent mod of this role.
/// </summary>
@@ -62,6 +55,36 @@ public interface ICustomRole
{
}
internal ConfigDefinition NumConfigDefinition => new("Roles", $"Num {GetType().FullName}");
internal ConfigDefinition ChanceConfigDefinition => new("Roles", $"Chance {GetType().FullName}");
/// <summary>
/// Get the role chance option.
/// </summary>
/// <returns>The role chance option.</returns>
public int? GetChance()
{
if (ParentMod.PluginConfig.TryGetEntry(ChanceConfigDefinition, out ConfigEntry<int> entry))
{
return Mathf.Clamp(entry.Value, 0, 100);
}
return null;
}
/// <summary>
/// Get the role count option.
/// </summary>
/// <returns>The role count option.</returns>
public int? GetCount()
{
if (ParentMod.PluginConfig.TryGetEntry(NumConfigDefinition, out ConfigEntry<int> entry))
{
return Mathf.Clamp(entry.Value, 0, Configuration.MaxRoleCount);
}
return null;
}
/// <summary>
/// This method runs on the HudManager.Update method ONLY when the LOCAL player has this role.
/// </summary>
+1 -1
View File
@@ -16,7 +16,7 @@ public enum RoleHintType
TaskHint,
/// <summary>
/// Use custom role tab.
/// Use Mira's custom role tab.
/// </summary>
RoleTab,
}