67 lines
2.0 KiB
C#
67 lines
2.0 KiB
C#
using CounterStrikeSharp.API;
|
|
using CounterStrikeSharp.API.Core;
|
|
using CounterStrikeSharp.API.Modules.Utils;
|
|
using Cs2SuperPowers.Players;
|
|
using Cs2SuperPowers.Players.PlayerValidations;
|
|
|
|
namespace Cs2SuperPowers.Powers;
|
|
|
|
public class OneShot(IPlayerHud playerHud) : BasePower(playerHud)
|
|
{
|
|
private const double InstantKillChance = 0.25;
|
|
private readonly Random _random = new();
|
|
|
|
public override int Id => 23;
|
|
public override string Name => "One Shot";
|
|
|
|
public override string Description => $"Each damaging hit has a {(int)(InstantKillChance * 100)}% chance to instantly eliminate the enemy.";
|
|
|
|
public override char ChatColor => ChatColors.Magenta;
|
|
|
|
public override string HtmlColor => "#ff5CD9";
|
|
|
|
protected override void OnInitialize()
|
|
{
|
|
RegisterEventHandler<EventPlayerHurt>(OnPlayerHurt);
|
|
}
|
|
|
|
private HookResult OnPlayerHurt(EventPlayerHurt @event, GameEventInfo info)
|
|
{
|
|
var attacker = @event.Attacker;
|
|
var victim = @event.Userid;
|
|
|
|
if (!Ensure.Player(attacker).IsValid().IsAlive().Check() ||
|
|
!Ensure.Player(victim).IsValid().Check() ||
|
|
attacker == victim ||
|
|
!IsAssignedTo(attacker!))
|
|
{
|
|
return HookResult.Continue;
|
|
}
|
|
|
|
// Check for at least 1 player in the opposing team
|
|
var attackerTeam = attacker!.Team;
|
|
var enemyCount = Utilities.GetPlayers().Count(p =>
|
|
p.IsValid && p.Team != CsTeam.None && p.Team != CsTeam.Spectator && p.Team != attackerTeam);
|
|
if (enemyCount < 1)
|
|
{
|
|
return HookResult.Continue;
|
|
}
|
|
|
|
var victimPawn = victim!.PlayerPawn.Value;
|
|
if (victimPawn == null)
|
|
{
|
|
return HookResult.Continue;
|
|
}
|
|
|
|
if (_random.NextDouble() > InstantKillChance)
|
|
{
|
|
return HookResult.Continue;
|
|
}
|
|
|
|
victimPawn.Health = 0;
|
|
Utilities.SetStateChanged(victimPawn, "CBaseEntity", "m_iHealth");
|
|
|
|
return HookResult.Continue;
|
|
}
|
|
}
|