mirror of
https://github.com/esx-framework/esx_core.git
synced 2026-08-29 17:28:53 +00:00
experimental: Class and Point imports
This commit is contained in:
@@ -0,0 +1,21 @@
|
||||
local class = {}
|
||||
class.__index = class
|
||||
|
||||
function class:new(...)
|
||||
local instance = setmetatable({}, self)
|
||||
if instance.constructor then
|
||||
local ret = instance:constructor(...)
|
||||
if type(ret) == 'table' then
|
||||
return ret
|
||||
end
|
||||
end
|
||||
return instance
|
||||
end
|
||||
|
||||
function Class(body, heritage)
|
||||
local prototype = body or {}
|
||||
prototype.__index = prototype
|
||||
return setmetatable(prototype, heritage or class)
|
||||
end
|
||||
|
||||
return Class
|
||||
@@ -0,0 +1,44 @@
|
||||
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.CreatePointIntenal(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
|
||||
|
||||
return Point
|
||||
@@ -54,7 +54,7 @@ ESX.SecureNetEvent("esx:playerLoaded", function(xPlayer, _, skin)
|
||||
end
|
||||
|
||||
Actions:Init()
|
||||
|
||||
StartPointsLoop()
|
||||
StartServerSyncLoops()
|
||||
end)
|
||||
|
||||
|
||||
@@ -0,0 +1,58 @@
|
||||
local points = {}
|
||||
|
||||
function ESX.CreatePointIntenal(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=1, #points do
|
||||
local point = points[i]
|
||||
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=1, #points do
|
||||
if points[i].resource == resource then
|
||||
points[i] = nil
|
||||
end
|
||||
end
|
||||
end)
|
||||
@@ -45,6 +45,7 @@ client_scripts {
|
||||
'client/modules/wrapper.lua',
|
||||
'client/modules/callback.lua',
|
||||
'client/modules/adjustments.lua',
|
||||
'client/modules/points.lua',
|
||||
|
||||
'client/main.lua',
|
||||
|
||||
@@ -73,6 +74,7 @@ files {
|
||||
|
||||
'html/fonts/pdown.ttf',
|
||||
'html/fonts/bankgothic.ttf',
|
||||
"client/imports/*.lua",
|
||||
}
|
||||
|
||||
dependencies {
|
||||
|
||||
@@ -21,4 +21,23 @@ if not IsDuplicityVersion() then -- Only register this event for the client
|
||||
ESX.PlayerLoaded = false
|
||||
ESX.PlayerData = {}
|
||||
end)
|
||||
|
||||
local external = {{"Class", "class.lua"}, {"Point", "point.lua"}}
|
||||
for i=1, #external do
|
||||
local module = external[i]
|
||||
local path = string.format("client/imports/%s", module[2])
|
||||
|
||||
local file = LoadResourceFile("es_extended", path)
|
||||
if file then
|
||||
local fn, err = load(file, ('@@es_extended/%s'):format(path))
|
||||
|
||||
if not fn or err then
|
||||
return error(('\n^1Error importing module (%s)'):format(external[i]))
|
||||
end
|
||||
|
||||
ESX[module[1]] = fn()
|
||||
else
|
||||
return error(('\n^1Error loading module (%s)'):format(external[i]))
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user