mirror of
https://github.com/CommunityOx/ox_lib.git
synced 2026-08-16 22:46:03 +01:00
refactor(zones): perf, args, and insideZone
lib.zones.new should take a single argument containing all data related to the new zone, then append new fields to the table. No need for a distance check and 'nearby', instead check if player is inside the polygon. Use a function ref instead of closures for debug (drawPolygon).
This commit is contained in:
@@ -5,99 +5,75 @@ local function removeZone(self)
|
||||
zones[self.id] = nil
|
||||
end
|
||||
|
||||
local nearby = {}
|
||||
local function debug(self)
|
||||
local points = self.polygon()
|
||||
for i = 1, #points do
|
||||
if i == #points then
|
||||
DrawPoly(points[i], points[1], self.centroid, 255, 0, 0, 50)
|
||||
DrawPoly(points[1], points[i], self.centroid, 255, 0, 0, 50)
|
||||
else
|
||||
DrawPoly(points[i], points[i + 1], self.centroid, 255, 0, 0, 50)
|
||||
DrawPoly(points[i + 1], points[i], self.centroid, 255, 0, 0, 50)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
local inside = {}
|
||||
local tick
|
||||
|
||||
CreateThread(function()
|
||||
while true do
|
||||
local coords = GetEntityCoords(cache.ped)
|
||||
Wait(300)
|
||||
table.wipe(nearby)
|
||||
table.wipe(inside)
|
||||
|
||||
for _, zone in pairs(zones) do
|
||||
local distance = #(coords - zone.polygon:centroid())
|
||||
local contains = zone.polygon:contains(coords, zone.thickness)
|
||||
|
||||
if distance <= zone.inRange then
|
||||
if zone.nearby then
|
||||
nearby[#nearby + 1] = zone
|
||||
if contains then
|
||||
if zone.inside then
|
||||
inside[#inside + 1] = zone
|
||||
end
|
||||
|
||||
if zone.polygon:contains(coords, zone.thickness) then
|
||||
if zone.onEnter and not zone.inside then
|
||||
zone.inside = true
|
||||
zone:onEnter()
|
||||
end
|
||||
elseif zone.inside then
|
||||
if zone.onExit then zone:onExit() end
|
||||
zone.inside = nil
|
||||
if zone.onEnter and not zone.insideZone then
|
||||
zone.insideZone = true
|
||||
zone:onEnter()
|
||||
end
|
||||
elseif zone.insideZone then
|
||||
zone.insideZone = nil
|
||||
if zone.onExit then
|
||||
zone:onExit()
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
if not tick then
|
||||
if #nearby > 0 then
|
||||
if #inside > 0 then
|
||||
tick = SetInterval(function()
|
||||
for i = 1, #nearby do
|
||||
nearby[i]:nearby()
|
||||
if nearby[i].debug then
|
||||
nearby[i]:drawPolygon()
|
||||
for i = 1, #inside do
|
||||
inside[i]:inside()
|
||||
if inside[i].debug then
|
||||
inside[i]:debug()
|
||||
end
|
||||
end
|
||||
end)
|
||||
end
|
||||
elseif #nearby == 0 then
|
||||
elseif #inside == 0 then
|
||||
tick = ClearInterval(tick)
|
||||
end
|
||||
end
|
||||
end)
|
||||
|
||||
return {
|
||||
new = function(points, thickness, data)
|
||||
local id = #zones + 1
|
||||
local self = {
|
||||
id = id,
|
||||
thickness = thickness,
|
||||
polygon = glm.polygon.new(points),
|
||||
remove = removeZone
|
||||
}
|
||||
new = function(data)
|
||||
data.id = #zones + 1
|
||||
data.thickness = data.thickness or 2
|
||||
data.polygon = glm.polygon.new(data.points)
|
||||
data.remove = removeZone
|
||||
data.centroid = data.polygon:centroid()
|
||||
data.debug = data.debug and debug
|
||||
|
||||
if data then
|
||||
for k, v in pairs(data) do
|
||||
self[k] = v
|
||||
end
|
||||
end
|
||||
|
||||
if self.debug and not self.nearby then
|
||||
self.nearby = function() end
|
||||
end
|
||||
|
||||
local centroid = self.polygon:centroid()
|
||||
local length = #self.polygon
|
||||
|
||||
self.maxDistance = 0
|
||||
for i = 1, length do
|
||||
local difference = #(centroid - points[i])
|
||||
if difference > self.maxDistance then
|
||||
self.maxDistance = difference
|
||||
end
|
||||
end
|
||||
|
||||
self.inRange = self.inRange or self.maxDistance * 1.5
|
||||
|
||||
self.drawPolygon = function()
|
||||
local points = self.polygon()
|
||||
for i = 1, #points do
|
||||
if i == #points then
|
||||
DrawPoly(points[i], points[1], centroid, 255, 0, 0, 50)
|
||||
DrawPoly(points[1], points[i], centroid, 255, 0, 0, 50)
|
||||
else
|
||||
DrawPoly(points[i], points[i + 1], centroid, 255, 0, 0, 50)
|
||||
DrawPoly(points[i + 1], points[i], centroid, 255, 0, 0, 50)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
zones[id] = self
|
||||
return self
|
||||
zones[data.id] = data
|
||||
return data
|
||||
end
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user