2025-03-08 13:44:05 +00:00
|
|
|
--[[
|
|
|
|
|
PolyZone Management Module
|
|
|
|
|
----------------------------
|
|
|
|
|
This module automatically detects the available polyzone library (ox_lib or PolyZone)
|
|
|
|
|
and creates polygonal and circular zones accordingly. It also provides a function to remove
|
|
|
|
|
previously created zones.
|
|
|
|
|
|
|
|
|
|
Functions:
|
|
|
|
|
• createPoly(data) - Creates a polygonal zone.
|
|
|
|
|
• createCirclePoly(data) - Creates a circular zone.
|
|
|
|
|
• removePolyZone(Location) - Removes a created zone.
|
|
|
|
|
]]
|
|
|
|
|
|
2025-08-13 18:27:07 +01:00
|
|
|
|
|
|
|
|
local polyCreation = {
|
|
|
|
|
{
|
|
|
|
|
polyZoneScript = OXLibExport,
|
|
|
|
|
createPoly = function(data)
|
|
|
|
|
data.minZ = data.minZ or -20.0
|
|
|
|
|
data.maxZ = data.maxZ or 1000.0
|
|
|
|
|
data.thickness = ((data.maxZ / 2) - (data.minZ / 2)) * 2
|
|
|
|
|
|
|
|
|
|
local mid = data.maxZ - ((data.maxZ / 2) - (data.minZ / 2))
|
|
|
|
|
for i = 1, #data.points do
|
|
|
|
|
data.points[i] = vec3(data.points[i].x, data.points[i].y, mid)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
return lib.zones.poly(data)
|
|
|
|
|
end,
|
|
|
|
|
createCircle = function(data)
|
|
|
|
|
return lib.zones.sphere(data)
|
|
|
|
|
end,
|
|
|
|
|
removeZone = function(Location)
|
|
|
|
|
Location:remove()
|
|
|
|
|
end,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
polyZoneScript = "PolyZone",
|
|
|
|
|
createPoly = function(data)
|
|
|
|
|
local zone = PolyZone:Create(data.points, {
|
|
|
|
|
name = data.name,
|
|
|
|
|
minZ = data.minZ or nil,
|
|
|
|
|
maxZ = data.maxZ or nil,
|
|
|
|
|
debugPoly = data.debug
|
|
|
|
|
})
|
|
|
|
|
zone:onPlayerInOut(function(isPointInside)
|
|
|
|
|
if isPointInside then
|
|
|
|
|
data.onEnter()
|
|
|
|
|
else
|
|
|
|
|
data.onExit()
|
|
|
|
|
end
|
|
|
|
|
end)
|
|
|
|
|
return zone
|
|
|
|
|
end,
|
|
|
|
|
createCircle = function(data)
|
|
|
|
|
local zone = CircleZone:Create(data.coords, data.radius, { name = data.name, debugPoly = debugMode })
|
|
|
|
|
zone:onPlayerInOut(function(isPointInside)
|
|
|
|
|
if isPointInside then
|
|
|
|
|
data.onEnter()
|
|
|
|
|
else
|
|
|
|
|
data.onExit()
|
|
|
|
|
end
|
|
|
|
|
end)
|
|
|
|
|
return zone
|
|
|
|
|
end,
|
|
|
|
|
removeZone = function(Location)
|
|
|
|
|
Location:destroy()
|
|
|
|
|
end,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2025-03-08 13:44:05 +00:00
|
|
|
-------------------------------------------------------------
|
|
|
|
|
-- Polygonal Zone Creation
|
|
|
|
|
-------------------------------------------------------------
|
|
|
|
|
|
2025-03-01 12:58:43 +00:00
|
|
|
--- Creates a polygonal zone using the detected polyzone library (ox_lib or PolyZone).
|
|
|
|
|
---
|
2025-03-08 13:44:05 +00:00
|
|
|
--- Automatically checks which polyzone script is active. When using ox_lib, it converts the provided
|
|
|
|
|
--- 2D points to 3D (setting a constant z value) and sets a thickness value. For PolyZone, it creates the zone
|
|
|
|
|
--- and attaches onEnter and onExit callbacks.
|
2025-03-01 12:58:43 +00:00
|
|
|
---
|
2025-03-08 13:44:05 +00:00
|
|
|
--- @param data table Zone configuration table with the following keys:
|
|
|
|
|
--- - name (string): The zone's identifier.
|
|
|
|
|
--- - debug (boolean): Whether debug mode is enabled.
|
|
|
|
|
--- - points (table): A list of vec2 points defining the polygon.
|
|
|
|
|
--- - onEnter (function): Callback when a player enters the zone.
|
|
|
|
|
--- - onExit (function): Callback when a player exits the zone.
|
2025-03-01 12:58:43 +00:00
|
|
|
---
|
2025-03-08 13:44:05 +00:00
|
|
|
--- @return table|nil table Returns the created zone object or nil if creation failed.
|
2025-03-01 12:58:43 +00:00
|
|
|
---
|
|
|
|
|
---@usage
|
2025-03-08 13:44:05 +00:00
|
|
|
---```lua
|
|
|
|
|
---createPoly({
|
|
|
|
|
--- name = 'testZone',
|
|
|
|
|
--- debug = true,
|
|
|
|
|
--- points = { vec2(100.0, 100.0), vec2(200.0, 100.0), vec2(200.0, 200.0), vec2(100.0, 200.0) },
|
|
|
|
|
--- onEnter = function() print("Entered Test Zone") end,
|
|
|
|
|
--- onExit = function() print("Exited Test Zone") end,
|
|
|
|
|
---})
|
|
|
|
|
---```
|
2025-03-01 12:58:43 +00:00
|
|
|
function createPoly(data)
|
2025-08-13 18:27:07 +01:00
|
|
|
for _, script in ipairs(polyCreation) do
|
|
|
|
|
if isStarted(script.polyZoneScript) then
|
|
|
|
|
debugPrint("^6Bridge^7: ^2Creating new poly with ^7'^4"..script.polyZoneScript.."^7': "..data.name)
|
|
|
|
|
return script.createPoly(data)
|
2025-03-01 12:58:43 +00:00
|
|
|
end
|
|
|
|
|
end
|
2025-08-13 18:27:07 +01:00
|
|
|
|
|
|
|
|
print("^4ERROR^7: ^2No PolyZone creation script detected ^7")
|
|
|
|
|
return nil
|
2025-03-01 12:58:43 +00:00
|
|
|
end
|
|
|
|
|
|
2025-03-08 13:44:05 +00:00
|
|
|
-------------------------------------------------------------
|
|
|
|
|
-- Circular Zone Creation
|
|
|
|
|
-------------------------------------------------------------
|
|
|
|
|
|
2025-03-01 12:58:43 +00:00
|
|
|
--- Creates a circular zone using the detected polyzone library (ox_lib or PolyZone).
|
|
|
|
|
---
|
2025-03-08 13:44:05 +00:00
|
|
|
--- When using ox_lib, it creates a sphere zone. For PolyZone, it creates a CircleZone and attaches
|
|
|
|
|
--- onEnter and onExit callbacks.
|
2025-03-01 12:58:43 +00:00
|
|
|
---
|
2025-03-08 13:44:05 +00:00
|
|
|
--- @param data table Zone configuration with the following keys:
|
|
|
|
|
--- - name (string): The zone's identifier.
|
|
|
|
|
--- - coords (vector3): The center of the circle.
|
|
|
|
|
--- - radius (number): The radius of the circle.
|
|
|
|
|
--- - onEnter (function): Callback when a player enters the zone.
|
|
|
|
|
--- - onExit (function): Callback when a player exits the zone.
|
2025-03-01 12:58:43 +00:00
|
|
|
---
|
2025-03-08 13:44:05 +00:00
|
|
|
--- @return table|nil table Returns the created circular zone object or nil if creation failed.
|
2025-03-01 12:58:43 +00:00
|
|
|
---
|
2025-03-08 13:44:05 +00:00
|
|
|
--- @usage
|
2025-03-01 12:58:43 +00:00
|
|
|
--- ```lua
|
|
|
|
|
--- createCirclePoly({
|
|
|
|
|
--- name = 'circleZone',
|
|
|
|
|
--- coords = vector3(150.0, 150.0, 20.0),
|
|
|
|
|
--- radius = 50.0,
|
|
|
|
|
--- onEnter = function() print("Entered Circle Zone") end,
|
|
|
|
|
--- onExit = function() print("Exited Circle Zone") end,
|
|
|
|
|
--- })
|
|
|
|
|
--- ```
|
|
|
|
|
function createCirclePoly(data)
|
2025-08-13 18:27:07 +01:00
|
|
|
for _, script in ipairs(polyCreation) do
|
|
|
|
|
if isStarted(script.polyZoneScript) then
|
|
|
|
|
debugPrint("^6Bridge^7: ^2Creating new ^3Cricle^2 poly with ^7"..script.polyZoneScript.." "..data.name)
|
|
|
|
|
debugPrint("^6Bridge^7: ^2Zone Stats - Coords: "..formatCoord(data.coords).." Radius: "..data.radius)
|
|
|
|
|
return script.createCircle(data)
|
|
|
|
|
end
|
2025-03-01 12:58:43 +00:00
|
|
|
end
|
2025-08-13 18:27:07 +01:00
|
|
|
|
|
|
|
|
print("^4ERROR^7: ^2No PolyZone creation script detected ^7")
|
|
|
|
|
return nil
|
2025-03-01 12:58:43 +00:00
|
|
|
end
|
|
|
|
|
|
2025-03-08 13:44:05 +00:00
|
|
|
-------------------------------------------------------------
|
|
|
|
|
-- PolyZone Removal Function
|
|
|
|
|
-------------------------------------------------------------
|
|
|
|
|
|
2025-03-01 12:58:43 +00:00
|
|
|
--- Removes a previously created polyzone.
|
|
|
|
|
---
|
2025-03-08 13:44:05 +00:00
|
|
|
--- Detects the active polyzone library and calls the appropriate removal method.
|
2025-03-01 12:58:43 +00:00
|
|
|
---
|
|
|
|
|
--- @param Location table The zone object to be removed.
|
|
|
|
|
---
|
|
|
|
|
--- @usage
|
|
|
|
|
--- ```lua
|
|
|
|
|
--- local zone = createPoly({...})
|
2025-03-08 13:44:05 +00:00
|
|
|
---
|
2025-03-01 12:58:43 +00:00
|
|
|
--- removePolyZone(zone)
|
|
|
|
|
--- ```
|
|
|
|
|
function removePolyZone(Location)
|
2025-08-13 18:27:07 +01:00
|
|
|
for _, script in ipairs(polyCreation) do
|
|
|
|
|
if isStarted(script.polyZoneScript) then
|
|
|
|
|
debugPrint("^6Bridge^7: ^2Removing ^3"..script.polyZoneScript.." ^2Zone^7")
|
|
|
|
|
script.removeZone(Location)
|
2025-08-13 22:40:54 +01:00
|
|
|
break
|
2025-08-13 18:27:07 +01:00
|
|
|
end
|
2025-03-01 12:58:43 +00:00
|
|
|
end
|
2025-02-21 13:47:15 +00:00
|
|
|
end
|