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)
|
2022-04-25 21:51:03 +10:00
|
|
|
local polygon = self.polygon
|
|
|
|
|
local polyCount = #self.polygon
|
|
|
|
|
|
|
|
|
|
for i = 1, polyCount do
|
|
|
|
|
if i == polyCount then
|
|
|
|
|
DrawPoly(polygon[i], polygon[1], self.centroid, 255, 0, 0, 50)
|
|
|
|
|
DrawPoly(polygon[1], polygon[i], self.centroid, 255, 0, 0, 50)
|
|
|
|
|
else
|
|
|
|
|
DrawPoly(polygon[i], polygon[i + 1], self.centroid, 255, 0, 0, 50)
|
|
|
|
|
DrawPoly(polygon[i + 1], polygon[i], self.centroid, 255, 0, 0, 50)
|
|
|
|
|
end
|
|
|
|
|
end
|
2022-04-25 21:33:56 +10:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
local inside = {}
|
2022-04-27 17:07:24 +10:00
|
|
|
local insideCount
|
2022-04-25 20:28:10 +10:00
|
|
|
local tick
|
|
|
|
|
|
|
|
|
|
CreateThread(function()
|
|
|
|
|
while true do
|
|
|
|
|
Wait(300)
|
2022-04-27 17:07:24 +10:00
|
|
|
local coords = GetEntityCoords(cache.ped)
|
2022-04-25 21:33:56 +10:00
|
|
|
table.wipe(inside)
|
2022-04-27 17:07:24 +10:00
|
|
|
insideCount = 0
|
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
|
2022-04-27 17:07:24 +10:00
|
|
|
insideCount += 1
|
|
|
|
|
inside[insideCount] = 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
|
2022-04-27 17:07:24 +10:00
|
|
|
elseif zone.debug then
|
|
|
|
|
insideCount += 1
|
|
|
|
|
inside[insideCount] = zone
|
2022-04-25 20:28:10 +10:00
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
if not tick then
|
2022-04-27 17:07:24 +10:00
|
|
|
if insideCount > 0 then
|
2022-04-25 20:28:10 +10:00
|
|
|
tick = SetInterval(function()
|
2022-04-27 17:07:24 +10:00
|
|
|
for i = 1, insideCount do
|
|
|
|
|
local zone = inside[i]
|
|
|
|
|
|
|
|
|
|
if zone.insideZone then
|
|
|
|
|
zone:inside()
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
if zone.debug then
|
|
|
|
|
zone:debug()
|
2022-04-25 20:28:10 +10:00
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end)
|
|
|
|
|
end
|
2022-04-27 17:07:24 +10:00
|
|
|
elseif insideCount == 0 then
|
2022-04-25 20:28:10 +10:00
|
|
|
tick = ClearInterval(tick)
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end)
|
|
|
|
|
|
2022-04-27 17:09:32 +10:00
|
|
|
local glm_polygon_contains = glm.polygon.contains
|
|
|
|
|
|
|
|
|
|
local function contains(self, coords)
|
|
|
|
|
return glm_polygon_contains(self.polygon, coords)
|
|
|
|
|
end
|
|
|
|
|
|
2022-04-25 20:28:10 +10:00
|
|
|
return {
|
2022-04-27 10:33:18 +10:00
|
|
|
poly = function(data)
|
2022-04-25 21:33:56 +10:00
|
|
|
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-27 17:09:32 +10:00
|
|
|
data.contains = contains
|
2022-04-25 20:28:10 +10:00
|
|
|
|
2022-04-26 23:06:23 +10:00
|
|
|
zones[data.id] = data
|
|
|
|
|
return data
|
|
|
|
|
end,
|
2022-04-27 10:33:18 +10:00
|
|
|
|
2022-04-26 23:06:23 +10:00
|
|
|
box = function(data)
|
|
|
|
|
data.id = #zones + 1
|
|
|
|
|
data.thickness = data.size.z or 2
|
2022-04-27 17:11:36 +10:00
|
|
|
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.centroid)
|
2022-04-26 23:06:23 +10:00
|
|
|
data.remove = removeZone
|
|
|
|
|
data.debug = data.debug and debug
|
2022-04-27 17:09:32 +10:00
|
|
|
data.contains = contains
|
2022-04-26 23:06:23 +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
|
|
|
|
|
}
|