mirror of
https://github.com/CommunityOx/ox_lib.git
synced 2026-08-19 07:56:02 +01:00
refactor(zones): rename variables and minor perf
Set box vertices and poly triangles once during creation (with debug).
This commit is contained in:
@@ -150,24 +150,6 @@ local function removeZone(self)
|
|||||||
zones[self.id] = nil
|
zones[self.id] = nil
|
||||||
end
|
end
|
||||||
|
|
||||||
local function debug(self)
|
|
||||||
local polygon = self.polygon
|
|
||||||
local polyCount = #self.polygon
|
|
||||||
|
|
||||||
if polygon.isConvex(polygon) then
|
|
||||||
for i = 2, polyCount - 1 do
|
|
||||||
DrawPoly(polygon[1], polygon[i], polygon[i + 1], 255, 0, 0, 50)
|
|
||||||
DrawPoly(polygon[i], polygon[1], polygon[i + 1], 255, 0, 0, 50)
|
|
||||||
end
|
|
||||||
else
|
|
||||||
self.triangles = self.triangles or getTriangles(polygon)
|
|
||||||
for i = 1, #self.triangles do
|
|
||||||
DrawPoly(self.triangles[i].a, self.triangles[i].b, self.triangles[i].c, 255, 0, 0, 50)
|
|
||||||
DrawPoly(self.triangles[i].b, self.triangles[i].a, self.triangles[i].c, 255, 0, 0, 50)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
local inside = {}
|
local inside = {}
|
||||||
local insideCount
|
local insideCount
|
||||||
local tick
|
local tick
|
||||||
@@ -225,11 +207,50 @@ CreateThread(function()
|
|||||||
end
|
end
|
||||||
end)
|
end)
|
||||||
|
|
||||||
|
local function debugPoly(self)
|
||||||
|
if self.triangles then
|
||||||
|
for i = 1, #self.triangles do
|
||||||
|
local triangle = self.triangles[i]
|
||||||
|
DrawPoly(triangle.a, triangle.b, triangle.c, 255, 0, 0, 50)
|
||||||
|
DrawPoly(triangle.b, triangle.a, triangle.c, 255, 0, 0, 50)
|
||||||
|
end
|
||||||
|
else
|
||||||
|
local polygon = self.polygon
|
||||||
|
|
||||||
|
for i = 2, #self.polygon - 1 do
|
||||||
|
DrawPoly(polygon[1], polygon[i], polygon[i + 1], 255, 0, 0, 50)
|
||||||
|
DrawPoly(polygon[i], polygon[1], polygon[i + 1], 255, 0, 0, 50)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
local DrawLine = DrawLine
|
local DrawLine = DrawLine
|
||||||
|
|
||||||
local function debugBox(self)
|
local function debugBox(self)
|
||||||
if not self.debugCoords then
|
DrawLine(self.vertices[1], self.vertices[2], 255, 0, 0, 150)
|
||||||
self.debugCoords = {
|
DrawLine(self.vertices[2], self.vertices[3], 255, 0, 0, 150)
|
||||||
|
DrawLine(self.vertices[3], self.vertices[4], 255, 0, 0, 150)
|
||||||
|
DrawLine(self.vertices[4], self.vertices[1], 255, 0, 0, 150)
|
||||||
|
|
||||||
|
DrawLine(self.vertices[5], self.vertices[6], 255, 0, 0, 150)
|
||||||
|
DrawLine(self.vertices[6], self.vertices[7], 255, 0, 0, 150)
|
||||||
|
DrawLine(self.vertices[7], self.vertices[8], 255, 0, 0, 150)
|
||||||
|
DrawLine(self.vertices[8], self.vertices[5], 255, 0, 0, 150)
|
||||||
|
|
||||||
|
DrawLine(self.vertices[1], self.vertices[7], 255, 0, 0, 150)
|
||||||
|
DrawLine(self.vertices[2], self.vertices[8], 255, 0, 0, 150)
|
||||||
|
DrawLine(self.vertices[3], self.vertices[5], 255, 0, 0, 150)
|
||||||
|
DrawLine(self.vertices[4], self.vertices[6], 255, 0, 0, 150)
|
||||||
|
end
|
||||||
|
|
||||||
|
local glm_polygon_contains = glm.polygon.contains
|
||||||
|
|
||||||
|
local function contains(self, coords)
|
||||||
|
return glm_polygon_contains(self.polygon, coords)
|
||||||
|
end
|
||||||
|
|
||||||
|
local function getBoxVertices(self)
|
||||||
|
return {
|
||||||
self.centroid + vec3(self.size.x, self.size.y, self.thickness) * self.rotation,
|
self.centroid + vec3(self.size.x, self.size.y, self.thickness) * self.rotation,
|
||||||
self.centroid + vec3(-self.size.x, self.size.y, self.thickness) * self.rotation,
|
self.centroid + vec3(-self.size.x, self.size.y, self.thickness) * self.rotation,
|
||||||
self.centroid + vec3(-self.size.x, -self.size.y, self.thickness) * self.rotation,
|
self.centroid + vec3(-self.size.x, -self.size.y, self.thickness) * self.rotation,
|
||||||
@@ -241,38 +262,20 @@ local function debugBox(self)
|
|||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
|
||||||
DrawLine(self.debugCoords[1], self.debugCoords[2], 255, 0, 0, 150)
|
|
||||||
DrawLine(self.debugCoords[2], self.debugCoords[3], 255, 0, 0, 150)
|
|
||||||
DrawLine(self.debugCoords[3], self.debugCoords[4], 255, 0, 0, 150)
|
|
||||||
DrawLine(self.debugCoords[4], self.debugCoords[1], 255, 0, 0, 150)
|
|
||||||
|
|
||||||
DrawLine(self.debugCoords[5], self.debugCoords[6], 255, 0, 0, 150)
|
|
||||||
DrawLine(self.debugCoords[6], self.debugCoords[7], 255, 0, 0, 150)
|
|
||||||
DrawLine(self.debugCoords[7], self.debugCoords[8], 255, 0, 0, 150)
|
|
||||||
DrawLine(self.debugCoords[8], self.debugCoords[5], 255, 0, 0, 150)
|
|
||||||
|
|
||||||
DrawLine(self.debugCoords[1], self.debugCoords[7], 255, 0, 0, 150)
|
|
||||||
DrawLine(self.debugCoords[2], self.debugCoords[8], 255, 0, 0, 150)
|
|
||||||
DrawLine(self.debugCoords[3], self.debugCoords[5], 255, 0, 0, 150)
|
|
||||||
DrawLine(self.debugCoords[4], self.debugCoords[6], 255, 0, 0, 150)
|
|
||||||
end
|
|
||||||
|
|
||||||
local glm_polygon_contains = glm.polygon.contains
|
|
||||||
|
|
||||||
local function contains(self, coords)
|
|
||||||
return glm_polygon_contains(self.polygon, coords)
|
|
||||||
end
|
|
||||||
|
|
||||||
return {
|
return {
|
||||||
poly = function(data)
|
poly = function(data)
|
||||||
data.id = #zones + 1
|
data.id = #zones + 1
|
||||||
data.thickness = data.thickness or 2
|
data.thickness = data.thickness or 2
|
||||||
data.polygon = glm.polygon.new(data.points)
|
data.polygon = glm.polygon.new(data.points)
|
||||||
data.remove = removeZone
|
|
||||||
data.centroid = data.polygon:centroid()
|
data.centroid = data.polygon:centroid()
|
||||||
data.debug = data.debug and debug
|
data.remove = removeZone
|
||||||
data.contains = contains
|
data.contains = contains
|
||||||
|
|
||||||
|
if data.debug then
|
||||||
|
data.triangles = not data.polygon:isConvex() and getTriangles(data.polygon)
|
||||||
|
data.debug = debugPoly
|
||||||
|
end
|
||||||
|
|
||||||
zones[data.id] = data
|
zones[data.id] = data
|
||||||
return data
|
return data
|
||||||
end,
|
end,
|
||||||
@@ -288,9 +291,13 @@ return {
|
|||||||
vec3(data.size.x, -data.size.y, 0),
|
vec3(data.size.x, -data.size.y, 0),
|
||||||
}) + data.centroid)
|
}) + data.centroid)
|
||||||
data.remove = removeZone
|
data.remove = removeZone
|
||||||
data.debug = data.debug and debugBox
|
|
||||||
data.contains = contains
|
data.contains = contains
|
||||||
|
|
||||||
|
if data.debug then
|
||||||
|
data.vertices = getBoxVertices(data)
|
||||||
|
data.debug = debugBox
|
||||||
|
end
|
||||||
|
|
||||||
zones[data.id] = data
|
zones[data.id] = data
|
||||||
return data
|
return data
|
||||||
end
|
end
|
||||||
|
|||||||
Reference in New Issue
Block a user