diff --git a/imports/points/client.lua b/imports/points/client.lua index 3463e60..4c546e7 100644 --- a/imports/points/client.lua +++ b/imports/points/client.lua @@ -1,21 +1,34 @@ +---@class CPoint +---@field id number +---@field coords vector3 +---@field distance number +---@field currentDistance number +---@field remove fun() +---@field onEnter fun(self: CPoint)? +---@field onExit fun(self: CPoint)? +---@field nearby fun(self: CPoint)? + local points = {} local function removePoint(self) points[self.id] = nil end -local nearby = {} -local closest +local nearbyPoints = {} +local nearbyCount = 0 +local closestPoint local tick CreateThread(function() while true do + if nearbyCount ~= 0 then + table.wipe(nearbyPoints) + nearbyCount = 0 + end + local coords = GetEntityCoords(cache.ped) cache.coords = coords - - Wait(300) - closest = nil - table.wipe(nearby) + closestPoint = nil for _, point in pairs(points) do local distance = #(coords - point.coords) @@ -24,12 +37,13 @@ CreateThread(function() point.currentDistance = distance ---@diagnostic disable-next-line: need-check-nil - if distance < (closest?.currentDistance or point.distance) then - closest = point + if distance < (closestPoint?.currentDistance or point.distance) then + closestPoint = point end if point.nearby then - nearby[#nearby + 1] = point + nearbyCount += 1 + nearbyPoints[nearbyCount] = point end if point.onEnter and not point.inside then @@ -44,20 +58,23 @@ CreateThread(function() end if not tick then - if #nearby > 0 then + if nearbyCount ~= 0 then tick = SetInterval(function() - for i = 1, #nearby do - nearby[i]:nearby() + for i = 1, nearbyCount do + nearbyPoints[i]:nearby() end end) end - elseif #nearby == 0 then + elseif nearbyCount == 0 then tick = ClearInterval(tick) end + + Wait(300) end end) lib.points = { + ---@return CPoint new = function(...) local args = {...} local id = #points + 1 @@ -91,8 +108,9 @@ lib.points = { return self end, + ---@return CPoint closest = function() - return closest + return closestPoint end }