feat: add BasePower abstraction
This commit is contained in:
@@ -0,0 +1,26 @@
|
||||
using CounterStrikeSharp.API.Core;
|
||||
|
||||
namespace Cs2SuperPowers.Players;
|
||||
|
||||
public static class Ensure
|
||||
{
|
||||
public static ValidationBuilder<CCSPlayerController> Player(CCSPlayerController? player)
|
||||
{
|
||||
return new ValidationBuilder<CCSPlayerController>(player);
|
||||
}
|
||||
}
|
||||
|
||||
public class ValidationBuilder<TEntity>(TEntity? entity)
|
||||
{
|
||||
private readonly Queue<IValidation<TEntity>> _validations = new();
|
||||
|
||||
public void Add(IValidation<TEntity> validation)
|
||||
{
|
||||
_validations.Enqueue(validation);
|
||||
}
|
||||
|
||||
public bool Check()
|
||||
{
|
||||
return _validations.All(v => v.Validate(entity));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
using CounterStrikeSharp.API.Core;
|
||||
|
||||
namespace Cs2SuperPowers.Players;
|
||||
|
||||
public interface IPlayerHud {
|
||||
void Initialize(BasePlugin pluginRef);
|
||||
void ShowMessage(CCSPlayerController player, string message, TimeSpan? duration = null);
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
namespace Cs2SuperPowers.Players;
|
||||
|
||||
public interface IValidation<in TEntity>
|
||||
{
|
||||
bool Validate(TEntity? entity);
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
using CounterStrikeSharp.API;
|
||||
using CounterStrikeSharp.API.Core;
|
||||
|
||||
namespace Cs2SuperPowers.Players;
|
||||
|
||||
public static class PlayerControllerExtensions
|
||||
{
|
||||
public static CCSPlayerController SetNewHealth(this CCSPlayerController player, int health)
|
||||
{
|
||||
var pawn = player.PlayerPawn.Value;
|
||||
if (pawn == null) return player;
|
||||
|
||||
player.MaxHealth = health;
|
||||
pawn.MaxHealth = health;
|
||||
|
||||
player.Health = health;
|
||||
pawn.Health = health;
|
||||
|
||||
Utilities.SetStateChanged(pawn, "CBaseEntity", "m_iHealth");
|
||||
return player;
|
||||
}
|
||||
|
||||
public static CCSPlayerController SetMaxHealth(this CCSPlayerController player, int maxHealth)
|
||||
{
|
||||
var pawn = player.PlayerPawn.Value;
|
||||
if (pawn == null) return player;
|
||||
|
||||
player.MaxHealth = maxHealth;
|
||||
pawn.MaxHealth = maxHealth;
|
||||
|
||||
player.Health = Math.Min(maxHealth, player.Health);
|
||||
pawn.Health = Math.Min(maxHealth, pawn.Health);;
|
||||
|
||||
Utilities.SetStateChanged(pawn, "CBaseEntity", "m_iHealth");
|
||||
return player;
|
||||
}
|
||||
|
||||
public static float GetSpeed(this CCSPlayerController player)
|
||||
{
|
||||
var pawn = player.PlayerPawn.Value;
|
||||
if (pawn == null) return 1.0f;
|
||||
return pawn.VelocityModifier;
|
||||
}
|
||||
|
||||
public static CCSPlayerController SetNewSpeed(this CCSPlayerController player, float speed)
|
||||
{
|
||||
var pawn = player.PlayerPawn.Value;
|
||||
if (pawn == null) return player;
|
||||
|
||||
pawn.VelocityModifier = speed;
|
||||
Utilities.SetStateChanged(pawn, "CCSPlayer_MovementServices", "m_flVelocityModifier");
|
||||
return player;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
using CounterStrikeSharp.API;
|
||||
using CounterStrikeSharp.API.Core;
|
||||
|
||||
namespace Cs2SuperPowers.Players;
|
||||
|
||||
public class PlayerHud : IPlayerHud
|
||||
{
|
||||
private readonly Dictionary<CCSPlayerController, DisposableMessage> _playerMessages = new();
|
||||
private readonly List<Delegate> _registeredDelegates = new();
|
||||
|
||||
public void Initialize(BasePlugin pluginRef)
|
||||
{
|
||||
Listeners.OnTick onTickListener = OnTick;
|
||||
_registeredDelegates.Add(onTickListener);
|
||||
pluginRef.RegisterListener(onTickListener);
|
||||
}
|
||||
|
||||
public void ShowMessage(CCSPlayerController player, string message, TimeSpan? duration = null)
|
||||
{
|
||||
duration ??= TimeSpan.FromSeconds(10);
|
||||
if (_playerMessages.ContainsKey(player))
|
||||
{
|
||||
_playerMessages.Remove(player);
|
||||
}
|
||||
_playerMessages.Add(player, new DisposableMessage(message, DateTime.UtcNow.Add(duration.Value)));
|
||||
}
|
||||
|
||||
private void OnTick()
|
||||
{
|
||||
var toRemove = new List<CCSPlayerController>();
|
||||
|
||||
foreach (var playerMessage in _playerMessages.ToList())
|
||||
{
|
||||
if (playerMessage.Value.DisplayUntil >= DateTime.UtcNow)
|
||||
{
|
||||
playerMessage.Key.PrintToCenterHtml(playerMessage.Value.Message);
|
||||
}
|
||||
else
|
||||
{
|
||||
toRemove.Add(playerMessage.Key);
|
||||
}
|
||||
}
|
||||
|
||||
foreach (var player in toRemove)
|
||||
{
|
||||
Action? wrappedCallback = null;
|
||||
wrappedCallback = () =>
|
||||
{
|
||||
_playerMessages.Remove(player);
|
||||
if (wrappedCallback != null)
|
||||
{
|
||||
_registeredDelegates.Remove(wrappedCallback);
|
||||
}
|
||||
};
|
||||
_registeredDelegates.Add(wrappedCallback);
|
||||
Server.NextFrame(wrappedCallback);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
record DisposableMessage(string Message, DateTime DisplayUntil);
|
||||
@@ -0,0 +1,20 @@
|
||||
using CounterStrikeSharp.API.Core;
|
||||
|
||||
namespace Cs2SuperPowers.Players.PlayerValidations;
|
||||
|
||||
public class IsAlive : IValidation<CCSPlayerController>
|
||||
{
|
||||
public bool Validate(CCSPlayerController? player)
|
||||
{
|
||||
return player?.PlayerPawn.Value?.LifeState == (byte)LifeState_t.LIFE_ALIVE;
|
||||
}
|
||||
}
|
||||
|
||||
public static class IsAliveExtensions
|
||||
{
|
||||
public static ValidationBuilder<CCSPlayerController> IsAlive(this ValidationBuilder<CCSPlayerController> builder)
|
||||
{
|
||||
builder.Add(new IsAlive());
|
||||
return builder;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
using CounterStrikeSharp.API.Core;
|
||||
|
||||
namespace Cs2SuperPowers.Players.PlayerValidations;
|
||||
|
||||
public class IsValid : IValidation<CCSPlayerController>
|
||||
{
|
||||
public bool Validate(CCSPlayerController? player)
|
||||
{
|
||||
return player?.IsValid == true;
|
||||
}
|
||||
}
|
||||
|
||||
public static class IsValidExtensions
|
||||
{
|
||||
public static ValidationBuilder<CCSPlayerController> IsValid(this ValidationBuilder<CCSPlayerController> builder)
|
||||
{
|
||||
builder.Add(new IsValid());
|
||||
return builder;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user