Skip to content

Commit

Permalink
Refactor markers
Browse files Browse the repository at this point in the history
* Replace drawables in public with interfaces
* Add service for tracking access to markers
* Add separate listener for placing markers
  • Loading branch information
Mooshua committed Feb 8, 2024
1 parent a05d743 commit e2013cc
Show file tree
Hide file tree
Showing 23 changed files with 411 additions and 132 deletions.
7 changes: 7 additions & 0 deletions Jailbreak.sln
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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}
Expand All @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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++)
{
Expand All @@ -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();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<CEnvBeam>("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;
Expand All @@ -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;
}
Expand All @@ -67,4 +56,6 @@ public float GetWidth()
{
return _width;
}
}

public Vector EndPosition => _end;
}
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -20,22 +19,20 @@ 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++)
{
Beams[i]?.Remove();
Beams[i] = null;
}
}
}
}
17 changes: 17 additions & 0 deletions mod/Jailbreak.Lasers/Drawing/DrawableFactory.cs
Original file line number Diff line number Diff line change
@@ -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();
}
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using System.Drawing;

using CounterStrikeSharp.API.Core;
using CounterStrikeSharp.API.Modules.Timers;
using CounterStrikeSharp.API.Modules.Utils;
Expand All @@ -8,13 +10,12 @@ namespace Jailbreak.Public.Mod.Draw;
/// <summary>
/// Represents a drawable shape
/// </summary>
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;
Expand All @@ -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();
}
RemoveInternal();
Drawn = false;
}
}
File renamed without changes.
37 changes: 37 additions & 0 deletions mod/Jailbreak.Lasers/Markers/CurrentMarkerState.cs
Original file line number Diff line number Diff line change
@@ -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;

/// <summary>
/// The time that the marker was placed.
/// Used for resize operations, to avoid making every operation a resize.
/// </summary>
public Instant? PlacedOn { get; set; } = null;

/// <summary>
/// The position of the last ping, for resize purposes.
/// </summary>
public Vector? PlacedAt { get; set; } = null;

/// <summary>
/// Invoked when the player dies or leaves.
/// </summary>
public void Dispose()
{
if (CurrentMarker != null)
{
CurrentMarker.Remove();
CurrentMarker.Dispose();
}
}
}
39 changes: 39 additions & 0 deletions mod/Jailbreak.Lasers/Markers/MarkerBehavior.cs
Original file line number Diff line number Diff line change
@@ -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<MarkerState> _state;

public MarkerBehavior(IPlayerStateFactory playerStateFactory)
{
_state = playerStateFactory.Global<MarkerState>();
}

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;
}
}
Loading

0 comments on commit e2013cc

Please sign in to comment.