Skip to content

Commit

Permalink
tweak(client/points): set point.isClosest
Browse files Browse the repository at this point in the history
  • Loading branch information
thelindat committed Sep 20, 2022
1 parent ad219b2 commit 025a8eb
Showing 1 changed file with 23 additions and 10 deletions.
33 changes: 23 additions & 10 deletions imports/points/client.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,26 @@
---@field coords vector3
---@field distance number
---@field currentDistance number
---@field isClosest boolean?
---@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 nearbyPoints = {}
local nearbyCount = 0
local closestPoint
local tick

local function removePoint(self)
if closestPoint?.id == self.id then
closestPoint = nil
end

points[self.id] = nil
end

CreateThread(function()
while true do
if nearbyCount ~= 0 then
Expand All @@ -28,18 +32,27 @@ CreateThread(function()

local coords = GetEntityCoords(cache.ped)
cache.coords = coords
closestPoint = nil

if closestPoint and #(coords - closestPoint.coords) > closestPoint.distance then
closestPoint = nil
end

for _, point in pairs(points) do
local distance = #(coords - point.coords)

if distance <= point.distance then
point.currentDistance = distance

---@diagnostic disable-next-line: need-check-nil
if distance < (closestPoint?.currentDistance or point.distance) then
closestPoint = point
end
if closestPoint then
if distance < closestPoint.currentDistance then
closestPoint.isClosest = nil
point.isClosest = true
closestPoint = point
end
elseif distance < point.distance then
point.isClosest = true
closestPoint = point
end

if point.nearby then
nearbyCount += 1
Expand Down

0 comments on commit 025a8eb

Please sign in to comment.