Skip to content

Commit

Permalink
feat(zones): add setDebug and debugColour (overextended#369)
Browse files Browse the repository at this point in the history
  • Loading branch information
BerkieBb authored Jul 23, 2023
1 parent 84b7f97 commit 8f5880a
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 21 deletions.
57 changes: 36 additions & 21 deletions imports/zones/client.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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)?
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -211,32 +214,28 @@ 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)
local a = self.polygon[i] + thickness
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)
Expand All @@ -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)
Expand Down Expand Up @@ -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

Expand All @@ -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
Expand All @@ -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
Expand All @@ -390,5 +406,4 @@ lib.zones = {
getCurrentZones = function() return insideZones end,
}


return lib.zones
6 changes: 6 additions & 0 deletions imports/zones/server.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down

0 comments on commit 8f5880a

Please sign in to comment.