Files
ox_lib/imports/points/client.lua

95 lines
1.7 KiB
Lua
Raw Normal View History

local points = {}
2022-04-14 10:58:17 +00:00
local function removePoint(self)
points[self.id] = nil
end
2022-04-14 10:58:17 +00:00
local nearby = {}
2022-04-14 11:08:56 +00:00
local closest
local tick
2022-04-14 10:58:17 +00:00
CreateThread(function()
while true do
local coords = GetEntityCoords(cache.ped)
Wait(300)
2022-04-14 11:08:56 +00:00
closest = nil
table.wipe(nearby)
2022-04-14 10:58:17 +00:00
for _, point in pairs(points) do
local distance = #(coords - point.coords)
if distance <= point.distance then
point.currentDistance = distance
2022-03-16 01:18:10 +00:00
2022-04-14 11:08:56 +00:00
if distance < (closest?.currentDistance or point.distance) then
closest = point
end
if point.nearby then
nearby[#nearby + 1] = point
end
2022-03-16 01:18:10 +00:00
if point.onEnter and not point.inside then
point.inside = true
point:onEnter()
end
elseif point.currentDistance then
if point.onExit then point:onExit() end
2022-03-16 01:18:10 +00:00
point.inside = nil
point.currentDistance = nil
end
end
if not tick then
if #nearby > 0 then
tick = SetInterval(function()
for i = 1, #nearby do
nearby[i]:nearby()
end
end)
end
elseif #nearby == 0 then
tick = ClearInterval(tick)
end
end
end)
2022-04-14 10:58:17 +00:00
return {
new = function(...)
local args = {...}
2022-04-14 10:58:17 +00:00
local id = #points + 1
local self
2022-04-14 10:58:17 +00:00
-- Support sending a single argument containing point data
if type(args[1]) == 'table' then
self = args[1]
self.id = id
self.remove = removePoint
else
-- Backwards compatibility for original implementation (args: coords, distance, data)
self = {
id = id,
coords = args[1],
remove = removePoint,
}
end
self.distance = self.distance or args[2]
2022-04-14 10:58:17 +00:00
if args[3] then
for k, v in pairs(args[3]) do
2022-04-14 10:58:17 +00:00
self[k] = v
end
end
points[id] = self
return self
2022-04-14 11:08:56 +00:00
end,
closest = function()
return closest
2022-04-14 10:58:17 +00:00
end
}