Skip to content

Commit

Permalink
Start fleshing out debug tool
Browse files Browse the repository at this point in the history
  • Loading branch information
Twinki14 committed Feb 3, 2024
1 parent a4ba091 commit fa48994
Show file tree
Hide file tree
Showing 7 changed files with 177 additions and 13 deletions.
49 changes: 49 additions & 0 deletions src/PolyZone.Debug/Drawing/Circle.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
using System.Drawing;
using CitizenFX.Core;
using CitizenFX.Core.Native;
using PolyZone.Extensions;
using PolyZone.Zones;

namespace PolyZone.Debug.Drawing;

public static class CircleDrawingExtensions
{
/// <summary>
/// Draws the circle in the world for the player, uses <see cref="API.GetGroundZFor_3dCoord"/> to determine the Z level
/// </summary>
/// <param name="circleZone">The <see cref="CircleZone"/></param>
/// <param name="color">The <see cref="Color"/></param>
public static void Draw(this CircleZone circleZone, in Color color) => circleZone.Draw(color, out _);

public static void Draw(this CircleZone circleZone, in Color color, out Vector3 centerOfCircle)
{
var groundZ = 999.0f;

API.GetGroundZFor_3dCoord(circleZone.Center.X, circleZone.Center.Y, groundZ, ref groundZ, false);

centerOfCircle = new Vector3(circleZone.Center.X, circleZone.Center.Y, groundZ);

World.DrawMarker(MarkerType.VerticalCylinder,
centerOfCircle,
Vector3.Zero,
Vector3.Zero,
new Vector3(circleZone.Radius * 2, circleZone.Radius * 2, 0.5f),
color);
}

public static void DrawDebug(this CircleZone circleZone, Entity? comparativeEntity = null)
{
comparativeEntity ??= Game.Player.Character;

const int alpha = 150;
var insideColor = Color.FromArgb(alpha, 0, 255, 0);
var outsideColor = Color.FromArgb(alpha, 255, 0, 0);

var isInside = comparativeEntity.IsInside(circleZone);
var color = isInside ? insideColor : outsideColor;

circleZone.Draw(color, out var centerOfCircle);

Internal.Drawing.Draw3dText(centerOfCircle, $"Distance: { comparativeEntity.DistanceTo(circleZone) }", color, 5.0f);
}
}
55 changes: 55 additions & 0 deletions src/PolyZone.Debug/Drawing/Polygon.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
using System.Drawing;
using CitizenFX.Core;
using CitizenFX.Core.Native;
using PolyZone.Shapes.Interfaces;

namespace PolyZone.Debug.Drawing;

public static class PolygonExtensions
{
public static IPolygon Draw(this IPolygon polygon, Color color)
{
/*for (var i = 0; i < polygon.Points.Count - 1; i++)
{
var startPoint = polygon.Points[i];
var endPoint = polygon.Points[i + 1];
DrawPoly(startPoint, endPoint, r, g, b, a);
}*/

var start = polygon.Points.ElementAt(polygon.Points.Count - 1);
var end = polygon.Points.ElementAt(0);

var bottomLeft = new Vector3(start.X, start.Y, 200);
var topLeft = new Vector3(start.X, start.Y, 400);
var bottomRight = new Vector3(end.X, end.Y, 200);
var topRight = new Vector3(end.X, end.Y, 400);

API.DrawPoly(
bottomLeft.X, bottomLeft.Y, bottomLeft.Z,
topLeft.X, topLeft.Y, topLeft.Z,
bottomRight.X, bottomRight.Y, bottomLeft.Z,
color.R, color.G, color.B, color.A);

API.DrawPoly(
topLeft.X, topLeft.Y, topLeft.Z,
topRight.X, topRight.Y, topRight.Z,
bottomRight.X, bottomRight.Y, bottomLeft.Z,
color.R, color.G, color.B, color.A);

API.DrawPoly(
bottomRight.X, bottomRight.Y, bottomRight.Z,
topRight.X, topRight.Y, topRight.Z,
topLeft.X, topLeft.Y, topLeft.Z,
color.R, color.G, color.B, color.A);

API.DrawPoly(
bottomRight.X, bottomRight.Y, bottomRight.Z,
topLeft.X, topLeft.Y, topLeft.Z,
bottomLeft.X, bottomLeft.Y, bottomLeft.Z,
color.R, color.G, color.B, color.A);


return polygon;
}
}
38 changes: 38 additions & 0 deletions src/PolyZone.Debug/Internal/Drawing.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
using System.Drawing;
using CitizenFX.Core;
using CitizenFX.Core.Native;

