Added behaviors and documentation to oxlibs zones

Will provide file in example when more features are implimented
This commit is contained in:
Mr.Crowley committed 2025-08-23 22:53:49 -05:00
1 parent 59fa75a618
commit 8c309e248e
11 files changed
+418 -134

No files matched your search

+5 -5
View File
@@ -84,7 +84,7 @@ end
---This will add target options to all specified models. This is useful for when you want to add target options to all models of a specific type.
---@param options table
Target.AddGlobalPed = function(options, distance)
Target.AddGlobalPed = function(options)
options = Target.FixOptions(options)
qb_target:AddGlobalPed({
options = options,
@@ -100,7 +100,7 @@ end
---This will add taget options to all vehicles.
---@param options table
Target.AddGlobalVehicle = function(options, distance)
Target.AddGlobalVehicle = function(options)
options = Target.FixOptions(options)
qb_target:AddGlobalVehicle({
options = options,
@@ -136,7 +136,7 @@ end
---This will generate targets on non networked entites with the passed options.
---@param entities number | table
---@param options table
Target.AddLocalEntity = function(entities, options, distance)
Target.AddLocalEntity = function(entities, options)
options = Target.FixOptions(options)
qb_target:AddTargetEntity(entities, {
options = options,
@@ -174,7 +174,7 @@ end
---@param size table
---@param heading number
---@param options table
Target.AddBoxZone = function(name, coords, size, heading, options, debug, distance)
Target.AddBoxZone = function(name, coords, size, heading, options, debug)
options = Target.FixOptions(options)
if not next(options) then return end
qb_target:AddBoxZone(name, coords, size.x, size.y, {
@@ -195,7 +195,7 @@ end
---@param coords table
---@param radius number
---@param options table
Target.AddSphereZone = function(name, coords, radius, options, debug, distance)
Target.AddSphereZone = function(name, coords, radius, options, debug)
options = Target.FixOptions(options)
qb_target:AddCircleZone(name, coords, radius, {
name = name,
+39
View File
@@ -1,3 +1,25 @@
--- ox_lib compatibility wrapper for zone creation and management.
--- Provides a small API around lib.zones (box, sphere, poly) to create/destroy
--- and lookup zones with automatic id generation and resource ownership.
---
--- Example:
--- local id = cZones.Create("box", { coords = vec3(0,0,0), size = vec3(2,2,2), onEnter = function(z) ... end })
--- cZones.Destroy(id)
---
---@module cZones
---@class ZoneData
---@field id string Unique id for the zone (generated)
---@field invoking string Resource that created the zone
---@field coords vector3? Center coordinates (box/sphere)
---@field points vector2[]? Array of points for poly zones
---@field radius number? Radius for sphere zones
---@field size table? Size table or vec3 for box zones
---@field zone any Internal zone handle returned by lib.zones
---@field debug boolean? Debug flag passed to lib.zones
---@field onEnter fun(data:ZoneData)? Callback invoked on enter (receives ZoneData)
---@field onExit fun(data:ZoneData)? Callback invoked on exit (receives ZoneData)
---@alias ZoneType "box"|"sphere"|"poly"
---@diagnostic disable: duplicate-set-field
local resourceName = "ox_lib"
if GetResourceState(resourceName) == 'missing' then return end
@@ -6,6 +28,12 @@ Ids = Ids or Require('lib/utility/shared/ids.lua')
cZones = {} --conflict with ox
cZones.All = {}
--- Create a zone of the specified type.
--- Supported types: "box", "sphere", "poly"
---@param type ZoneType
---@param data ZoneData
---@return string|nil id Returns the generated zone id on success, or nil on failure
function cZones.Create(type, data)
assert(type, "Requires zone type ('box', 'sphere', or 'poly')")
assert(data, "Requires data table")
@@ -93,6 +121,9 @@ function cZones.Create(type, data)
return nil
end
--- Destroy a zone by id.
---@param id string
---@return boolean success True if destroyed, false otherwise
function cZones.Destroy(id)
if not id then return false end
local zone = cZones.All[id].zone
@@ -102,6 +133,9 @@ function cZones.Destroy(id)
return true
end
--- Destroy all zones created by a specific resource.
---@param resource string Name of the invoking resource
---@return boolean success True if operation completed (no guarantee zones existed)
function cZones.DestroyByResource(resource)
if not resource then return false end
for id, zone in pairs(cZones.All or {}) do
@@ -112,11 +146,16 @@ function cZones.DestroyByResource(resource)
return true
end
--- Retrieve zone data by id.
---@param id string
---@return ZoneData|nil
function cZones.Get(id)
if not id then return nil end
return cZones.All[id]
end
--- Event handler: clean up zones when a resource stops.
--- This is registered to the "onResourceStop" event and delegates to DestroyByResource.
RegisterNetEvent("onResourceStop", function(resource)
cZones.DestroyByResource(resource)
end)