mirror of
https://github.com/esx-framework/esx_core.git
synced 2026-08-29 01:08:54 +00:00
refactor(es_extended/client/imports/point.lua) and (es_extended/client/modules/points.lua): Performace Improvement
Inside loop is now handled in the same resource as long more than one point is nearby.
This commit is contained in:
@@ -1,44 +1,53 @@
|
||||
Point = ESX.Class()
|
||||
|
||||
function Point:constructor(properties)
|
||||
self.coords = properties.coords
|
||||
self.hidden = properties.hidden or false
|
||||
self.inside = properties.inside or function() end
|
||||
self.enter = properties.enter or function() end
|
||||
self.leave = properties.leave or function() end
|
||||
self.handle = ESX.CreatePointInternal(properties.coords, properties.distance, properties.hidden, function()
|
||||
self.nearby = true
|
||||
if self.enter then
|
||||
self:enter()
|
||||
end
|
||||
if self.inside then
|
||||
CreateThread(function()
|
||||
while self.nearby do
|
||||
local coords = GetEntityCoords(ESX.PlayerData.ped)
|
||||
self.currDistance = #(coords - self.coords)
|
||||
self:inside()
|
||||
Wait(0)
|
||||
end
|
||||
end)
|
||||
end
|
||||
end, function()
|
||||
self.nearby = false
|
||||
if self.leave then
|
||||
self:leave()
|
||||
end
|
||||
end)
|
||||
end
|
||||
|
||||
function Point:delete()
|
||||
ESX.RemovePointInternal(self.handle)
|
||||
end
|
||||
|
||||
function Point:toggle(hidden)
|
||||
if hidden == nil then
|
||||
hidden = not self.hidden
|
||||
end
|
||||
self.hidden = hidden
|
||||
ESX.HidePointInternal(self.handle, hidden)
|
||||
end
|
||||
|
||||
local Point = ESX.Class()
|
||||
|
||||
local nearby, loop = {}
|
||||
|
||||
function Point:constructor(properties)
|
||||
self.coords = properties.coords
|
||||
self.hidden = properties.hidden
|
||||
self.enter = properties.enter
|
||||
self.leave = properties.leave
|
||||
self.inside = properties.inside
|
||||
self.handle = ESX.CreatePointInternal(properties.coords, properties.distance, properties.hidden, function()
|
||||
nearby[self.handle] = self
|
||||
if self.enter then
|
||||
self:enter()
|
||||
end
|
||||
if not loop then
|
||||
loop = true
|
||||
CreateThread(function()
|
||||
while loop do
|
||||
local coords = GetEntityCoords(ESX.PlayerData.ped)
|
||||
for handle, point in pairs(nearby) do
|
||||
if point.inside then
|
||||
point:inside(#(coords - point.coords))
|
||||
end
|
||||
end
|
||||
Wait()
|
||||
end
|
||||
end)
|
||||
end
|
||||
end, function()
|
||||
nearby[self.handle] = nil
|
||||
if self.leave then
|
||||
self:leave()
|
||||
end
|
||||
if #nearby == 0 then
|
||||
loop = false
|
||||
end
|
||||
end)
|
||||
end
|
||||
|
||||
function Point:delete()
|
||||
ESX.RemovePointInternal(self.handle)
|
||||
end
|
||||
|
||||
function Point:toggle(hidden)
|
||||
if hidden == nil then
|
||||
hidden = not self.hidden
|
||||
end
|
||||
self.hidden = hidden
|
||||
ESX.HidePointInternal(self.handle, hidden)
|
||||
end
|
||||
|
||||
return Point
|
||||
@@ -1,57 +1,54 @@
|
||||
local points = {}
|
||||
|
||||
function ESX.CreatePointInternal(coords, distance, hidden, enter, leave)
|
||||
local point = {
|
||||
coords = coords,
|
||||
distance = distance,
|
||||
hidden = hidden,
|
||||
enter = enter,
|
||||
leave = leave,
|
||||
resource = GetInvokingResource()
|
||||
}
|
||||
local handle = ESX.Table.SizeOf(points) + 1
|
||||
points[handle] = point
|
||||
return handle
|
||||
end
|
||||
|
||||
function ESX.RemovePointInternal(handle)
|
||||
points[handle] = nil
|
||||
end
|
||||
|
||||
function ESX.HidePointInternal(handle, hidden)
|
||||
if points[handle] then
|
||||
points[handle].hidden = hidden
|
||||
end
|
||||
end
|
||||
|
||||
function StartPointsLoop()
|
||||
CreateThread(function()
|
||||
while true do
|
||||
local coords = GetEntityCoords(ESX.PlayerData.ped)
|
||||
for i, point in pairs(points) do
|
||||
local distance = #(coords - point.coords)
|
||||
|
||||
if not point.hidden and distance <= point.distance then
|
||||
if not point.nearby then
|
||||
points[i].nearby = true
|
||||
points[i].enter()
|
||||
end
|
||||
point.currentDistance = distance
|
||||
elseif point.nearby then
|
||||
points[i].nearby = false
|
||||
points[i].leave()
|
||||
end
|
||||
end
|
||||
Wait(500)
|
||||
end
|
||||
end)
|
||||
end
|
||||
|
||||
|
||||
AddEventHandler('onResourceStop', function(resource)
|
||||
for i, point in pairs(points) do
|
||||
if point.resource == resource then
|
||||
points[i] = nil
|
||||
end
|
||||
end
|
||||
end)
|
||||
local points = {}
|
||||
|
||||
function ESX.CreatePointInternal(coords, distance, hidden, enter, leave)
|
||||
local point = {
|
||||
coords = coords,
|
||||
distance = distance,
|
||||
hidden = hidden,
|
||||
enter = enter,
|
||||
leave = leave,
|
||||
resource = GetInvokingResource()
|
||||
}
|
||||
local handle = ESX.Table.SizeOf(points) + 1
|
||||
points[handle] = point
|
||||
return handle
|
||||
end
|
||||
|
||||
function ESX.RemovePointInternal(handle)
|
||||
points[handle] = nil
|
||||
end
|
||||
|
||||
function ESX.HidePointInternal(handle, hidden)
|
||||
if points[handle] then
|
||||
points[handle].hidden = hidden
|
||||
end
|
||||
end
|
||||
|
||||
function StartPointsLoop()
|
||||
CreateThread(function()
|
||||
while true do
|
||||
local coords = GetEntityCoords(ESX.PlayerData.ped)
|
||||
for handle, point in pairs(points) do
|
||||
if not point.hidden and #(coords - point.coords) <= point.distance then
|
||||
if not point.nearby then
|
||||
points[handle].nearby = true
|
||||
points[handle].enter()
|
||||
end
|
||||
elseif point.nearby then
|
||||
points[handle].nearby = false
|
||||
points[handle].leave()
|
||||
end
|
||||
end
|
||||
Wait(500)
|
||||
end
|
||||
end)
|
||||
end
|
||||
|
||||
|
||||
AddEventHandler('onResourceStop', function(resource)
|
||||
for handle, point in pairs(points) do
|
||||
if point.resource == resource then
|
||||
points[handle] = nil
|
||||
end
|
||||
end
|
||||
end)
|
||||
Reference in New Issue
Block a user