Skip to content

Commit

Permalink
refactor(client): improve spawn transitions
Browse files Browse the repository at this point in the history
  • Loading branch information
thelindat committed Mar 2, 2022
1 parent 4145aa9 commit 4ba5203
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 97 deletions.
14 changes: 14 additions & 0 deletions client/init.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
DoScreenFadeOut(0)
exports.spawnmanager:spawnPlayer({
x = shared.spawn.x,
y = shared.spawn.y,
z = shared.spawn.z,
heading = shared.spawn.w,
model = `mp_m_freemode_01`,
skipFade = true,
})

SetTimeout(500, function()
TriggerServerEvent('ox:playerJoined')
exports.spawnmanager:setAutoSpawn(false)
end)
74 changes: 1 addition & 73 deletions client/main.lua
Original file line number Diff line number Diff line change
@@ -1,56 +1,9 @@
local cache = {}
cache = {}

exports('IsPlayerLoaded', function()
return cache.loaded
end)

RegisterNetEvent('ox:selectCharacter', function(characters)
if cache then TriggerEvent('ox:playerLogout') end
DoScreenFadeOut(0)

exports.spawnmanager:spawnPlayer({
x = shared.spawn.x,
y = shared.spawn.y,
z = shared.spawn.z,
heading = shared.spawn.w,
skipFade = true
})

client.setupCharacters(cache, characters)
local concealed = {}

while cache.cam do
DisableAllControlActions(0)
ThefeedHideThisFrame()
HideHudAndRadarThisFrame()

local players = GetActivePlayers()

for i = 1, #players do
local player = players[i]
if player ~= cache.id and not concealed[player] then
concealed[#concealed + 1] = player
NetworkConcealPlayer(player, true, true)
end
end

if cache.hidePlayer then
SetLocalPlayerInvisibleLocally(true)
end

Wait(0)
end

for i = 1, #concealed do
NetworkConcealPlayer(concealed[i], false, false)
end

DoScreenFadeIn(200)
SetMaxWantedLevel(0)
NetworkSetFriendlyFireOption(true)
SetPlayerInvincible(cache.id, false)
end)

RegisterNUICallback('ox:selectCharacter', function(data, cb)
cb(1)

Expand Down Expand Up @@ -85,31 +38,6 @@ RegisterNUICallback('ox:deleteCharacter', function(data, cb)
TriggerServerEvent('ox:deleteCharacter', data)
end)

RegisterNetEvent('ox:playerLoaded', function(data, spawn)
client.playerLoaded(cache, spawn)

cache = data
cache.id = PlayerId()
cache.loaded = true

if cache.dead then
client.onPlayerDeath(cache, true)
end

while cache.loaded do
Wait(200)
cache.ped = PlayerPedId()

if not cache.dead and IsPedDeadOrDying(cache.ped) then
client.onPlayerDeath(cache)
end
end
end)

AddEventHandler('ox:playerLogout', function()
table.wipe(cache)
end)

RegisterCommand('saveveh', function()
local data = lib.getVehicleProperties(GetVehiclePedIsUsing(PlayerPedId()))
TriggerServerEvent('saveProperties', data)
Expand Down
99 changes: 75 additions & 24 deletions client/spawn.lua
Original file line number Diff line number Diff line change
@@ -1,31 +1,67 @@
DoScreenFadeOut(0)
RegisterNetEvent('ox:selectCharacter', function(characters)
DoScreenFadeOut(0)

SetTimeout(1000, function()
TriggerServerEvent('ox:playerJoined')
end)
if cache.id then
table.wipe(cache)
TriggerEvent('ox:playerLogout')
end

function client.setupCharacters(cache, characters)
Wait(100)
CreateThread(function()
local concealed = {}

while not cache.loaded do
DisableAllControlActions(0)
ThefeedHideThisFrame()
HideHudAndRadarThisFrame()

local players = GetActivePlayers()

for i = 1, #players do
local player = players[i]
if player ~= cache.id and not concealed[player] then
concealed[#concealed + 1] = player
NetworkConcealPlayer(player, true, true)
end
end

if cache.hidePlayer then
SetLocalPlayerInvisibleLocally(true)
end

Wait(0)
end

for i = 1, #concealed do
NetworkConcealPlayer(concealed[i], false, false)
end

DoScreenFadeIn(200)
SetMaxWantedLevel(0)
NetworkSetFriendlyFireOption(true)
SetPlayerInvincible(cache.id, false)
end)

cache.ped = PlayerPedId()
cache.id = PlayerId()

SetPlayerInvincible(cache.id, true)
local offset = GetOffsetFromEntityInWorldCoords(cache.ped, 0.0, 4.7, 0.2)
cache.cam = CreateCameraWithParams('DEFAULT_SCRIPTED_CAMERA', offset.x, offset.y, offset.z, 0.0, 0.0, 0.0, 30.0, false, 0)
StartPlayerTeleport(cache.id, shared.spawn.x, shared.spawn.y, shared.spawn.z, shared.spawn.w, false, true)

SetCamActive(cache.cam, true)
RenderScriptCams(cache.cam, false, 0, true, true)
PointCamAtCoord(cache.cam, shared.spawn.x, shared.spawn.y, shared.spawn.z + 0.1)
while IsPlayerTeleportActive() do Wait(0) end

if characters[1]?.appearance then
exports['fivem-appearance']:setPlayerAppearance(characters[1].appearance)
else
cache.hidePlayer = true
end

Wait(200)
DoScreenFadeIn(500)
cache.ped = PlayerPedId()

local offset = GetOffsetFromEntityInWorldCoords(cache.ped, 0.0, 4.7, 0.2)
cache.cam = CreateCameraWithParams('DEFAULT_SCRIPTED_CAMERA', offset.x, offset.y, offset.z, 0.0, 0.0, 0.0, 30.0, false, 0)

SetCamActive(cache.cam, true)
RenderScriptCams(cache.cam, false, 0, true, true)
PointCamAtCoord(cache.cam, shared.spawn.x, shared.spawn.y, shared.spawn.z + 0.1)

cache.appearance = {}

Expand All @@ -41,11 +77,13 @@ function client.setupCharacters(cache, characters)
data = characters
})

DoScreenFadeIn(500)
Wait(500)
SetNuiFocus(true, true)
SetNuiFocusKeepInput(false)
end
end)

function client.playerLoaded(cache, spawn)
RegisterNetEvent('ox:playerLoaded', function(data, spawn)
Wait(500)
RenderScriptCams(false, false, 0, true, true)
DestroyCam(cache.cam, false)
Expand All @@ -64,15 +102,28 @@ function client.playerLoaded(cache, spawn)
Citizen.Await(p)
end

DoScreenFadeOut(200)

if not spawn then
spawn = shared.spawn
if spawn then
StartPlayerTeleport(cache.id, spawn.x, spawn.y, spawn.z, spawn.w, false, true)
while IsPlayerTeleportActive() do Wait(0) end
else
StartPlayerTeleport(cache.id, shared.spawn.x, shared.spawn.y, shared.spawn.z, shared.spawn.w, false, true)
end

cache = data
cache.id = PlayerId()
cache.ped = PlayerPedId()
if spawn then
SetEntityCoordsNoOffset(cache.ped, spawn.x, spawn.y, spawn.z, true, true, true)
SetEntityHeading(cache.ped, spawn.w or 357.165)
cache.loaded = true

if cache.dead then
client.onPlayerDeath(cache, true)
end
end

while cache.loaded do
Wait(200)
cache.ped = PlayerPedId()

if not cache.dead and IsPedDeadOrDying(cache.ped) then
client.onPlayerDeath(cache)
end
end
end)

0 comments on commit 4ba5203

Please sign in to comment.