mirror of
https://github.com/CommunityOx/ox_lib.git
synced 2026-08-16 22:46:03 +01:00
refactor(zones): move to shared module
This commit is contained in:
@@ -1,110 +0,0 @@
|
|||||||
local glm = require 'glm'
|
|
||||||
|
|
||||||
---@type table<number, CZone>
|
|
||||||
local Zones = {}
|
|
||||||
_ENV.Zones = Zones
|
|
||||||
|
|
||||||
local function removeZone(self)
|
|
||||||
Zones[self.id] = nil
|
|
||||||
end
|
|
||||||
|
|
||||||
local glm_polygon_contains = glm.polygon.contains
|
|
||||||
|
|
||||||
local function contains(self, coords)
|
|
||||||
return glm_polygon_contains(self.polygon, coords, self.thickness / 4)
|
|
||||||
end
|
|
||||||
|
|
||||||
local function insideSphere(self, coords)
|
|
||||||
return #(self.coords - coords) < self.radius
|
|
||||||
end
|
|
||||||
|
|
||||||
local function convertToVector(coords)
|
|
||||||
local _type = type(coords)
|
|
||||||
|
|
||||||
if _type ~= 'vector3' then
|
|
||||||
if _type == 'table' or _type == 'vector4' then
|
|
||||||
return vec3(coords[1] or coords.x, coords[2] or coords.y, coords[3] or coords.z)
|
|
||||||
end
|
|
||||||
|
|
||||||
error(("expected type 'vector3' or 'table' (received %s)"):format(_type))
|
|
||||||
end
|
|
||||||
|
|
||||||
return coords
|
|
||||||
end
|
|
||||||
|
|
||||||
lib.zones = {
|
|
||||||
---@return CZone
|
|
||||||
poly = function(data)
|
|
||||||
data.id = #Zones + 1
|
|
||||||
data.thickness = data.thickness or 4
|
|
||||||
|
|
||||||
local pointN = #data.points
|
|
||||||
local points = table.create(pointN, 0)
|
|
||||||
|
|
||||||
for i = 1, pointN do
|
|
||||||
points[i] = convertToVector(data.points[i])
|
|
||||||
end
|
|
||||||
|
|
||||||
data.polygon = glm.polygon.new(points)
|
|
||||||
data.coords = data.polygon:centroid()
|
|
||||||
data.type = 'poly'
|
|
||||||
data.remove = removeZone
|
|
||||||
data.contains = contains
|
|
||||||
data.debug = nil
|
|
||||||
data.debugColour = nil
|
|
||||||
data.inside = nil
|
|
||||||
data.onEnter = nil
|
|
||||||
data.onExit = nil
|
|
||||||
|
|
||||||
Zones[data.id] = data
|
|
||||||
return data
|
|
||||||
end,
|
|
||||||
|
|
||||||
---@return CZone
|
|
||||||
box = function(data)
|
|
||||||
data.id = #Zones + 1
|
|
||||||
data.coords = convertToVector(data.coords)
|
|
||||||
data.size = data.size and convertToVector(data.size) / 2 or vec3(2)
|
|
||||||
data.thickness = data.size.z * 2 or 4
|
|
||||||
data.rotation = quat(data.rotation or 0, vec3(0, 0, 1))
|
|
||||||
data.polygon = (data.rotation * glm.polygon.new({
|
|
||||||
vec3(data.size.x, data.size.y, 0),
|
|
||||||
vec3(-data.size.x, data.size.y, 0),
|
|
||||||
vec3(-data.size.x, -data.size.y, 0),
|
|
||||||
vec3(data.size.x, -data.size.y, 0),
|
|
||||||
}) + data.coords)
|
|
||||||
data.type = 'box'
|
|
||||||
data.remove = removeZone
|
|
||||||
data.contains = contains
|
|
||||||
data.debug = nil
|
|
||||||
data.debugColour = nil
|
|
||||||
data.inside = nil
|
|
||||||
data.onEnter = nil
|
|
||||||
data.onExit = nil
|
|
||||||
|
|
||||||
Zones[data.id] = data
|
|
||||||
return data
|
|
||||||
end,
|
|
||||||
|
|
||||||
---@return CZone
|
|
||||||
sphere = function(data)
|
|
||||||
data.id = #Zones + 1
|
|
||||||
data.coords = convertToVector(data.coords)
|
|
||||||
data.radius = (data.radius or 2) + 0.0
|
|
||||||
data.type = 'sphere'
|
|
||||||
data.remove = removeZone
|
|
||||||
data.contains = insideSphere
|
|
||||||
data.debug = nil
|
|
||||||
data.debugColour = nil
|
|
||||||
data.inside = nil
|
|
||||||
data.onEnter = nil
|
|
||||||
data.onExit = nil
|
|
||||||
|
|
||||||
Zones[data.id] = data
|
|
||||||
return data
|
|
||||||
end,
|
|
||||||
|
|
||||||
getAllZones = function() return Zones end,
|
|
||||||
}
|
|
||||||
|
|
||||||
return lib.zones
|
|
||||||
@@ -96,20 +96,17 @@ local function getTriangles(polygon)
|
|||||||
return triangles
|
return triangles
|
||||||
end
|
end
|
||||||
|
|
||||||
---@type table<number, CZone>
|
local insideZones = lib.context == 'client' and {} --[[@as table<number, CZone>]]
|
||||||
local insideZones = {}
|
local exitingZones = lib.context == 'client' and lib.array:new() --[[@as Array<CZone>]]
|
||||||
|
local enteringZones = lib.context == 'client' and lib.array:new() --[[@as Array<CZone>]]
|
||||||
---@type CZone[] | Array
|
|
||||||
local exitingZones = lib.array:new()
|
|
||||||
|
|
||||||
---@type CZone[] | Array
|
|
||||||
local enteringZones = lib.array:new()
|
|
||||||
|
|
||||||
local tick
|
|
||||||
local glm_polygon_contains = glm.polygon.contains
|
local glm_polygon_contains = glm.polygon.contains
|
||||||
|
local tick
|
||||||
|
|
||||||
local function removeZone(zone)
|
local function removeZone(zone)
|
||||||
Zones[zone.id] = nil
|
Zones[zone.id] = nil
|
||||||
|
|
||||||
|
if lib.context == 'server' then return end
|
||||||
|
|
||||||
insideZones[zone.id] = nil
|
insideZones[zone.id] = nil
|
||||||
|
|
||||||
table.remove(exitingZones, exitingZones:indexOf(zone))
|
table.remove(exitingZones, exitingZones:indexOf(zone))
|
||||||
@@ -117,6 +114,8 @@ local function removeZone(zone)
|
|||||||
end
|
end
|
||||||
|
|
||||||
CreateThread(function()
|
CreateThread(function()
|
||||||
|
if lib.context == 'server' then return end
|
||||||
|
|
||||||
while true do
|
while true do
|
||||||
local coords = GetEntityCoords(cache.ped)
|
local coords = GetEntityCoords(cache.ped)
|
||||||
cache.coords = coords
|
cache.coords = coords
|
||||||
@@ -292,9 +291,32 @@ local function setDebug(self, bool, colour)
|
|||||||
self.debug = self.__type == 'sphere' and debugSphere or debugPoly or nil
|
self.debug = self.__type == 'sphere' and debugSphere or debugPoly or nil
|
||||||
end
|
end
|
||||||
|
|
||||||
lib.zones = {
|
---@param data CZone
|
||||||
---@return CZone
|
---@return CZone
|
||||||
poly = function(data)
|
local function setZone(data)
|
||||||
|
data.remove = removeZone
|
||||||
|
data.contains = data.contains or contains
|
||||||
|
|
||||||
|
if lib.context == 'client' then
|
||||||
|
data.setDebug = setDebug
|
||||||
|
|
||||||
|
if data.debug then
|
||||||
|
data.debug = nil
|
||||||
|
|
||||||
|
data:setDebug(true, data.debugColour)
|
||||||
|
end
|
||||||
|
else
|
||||||
|
data.debug = nil
|
||||||
|
end
|
||||||
|
|
||||||
|
Zones[data.id] = data
|
||||||
|
|
||||||
|
return data
|
||||||
|
end
|
||||||
|
|
||||||
|
lib.zones = {}
|
||||||
|
|
||||||
|
function lib.zones.poly(data)
|
||||||
data.id = #Zones + 1
|
data.id = #Zones + 1
|
||||||
data.thickness = data.thickness or 4
|
data.thickness = data.thickness or 4
|
||||||
|
|
||||||
@@ -360,69 +382,39 @@ lib.zones = {
|
|||||||
|
|
||||||
data.coords = data.polygon:centroid()
|
data.coords = data.polygon:centroid()
|
||||||
data.__type = 'poly'
|
data.__type = 'poly'
|
||||||
data.remove = removeZone
|
|
||||||
data.contains = contains
|
|
||||||
data.setDebug = setDebug
|
|
||||||
|
|
||||||
if data.debug then
|
return setZone(data)
|
||||||
data.debug = nil
|
|
||||||
|
|
||||||
data:setDebug(true, data.debugColour)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
Zones[data.id] = data
|
function lib.zones.box(data)
|
||||||
return data
|
|
||||||
end,
|
|
||||||
|
|
||||||
---@return CZone
|
|
||||||
box = function(data)
|
|
||||||
data.id = #Zones + 1
|
data.id = #Zones + 1
|
||||||
data.coords = convertToVector(data.coords)
|
data.coords = convertToVector(data.coords)
|
||||||
data.size = data.size and convertToVector(data.size) / 2 or vec3(2)
|
data.size = data.size and convertToVector(data.size) / 2 or vec3(2)
|
||||||
data.thickness = data.size.z * 2 or 4
|
data.thickness = data.size.z * 2 or 4
|
||||||
data.rotation = quat(data.rotation or 0, vec3(0, 0, 1))
|
data.rotation = quat(data.rotation or 0, vec3(0, 0, 1))
|
||||||
|
data.__type = 'box'
|
||||||
data.polygon = (data.rotation * glm.polygon.new({
|
data.polygon = (data.rotation * glm.polygon.new({
|
||||||
vec3(data.size.x, data.size.y, 0),
|
vec3(data.size.x, data.size.y, 0),
|
||||||
vec3(-data.size.x, data.size.y, 0),
|
vec3(-data.size.x, data.size.y, 0),
|
||||||
vec3(-data.size.x, -data.size.y, 0),
|
vec3(-data.size.x, -data.size.y, 0),
|
||||||
vec3(data.size.x, -data.size.y, 0),
|
vec3(data.size.x, -data.size.y, 0),
|
||||||
}) + data.coords)
|
}) + data.coords)
|
||||||
data.__type = 'box'
|
|
||||||
data.remove = removeZone
|
|
||||||
data.contains = contains
|
|
||||||
data.setDebug = setDebug
|
|
||||||
|
|
||||||
if data.debug then
|
return setZone(data)
|
||||||
data.debug = nil
|
|
||||||
|
|
||||||
data:setDebug(true, data.debugColour)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
Zones[data.id] = data
|
function lib.zones.sphere(data)
|
||||||
return data
|
|
||||||
end,
|
|
||||||
|
|
||||||
---@return CZone
|
|
||||||
sphere = function(data)
|
|
||||||
data.id = #Zones + 1
|
data.id = #Zones + 1
|
||||||
data.coords = convertToVector(data.coords)
|
data.coords = convertToVector(data.coords)
|
||||||
data.radius = (data.radius or 2) + 0.0
|
data.radius = (data.radius or 2) + 0.0
|
||||||
data.__type = 'sphere'
|
data.__type = 'sphere'
|
||||||
data.remove = removeZone
|
|
||||||
data.contains = insideSphere
|
data.contains = insideSphere
|
||||||
data.setDebug = setDebug
|
|
||||||
|
|
||||||
if data.debug then
|
return setZone(data)
|
||||||
data:setDebug(true, data.debugColour)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
Zones[data.id] = data
|
function lib.zones.getAllZones() return Zones end
|
||||||
return data
|
|
||||||
end,
|
|
||||||
|
|
||||||
getAllZones = function() return Zones end,
|
function lib.zones.getCurrentZones() return insideZones end
|
||||||
|
|
||||||
getCurrentZones = function() return insideZones end,
|
|
||||||
}
|
|
||||||
|
|
||||||
return lib.zones
|
return lib.zones
|
||||||
Reference in New Issue
Block a user