feat: add BasePower abstraction
This commit is contained in:
@@ -0,0 +1,110 @@
|
||||
using System.Text;
|
||||
using CounterStrikeSharp.API;
|
||||
using CounterStrikeSharp.API.Core;
|
||||
using CounterStrikeSharp.API.Modules.Events;
|
||||
using Cs2SuperPowers.Players;
|
||||
|
||||
namespace Cs2SuperPowers.Powers;
|
||||
|
||||
public abstract class BasePower(IPlayerHud playerHud) : ISuperPower
|
||||
{
|
||||
private readonly HashSet<ulong> _playerIds = new();
|
||||
|
||||
protected BasePlugin? Plugin { get; private set; }
|
||||
|
||||
public abstract int Id { get; }
|
||||
public abstract string Name { get; }
|
||||
public abstract string Description { get; }
|
||||
public abstract char ChatColor { get; }
|
||||
public abstract string HtmlColor { get; }
|
||||
public virtual bool CanBeRandomlyAssigned => true;
|
||||
public virtual int MinimumPlayersRequired => 0;
|
||||
|
||||
public void Initialize(BasePlugin pluginRef)
|
||||
{
|
||||
Plugin = pluginRef;
|
||||
OnInitialize();
|
||||
}
|
||||
|
||||
protected abstract void OnInitialize();
|
||||
|
||||
public virtual void AssignToPlayer(CCSPlayerController player, bool showMessages)
|
||||
{
|
||||
_playerIds.Add(player.SteamID);
|
||||
|
||||
if (showMessages)
|
||||
{
|
||||
player.PrintToChat($"You have been granted the power of {ChatColor}{Name}");
|
||||
player.PrintToChat(Description);
|
||||
|
||||
var centerMessageBuilder = new StringBuilder();
|
||||
centerMessageBuilder.Append("<font class='fontSize-l' class='fontWeight-Bold' color='#FFFFFF'>Your superpower:</font> <br>");
|
||||
centerMessageBuilder.Append($"<font class='fontSize-l' class='fontWeight-Bold' color='{HtmlColor}'>{Name}</font> <br>");
|
||||
centerMessageBuilder.Append($"<font class='fontSize-s' class='fontWeight-Normal' color='#FFFFFF'>{Description}</font> <br>");
|
||||
playerHud.ShowMessage(player, centerMessageBuilder.ToString(), TimeSpan.FromSeconds(10));
|
||||
}
|
||||
}
|
||||
|
||||
public virtual void UnassignFromPlayer(CCSPlayerController player)
|
||||
{
|
||||
_playerIds.Remove(player.SteamID);
|
||||
}
|
||||
|
||||
private readonly List<Delegate> _registeredDelegates = new();
|
||||
|
||||
protected void RegisterEventHandler<T>(BasePlugin.GameEventHandler<T> handler, HookMode hookMode = HookMode.Post)
|
||||
where T : GameEvent
|
||||
{
|
||||
_registeredDelegates.Add(handler);
|
||||
Plugin!.RegisterEventHandler(handler, hookMode);
|
||||
}
|
||||
|
||||
protected void RegisterListener<T>(T handler) where T : Delegate
|
||||
{
|
||||
_registeredDelegates.Add(handler);
|
||||
Plugin!.RegisterListener(handler);
|
||||
}
|
||||
|
||||
protected void AddTimer(float seconds, Action callback, bool repeat = false)
|
||||
{
|
||||
if (repeat)
|
||||
{
|
||||
_registeredDelegates.Add(callback);
|
||||
Plugin!.AddTimer(seconds, callback);
|
||||
}
|
||||
else
|
||||
{
|
||||
Action? wrappedCallback = null;
|
||||
wrappedCallback = () =>
|
||||
{
|
||||
callback();
|
||||
if (wrappedCallback != null)
|
||||
{
|
||||
_registeredDelegates.Remove(wrappedCallback);
|
||||
}
|
||||
};
|
||||
_registeredDelegates.Add(wrappedCallback);
|
||||
Plugin!.AddTimer(seconds, wrappedCallback);
|
||||
}
|
||||
}
|
||||
|
||||
protected void NextFrame(Action callback)
|
||||
{
|
||||
Action? wrappedCallback = null;
|
||||
wrappedCallback = () =>
|
||||
{
|
||||
callback();
|
||||
if (wrappedCallback != null)
|
||||
{
|
||||
_registeredDelegates.Remove(wrappedCallback);
|
||||
}
|
||||
};
|
||||
_registeredDelegates.Add(wrappedCallback);
|
||||
Server.NextFrame(wrappedCallback);
|
||||
}
|
||||
|
||||
protected bool IsAssignedTo(CCSPlayerController player)
|
||||
{
|
||||
return _playerIds.Contains(player.SteamID);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user