Skip to content

Commit

Permalink
refactor: re-implement bounce
Browse files Browse the repository at this point in the history
  • Loading branch information
oscar-wos committed Nov 14, 2024
1 parent 9a0d747 commit facecd3
Show file tree
Hide file tree
Showing 7 changed files with 85 additions and 16 deletions.
13 changes: 3 additions & 10 deletions src/ControllerExtends.cs
Original file line number Diff line number Diff line change
@@ -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);
Expand Down
15 changes: 14 additions & 1 deletion src/Events.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using RetakesPluginShared.Events;
using CounterStrikeSharp.API;
using CounterStrikeSharp.API.Modules.Utils;
using RetakesPluginShared.Events;

namespace Zones;

Expand All @@ -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 = [];
}
}
}
5 changes: 4 additions & 1 deletion src/Globals.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using CounterStrikeSharp.API.Core.Capabilities;
using CounterStrikeSharp.API.Core;
using CounterStrikeSharp.API.Core.Capabilities;
using RetakesPluginShared;

namespace Zones;
Expand All @@ -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<IRetakesPluginEventSender> RetakesPluginEventSenderCapability { get; } = new("retakes_plugin:event_sender");

private readonly List<Zone> _zones = [];
private readonly Dictionary<CCSPlayerController, PlayerData> _playerData = [];
}
49 changes: 48 additions & 1 deletion src/Listeners.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,46 @@
using CounterStrikeSharp.API;
using Zones.Enums;

namespace Zones;

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)
Expand All @@ -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);
}
}
7 changes: 7 additions & 0 deletions src/PlayerData.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace Zones;

public class PlayerData
{
public List<Zone> Zones { get; set; } = [];
public List<Zone> GreenZones { get; set; } = [];
}
6 changes: 3 additions & 3 deletions src/Zone.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
6 changes: 6 additions & 0 deletions src/Zones.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ public override void Load(bool isReload)
RegisterListener<Listeners.OnTick>(OnTick);
RegisterListener<Listeners.OnMapStart>(OnMapStart);
RegisterListener<Listeners.OnClientPutInServer>(OnClientPutInServer);
RegisterListener<Listeners.OnClientDisconnect>(OnClientDisconnect);

if (!isReload)
return;
Expand All @@ -21,4 +22,9 @@ public override void OnAllPluginsLoaded(bool isReload)
{
RetakesPluginEventSenderCapability.Get()!.RetakesPluginEventHandlers += OnRetakesEvent;
}

public override void Unload(bool isReload)
{
RetakesPluginEventSenderCapability.Get()!.RetakesPluginEventHandlers -= OnRetakesEvent;
}
}

0 comments on commit facecd3

Please sign in to comment.