diff --git a/examples/zones.lua b/examples/zones.lua new file mode 100644 index 0000000..d3f676a --- /dev/null +++ b/examples/zones.lua @@ -0,0 +1,53 @@ +Bridge = Bridge or exports['community_bridge']:Bridge() + +RegisterCommand('testboxzone', function(source, args, rawCommand) + local playerPos = GetEntityCoords(PlayerPedId()) + Bridge.Zones.Create("box", { + coords = vector3(playerPos.x, playerPos.y, playerPos.z), + size = vector3(2, 2, 6), + heading = 90, + debug = true, + onEnter = function(data) + print("Entered Zone: " .. data.id) + end, + onExit = function(data) + print("Exited Zone:") + end + }) +end, false) + +RegisterCommand('testpolyzone', function(source, args, rawCommand) + local playerPos = GetEntityCoords(PlayerPedId()) + Bridge.Zones.Create("poly", { + points = { + vector3(playerPos.x - 1, playerPos.y - 1, playerPos.z), + vector3(playerPos.x + 1, playerPos.y - 1, playerPos.z), + vector3(playerPos.x + 1, playerPos.y + 1, playerPos.z), + vector3(playerPos.x - 1, playerPos.y + 1, playerPos.z) + }, + size = vector3(1, 1, 1), + heading = 90, + debug = true, + onEnter = function(data) + print("Entered Zone: " .. data.id) + end, + onExit = function(data) + print("Exited Zone:") + end + }) +end, false) + +RegisterCommand('testspherezone', function(source, args, rawCommand) + local playerPos = GetEntityCoords(PlayerPedId()) + Bridge.Zones.Create("sphere", { + coords = vector3(playerPos.x, playerPos.y, playerPos.z), + radius = 2.0, + debug = true, + onEnter = function(data) + print("Entered Zone: " .. data.id) + end, + onExit = function(data) + print("Exited Zone:") + end + }) +end, false) diff --git a/fxmanifest.lua b/fxmanifest.lua index 8fa380e..361d07c 100644 --- a/fxmanifest.lua +++ b/fxmanifest.lua @@ -61,6 +61,7 @@ client_scripts { 'modules/housing/**/client.lua', 'modules/skills/**/client.lua', 'modules/bossmenu/**/client.lua', + 'modules/zones/**/client.lua', 'init.lua', -- 'unit_tests/*.lua', } diff --git a/init.lua b/init.lua index f4b020b..0d6bc0d 100644 --- a/init.lua +++ b/init.lua @@ -106,4 +106,5 @@ Bridge.RegisterModule("Weather", Weather) Bridge.RegisterModule("Target", Target) Bridge.RegisterModule("Menu", Menu) -Bridge.RegisterModule("Dialogue", Dialogue) \ No newline at end of file +Bridge.RegisterModule("Dialogue", Dialogue) +Bridge.RegisterModule("Zones", cZones) \ No newline at end of file diff --git a/modules/menu/ox_lib/client.lua b/modules/menu/ox_lib/client.lua index 6b65140..710dac0 100644 --- a/modules/menu/ox_lib/client.lua +++ b/modules/menu/ox_lib/client.lua @@ -3,6 +3,8 @@ local configValue = BridgeClientConfig.MenuSystem if (configValue == "auto" and GetResourceState(resourceName) ~= "started") or (configValue ~= "auto" and configValue ~= resourceName) then return end Menus = Menus or {} +lib = lib or Require("init.lua", "ox_lib") + ---Converts a QB menu to an Ox menu. ---@param id string The menu ID. ---@param menu table The QB menu data. diff --git a/modules/zones/oxlib/client.lua b/modules/zones/oxlib/client.lua new file mode 100644 index 0000000..26f67ca --- /dev/null +++ b/modules/zones/oxlib/client.lua @@ -0,0 +1,122 @@ +---@diagnostic disable: duplicate-set-field +local resourceName = "ox_lib" +if GetResourceState(resourceName) == 'missing' then return end +lib = lib or Require("init.lua", "ox_lib") +Ids = Ids or Require('lib/utility/shared/ids.lua') + +cZones = {} --conflict with ox +cZones.All = {} +function cZones.Create(type, data) + assert(type, "Requires zone type ('box', 'sphere', or 'poly')") + assert(data, "Requires data table") + data.id = Ids.CreateUniqueId(cZones.All) + data.invoking = GetInvokingResource() or "unknown" + + local onEnter = data.onEnter or data.OnEnter + local onExit = data.onExit or data.OnExit + local zone = nil + if type == "box" then + assert(data.coords, "Requires .coords as vector3 in data") + local width = data.size?.x or data.length or 1.0 + local height = data.size?.y or data.width or 1.0 + local z = data.size?.z or data.height or 1.0 + zone = lib.zones.box({ + coords = data.coords, + size = vec3(width, height, z), + rotation = data.heading or data.rotation or 0.0, + debug = data.debug or false, + onEnter = function(point) + if onEnter then + onEnter(data) + end + end, + onExit = function(point) + if onExit then + onExit(data) + end + end, + }) + data.zone = zone + print("Created Zone: " .. data.id, json.encode(cZones.All)) + cZones.All[data.id] = data + return data.id + elseif type == "sphere" then + assert(data.coords, "Requires .coords as vector3 in data") + data.radius = data.radius or data.size?.x or 1 + lib.zones.sphere(data) + local zone = lib.zones.sphere({ + coords = data.coords, + radius = data.radius, + debug = data.debug or false, + onEnter = function(point) + data.zone = data.zone or point + if onEnter then + onEnter(data) + end + end, + onExit = function(point) + data.zone = data.zone or point + if onExit then + onExit(data) + end + end, + }) + data.zone = zone + cZones.All[data.id] = data + return data.id + elseif type == "poly" then + assert(data.points, "Requires an array of vector2 in .points") + assert(#data.points > 2, "Requires at least three points in .points") + local zSize = data.minZ or data.size?.z or data.height or data.thickness or 2 + + local polyZone = lib.zones.poly({ + points = data.points, + thickness = zSize, + debug = data.debug or false, + onEnter = function(point) + data.zone = data.zone or point + if onEnter then + onEnter(data) + end + end, + onExit = function(point) + data.zone = data.zone or point + if onExit then + onExit(data) + end + end, + }) + data.zone = polyZone + cZones.All[data.id] = data + return data.id + end + return nil +end + +function cZones.Destroy(id) + if not id then return false end + local zone = cZones.All[id].zone + if not zone then return false end + zone:remove() + cZones.All[id] = nil + return true +end + +function cZones.DestroyByResource(resource) + if not resource then return false end + for id, zone in pairs(cZones.All or {}) do + if zone.invoking == resource then + zone.zone:remove() + end + end + return true +end + +function cZones.Get(id) + if not id then return nil end + return cZones.All[id] +end + +RegisterNetEvent("onResourceStop", function(resource) + cZones.DestroyByResource(resource) +end) diff --git a/modules/zones/polyzone/client.lua b/modules/zones/polyzone/client.lua new file mode 100644 index 0000000..7d9fa42 --- /dev/null +++ b/modules/zones/polyzone/client.lua @@ -0,0 +1,116 @@ +---@diagnostic disable: duplicate-set-field + +local resourceName = "PolyZone" +if GetResourceState(resourceName) == 'missing' or GetResourceState('ox_lib') ~= 'missing' then return end + +Require("client.lua", resourceName) +Require("BoxZone.lua", resourceName) +Require("EntityZone.lua", resourceName) +Require("CircleZone.lua", resourceName) +Require("ComboZone.lua", resourceName) +Ids = Ids or Require('lib/utility/shared/ids.lua') +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") + data.id = Ids.CreateUniqueId(cZones.All) + data.invoking = GetInvokingResource() or "unknown" + local zone = nil + if type == "box" then + assert(data.coords, "Requires .coords as vector3 in data") + local width = data.size.x or data.length or 1.0 + local height = data.size.y or data.width or 1.0 + local z = data.size?.z or data.height or 1.0 + data.size = vec3(width, height, z) + data.heading = data.heading or data.rotation + data.rotation = data.heading + zone = BoxZone:Create(data.coords, width, height, { + name = data.id, + debugPoly = data.debug, + minZ = data.coords.z - 0.5, + maxZ = data.coords.z + z - 0.5, + heading = data.heading or data.rotation + }) + elseif type == "sphere" then + assert(data.coords, "Requires .coords as vector3 in data") + data.radius = data.radius or data.size?.x or 1 + zone = CircleZone:Create(data.coords, data.radius, { + name = data.id, + debugPoly = data.debug, + }) + elseif type == "poly" then + assert(data.points, "Requires an array of vector2 in .points") + assert(#data.points > 2, "Requires at least three points in .points") + local zSize = data.minZ or data.size?.z or data.height or data.thickness or 2 + local firstPoint = data.points[1] + local firstZ = firstPoint.z + zone = PolyZone:Create(data.points, { + name = data.id, + debugPoly = data.debug, + minZ = firstZ - 0.5, + maxZ = firstZ and (firstZ + zSize - 0.5) or nil, + }) + end + if not zone then return end + data.zone = zone + zone:onPlayerInOut(function(isInside, _) + if isInside then + if data.onEnter or data.OnEnter then + (data.onEnter or data.OnEnter)(data) + end + else + if data.onExit or data.OnExit then + (data.onExit or data.OnExit)(data) + end + end + end) + + cZones.All[data.id] = data + return data.id +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 + if not zone then return false end + zone:destroy() + cZones.All[id] = nil + 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) do + if zone.invoking == resource then + zone.zone:destroy() + end + end + 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. +---@param resource string +RegisterNetEvent("onResourceStop", function(resource) + cZones.DestroyByResource(resource) +end)