Skip to content

Commit

Permalink
Feat/warden laser (#15)
Browse files Browse the repository at this point in the history
* Start work on warden laser

* Working commit

* Add warden beam

* Improvements to drawing
  • Loading branch information
MSWS authored Feb 4, 2024
1 parent 971e93e commit a4caf4e
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 28 deletions.
35 changes: 34 additions & 1 deletion mod/Jailbreak.Warden/Markers/WardenMarkerBehavior.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
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;

Expand All @@ -12,7 +13,12 @@ public class WardenMarkerBehavior : IPluginBehavior
{
private readonly IWardenService _warden;

private DrawableShape? _marker;
private BeamCircle? _marker;

private Vector? currentPos;
private const float MIN_RADIUS = 60f, MAX_RADIUS = 360f;
private float radius = MIN_RADIUS;
private long placementTime = 0;

public WardenMarkerBehavior(IWardenService warden)
{
Expand All @@ -33,7 +39,34 @@ public HookResult OnPing(EventPlayerPing @event, GameEventInfo info)
if (!_warden.IsWarden(player))
return HookResult.Handled;
Vector vec = new Vector(@event.X, @event.Y, @event.Z);

if (currentPos != null)
{
float distance = currentPos.Distance(vec);
long timeElapsed = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds() - placementTime;
if (timeElapsed < 500)
{
if (distance <= MAX_RADIUS * 1.5)
{
distance = Math.Clamp(distance, MIN_RADIUS, MAX_RADIUS);
_marker?.SetRadius(distance);
_marker?.Update();
radius = distance;
return HookResult.Handled;
}
}
else if (distance <= radius)
{
_marker?.Remove();
return HookResult.Handled;
}
}

radius = MIN_RADIUS;
currentPos = vec;
placementTime = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
_marker?.Move(vec);
_marker?.SetRadius(radius);
_marker?.Update();
return HookResult.Handled;
}
Expand Down
24 changes: 3 additions & 21 deletions mod/Jailbreak.Warden/Paint/WardenPaintBehavior.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ public class WardenPaintBehavior : IPluginBehavior
private IWardenService _warden;
private BasePlugin parent;
private Vector? _lastPosition;
private BeamLine? _beamLine;

public WardenPaintBehavior(IWardenService warden)

Check warning on line 19 in mod/Jailbreak.Warden/Paint/WardenPaintBehavior.cs

View workflow job for this annotation

GitHub Actions / build

Non-nullable field 'parent' must contain a non-null value when exiting constructor. Consider declaring the field as nullable.
{
Expand All @@ -25,59 +24,42 @@ public WardenPaintBehavior(IWardenService warden)
public void Start(BasePlugin parent)
{
this.parent = parent;
// parent.AddTimer(0.1f, Paint, TimerFlags.REPEAT);
parent.RegisterListener<Listeners.OnTick>(Paint);
}

private void Paint()
{
if (!_warden.HasWarden)
{
_beamLine?.Remove();
_beamLine = null;
return;
}

var warden = _warden.Warden;
if (warden == null || !warden.IsValid)
return;

if ((warden.Buttons & PlayerButtons.Use) == 0)
{
_beamLine?.Remove();
_beamLine = null;
return;
}

Vector? position = FindFloorIntersection(warden);
if (position == null)
return;

if (_beamLine == null)
{
_beamLine = new BeamLine(parent, position, position);
_beamLine.SetColor(Color.FromArgb(200, 150, 150, 255));
_beamLine.Draw();
}

var start = _lastPosition ?? position;
start = start.Clone();

var eyeLocation = warden.Pawn.Value!.AbsOrigin!.Clone();
eyeLocation.Z += warden.Pawn.Value!.CameraServices!.OldPlayerViewOffsetZ;
if (_lastPosition != null && position.DistanceSquared(_lastPosition) < 25 * 25)
{
return;
}

_lastPosition = position;
if (start.DistanceSquared(position) > 150 * 150 || start.Z - position.Z > 0.001f)
{
start = position;
}

new BeamLine(parent, start.Clone(), position.Clone()).Draw(10f);
_beamLine.Move(eyeLocation, position.Clone());
_beamLine.Update();
}

private Vector? FindFloorIntersection(CCSPlayerController player)
Expand Down
2 changes: 1 addition & 1 deletion public/Jailbreak.Public/Extensions/VectorExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public static Vector Clone(this Vector vector)
};
return vec;
}

public static Vector Add(this Vector vector, Vector other)
{
vector.X += other.X;
Expand Down
6 changes: 6 additions & 0 deletions public/Jailbreak.Public/Mod/Draw/BeamCircle.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,10 @@ public override void Draw()
}
}
}

public void SetRadius(float radius)
{
this.radius = radius;
offsets = generateOffsets();
}
}
6 changes: 1 addition & 5 deletions public/Jailbreak.Public/Mod/Draw/BeamLine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,7 @@ public void Move(Vector start, Vector end)

public override void Draw()
{
if(this.beam != null)
{
this.beam.Remove();
this.beam = null;
}
Remove();
var beam = Utilities.CreateEntityByName<CEnvBeam>("env_beam");
if (beam == null) return;
beam.RenderMode = RenderMode_t.kRenderTransColor;
Expand Down

0 comments on commit a4caf4e

Please sign in to comment.