mirror of
https://github.com/CommunityOx/ox_lib.git
synced 2026-08-16 22:46:03 +01:00
feat(points): utilise the grid system
This commit is contained in:
@@ -26,6 +26,8 @@ local function removePoint(self)
|
||||
closestPoint = nil
|
||||
end
|
||||
|
||||
lib.grid.removeEntry(self)
|
||||
|
||||
points[self.id] = nil
|
||||
end
|
||||
|
||||
@@ -37,25 +39,41 @@ CreateThread(function()
|
||||
end
|
||||
|
||||
local coords = GetEntityCoords(cache.ped)
|
||||
local newPoints = lib.grid.getNearbyEntries(coords, function(entry) return entry.remove == removePoint end) --[[@as CPoint[] ]]
|
||||
local cellX, cellY = lib.grid.getCellPosition(coords)
|
||||
cache.coords = coords
|
||||
|
||||
if closestPoint and #(coords - closestPoint.coords) > closestPoint.distance then
|
||||
closestPoint = nil
|
||||
end
|
||||
|
||||
for _, point in pairs(points) do
|
||||
if cellX ~= cache.lastCellX or cellY ~= cache.lastCellY then
|
||||
for i = 1, #nearbyPoints do
|
||||
local point = nearbyPoints[i]
|
||||
|
||||
if point.inside then
|
||||
local distance = #(coords - point.coords)
|
||||
|
||||
if distance <= point.distance then
|
||||
if distance > point.radius then
|
||||
if point.onExit then point:onExit() end
|
||||
|
||||
point.inside = nil
|
||||
point.currentDistance = nil
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
cache.lastCellX = cellX
|
||||
cache.lastCellY = cellY
|
||||
end
|
||||
|
||||
for i = 1, #newPoints do
|
||||
local point = newPoints[i]
|
||||
local distance = #(coords - point.coords)
|
||||
|
||||
if distance <= point.radius then
|
||||
point.currentDistance = distance
|
||||
|
||||
if closestPoint then
|
||||
if distance < closestPoint.currentDistance then
|
||||
closestPoint.isClosest = nil
|
||||
point.isClosest = true
|
||||
closestPoint = point
|
||||
end
|
||||
elseif distance < point.distance then
|
||||
if not closestPoint or distance < (closestPoint.currentDistance or point.radius) then
|
||||
if closestPoint then closestPoint.isClosest = nil end
|
||||
|
||||
point.isClosest = true
|
||||
closestPoint = point
|
||||
end
|
||||
@@ -71,6 +89,7 @@ CreateThread(function()
|
||||
end
|
||||
elseif point.currentDistance then
|
||||
if point.onExit then point:onExit() end
|
||||
|
||||
point.inside = nil
|
||||
point.currentDistance = nil
|
||||
end
|
||||
@@ -79,10 +98,10 @@ CreateThread(function()
|
||||
if not tick then
|
||||
if nearbyCount ~= 0 then
|
||||
tick = SetInterval(function()
|
||||
for i = 1, nearbyCount do
|
||||
for i = nearbyCount, 1, -1 do
|
||||
local point = nearbyPoints[i]
|
||||
|
||||
if point then
|
||||
if point and point.nearby then
|
||||
point:nearby()
|
||||
end
|
||||
end
|
||||
@@ -110,12 +129,13 @@ local function toVector(coords)
|
||||
return coords
|
||||
end
|
||||
|
||||
lib.points = {
|
||||
---@return CPoint
|
||||
---@overload fun(data: PointProperties): CPoint
|
||||
---@overload fun(coords: vector3, distance: number, data?: PointProperties): CPoint
|
||||
new = function(...)
|
||||
local args = {...}
|
||||
lib.points = {}
|
||||
|
||||
---@return CPoint
|
||||
---@overload fun(data: PointProperties): CPoint
|
||||
---@overload fun(coords: vector3, distance: number, data?: PointProperties): CPoint
|
||||
function lib.points.new(...)
|
||||
local args = { ... }
|
||||
local id = #points + 1
|
||||
local self
|
||||
|
||||
@@ -135,6 +155,7 @@ lib.points = {
|
||||
|
||||
self.coords = toVector(self.coords)
|
||||
self.distance = self.distance or args[2]
|
||||
self.radius = self.distance
|
||||
|
||||
if args[3] then
|
||||
for k, v in pairs(args[3]) do
|
||||
@@ -142,18 +163,18 @@ lib.points = {
|
||||
end
|
||||
end
|
||||
|
||||
lib.grid.addEntry(self)
|
||||
points[id] = self
|
||||
|
||||
return self
|
||||
end,
|
||||
end
|
||||
|
||||
getAllPoints = function() return points end,
|
||||
function lib.points.getAllPoints() return points end
|
||||
|
||||
getNearbyPoints = function() return nearbyPoints end,
|
||||
function lib.points.getNearbyPoints() return nearbyPoints end
|
||||
|
||||
---@return CPoint?
|
||||
getClosestPoint = function() return closestPoint end,
|
||||
}
|
||||
---@return CPoint?
|
||||
function lib.points.getClosestPoint() return closestPoint end
|
||||
|
||||
---@deprecated
|
||||
lib.points.closest = lib.points.getClosestPoint
|
||||
|
||||
Reference in New Issue
Block a user