Skip to content

Commit

Permalink
feat: Implement delays to zones
Browse files Browse the repository at this point in the history
feat: Implement ZoneType.Hurt & ZoneType.Kill
  • Loading branch information
oscar-wos committed Jun 27, 2024
1 parent f666882 commit 387c7ca
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 17 deletions.
6 changes: 3 additions & 3 deletions src/Globals.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ namespace AntiRush;
[MinimumApiVersion(245)]
public partial class AntiRush
{
public override string ModuleName => "Anti-Rush";
public override string ModuleName => "AntiRush";
public override string ModuleVersion => "1.0.0";
public override string ModuleAuthor => "https://github.com/oscar-wos/Anti-Rush";
public override string ModuleAuthor => "https://github.com/oscar-wos/AntiRush";
public Menu.Menu Menu { get; } = new();

private string Prefix { get; } = $"[{ChatColors.Lime}Anti-Rush{ChatColors.White}] ";
private string Prefix { get; } = $"[{ChatColors.Lime}AntiRush{ChatColors.White}] ";
private readonly List<Zone> _zones = [];
private readonly Dictionary<CCSPlayerController, PlayerData> _playerData = [];
}
75 changes: 66 additions & 9 deletions src/Listeners.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using AntiRush.Enums;
using System.Diagnostics;
using AntiRush.Enums;
using CounterStrikeSharp.API;
using CounterStrikeSharp.API.Core;
using CounterStrikeSharp.API.Modules.Utils;

namespace AntiRush;
Expand All @@ -15,21 +17,36 @@ private void OnTick()

foreach (var zone in _zones)
{
if (!zone.IsInZone(pos))
continue;
var isInZone = zone.IsInZone(pos);

if (zone.Type == ZoneType.Bounce)
if (!isInZone)
{
bounce = true;
BouncePlayer(controller);
zone.Entry.Remove(controller);
continue;
}

if (zone.Type == ZoneType.Teleport)
if (!zone.Teams.Contains(controller.Team))
continue;

if (zone.Delay != 0 && !zone.Entry.ContainsKey(controller))
zone.Entry[controller] = Server.CurrentTime;

if (zone.Delay != 0)
{
controller.PlayerPawn.Value!.Teleport(_playerData[controller].SpawnPos, controller.PlayerPawn!.Value.EyeAngles, Vector.Zero);
var diff = (zone.Entry[controller] + zone.Delay) - Server.CurrentTime;

if (diff > 0)
{
if (diff % 1 == 0)
controller!.PrintToChat($"{Prefix} {FormatZoneString(zone.Type)} in {diff} seconds");
}
else
DoAction(controller, zone);

continue;
}

DoAction(controller, zone);
}

if (bounce)
Expand All @@ -40,8 +57,48 @@ private void OnTick()
}
}

private void DoAction(CCSPlayerController controller, Zone zone)
{
switch (zone.Type)
{
case ZoneType.Bounce:
BouncePlayer(controller);
break;

case ZoneType.Hurt:
if (Server.CurrentTime % 1 == 0)
{
controller.PlayerPawn.Value!.Health -= zone.Damage;
Utilities.SetStateChanged(controller.PlayerPawn.Value, "CBaseEntity", "m_iHealth");

if (controller.PlayerPawn.Value.Health <= 0)
controller.PlayerPawn.Value.CommitSuicide(true, true);
}

break;

case ZoneType.Kill:
controller.PlayerPawn.Value!.CommitSuicide(true, true);
break;

case ZoneType.Teleport:
controller.PlayerPawn.Value!.Teleport(_playerData[controller].SpawnPos, controller.PlayerPawn!.Value.EyeAngles, Vector.Zero);
break;
}
}

private string FormatZoneString(ZoneType type)
{
return type switch

Check warning on line 92 in src/Listeners.cs

View workflow job for this annotation

GitHub Actions / build

The switch expression does not handle all possible values of its input type (it is not exhaustive). For example, the pattern 'AntiRush.Enums.ZoneType.Bounce' is not covered.

Check warning on line 92 in src/Listeners.cs

View workflow job for this annotation

GitHub Actions / build

The switch expression does not handle all possible values of its input type (it is not exhaustive). For example, the pattern 'AntiRush.Enums.ZoneType.Bounce' is not covered.

Check warning on line 92 in src/Listeners.cs

View workflow job for this annotation

GitHub Actions / release

The switch expression does not handle all possible values of its input type (it is not exhaustive). For example, the pattern 'AntiRush.Enums.ZoneType.Bounce' is not covered.

Check warning on line 92 in src/Listeners.cs

View workflow job for this annotation

GitHub Actions / release

The switch expression does not handle all possible values of its input type (it is not exhaustive). For example, the pattern 'AntiRush.Enums.ZoneType.Bounce' is not covered.
{
ZoneType.Hurt => $"{ChatColors.Orange}{Localizer["zone.Hurt"]}{ChatColors.White}",
ZoneType.Kill => $"{ChatColors.Red}{Localizer["zone.Kill"]}{ChatColors.White}",
ZoneType.Teleport => $"{ChatColors.Magenta}{Localizer["zone.Teleport"]}{ChatColors.White}",
};
}

