mirror of
https://github.com/CommunityOx/ox_lib.git
synced 2026-08-17 06:56:03 +01:00
feat(zones): add setDebug and debugColour (#369)
This commit is contained in:
@@ -4,6 +4,9 @@ local glm = require 'glm'
|
|||||||
---@field id number
|
---@field id number
|
||||||
---@field coords vector3
|
---@field coords vector3
|
||||||
---@field distance number
|
---@field distance number
|
||||||
|
---@field type 'poly' | 'sphere' | 'box'
|
||||||
|
---@field debugColour vector4?
|
||||||
|
---@field setDebug fun(self: CZone)
|
||||||
---@field remove fun()
|
---@field remove fun()
|
||||||
---@field contains fun(self: CZone, coords?: vector3): boolean
|
---@field contains fun(self: CZone, coords?: vector3): boolean
|
||||||
---@field onEnter fun(self: CZone)?
|
---@field onEnter fun(self: CZone)?
|
||||||
@@ -117,7 +120,7 @@ CreateThread(function()
|
|||||||
|
|
||||||
for _, zone in pairs(Zones) do
|
for _, zone in pairs(Zones) do
|
||||||
zone.distance = #(zone.coords - coords)
|
zone.distance = #(zone.coords - coords)
|
||||||
local radius, contains = zone.radius
|
local radius, contains = zone.radius, nil
|
||||||
|
|
||||||
if radius then
|
if radius then
|
||||||
contains = zone.distance < radius
|
contains = zone.distance < radius
|
||||||
@@ -211,12 +214,8 @@ local DrawPoly = DrawPoly
|
|||||||
local function debugPoly(self)
|
local function debugPoly(self)
|
||||||
for i = 1, #self.triangles do
|
for i = 1, #self.triangles do
|
||||||
local triangle = self.triangles[i]
|
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
|
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)
|
||||||
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)
|
|
||||||
end
|
end
|
||||||
for i = 1, #self.polygon do
|
for i = 1, #self.polygon do
|
||||||
local thickness = vec(0, 0, self.thickness / 2)
|
local thickness = vec(0, 0, self.thickness / 2)
|
||||||
@@ -224,19 +223,19 @@ local function debugPoly(self)
|
|||||||
local b = self.polygon[i] - thickness
|
local b = self.polygon[i] - thickness
|
||||||
local c = (self.polygon[i + 1] or self.polygon[1]) + thickness
|
local c = (self.polygon[i + 1] or self.polygon[1]) + thickness
|
||||||
local d = (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, 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, 255, 42, 24, 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, 255, 42, 24, 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, 255, 42, 24, 100)
|
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, 255, 42, 24, 100)
|
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, 255, 42, 24, 100)
|
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, 255, 42, 24, 100)
|
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
|
||||||
end
|
end
|
||||||
|
|
||||||
local function debugSphere(self)
|
local function debugSphere(self)
|
||||||
---@diagnostic disable-next-line: param-type-mismatch
|
---@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
|
end
|
||||||
|
|
||||||
local function contains(self, coords)
|
local function contains(self, coords)
|
||||||
@@ -261,6 +260,19 @@ local function convertToVector(coords)
|
|||||||
return coords
|
return coords
|
||||||
end
|
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 = {
|
lib.zones = {
|
||||||
---@return CZone
|
---@return CZone
|
||||||
poly = function(data)
|
poly = function(data)
|
||||||
@@ -328,15 +340,16 @@ lib.zones = {
|
|||||||
end
|
end
|
||||||
|
|
||||||
data.coords = data.polygon:centroid()
|
data.coords = data.polygon:centroid()
|
||||||
|
data.type = 'poly'
|
||||||
data.remove = removeZone
|
data.remove = removeZone
|
||||||
data.contains = contains
|
data.contains = contains
|
||||||
|
data.setDebug = setDebug
|
||||||
|
|
||||||
if data.debug then
|
if data.debug then
|
||||||
data.debug = nil
|
data.debug = nil
|
||||||
|
|
||||||
CreateThread(function()
|
CreateThread(function()
|
||||||
data.triangles = getTriangles(data.polygon)
|
data:setDebug(true, data.debugColour)
|
||||||
data.debug = debugPoly
|
|
||||||
end)
|
end)
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -357,12 +370,13 @@ lib.zones = {
|
|||||||
vec3(-data.size.x, -data.size.y, 0),
|
vec3(-data.size.x, -data.size.y, 0),
|
||||||
vec3(data.size.x, -data.size.y, 0),
|
vec3(data.size.x, -data.size.y, 0),
|
||||||
}) + data.coords)
|
}) + data.coords)
|
||||||
|
data.type = 'box'
|
||||||
data.remove = removeZone
|
data.remove = removeZone
|
||||||
data.contains = contains
|
data.contains = contains
|
||||||
|
data.setDebug = setDebug
|
||||||
|
|
||||||
if data.debug then
|
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:setDebug(true, data.debugColour)
|
||||||
data.debug = debugPoly
|
|
||||||
end
|
end
|
||||||
|
|
||||||
Zones[data.id] = data
|
Zones[data.id] = data
|
||||||
@@ -374,11 +388,13 @@ lib.zones = {
|
|||||||
data.id = #Zones + 1
|
data.id = #Zones + 1
|
||||||
data.coords = convertToVector(data.coords)
|
data.coords = convertToVector(data.coords)
|
||||||
data.radius = (data.radius or 2) + 0.0
|
data.radius = (data.radius or 2) + 0.0
|
||||||
|
data.type = 'sphere'
|
||||||
data.remove = removeZone
|
data.remove = removeZone
|
||||||
data.contains = insideSphere
|
data.contains = insideSphere
|
||||||
|
data.setDebug = setDebug
|
||||||
|
|
||||||
if data.debug then
|
if data.debug then
|
||||||
data.debug = debugSphere
|
data:setDebug(true, data.debugColour)
|
||||||
end
|
end
|
||||||
|
|
||||||
Zones[data.id] = data
|
Zones[data.id] = data
|
||||||
@@ -390,5 +406,4 @@ lib.zones = {
|
|||||||
getCurrentZones = function() return insideZones end,
|
getCurrentZones = function() return insideZones end,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
return lib.zones
|
return lib.zones
|
||||||
|
|||||||
@@ -46,9 +46,11 @@ lib.zones = {
|
|||||||
|
|
||||||
data.polygon = glm.polygon.new(points)
|
data.polygon = glm.polygon.new(points)
|
||||||
data.coords = data.polygon:centroid()
|
data.coords = data.polygon:centroid()
|
||||||
|
data.type = 'poly'
|
||||||
data.remove = removeZone
|
data.remove = removeZone
|
||||||
data.contains = contains
|
data.contains = contains
|
||||||
data.debug = nil
|
data.debug = nil
|
||||||
|
data.debugColour = nil
|
||||||
data.inside = nil
|
data.inside = nil
|
||||||
data.onEnter = nil
|
data.onEnter = nil
|
||||||
data.onExit = 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),
|
||||||
vec3(data.size.x, -data.size.y, 0),
|
vec3(data.size.x, -data.size.y, 0),
|
||||||
}) + data.coords)
|
}) + data.coords)
|
||||||
|
data.type = 'box'
|
||||||
data.remove = removeZone
|
data.remove = removeZone
|
||||||
data.contains = contains
|
data.contains = contains
|
||||||
data.debug = nil
|
data.debug = nil
|
||||||
|
data.debugColour = nil
|
||||||
data.inside = nil
|
data.inside = nil
|
||||||
data.onEnter = nil
|
data.onEnter = nil
|
||||||
data.onExit = nil
|
data.onExit = nil
|
||||||
@@ -86,9 +90,11 @@ lib.zones = {
|
|||||||
data.id = #Zones + 1
|
data.id = #Zones + 1
|
||||||
data.coords = convertToVector(data.coords)
|
data.coords = convertToVector(data.coords)
|
||||||
data.radius = (data.radius or 2) + 0.0
|
data.radius = (data.radius or 2) + 0.0
|
||||||
|
data.type = 'sphere'
|
||||||
data.remove = removeZone
|
data.remove = removeZone
|
||||||
data.contains = insideSphere
|
data.contains = insideSphere
|
||||||
data.debug = nil
|
data.debug = nil
|
||||||
|
data.debugColour = nil
|
||||||
data.inside = nil
|
data.inside = nil
|
||||||
data.onEnter = nil
|
data.onEnter = nil
|
||||||
data.onExit = nil
|
data.onExit = nil
|
||||||
|
|||||||
Reference in New Issue
Block a user