Skip to content

Commit

Permalink
refactor: qbx_teleports (#101)
Browse files Browse the repository at this point in the history
* refactor: qbx_teleports feat: correct placement on the ground by default on teleport

* fix: vehicle teleport

* refactor: remove .xyz from the coordinate parameter of the created zone

* refactor: unnecessary brackets

Co-authored-by: Manason <[email protected]>

* refactor: passage items to variables, ground detection to shared section

* chore: modulo explanation

---------

Co-authored-by: Manason <[email protected]>
  • Loading branch information
artur-michalak and Manason authored May 14, 2024
1 parent 89717f6 commit 05ef67c
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 35 deletions.
122 changes: 88 additions & 34 deletions qbx_teleports/client.lua
Original file line number Diff line number Diff line change
@@ -1,40 +1,94 @@
local config = lib.loadJson('qbx_teleports.config')

CreateThread(function()
local sleep
while true do
sleep = 1000
local pedCoords = GetEntityCoords(cache.ped)

for _, passage in ipairs(config.teleports) do
passage[1].coords = vec4(passage[1].coords)
passage[2].coords = vec4(passage[2].coords)
for k, v in ipairs(passage) do
local dist = #(pedCoords - v.coords.xyz)
if dist < 2 then
sleep = 0
DrawMarker(2, v.coords.x, v.coords.y, v.coords.z, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.3, 0.3, 0.15, 255, 255, 255, 255, false, false, 0, true, nil, nil, false)

if dist < 1 then
qbx.drawText3d({ text = v.drawText, coords = v.coords})
if IsControlJustReleased(0, 51) then
local i = k % 2 + 1
local dstCoords = passage[i].coords
if v.allowVehicle then
SetPedCoordsKeepVehicle(cache.ped, dstCoords.x, dstCoords.y, dstCoords.z)
else
SetEntityCoords(cache.ped, dstCoords.x, dstCoords.y, dstCoords.z, true, false, false, false)
end

if type(dstCoords) == 'vector4' then
SetEntityHeading(cache.ped, dstCoords.w)
end
end
end
if #config.teleports == 0 then return end

local zones = {}

AddEventHandler('onResourceStop', function (resourceName)
if GetCurrentResourceName() ~= resourceName then return end

for _, zone in ipairs(zones) do
zone:remove()
end
end)

local destination

CreateThread(function ()
for _, passage in ipairs(config.teleports) do
for i = 1, #passage do
local entrance = passage[i]
local exit = passage[(i % 2) + 1] -- (1 % 2) -> 1 (2 % 2) -> 0
local coords = vec3(entrance.coords)
zones[#zones+1] = lib.zones.sphere({
coords = coords,
radius = 2,
onEnter = function ()
lib.showTextUI(entrance.drawText)
destination = {
coords = vec(exit.coords),
ignoreGround = exit.ignoreGround,
allowVehicle = entrance.allowVehicle
}
end,
onExit = function ()
lib.hideTextUI()
destination = nil
end
end
})
end

Wait(sleep)
end
end)

local keybind

local function onPressed()
keybind:disable(true)
if destination then
local coordZ = destination.coords.z

if not destination.ignoreGround then
local isSafe, z = GetGroundZFor_3dCoord(
destination.coords.x,
destination.coords.y,
destination.coords.z,
false
)

if isSafe then coordZ = z end
end

if destination.allowVehicle and cache.vehicle then
SetPedCoordsKeepVehicle(
cache.ped,
destination.coords.x,
destination.coords.y,
coordZ
)

SetVehicleOnGroundProperly(cache.vehicle)
else
SetEntityCoords(
cache.ped,
destination.coords.x,
destination.coords.y,
coordZ,
true, false, false, false
)
end

if type(destination.coords) == 'vector4' then
SetEntityHeading(cache.ped, destination.coords.w)
end
end
keybind:disable(false)
end

keybind = lib.addKeybind({
name = 'passage',
description = 'entry through passage',
defaultKey = 'E',
secondaryMapper = 'PAD_DIGITALBUTTONANY',
secondaryKey = 'LRIGHT_INDEX',
onPressed = onPressed
})
3 changes: 2 additions & 1 deletion qbx_teleports/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
"_name": "Elevator @ labs",
"coords": [3540.74, 3675.59, 20.99, 167.5],
"allowVehicle": false,
"drawText": "[E] Take Elevator Up"
"drawText": "[E] Take Elevator Up",
"ignoreGround": true
},
{
"coords": [3540.74, 3675.59, 28.11, 172.5],
Expand Down

0 comments on commit 05ef67c

Please sign in to comment.