private void OnMapStart(string mapName)
{
LoadJson(mapName);
}
}
}
13 changes: 9 additions & 4 deletions src/Menus.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,16 @@ public partial class AntiRush
{
private void BuildMenu(CCSPlayerController controller, MenuType type = MenuType.Main)
{
if (!controller.IsValid)
if (!IsValidPlayer(controller))
return;

if (_playerData[controller].AddZone != null)
{
for (var i = 0; i < 2; i++)
_playerData[controller].AddZone!.Points[i] = Vector.Zero;

_playerData[controller].AddZone = null;
}

switch (type)
{
Expand All @@ -34,7 +39,7 @@ private void BuildMenu(CCSPlayerController controller, MenuType type = MenuType.

private void BuildMainMenu(CCSPlayerController controller, bool updateMenu = false)
{
var mainMenu = new MenuBase(new MenuValue("Anti-Rush") { Prefix = "<font class=\"fontSize-l\">", Suffix = "<font class=\"fontSize-m\">" });
var mainMenu = new MenuBase(new MenuValue("AntiRush") { Prefix = "<font class=\"fontSize-l\">", Suffix = "<font class=\"fontSize-m\">" });

var customButtons = new List<MenuValue>
{
Expand Down Expand Up @@ -96,7 +101,7 @@ private void BuildMainMenu(CCSPlayerController controller, bool updateMenu = fal

private void BuildAddZoneMenu(CCSPlayerController controller)
{
var addZoneMenu = new AddZoneMenu(new MenuValue("Add Zone") { Suffix = "<font class=\"fontSize-m\">" }) { Input = new MenuValue("____") { Prefix = "<font color=\"#00FF00\">", Suffix = "<font color=\"#FFFFFF\">" } };
var addZoneMenu = new AddZoneMenu(new MenuValue(Localizer["menu.Add"]) { Suffix = "<font class=\"fontSize-m\">" }) { Input = new MenuValue("____") { Prefix = "<font color=\"#00FF00\">", Suffix = "<font color=\"#FFFFFF\">" } };

if (_playerData[controller].AddZone == null)
addZoneMenu.AddItem(new MenuItem(MenuItemType.Text, new MenuValue(Localizer["menu.Shoot", "1"])));
Expand Down Expand Up @@ -131,7 +136,7 @@ private void BuildAddZoneMenu(CCSPlayerController controller)
? new MenuItem(MenuItemType.Input, new MenuValue(Localizer["menu.Damage"]))
: new MenuItem(MenuItemType.Spacer));

addZoneMenu.AddItem(new MenuItem(MenuItemType.Button, [new CustomButton(Localizer["menu.Save"], c => SaveZone(c)) { Prefix = "<font color=\"#ADD8E6\">" }]));
addZoneMenu.AddItem(new MenuItem(MenuItemType.Button, [new CustomButton(Localizer["menu.Save"], SaveZone) { Prefix = "<font color=\"#ADD8E6\">" }]));

addZoneMenu.Items[3].DataString = "0.0";
addZoneMenu.Items[4].DataString = "10";
Expand Down
5 changes: 4 additions & 1 deletion src/Zone.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using CounterStrikeSharp.API.Modules.Utils;
using CounterStrikeSharp.API.Core;
using CounterStrikeSharp.API.Modules.Utils;
using AntiRush.Enums;


namespace AntiRush;

public class Zone(ZoneType type, CsTeam[] teams, Vector minPoint, Vector maxPoint, string name, float delay, int damage)
Expand All @@ -12,6 +14,7 @@ public class Zone(ZoneType type, CsTeam[] teams, Vector minPoint, Vector maxPoin
public string Name { get; init; } = name;
public float Delay { get; init; } = delay;
public int Damage { get; init; } = damage;
public Dictionary<CCSPlayerController, float> Entry { get; } = [];

public bool IsInZone(Vector point)
{
Expand Down

0 comments on commit 387c7ca

Please sign in to comment.