refactor(client/zoneCreator): create temporary thread

Move the on-tick thread into the startCreator function, and kill it once
creatorActive is set to false.
This commit is contained in:
Linden
2022-09-14 15:15:36 +10:00
parent 032e0e2db1
commit c95fd8ecf4
2 changed files with 153 additions and 151 deletions

1
.gitignore vendored
View File

@@ -1 +1,2 @@
.idea .idea
created_zones.lua

View File

@@ -42,33 +42,6 @@ local function round(number)
return number >= 0 and math.floor(number + 0.5) or math.ceil(number - 0.5) return number >= 0 and math.floor(number + 0.5) or math.ceil(number - 0.5)
end end
local function startCreator(arg)
creatorActive = true
zoneType = arg
step = 5
local coords = GetEntityCoords(cache.ped)
xCoord = round(coords.x) + 0.0
yCoord = round(coords.y) + 0.0
zCoord = round(coords.z) + 0.0
heading = 0.0
height = 4.0
width = 4.0
length = 4.0
points = {}
updateText()
end
RegisterCommand('zone', function(source, args, rawCommand)
if args[1] == 'poly' or args[1] == 'box' or args[1] == 'sphere' then
if creatorActive then
lib.notify({title = 'Already creating a zone', type = 'error'})
else
startCreator(args[1])
end
end
end, true)
local function closeCreator(cancel) local function closeCreator(cancel)
if not cancel then if not cancel then
@@ -76,7 +49,7 @@ local function closeCreator(cancel)
points[#points + 1] = vec(xCoord, yCoord) points[#points + 1] = vec(xCoord, yCoord)
end end
local name = lib.inputDialog(('Name your %s Zone'):format(firstToUpper(zoneType)), {'Name'}) local name = lib.inputDialog(('Name your %s Zone'):format(firstToUpper(zoneType)), {'Name'})?[1]
TriggerServerEvent('ox_lib:saveZone', { TriggerServerEvent('ox_lib:saveZone', {
zoneType = zoneType, zoneType = zoneType,
@@ -115,10 +88,26 @@ local function drawLines()
end end
end end
CreateThread(function() local function startCreator(arg)
while true do creatorActive = true
zoneType = arg
step = 5
local coords = GetEntityCoords(cache.ped)
xCoord = round(coords.x) + 0.0
yCoord = round(coords.y) + 0.0
zCoord = round(coords.z) + 0.0
heading = 0.0
height = 4.0
width = 4.0
length = 4.0
points = {}
updateText()
while creatorActive do
Wait(0) Wait(0)
if creatorActive then
if IsDisabledControlJustPressed(0, 73) then -- x if IsDisabledControlJustPressed(0, 73) then -- x
if creatorActive then if creatorActive then
controlsActive = not controlsActive controlsActive = not controlsActive
@@ -134,6 +123,7 @@ CreateThread(function()
local sinT = math.sin(offset) local sinT = math.sin(offset)
local cosT = math.cos(offset) local cosT = math.cos(offset)
local center = vec(xCoord, yCoord) local center = vec(xCoord, yCoord)
---@type vector2[]
points = { points = {
center + vec(width * sinT, length * cosT), center + vec(width * sinT, length * cosT),
center + vec(-width * cosT, length * sinT), center + vec(-width * cosT, length * sinT),
@@ -143,8 +133,7 @@ CreateThread(function()
drawLines() drawLines()
elseif zoneType == 'sphere' then elseif zoneType == 'sphere' then
DrawMarker(28, xCoord, yCoord, zCoord, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, height, height, height, 255, 42, 24, 100, false, false, false, true, false, false, false) DrawMarker(28, xCoord, yCoord, zCoord, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, height, height, height, 255, 42, 24, 100, false, false, 0, true, false, false, false)
end
end end
if controlsActive then if controlsActive then
@@ -239,4 +228,16 @@ CreateThread(function()
end end
end end
end end
end
RegisterCommand('zone', function(source, args, rawCommand)
if args[1] == 'poly' or args[1] == 'box' or args[1] == 'sphere' then
if creatorActive then
lib.notify({title = 'Already creating a zone', type = 'error'})
else
CreateThread(function()
startCreator(args[1])
end) end)
end
end
end, true)