From facecd38c1706daa545b68f5945e6a20e52829bf Mon Sep 17 00:00:00 2001 From: Oscar <29248751+oscar-wos@users.noreply.github.com> Date: Thu, 14 Nov 2024 21:14:27 +0000 Subject: [PATCH] refactor: re-implement bounce --- src/ControllerExtends.cs | 13 +++-------- src/Events.cs | 15 +++++++++++- src/Globals.cs | 5 +++- src/Listeners.cs | 49 +++++++++++++++++++++++++++++++++++++++- src/PlayerData.cs | 7 ++++++ src/Zone.cs | 6 ++--- src/Zones.cs | 6 +++++ 7 files changed, 85 insertions(+), 16 deletions(-) create mode 100644 src/PlayerData.cs diff --git a/src/ControllerExtends.cs b/src/ControllerExtends.cs index 6f1ee34..0f22f64 100644 --- a/src/ControllerExtends.cs +++ b/src/ControllerExtends.cs @@ -1,23 +1,16 @@ using CounterStrikeSharp.API.Core; +using CounterStrikeSharp.API.Modules.Utils; namespace Zones; public static class ControllerExtends { - public static bool IsValid(this CCSPlayerController? controller, bool checkBot = false) - { - if (checkBot) - return controller is { IsValid: true, IsBot: false }; - - return controller is { IsValid: true }; - } - public static void Bounce(this CCSPlayerController? controller) { - if (controller == null || !controller.IsValid() || controller.PlayerPawn.Value == null) + if (controller == null || !controller.IsValid || controller.PlayerPawn.Value == null) return; - var vel = controller.PlayerPawn.Value.AbsVelocity; + var vel = new Vector(controller.PlayerPawn.Value.AbsVelocity.X, controller.PlayerPawn.Value.AbsVelocity.Y, controller.PlayerPawn.Value.AbsVelocity.Z); var speed = Math.Sqrt(vel.X * vel.X + vel.Y * vel.Y); vel *= (-350 / (float)speed); diff --git a/src/Events.cs b/src/Events.cs index 46b0d0c..840b497 100644 --- a/src/Events.cs +++ b/src/Events.cs @@ -1,4 +1,6 @@ -using RetakesPluginShared.Events; +using CounterStrikeSharp.API; +using CounterStrikeSharp.API.Modules.Utils; +using RetakesPluginShared.Events; namespace Zones; @@ -8,5 +10,16 @@ private void OnRetakesEvent(object? sender, IRetakesPluginEvent @event) { if (@event is not AnnounceBombsiteEvent announceBombsiteEvent) return; + + var zones = _zones.Where(z => z.Bombsite == announceBombsiteEvent.Bombsite).ToList(); + + foreach (var controller in Utilities.GetPlayers().Where(c => c is { IsValid: true, PawnIsAlive: true })) + { + if (controller.PlayerPawn.Value == null || !_playerData.TryGetValue(controller, out var playerData)) + continue; + + playerData.Zones = zones.Where(z => z.Teams.Contains((CsTeam)controller.PlayerPawn.Value.TeamNum)).ToList(); + playerData.GreenZones = []; + } } } \ No newline at end of file diff --git a/src/Globals.cs b/src/Globals.cs index 1573c3c..d0a7886 100644 --- a/src/Globals.cs +++ b/src/Globals.cs @@ -1,4 +1,5 @@ -using CounterStrikeSharp.API.Core.Capabilities; +using CounterStrikeSharp.API.Core; +using CounterStrikeSharp.API.Core.Capabilities; using RetakesPluginShared; namespace Zones; @@ -9,5 +10,7 @@ public partial class Zones public override string ModuleVersion => "1.1.0"; public override string ModuleAuthor => "https://github.com/oscar-wos/Retakes-Zones"; private static PluginCapability RetakesPluginEventSenderCapability { get; } = new("retakes_plugin:event_sender"); + private readonly List _zones = []; + private readonly Dictionary _playerData = []; } \ No newline at end of file diff --git a/src/Listeners.cs b/src/Listeners.cs index 8c5eef7..4eb982a 100644 --- a/src/Listeners.cs +++ b/src/Listeners.cs @@ -1,4 +1,5 @@ using CounterStrikeSharp.API; +using Zones.Enums; namespace Zones; @@ -6,6 +7,40 @@ public partial class Zones { private void OnTick() { + foreach (var controller in Utilities.GetPlayers().Where(c => c is { IsValid: true, PawnIsAlive: true })) + { + if (controller.PlayerPawn.Value == null || !_playerData.TryGetValue(controller, out var playerData)) + continue; + + foreach (var zone in playerData.Zones) + { + var isInZone = zone.IsInZone(controller.PlayerPawn.Value.AbsOrigin!); + + if (zone.Type == ZoneType.Red && isInZone) + { + controller.Bounce(); + continue; + } + + if (zone.Type != ZoneType.Green) + continue; + + switch (isInZone) + { + case true when !playerData.GreenZones.Contains(zone): + playerData.GreenZones.Add(zone); + break; + + case false when playerData.GreenZones.Contains(zone): + playerData.GreenZones.Remove(zone); + + if (playerData.GreenZones.Count == 0) + controller.Bounce(); + + break; + } + } + } } private void OnMapStart(string mapName) @@ -17,7 +52,19 @@ private void OnClientPutInServer(int playerSlot) { var controller = Utilities.GetPlayerFromSlot(playerSlot); - if (controller == null || !controller.IsValid()) + if (controller == null || !controller.IsValid) return; + + _playerData[controller] = new PlayerData(); + } + + private void OnClientDisconnect(int playerSlot) + { + var controller = Utilities.GetPlayerFromSlot(playerSlot); + + if (controller == null || !controller.IsValid) + return; + + _playerData.Remove(controller); } } \ No newline at end of file diff --git a/src/PlayerData.cs b/src/PlayerData.cs new file mode 100644 index 0000000..6e41641 --- /dev/null +++ b/src/PlayerData.cs @@ -0,0 +1,7 @@ +namespace Zones; + +public class PlayerData +{ + public List Zones { get; set; } = []; + public List GreenZones { get; set; } = []; +} \ No newline at end of file diff --git a/src/Zone.cs b/src/Zone.cs index bfa37ca..b611b3f 100644 --- a/src/Zone.cs +++ b/src/Zone.cs @@ -6,9 +6,9 @@ namespace Zones; public class Zone(Bombsite bombsite, ZoneType type, CsTeam[] teams, float[] minPoint, float[] maxPoint) { - public Bombsite Bombsite { get; init; } = bombsite; - public ZoneType Type { get; init; } = type; - public CsTeam[] Teams { get; init; } = teams; + public Bombsite Bombsite { get; } = bombsite; + public ZoneType Type { get; } = type; + public CsTeam[] Teams { get; } = teams; private float[] MinPoint { get; } = minPoint; private float[] MaxPoint { get; } = maxPoint; diff --git a/src/Zones.cs b/src/Zones.cs index 974578f..fa1f41c 100644 --- a/src/Zones.cs +++ b/src/Zones.cs @@ -10,6 +10,7 @@ public override void Load(bool isReload) RegisterListener(OnTick); RegisterListener(OnMapStart); RegisterListener(OnClientPutInServer); + RegisterListener(OnClientDisconnect); if (!isReload) return; @@ -21,4 +22,9 @@ public override void OnAllPluginsLoaded(bool isReload) { RetakesPluginEventSenderCapability.Get()!.RetakesPluginEventHandlers += OnRetakesEvent; } + + public override void Unload(bool isReload) + { + RetakesPluginEventSenderCapability.Get()!.RetakesPluginEventHandlers -= OnRetakesEvent; + } } \ No newline at end of file