2022-04-25 20:28:10 +10:00
|
|
|
local glm = require 'glm'
|
|
|
|
|
local zones = {}
|
|
|
|
|
|
|
|
|
|
local function removeZone(self)
|
|
|
|
|
zones[self.id] = nil
|
|
|
|
|
end
|
|
|
|
|
|
2022-04-25 21:33:56 +10:00
|
|
|
local function debug(self)
|
|
|
|
|
local points = self.polygon()
|
|
|
|
|
for i = 1, #points do
|
|
|
|
|
if i == #points then
|
|
|
|
|
DrawPoly(points[i], points[1], self.centroid, 255, 0, 0, 50)
|
|
|
|
|
DrawPoly(points[1], points[i], self.centroid, 255, 0, 0, 50)
|
|
|
|
|
else
|
|
|
|
|
DrawPoly(points[i], points[i + 1], self.centroid, 255, 0, 0, 50)
|
|
|
|
|
DrawPoly(points[i + 1], points[i], self.centroid, 255, 0, 0, 50)
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
local inside = {}
|
2022-04-25 20:28:10 +10:00
|
|
|
local tick
|
|
|
|
|
|
|
|
|
|
CreateThread(function()
|
|
|
|
|
while true do
|
|
|
|
|
local coords = GetEntityCoords(cache.ped)
|
|
|
|
|
Wait(300)
|
2022-04-25 21:33:56 +10:00
|
|
|
table.wipe(inside)
|
2022-04-25 20:28:10 +10:00
|
|
|
|
|
|
|
|
for _, zone in pairs(zones) do
|
2022-04-25 21:33:56 +10:00
|
|
|
local contains = zone.polygon:contains(coords, zone.thickness)
|
2022-04-25 20:28:10 +10:00
|
|
|
|
2022-04-25 21:33:56 +10:00
|
|
|
if contains then
|
|
|
|
|
if zone.inside then
|
|
|
|
|
inside[#inside + 1] = zone
|
2022-04-25 20:28:10 +10:00
|
|
|
end
|
|
|
|
|
|
2022-04-25 21:33:56 +10:00
|
|
|
if zone.onEnter and not zone.insideZone then
|
|
|
|
|
zone.insideZone = true
|
|
|
|
|
zone:onEnter()
|
|
|
|
|
end
|
|
|
|
|
elseif zone.insideZone then
|
|
|
|
|
zone.insideZone = nil
|
|
|
|
|
if zone.onExit then
|
|
|
|
|
zone:onExit()
|
2022-04-25 20:28:10 +10:00
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
if not tick then
|
2022-04-25 21:33:56 +10:00
|
|
|
if #inside > 0 then
|
2022-04-25 20:28:10 +10:00
|
|
|
tick = SetInterval(function()
|
2022-04-25 21:33:56 +10:00
|
|
|
for i = 1, #inside do
|
|
|
|
|
inside[i]:inside()
|
|
|
|
|
if inside[i].debug then
|
|
|
|
|
inside[i]:debug()
|
2022-04-25 20:28:10 +10:00
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end)
|
|
|
|
|
end
|
2022-04-25 21:33:56 +10:00
|
|
|
elseif #inside == 0 then
|
2022-04-25 20:28:10 +10:00
|
|
|
tick = ClearInterval(tick)
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end)
|
|
|
|
|
|
|
|
|
|
return {
|
2022-04-25 21:33:56 +10:00
|
|
|
new = function(data)
|
|
|
|
|
data.id = #zones + 1
|
|
|
|
|
data.thickness = data.thickness or 2
|
|
|
|
|
data.polygon = glm.polygon.new(data.points)
|
|
|
|
|
data.remove = removeZone
|
|
|
|
|
data.centroid = data.polygon:centroid()
|
|
|
|
|
data.debug = data.debug and debug
|
2022-04-25 20:28:10 +10:00
|
|
|
|
2022-04-25 21:33:56 +10:00
|
|
|
zones[data.id] = data
|
|
|
|
|
return data
|
2022-04-25 20:28:10 +10:00
|
|
|
end
|
|
|
|
|
}
|