Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Start work on warden laser #13

Merged
merged 1 commit into from
Feb 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 87 additions & 0 deletions mod/Jailbreak.Warden/Paint/WardenPaintBehavior.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
using System.Drawing;
using CounterStrikeSharp.API;
using CounterStrikeSharp.API.Core;
using CounterStrikeSharp.API.Modules.Timers;
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.Paint;

public class WardenPaintBehavior : IPluginBehavior
{
private IWardenService _warden;
private BasePlugin parent;

public WardenPaintBehavior(IWardenService warden)

Check warning on line 18 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.

Check warning on line 18 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.
{
_warden = warden;
}

public void Start(BasePlugin parent)
{
this.parent = parent;
parent.AddTimer(0.5f, Paint, TimerFlags.REPEAT);
}

private void Paint()
{
if (!_warden.HasWarden)
return;
var warden = _warden.Warden;
if (warden == null || !warden.IsValid)
return;

if ((warden.Buttons & PlayerButtons.Use) == 0)
return;
if (warden.Pawn.Value == null || warden.PlayerPawn.Value == null)
return;
CBasePlayerPawn pawn = warden.Pawn.Value;
CCSPlayerPawn playerPawn = warden.PlayerPawn.Value;
if (!pawn.IsValid || !playerPawn.IsValid || pawn.CameraServices == null)
return;

CPlayer_CameraServices camera = pawn.CameraServices;

var start = pawn.LookTargetPosition;

new BeamCircle(parent, start, 40, 5).Draw(15f);

Vector cameraOrigin = new Vector(pawn?.AbsOrigin?.X, pawn?.AbsOrigin?.Y,
pawn.AbsOrigin.Z + camera.OldPlayerViewOffsetZ);

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

View workflow job for this annotation

GitHub Actions / build

Dereference of a possibly null reference.

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

View workflow job for this annotation

GitHub Actions / build

Dereference of a possibly null reference.

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

View workflow job for this annotation

GitHub Actions / build

Dereference of a possibly null reference.

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

View workflow job for this annotation

GitHub Actions / build

Dereference of a possibly null reference.
QAngle eye_angle = warden.PlayerPawn.Value.EyeAngles;
double pitch = (Math.PI / 180) * eye_angle.X;
double yaw = (Math.PI / 180) * eye_angle.Y;
Vector eye_vector = new Vector((float)(Math.Cos(pitch) * Math.Cos(yaw)),
(float)(Math.Cos(pitch) * Math.Sin(yaw)), (float)(-Math.Sin(pitch)));

start = FindFloorIntersection(cameraOrigin, eye_vector, pawn.AbsOrigin.Z);
if (start == null)
return;
var circle = new BeamCircle(parent, start, 40, 5);
circle.SetColor(Color.Red);
circle.Draw(15f);
}

private Vector? FindFloorIntersection(Vector start, Vector angle, float z)
{
float pitch = angle.X; // 90 = straight down, -90 = straight up
// normalize so 0 = straight down, 180 = straight up
pitch = (pitch + 270) % 360;
if (pitch > 180)
pitch -= 180;
if (pitch >= 90)
return null;
float angle_a = 90;
float side_b = z;
float angle_c = pitch;

float side_a = (float)(angle_c * Math.Sin(angle_a) / Math.Sin(180 - angle_a - angle_c));

Vector destination = start.Clone();
destination.Add(angle.Clone().Normalize().Scale(side_a));
return destination;
}
}
2 changes: 2 additions & 0 deletions mod/Jailbreak.Warden/WardenServiceExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using Jailbreak.Warden.Commands;
using Jailbreak.Warden.Global;
using Jailbreak.Warden.Markers;
using Jailbreak.Warden.Paint;
using Jailbreak.Warden.Selection;

using Microsoft.Extensions.DependencyInjection;
Expand All @@ -20,5 +21,6 @@ public static void AddJailbreakWarden(this IServiceCollection serviceCollection)

serviceCollection.AddPluginBehavior<WardenCommandsBehavior>();
serviceCollection.AddPluginBehavior<WardenMarkerBehavior>();
serviceCollection.AddPluginBehavior<WardenPaintBehavior>();
}
}
42 changes: 42 additions & 0 deletions public/Jailbreak.Public/Extensions/VectorExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
using CounterStrikeSharp.API.Modules.Utils;

namespace Jailbreak.Public.Extensions;

public static class VectorExtensions
{
public static Vector Clone(this Vector vector)
{
var vec = new Vector
{
X = vector.X,
Y = vector.Y,
Z = vector.Z
};
return vec;
}

public static Vector Add(this Vector vector, Vector other)
{
vector.X += other.X;
vector.Y += other.Y;
vector.Z += other.Z;
return vector;
}

public static Vector Scale(this Vector vector, float scale)
{
vector.X *= scale;
vector.Y *= scale;
vector.Z *= scale;
return vector;
}

public static Vector Normalize(this Vector vector)
{
var length = vector.Length();
vector.X /= length;
vector.Y /= length;
vector.Z /= length;
return vector;
}
}
Loading