feat(zones): add setDebug and debugColour (#369)

This commit is contained in:
BerkieBb
2023-07-23 11:37:29 +02:00
committed by GitHub
parent 84b7f97a1c
commit 8f5880aad7
2 changed files with 42 additions and 21 deletions

View File

@@ -4,6 +4,9 @@ local glm = require 'glm'
---@field id number
---@field coords vector3
---@field distance number
---@field type 'poly' | 'sphere' | 'box'
---@field debugColour vector4?
---@field setDebug fun(self: CZone)
---@field remove fun()
---@field contains fun(self: CZone, coords?: vector3): boolean
---@field onEnter fun(self: CZone)?
@@ -117,7 +120,7 @@ CreateThread(function()
for _, zone in pairs(Zones) do
zone.distance = #(zone.coords - coords)
local radius, contains = zone.radius
local radius, contains = zone.radius, nil
if radius then
contains = zone.distance < radius
@@ -211,12 +214,8 @@ local DrawPoly = DrawPoly
local function debugPoly(self)
for i = 1, #self.triangles do
local triangle = self.triangles[i]
DrawPoly(triangle[1].x, triangle[1].y, triangle[1].z, triangle[2].x, triangle[2].y, triangle[2].z, triangle[3].x
,
triangle[3].y, triangle[3].z, 255, 42, 24, 100)
DrawPoly(triangle[2].x, triangle[2].y, triangle[2].z, triangle[1].x, triangle[1].y, triangle[1].z, triangle[3].x
,
triangle[3].y, triangle[3].z, 255, 42, 24, 100)
DrawPoly(triangle[1].x, triangle[1].y, triangle[1].z, triangle[2].x, triangle[2].y, triangle[2].z, triangle[3].x, triangle[3].y, triangle[3].z, self.debugColour.r, self.debugColour.g, self.debugColour.b, self.debugColour.a)
DrawPoly(triangle[2].x, triangle[2].y, triangle[2].z, triangle[1].x, triangle[1].y, triangle[1].z, triangle[3].x, triangle[3].y, triangle[3].z, self.debugColour.r, self.debugColour.g, self.debugColour.b, self.debugColour.a)
end
for i = 1, #self.polygon do
local thickness = vec(0, 0, self.thickness / 2)
@@ -224,19 +223,19 @@ local function debugPoly(self)
local b = self.polygon[i] - thickness
local c = (self.polygon[i + 1] or self.polygon[1]) + thickness
local d = (self.polygon[i + 1] or self.polygon[1]) - thickness
DrawLine(a.x, a.y, a.z, b.x, b.y, b.z, 255, 42, 24, 225)
DrawLine(a.x, a.y, a.z, c.x, c.y, c.z, 255, 42, 24, 225)
DrawLine(b.x, b.y, b.z, d.x, d.y, d.z, 255, 42, 24, 225)
DrawPoly(a.x, a.y, a.z, b.x, b.y, b.z, c.x, c.y, c.z, 255, 42, 24, 100)
DrawPoly(c.x, c.y, c.z, b.x, b.y, b.z, a.x, a.y, a.z, 255, 42, 24, 100)
DrawPoly(b.x, b.y, b.z, c.x, c.y, c.z, d.x, d.y, d.z, 255, 42, 24, 100)
DrawPoly(d.x, d.y, d.z, c.x, c.y, c.z, b.x, b.y, b.z, 255, 42, 24, 100)
DrawLine(a.x, a.y, a.z, b.x, b.y, b.z, self.debugColour.r, self.debugColour.g, self.debugColour.b, 225)
DrawLine(a.x, a.y, a.z, c.x, c.y, c.z, self.debugColour.r, self.debugColour.g, self.debugColour.b, 225)
DrawLine(b.x, b.y, b.z, d.x, d.y, d.z, self.debugColour.r, self.debugColour.g, self.debugColour.b, 225)
DrawPoly(a.x, a.y, a.z, b.x, b.y, b.z, c.x, c.y, c.z, self.debugColour.r, self.debugColour.g, self.debugColour.b, self.debugColour.a)
DrawPoly(c.x, c.y, c.z, b.x, b.y, b.z, a.x, a.y, a.z, self.debugColour.r, self.debugColour.g, self.debugColour.b, self.debugColour.a)
DrawPoly(b.x, b.y, b.z, c.x, c.y, c.z, d.x, d.y, d.z, self.debugColour.r, self.debugColour.g, self.debugColour.b, self.debugColour.a)
DrawPoly(d.x, d.y, d.z, c.x, c.y, c.z, b.x, b.y, b.z, self.debugColour.r, self.debugColour.g, self.debugColour.b, self.debugColour.a)
end
end
local function debugSphere(self)
---@diagnostic disable-next-line: param-type-mismatch
DrawMarker(28, self.coords.x, self.coords.y, self.coords.z, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, self.radius, self.radius, self.radius, 255, 42, 24, 100, false, false, 0, false, false, false, false)
DrawMarker(28, self.coords.x, self.coords.y, self.coords.z, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, self.radius, self.radius, self.radius, self.debugColour.r, self.debugColour.g, self.debugColour.b, self.debugColour.a, false, false, 0, false, false, false, false)
end
local function contains(self, coords)
@@ -261,6 +260,19 @@ local function convertToVector(coords)
return coords
end
local function setDebug(self, bool, colour)
if not bool and insideZones[self.id] then
insideZones[self.id] = nil
end
self.debugColour = bool and vec4(colour?.r or self.debugColour?.r or 255, colour?.g or self.debugColour?.g or 42, colour?.b or self.debugColour?.b or 24, colour?.a or self.debugColour?.a or 100) or nil
if bool and self.debug or not bool and not self.debug then return end
self.triangles = bool and (self.type == 'poly' and getTriangles(self.polygon) or self.type == 'box' and { mat(self.polygon[1], self.polygon[2], self.polygon[3]), mat(self.polygon[1], self.polygon[3], self.polygon[4]) }) or nil
self.debug = bool and (self.type == 'sphere' and debugSphere or debugPoly) or nil
end
lib.zones = {
---@return CZone
poly = function(data)
@@ -328,15 +340,16 @@ lib.zones = {
end
data.coords = data.polygon:centroid()
data.type = 'poly'
data.remove = removeZone
data.contains = contains
data.setDebug = setDebug
if data.debug then
data.debug = nil
CreateThread(function()
data.triangles = getTriangles(data.polygon)
data.debug = debugPoly
data:setDebug(true, data.debugColour)
end)
end
@@ -357,12 +370,13 @@ lib.zones = {
vec3(-data.size.x, -data.size.y, 0),
vec3(data.size.x, -data.size.y, 0),
}) + data.coords)
data.type = 'box'
data.remove = removeZone
data.contains = contains
data.setDebug = setDebug
if data.debug then
data.triangles = { mat(data.polygon[1], data.polygon[2], data.polygon[3]), mat(data.polygon[1], data.polygon[3], data.polygon[4]) }
data.debug = debugPoly
data:setDebug(true, data.debugColour)
end
Zones[data.id] = data
@@ -374,11 +388,13 @@ lib.zones = {
data.id = #Zones + 1
data.coords = convertToVector(data.coords)
data.radius = (data.radius or 2) + 0.0
data.type = 'sphere'
data.remove = removeZone
data.contains = insideSphere
data.setDebug = setDebug
if data.debug then
data.debug = debugSphere
data:setDebug(true, data.debugColour)
end
Zones[data.id] = data
@@ -390,5 +406,4 @@ lib.zones = {
getCurrentZones = function() return insideZones end,
}
return lib.zones

View File

@@ -46,9 +46,11 @@ lib.zones = {
data.polygon = glm.polygon.new(points)
data.coords = data.polygon:centroid()
data.type = 'poly'
data.remove = removeZone
data.contains = contains
data.debug = nil
data.debugColour = nil
data.inside = nil
data.onEnter = nil
data.onExit = nil
@@ -70,9 +72,11 @@ lib.zones = {
vec3(-data.size.x, -data.size.y, 0),
vec3(data.size.x, -data.size.y, 0),
}) + data.coords)
data.type = 'box'
data.remove = removeZone
data.contains = contains
data.debug = nil
data.debugColour = nil
data.inside = nil
data.onEnter = nil
data.onExit = nil
@@ -86,9 +90,11 @@ lib.zones = {
data.id = #Zones + 1
data.coords = convertToVector(data.coords)
data.radius = (data.radius or 2) + 0.0
data.type = 'sphere'
data.remove = removeZone
data.contains = insideSphere
data.debug = nil
data.debugColour = nil
data.inside = nil
data.onEnter = nil
data.onExit = nil