namespace PolyZone.Debug.Internal;

internal static class Drawing
{
internal static void Draw3dText(Vector3 pos, string text, Color color, float size = 0.35f)
{
float _x = 0;
float _y = 0;

var onScreen = API.World3dToScreen2d(pos.X, pos.Y, pos.Z, ref _x, ref _y);
var pCoords = API.GetGameplayCamCoords();

var distance = API.GetDistanceBetweenCoords(pCoords.X,pCoords.Y,pCoords.Z, pos.X, pos.Y, pos.Z, true);
var txtScale = 1 / distance * 2;
var fov = 1 / API.GetGameplayCamFov() * 100;
var scale = txtScale * fov * size;

if (onScreen)
{
API.SetTextScale(0.0f, scale);
API.SetTextFont(4);
API.SetTextProportional(true);
API.SetTextColour(color.R, color.G, color.B, color.A);
API.SetTextDropShadow();
API.SetTextEdge(0, 0, 0, 0, 150);
API.SetTextDropShadow();
API.SetTextOutline();
API.SetTextEntry("STRING");
API.SetTextCentre(false);
API.AddTextComponentString(text);
API.DrawText(_x, _y);
}
}
}
18 changes: 13 additions & 5 deletions src/PolyZone.Debug/PolyZone.Debug.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,26 @@
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<LangVersion>12</LangVersion>
<OutputPath>./bin/fivem/client</OutputPath>
<AssemblyName>CitizenFX.Extensions.PolyZone.Debug.net</AssemblyName>
<OutputPath>./bin/polyzone-debug/client</OutputPath>
<AssemblyName>polyzone.debug.net</AssemblyName>
<AppendTargetFrameworkToOutputPath>false</AppendTargetFrameworkToOutputPath>
<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<Folder Include="Drawing\" />
<PackageReference Include="CitizenFX.Core.Client" Version="1.0.7277" />
<PackageReference Include="CitizenFX.Extensions.Client.Serilog" Version="2.12.0" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="CitizenFX.Core.Client" Version="1.0.7277" />
<None Include="fxmanifest.lua" >
<TargetPath>../fxmanifest.lua</TargetPath>
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\PolyZone\PolyZone.csproj" />
</ItemGroup>

</Project>
20 changes: 20 additions & 0 deletions src/PolyZone.Debug/Script.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,28 @@
using CitizenFX.Core;
using PolyZone.Debug.Drawing;
using PolyZone.Zones;
using Serilog;
using Serilog.Sinks;

namespace PolyZone.Debug;

public class Script : BaseScript
{
private readonly CircleZone _circleZone;

public Script()
{
Log.Logger = new LoggerConfiguration()
.MinimumLevel.Debug()
.WriteTo.FiveM()
.CreateLogger();

_circleZone = new CircleZone(new Vector2(242, -1312), 15.0f);
}

[Tick]
public async Task OnTick()

Check warning on line 24 in src/PolyZone.Debug/Script.cs

View workflow job for this annotation

GitHub Actions / build

This async method lacks 'await' operators and will run synchronously. Consider using the 'await' operator to await non-blocking API calls, or 'await Task.Run(...)' to do CPU-bound work on a background thread.
{
_circleZone.DrawDebug();
}
}
6 changes: 0 additions & 6 deletions src/PolyZone.Debug/Shapes/Polygons.cs

This file was deleted.

4 changes: 2 additions & 2 deletions src/PolyZone.Debug/fxmanifest.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ description 'A resource to debug & test CitizenFX.Extensions.PolyZone'
fx_version 'bodacious'
game 'gta5'

file 'publish/**/*.dll'
file 'client/**/*.dll'

client_script 'publish/client/client.net.dll'
client_script 'client/polyzone.debug.net.dll'

0 comments on commit fa48994

Please sign in to comment.