From e2013cc12af8e0230f0f945a7b9dd3f8808d826c Mon Sep 17 00:00:00 2001 From: Mooshua <43320783+mooshua@users.noreply.github.com> Date: Wed, 7 Feb 2024 18:34:06 -0800 Subject: [PATCH] Refactor markers * Replace drawables in public with interfaces * Add service for tracking access to markers * Add separate listener for placing markers --- Jailbreak.sln | 7 ++ .../Jailbreak.Lasers/Drawing}/BeamCircle.cs | 12 +-- .../Jailbreak.Lasers/Drawing}/BeamLine.cs | 25 ++---- .../Jailbreak.Lasers/Drawing}/BeamedShape.cs | 11 +-- .../Drawing/DrawableFactory.cs | 17 ++++ .../Drawing}/DrawableShape.cs | 44 +++++++---- .../Jailbreak.Lasers.csproj} | 0 .../Markers/CurrentMarkerState.cs | 37 +++++++++ .../Markers/MarkerBehavior.cs | 39 ++++++++++ .../Markers/MarkerListener.cs | 77 ++++++++++++++++++ mod/Jailbreak.Lasers/Markers/MarkerState.cs | 12 +++ mod/Jailbreak.Warden/Global/WardenBehavior.cs | 19 +++-- mod/Jailbreak.Warden/Jailbreak.Warden.csproj | 4 + .../Markers/WardenMarkerBehavior.cs | 78 ------------------- .../Selection/WardenSelectionBehavior.cs | 1 + .../Jailbreak.Public/Jailbreak.Public.csproj | 1 + .../Mod/{Draw => Lasers}/IColorable.cs | 5 +- .../Jailbreak.Public/Mod/Lasers/IDrawable.cs | 36 +++++++++ .../Mod/Lasers/IDrawableFactory.cs | 45 +++++++++++ public/Jailbreak.Public/Mod/Lasers/ILine.cs | 12 +++ public/Jailbreak.Public/Mod/Lasers/IMarker.cs | 8 ++ .../Mod/Lasers/IMarkerService.cs | 34 ++++++++ .../PlayerState/PlayerStateImpl.cs | 19 ++++- 23 files changed, 411 insertions(+), 132 deletions(-) rename {public/Jailbreak.Public/Mod/Draw => mod/Jailbreak.Lasers/Drawing}/BeamCircle.cs (90%) rename {public/Jailbreak.Public/Mod/Draw => mod/Jailbreak.Lasers/Drawing}/BeamLine.cs (76%) rename {public/Jailbreak.Public/Mod/Draw => mod/Jailbreak.Lasers/Drawing}/BeamedShape.cs (85%) create mode 100644 mod/Jailbreak.Lasers/Drawing/DrawableFactory.cs rename {public/Jailbreak.Public/Mod/Draw => mod/Jailbreak.Lasers/Drawing}/DrawableShape.cs (53%) rename mod/{Jailbreak.Draw/Jailbreak.Draw.csproj => Jailbreak.Lasers/Jailbreak.Lasers.csproj} (100%) create mode 100644 mod/Jailbreak.Lasers/Markers/CurrentMarkerState.cs create mode 100644 mod/Jailbreak.Lasers/Markers/MarkerBehavior.cs create mode 100644 mod/Jailbreak.Lasers/Markers/MarkerListener.cs create mode 100644 mod/Jailbreak.Lasers/Markers/MarkerState.cs delete mode 100644 mod/Jailbreak.Warden/Markers/WardenMarkerBehavior.cs rename public/Jailbreak.Public/Mod/{Draw => Lasers}/IColorable.cs (81%) create mode 100644 public/Jailbreak.Public/Mod/Lasers/IDrawable.cs create mode 100644 public/Jailbreak.Public/Mod/Lasers/IDrawableFactory.cs create mode 100644 public/Jailbreak.Public/Mod/Lasers/ILine.cs create mode 100644 public/Jailbreak.Public/Mod/Lasers/IMarker.cs create mode 100644 public/Jailbreak.Public/Mod/Lasers/IMarkerService.cs diff --git a/Jailbreak.sln b/Jailbreak.sln index 6098dfce..49ae3334 100644 --- a/Jailbreak.sln +++ b/Jailbreak.sln @@ -28,6 +28,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Jailbreak.Logs", "mod\Jailb EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Jailbreak.Debug", "mod\Jailbreak.Debug\Jailbreak.Debug.csproj", "{4F10E2B5-E8BB-4253-9BE6-9187575ADB13}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Jailbreak.Lasers", "mod\Jailbreak.Lasers\Jailbreak.Lasers.csproj", "{FFF49F66-44EE-4BB2-8232-7002718DD3E5}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -74,6 +76,10 @@ Global {4F10E2B5-E8BB-4253-9BE6-9187575ADB13}.Debug|Any CPU.Build.0 = Debug|Any CPU {4F10E2B5-E8BB-4253-9BE6-9187575ADB13}.Release|Any CPU.ActiveCfg = Release|Any CPU {4F10E2B5-E8BB-4253-9BE6-9187575ADB13}.Release|Any CPU.Build.0 = Release|Any CPU + {FFF49F66-44EE-4BB2-8232-7002718DD3E5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {FFF49F66-44EE-4BB2-8232-7002718DD3E5}.Debug|Any CPU.Build.0 = Debug|Any CPU + {FFF49F66-44EE-4BB2-8232-7002718DD3E5}.Release|Any CPU.ActiveCfg = Release|Any CPU + {FFF49F66-44EE-4BB2-8232-7002718DD3E5}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(NestedProjects) = preSolution {9135CCC9-66C5-4A9C-AE3C-91475B5F0437} = {177DA48D-8306-4102-918D-992569878581} @@ -86,5 +92,6 @@ Global {CB2391A1-6CDD-496F-B8D6-674FD6268038} = {36BA84C0-291C-4930-A7C6-97CDF8F7F0D7} {CE6EC648-E7F9-4CE7-B28F-8C7995830F35} = {36BA84C0-291C-4930-A7C6-97CDF8F7F0D7} {4F10E2B5-E8BB-4253-9BE6-9187575ADB13} = {36BA84C0-291C-4930-A7C6-97CDF8F7F0D7} + {FFF49F66-44EE-4BB2-8232-7002718DD3E5} = {36BA84C0-291C-4930-A7C6-97CDF8F7F0D7} EndGlobalSection EndGlobal diff --git a/public/Jailbreak.Public/Mod/Draw/BeamCircle.cs b/mod/Jailbreak.Lasers/Drawing/BeamCircle.cs similarity index 90% rename from public/Jailbreak.Public/Mod/Draw/BeamCircle.cs rename to mod/Jailbreak.Lasers/Drawing/BeamCircle.cs index 58fbb7ed..be83554a 100644 --- a/public/Jailbreak.Public/Mod/Draw/BeamCircle.cs +++ b/mod/Jailbreak.Lasers/Drawing/BeamCircle.cs @@ -3,7 +3,7 @@ namespace Jailbreak.Public.Mod.Draw; -public class BeamCircle : BeamedShape +public class BeamCircle : BeamedShape, IMarker { private readonly BeamLine?[] _lines; private Vector[] _offsets; @@ -37,7 +37,8 @@ private Vector[] GenerateOffsets() return offsets; } - public override void Draw() + + protected override void DrawInternal() { for (var i = 0; i < _lines.Length; i++) { @@ -53,15 +54,16 @@ public override void Draw() } else { - line.Move(start, end); - line.Update(); + line.SetPoints(start, end); } } } + public float Radius => _radius; + public void SetRadius(float radius) { _radius = radius; _offsets = GenerateOffsets(); } -} \ No newline at end of file +} diff --git a/public/Jailbreak.Public/Mod/Draw/BeamLine.cs b/mod/Jailbreak.Lasers/Drawing/BeamLine.cs similarity index 76% rename from public/Jailbreak.Public/Mod/Draw/BeamLine.cs rename to mod/Jailbreak.Lasers/Drawing/BeamLine.cs index 7587ee30..38f5d690 100644 --- a/public/Jailbreak.Public/Mod/Draw/BeamLine.cs +++ b/mod/Jailbreak.Lasers/Drawing/BeamLine.cs @@ -5,7 +5,7 @@ namespace Jailbreak.Public.Mod.Draw; -public class BeamLine : DrawableShape, IColorable +public class BeamLine : DrawableShape, ILine { private CEnvBeam? _beam; private Color _color = Color.White; @@ -17,30 +17,20 @@ public BeamLine(BasePlugin plugin, Vector position, Vector end) : base(plugin, p _end = end; } - public void SetColor(Color color) - { - _color = color; - } - - public Color GetColor() - { - return _color; - } - - public void Move(Vector start, Vector end) + public void SetPoints(Vector start, Vector end) { Position = start; _end = end; } - public override void Draw() + protected override void DrawInternal() { Remove(); var beam = Utilities.CreateEntityByName("env_beam"); if (beam == null) return; beam.RenderMode = RenderMode_t.kRenderTransColor; beam.Width = _width; - beam.Render = GetColor(); + beam.Render = _color; beam.Teleport(Position, new QAngle(), new Vector()); beam.EndPos.X = _end.X; @@ -51,9 +41,8 @@ public override void Draw() Utilities.SetStateChanged(beam, "CBeam", "m_vecEndPos"); } - public override void Remove() + protected override void RemoveInternal() { - KillTimer?.Kill(); _beam?.Remove(); _beam = null; } @@ -67,4 +56,6 @@ public float GetWidth() { return _width; } -} \ No newline at end of file + + public Vector EndPosition => _end; +} diff --git a/public/Jailbreak.Public/Mod/Draw/BeamedShape.cs b/mod/Jailbreak.Lasers/Drawing/BeamedShape.cs similarity index 85% rename from public/Jailbreak.Public/Mod/Draw/BeamedShape.cs rename to mod/Jailbreak.Lasers/Drawing/BeamedShape.cs index 431864f9..11d17b32 100644 --- a/public/Jailbreak.Public/Mod/Draw/BeamedShape.cs +++ b/mod/Jailbreak.Lasers/Drawing/BeamedShape.cs @@ -10,7 +10,6 @@ namespace Jailbreak.Public.Mod.Draw; public abstract class BeamedShape : DrawableShape, IColorable { protected BeamLine?[] Beams; - protected Color Color = Color.White; protected int Resolution; protected BeamedShape(BasePlugin plugin, Vector position, int resolution) : base(plugin, position) @@ -20,17 +19,15 @@ protected BeamedShape(BasePlugin plugin, Vector position, int resolution) : base // TODO: Add support for rotation across arbitrary axis - public Color GetColor() - { - return Color; - } public void SetColor(Color color) { Color = color; } - public override void Remove() + public Color Color { get; protected set; } + + protected override void RemoveInternal() { for (var i = 0; i < Beams.Length; i++) { @@ -38,4 +35,4 @@ public override void Remove() Beams[i] = null; } } -} \ No newline at end of file +} diff --git a/mod/Jailbreak.Lasers/Drawing/DrawableFactory.cs b/mod/Jailbreak.Lasers/Drawing/DrawableFactory.cs new file mode 100644 index 00000000..0b92e764 --- /dev/null +++ b/mod/Jailbreak.Lasers/Drawing/DrawableFactory.cs @@ -0,0 +1,17 @@ +using CounterStrikeSharp.API.Core; +using CounterStrikeSharp.API.Modules.Utils; + +namespace Jailbreak.Public.Mod.Draw; + +public class DrawableFactory : IDrawableFactory +{ + public ILine LineFor(CCSPlayerController? player, Vector from, Vector to) + { + throw new NotImplementedException(); + } + + public IMarker MarkerFor(CCSPlayerController? player, Vector position, float radius) + { + throw new NotImplementedException(); + } +} diff --git a/public/Jailbreak.Public/Mod/Draw/DrawableShape.cs b/mod/Jailbreak.Lasers/Drawing/DrawableShape.cs similarity index 53% rename from public/Jailbreak.Public/Mod/Draw/DrawableShape.cs rename to mod/Jailbreak.Lasers/Drawing/DrawableShape.cs index b027a6f9..c193fbde 100644 --- a/public/Jailbreak.Public/Mod/Draw/DrawableShape.cs +++ b/mod/Jailbreak.Lasers/Drawing/DrawableShape.cs @@ -1,3 +1,5 @@ +using System.Drawing; + using CounterStrikeSharp.API.Core; using CounterStrikeSharp.API.Modules.Timers; using CounterStrikeSharp.API.Modules.Utils; @@ -8,13 +10,12 @@ namespace Jailbreak.Public.Mod.Draw; /// /// Represents a drawable shape /// -public abstract class DrawableShape +public abstract class DrawableShape : IDrawable, IColorable { protected Timer? KillTimer; // Internal timer used to remove the shape after a certain amount of time protected BasePlugin Plugin; - protected Vector Position; // Represents the origin of the shape // Note that this can mean different things for different shapes protected DateTime StartTime = DateTime.Now; @@ -25,28 +26,41 @@ public DrawableShape(BasePlugin plugin, Vector position) Position = position; } - public abstract void Draw(); + public Vector Position { get; protected set; } - public virtual void Update() + public void SetPosition(Vector position) { - Remove(); - Draw(); + Position = position; } - public virtual void Tick() + public Color Color { get; protected set; } + + public void SetColor(Color color) { + Color = color; } - public void Draw(float lifetime) + public bool Drawn { get; protected set; } + + protected abstract void DrawInternal(); + + public void Draw() { - Draw(); - KillTimer = Plugin.AddTimer(lifetime, Remove, TimerFlags.STOP_ON_MAPCHANGE); + if (Drawn) + Remove(); + + DrawInternal(); + Drawn = true; } - public virtual void Move(Vector position) + protected abstract void RemoveInternal(); + + public void Remove() { - Position = position; - } + if (!Drawn) + return; - public abstract void Remove(); -} \ No newline at end of file + RemoveInternal(); + Drawn = false; + } +} diff --git a/mod/Jailbreak.Draw/Jailbreak.Draw.csproj b/mod/Jailbreak.Lasers/Jailbreak.Lasers.csproj similarity index 100% rename from mod/Jailbreak.Draw/Jailbreak.Draw.csproj rename to mod/Jailbreak.Lasers/Jailbreak.Lasers.csproj diff --git a/mod/Jailbreak.Lasers/Markers/CurrentMarkerState.cs b/mod/Jailbreak.Lasers/Markers/CurrentMarkerState.cs new file mode 100644 index 00000000..236c7ca9 --- /dev/null +++ b/mod/Jailbreak.Lasers/Markers/CurrentMarkerState.cs @@ -0,0 +1,37 @@ +using CounterStrikeSharp.API.Modules.Utils; + +using Jailbreak.Public.Mod.Draw; + +using NodaTime; + +namespace Jailbreak.Drawable.Markers; + +public class CurrentMarkerState : IDisposable +{ + public bool HasMarker { get; set; } = false; + + public IMarker? CurrentMarker { get; set; } = null; + + /// + /// The time that the marker was placed. + /// Used for resize operations, to avoid making every operation a resize. + /// + public Instant? PlacedOn { get; set; } = null; + + /// + /// The position of the last ping, for resize purposes. + /// + public Vector? PlacedAt { get; set; } = null; + + /// + /// Invoked when the player dies or leaves. + /// + public void Dispose() + { + if (CurrentMarker != null) + { + CurrentMarker.Remove(); + CurrentMarker.Dispose(); + } + } +} diff --git a/mod/Jailbreak.Lasers/Markers/MarkerBehavior.cs b/mod/Jailbreak.Lasers/Markers/MarkerBehavior.cs new file mode 100644 index 00000000..a2900ea4 --- /dev/null +++ b/mod/Jailbreak.Lasers/Markers/MarkerBehavior.cs @@ -0,0 +1,39 @@ +using CounterStrikeSharp.API.Core; + +using Jailbreak.Public.Behaviors; +using Jailbreak.Public.Generic; +using Jailbreak.Public.Mod.Draw; + +namespace Jailbreak.Drawable.Markers; + +public class MarkerBehavior : IPluginBehavior, IMarkerService +{ + private IPlayerState _state; + + public MarkerBehavior(IPlayerStateFactory playerStateFactory) + { + _state = playerStateFactory.Global(); + } + + public bool TryEnable(CCSPlayerController player) + { + _state.Get(player) + .Enabled = true; + + return true; + } + + public bool TryDisable(CCSPlayerController player) + { + _state.Get(player) + .Enabled = false; + + return true; + } + + public bool IsEnabled(CCSPlayerController player) + { + return _state.Get(player) + .Enabled; + } +} diff --git a/mod/Jailbreak.Lasers/Markers/MarkerListener.cs b/mod/Jailbreak.Lasers/Markers/MarkerListener.cs new file mode 100644 index 00000000..9524dacf --- /dev/null +++ b/mod/Jailbreak.Lasers/Markers/MarkerListener.cs @@ -0,0 +1,77 @@ +using CounterStrikeSharp.API.Core; +using CounterStrikeSharp.API.Core.Attributes.Registration; +using CounterStrikeSharp.API.Modules.Utils; + +using Jailbreak.Public.Behaviors; +using Jailbreak.Public.Extensions; +using Jailbreak.Public.Generic; +using Jailbreak.Public.Mod.Draw; + +using NodaTime; + +namespace Jailbreak.Drawable.Markers; + +public class MarkerListener : IPluginBehavior +{ + public const int DEFAULT_SIZE = 150; + + private IMarkerService _markers; + private IPlayerState _current; + private IDrawableFactory _drawable; + + public MarkerListener(IMarkerService markers, IPlayerStateFactory factory, IDrawableFactory drawable) + { + _markers = markers; + _drawable = drawable; + + // Use alive so the state tracker will dispose + // of the current state when the player dies. + _current = factory.Alive(); + } + + protected void HandlePing(CCSPlayerController player, CurrentMarkerState markerState, Vector position) + { + if (markerState.HasMarker + && markerState.CurrentMarker != null + && markerState.PlacedOn != null + && markerState.PlacedAt != null) + { + // The player already has a marker down. + // Do we qualify for a resize operation? + var elapsed = SystemClock.Instance.GetCurrentInstant() - markerState.PlacedOn; + + if (elapsed <= Duration.FromMilliseconds(750)) + { + // Resize the existing marker + var distance = Math.Clamp(markerState.PlacedAt.Distance(position), 100, 500); + markerState.CurrentMarker.SetRadius(distance); + markerState.CurrentMarker.Draw(); + return; + } + } + + // Create the marker if it does not exist + markerState.HasMarker = true; + markerState.CurrentMarker ??= _drawable.MarkerFor(player, position, DEFAULT_SIZE); + markerState.CurrentMarker.SetPosition(position); + markerState.CurrentMarker.Draw(); + + markerState.PlacedAt = position; + markerState.PlacedOn = SystemClock.Instance.GetCurrentInstant(); + } + + [GameEventHandler] + public HookResult OnPing(EventPlayerPing ev, GameEventInfo info) + { + var player = ev.Userid; + + if (!_markers.IsEnabled(player)) + return HookResult.Handled; + + var vec = new Vector(ev.X, ev.Y, ev.Z); + var current = _current.Get(player); + + this.HandlePing(player, current, vec); + return HookResult.Handled; + } +} diff --git a/mod/Jailbreak.Lasers/Markers/MarkerState.cs b/mod/Jailbreak.Lasers/Markers/MarkerState.cs new file mode 100644 index 00000000..abc00c76 --- /dev/null +++ b/mod/Jailbreak.Lasers/Markers/MarkerState.cs @@ -0,0 +1,12 @@ +using System.Drawing; + +using Jailbreak.Public.Mod.Draw; + +namespace Jailbreak.Drawable.Markers; + +public class MarkerState +{ + public bool Enabled { get; set; } = false; + + public Color Color { get; set; }= Color.White; +} diff --git a/mod/Jailbreak.Warden/Global/WardenBehavior.cs b/mod/Jailbreak.Warden/Global/WardenBehavior.cs index d29352d4..95b46499 100644 --- a/mod/Jailbreak.Warden/Global/WardenBehavior.cs +++ b/mod/Jailbreak.Warden/Global/WardenBehavior.cs @@ -7,6 +7,7 @@ using Jailbreak.Formatting.Views; using Jailbreak.Public.Behaviors; using Jailbreak.Public.Extensions; +using Jailbreak.Public.Mod.Draw; using Jailbreak.Public.Mod.Logs; using Jailbreak.Public.Mod.Warden; using Microsoft.Extensions.Logging; @@ -16,18 +17,20 @@ namespace Jailbreak.Warden.Global; public class WardenBehavior : IPluginBehavior, IWardenService { private ILogger _logger; - private IRichLogService logs; + private IRichLogService _logs; + private IMarkerService _markers; private IWardenNotifications _notifications; private bool _hasWarden; private CCSPlayerController? _warden; - public WardenBehavior(ILogger logger, IWardenNotifications notifications, IRichLogService logs) + public WardenBehavior(ILogger logger, IWardenNotifications notifications, IRichLogService logs, IMarkerService markers) { _logger = logger; _notifications = notifications; - logs = logs; + _logs = logs; + _markers = markers; } /// @@ -61,11 +64,14 @@ public bool TrySetWarden(CCSPlayerController controller) Utilities.SetStateChanged(_warden.Pawn.Value, "CBaseModelEntity", "m_clrRender"); } + // Grant marker perms + _markers.TryEnable(_warden); + _notifications.NEW_WARDEN(_warden) .ToAllChat() .ToAllCenter(); - logs.Append( logs.Player(_warden), "is now the warden."); + _logs.Append( _logs.Player(_warden), "is now the warden."); return true; } @@ -81,7 +87,10 @@ public bool TryRemoveWarden() _warden.Pawn.Value.RenderMode = RenderMode_t.kRenderTransColor; _warden.Pawn.Value.Render = Color.FromArgb(254, 255, 255, 255); Utilities.SetStateChanged(_warden.Pawn.Value, "CBaseModelEntity", "m_clrRender"); - logs.Append( logs.Player(_warden), "is no longer the warden."); + _logs.Append( _logs.Player(_warden), "is no longer the warden."); + + // Remove marker privileges. + _markers.TryDisable(_warden!); } _warden = null; diff --git a/mod/Jailbreak.Warden/Jailbreak.Warden.csproj b/mod/Jailbreak.Warden/Jailbreak.Warden.csproj index 7997b564..4daf26d5 100644 --- a/mod/Jailbreak.Warden/Jailbreak.Warden.csproj +++ b/mod/Jailbreak.Warden/Jailbreak.Warden.csproj @@ -11,4 +11,8 @@ + + + + diff --git a/mod/Jailbreak.Warden/Markers/WardenMarkerBehavior.cs b/mod/Jailbreak.Warden/Markers/WardenMarkerBehavior.cs deleted file mode 100644 index be1190f0..00000000 --- a/mod/Jailbreak.Warden/Markers/WardenMarkerBehavior.cs +++ /dev/null @@ -1,78 +0,0 @@ -using CounterStrikeSharp.API.Core; -using CounterStrikeSharp.API.Core.Attributes.Registration; -using CounterStrikeSharp.API.Modules.Commands; -using CounterStrikeSharp.API.Modules.Utils; -using Jailbreak.Public.Behaviors; -using Jailbreak.Public.Extensions; -using Jailbreak.Public.Mod.Draw; -using Jailbreak.Public.Mod.Warden; - -namespace Jailbreak.Warden.Markers; - -public class WardenMarkerBehavior : IPluginBehavior -{ - private const float MinRadius = 60f, MaxRadius = 360f; - private readonly IWardenService _warden; - - private BeamCircle? _marker; - - private Vector? _currentPos; - private long _placementTime; - private float _radius = MinRadius; - - public WardenMarkerBehavior(IWardenService warden) - { - _warden = warden; - } - - public void Start(BasePlugin plugin) - { - _marker = new BeamCircle(plugin, new Vector(), 60f, (int)Math.PI * 15); - plugin.AddCommandListener("player_ping", CommandListener_PlayerPing); - } - - [GameEventHandler] - public HookResult OnPing(EventPlayerPing @event, GameEventInfo info) - { - var player = @event.Userid; - - if (!_warden.IsWarden(player)) - return HookResult.Handled; - var vec = new Vector(@event.X, @event.Y, @event.Z); - - if (_currentPos != null) - { - var distance = _currentPos.Distance(vec); - var timeElapsed = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds() - _placementTime; - if (timeElapsed < 500) - { - if (distance <= MaxRadius * 1.5) - { - distance = Math.Clamp(distance, MinRadius, MaxRadius); - _marker?.SetRadius(distance); - _marker?.Update(); - _radius = distance; - return HookResult.Handled; - } - } - else if (distance <= _radius) - { - _marker?.Remove(); - return HookResult.Handled; - } - } - - _radius = MinRadius; - _currentPos = vec; - _placementTime = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds(); - _marker?.Move(vec); - _marker?.SetRadius(_radius); - _marker?.Update(); - return HookResult.Handled; - } - - private HookResult CommandListener_PlayerPing(CCSPlayerController? player, CommandInfo info) - { - return _warden.IsWarden(player) ? HookResult.Continue : HookResult.Handled; - } -} \ No newline at end of file diff --git a/mod/Jailbreak.Warden/Selection/WardenSelectionBehavior.cs b/mod/Jailbreak.Warden/Selection/WardenSelectionBehavior.cs index 315a51d6..c057198e 100644 --- a/mod/Jailbreak.Warden/Selection/WardenSelectionBehavior.cs +++ b/mod/Jailbreak.Warden/Selection/WardenSelectionBehavior.cs @@ -8,6 +8,7 @@ using Jailbreak.Public.Behaviors; using Jailbreak.Public.Extensions; using Jailbreak.Public.Generic; +using Jailbreak.Public.Mod.Draw; using Jailbreak.Public.Mod.Warden; using Microsoft.Extensions.Logging; diff --git a/public/Jailbreak.Public/Jailbreak.Public.csproj b/public/Jailbreak.Public/Jailbreak.Public.csproj index 83079e55..a89b5c1b 100644 --- a/public/Jailbreak.Public/Jailbreak.Public.csproj +++ b/public/Jailbreak.Public/Jailbreak.Public.csproj @@ -8,6 +8,7 @@ + diff --git a/public/Jailbreak.Public/Mod/Draw/IColorable.cs b/public/Jailbreak.Public/Mod/Lasers/IColorable.cs similarity index 81% rename from public/Jailbreak.Public/Mod/Draw/IColorable.cs rename to public/Jailbreak.Public/Mod/Lasers/IColorable.cs index b0e9f1fe..55e08956 100644 --- a/public/Jailbreak.Public/Mod/Draw/IColorable.cs +++ b/public/Jailbreak.Public/Mod/Lasers/IColorable.cs @@ -5,5 +5,6 @@ namespace Jailbreak.Public.Mod.Draw; public interface IColorable { void SetColor(Color color); - Color GetColor(); -} \ No newline at end of file + + Color Color { get; } +} diff --git a/public/Jailbreak.Public/Mod/Lasers/IDrawable.cs b/public/Jailbreak.Public/Mod/Lasers/IDrawable.cs new file mode 100644 index 00000000..79c475e5 --- /dev/null +++ b/public/Jailbreak.Public/Mod/Lasers/IDrawable.cs @@ -0,0 +1,36 @@ + +using CounterStrikeSharp.API.Modules.Utils; + +namespace Jailbreak.Public.Mod.Draw; + +public interface IDrawable : IDisposable +{ + + /// + /// The position of the drawable object + /// + Vector Position { get; } + + void SetPosition(Vector position); + + /// + /// Has this object been written to the world? + /// + bool Drawn { get; } + + /// + /// Draw this item + /// The drawable should remove itself if it is currently drawn + /// + void Draw(); + + /// + /// Remove this item + /// + void Remove(); + + void IDisposable.Dispose() + { + this.Remove(); + } +} diff --git a/public/Jailbreak.Public/Mod/Lasers/IDrawableFactory.cs b/public/Jailbreak.Public/Mod/Lasers/IDrawableFactory.cs new file mode 100644 index 00000000..b391fc4f --- /dev/null +++ b/public/Jailbreak.Public/Mod/Lasers/IDrawableFactory.cs @@ -0,0 +1,45 @@ +using CounterStrikeSharp.API.Core; +using CounterStrikeSharp.API.Modules.Utils; + +namespace Jailbreak.Public.Mod.Draw; + +public interface IDrawableFactory +{ + /// + /// Draw a line beginning at from and ending at to. + /// + /// + /// + /// + ILine Line(Vector from, Vector to) + => this.LineFor(null, from, to); + + /// + /// Draw a line beginning at from and ending at to, + /// using the specified player's style configurations. + /// + /// + /// + /// + /// + ILine LineFor(CCSPlayerController? player, Vector from, Vector to); + + /// + /// Draw a marker at the specified position with size "radius". + /// + /// + /// + /// + IMarker Marker(Vector position, float radius) + => MarkerFor(null, position, radius); + + /// + /// Draw a marker at the position with size radius, + /// using the specified player's style configurations. + /// + /// + /// + /// + /// + IMarker MarkerFor(CCSPlayerController? player, Vector position, float radius); +} diff --git a/public/Jailbreak.Public/Mod/Lasers/ILine.cs b/public/Jailbreak.Public/Mod/Lasers/ILine.cs new file mode 100644 index 00000000..2e2c7c49 --- /dev/null +++ b/public/Jailbreak.Public/Mod/Lasers/ILine.cs @@ -0,0 +1,12 @@ +using CounterStrikeSharp.API.Modules.Utils; + +namespace Jailbreak.Public.Mod.Draw; + +public interface ILine : IDrawable, IColorable +{ + /// + /// The second position that makes up this line. + /// The line will terminate here. + /// + Vector EndPosition { get; } +} diff --git a/public/Jailbreak.Public/Mod/Lasers/IMarker.cs b/public/Jailbreak.Public/Mod/Lasers/IMarker.cs new file mode 100644 index 00000000..7efe3d04 --- /dev/null +++ b/public/Jailbreak.Public/Mod/Lasers/IMarker.cs @@ -0,0 +1,8 @@ +namespace Jailbreak.Public.Mod.Draw; + +public interface IMarker : IDrawable, IColorable +{ + float Radius { get; } + + void SetRadius(float radius); +} diff --git a/public/Jailbreak.Public/Mod/Lasers/IMarkerService.cs b/public/Jailbreak.Public/Mod/Lasers/IMarkerService.cs new file mode 100644 index 00000000..0c4931fe --- /dev/null +++ b/public/Jailbreak.Public/Mod/Lasers/IMarkerService.cs @@ -0,0 +1,34 @@ +using System.Drawing; + +using CounterStrikeSharp.API.Core; + +namespace Jailbreak.Public.Mod.Draw; + +public interface IMarkerService +{ + + /// + /// Grant this player the ability to use a marker + /// This persists until is called. + /// Succeeds if the player already has marker permissions. + /// + /// + /// + bool TryEnable(CCSPlayerController player); + + /// + /// Try to remove marker permissions from this player. + /// Succeeds if the player does not have marker permissions in the first place. + /// + /// + /// + bool TryDisable(CCSPlayerController player); + + /// + /// Returns true if the specified player has marker privileges. + /// + /// + /// + bool IsEnabled(CCSPlayerController player); + +} diff --git a/src/Jailbreak.Generic/PlayerState/PlayerStateImpl.cs b/src/Jailbreak.Generic/PlayerState/PlayerStateImpl.cs index 0dd1c4d2..5502be70 100644 --- a/src/Jailbreak.Generic/PlayerState/PlayerStateImpl.cs +++ b/src/Jailbreak.Generic/PlayerState/PlayerStateImpl.cs @@ -18,11 +18,24 @@ public TState Get(CCSPlayerController controller) public void Reset(CCSPlayerController controller) { - _states.Remove(controller.Slot); + ResetInternal(controller.Slot); } public void Drop() { - _states.Clear(); + foreach (var (key, _) in _states) + ResetInternal(key); } -} \ No newline at end of file + + private void ResetInternal(int slot) + { + var entry = _states[slot]; + + // If the state is disposable, + // give the plugin a nice clean place to cleanup the state here. + if (entry is IDisposable disposable) + disposable.Dispose(); + + _states.Remove(slot); + } +}