From 8f5880aad7ec9f932f0ab70d896e76e534c16e91 Mon Sep 17 00:00:00 2001 From: BerkieBb <82737367+BerkieBb@users.noreply.github.com> Date: Sun, 23 Jul 2023 11:37:29 +0200 Subject: [PATCH] feat(zones): add setDebug and debugColour (#369) --- imports/zones/client.lua | 57 +++++++++++++++++++++++++--------------- imports/zones/server.lua | 6 +++++ 2 files changed, 42 insertions(+), 21 deletions(-) diff --git a/imports/zones/client.lua b/imports/zones/client.lua index 1c53cad..f69cbf0 100644 --- a/imports/zones/client.lua +++ b/imports/zones/client.lua @@ -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 diff --git a/imports/zones/server.lua b/imports/zones/server.lua index 37face3..01cdb56 100644 --- a/imports/zones/server.lua +++ b/imports/zones/server.lua @@ -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