Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: using death state as a statebag #50

Merged
merged 2 commits into from
Nov 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion client/dead.lua
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ exports('playDeadAnimation', playDeadAnimation)
---put player in death animation and make invincible
function OnDeath()
if DeathState == Config.DeathState.DEAD then return end
DeathState = Config.DeathState.DEAD
SetDeathState(Config.DeathState.DEAD)
TriggerServerEvent('qbx_medical:server:playerDied')
TriggerServerEvent("InteractSound_SV:PlayOnSource", "demo", 0.1)
local player = cache.ped
Expand Down
2 changes: 1 addition & 1 deletion client/laststand.lua
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ function StartLastStand()
ResurrectPlayer()
SetEntityHealth(ped, 150)
PlayUnescortedLastStandAnimation()
DeathState = Config.DeathState.LAST_STAND
SetDeathState(Config.DeathState.LAST_STAND)
TriggerServerEvent('qbx_medical:server:onPlayerLaststand')
CreateThread(function()
while DeathState == Config.DeathState.LAST_STAND do
Expand Down
18 changes: 13 additions & 5 deletions client/main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,22 @@ function SetBleedLevel(level)
playerState:set(BLEED_LEVEL_STATE_BAG, level, true)
end

DeathState = playerState[DEATH_STATE_STATE_BAG] or Config.DeathState.ALIVE

AddStateBagChangeHandler(DEATH_STATE_STATE_BAG, ('player:%s'):format(cache.serverId), function(_, _, value)
DeathState = value
end)

function SetDeathState(deathState)
playerState:set(DEATH_STATE_STATE_BAG, deathState, true)
end

BleedTickTimer, AdvanceBleedTimer = 0, 0
FadeOutTimer, BlackoutTimer = 0, 0

---@type number
Hp = nil

DeathState = Config.DeathState.ALIVE

DeathTime = 0
LaststandTime = 0
RespawnHoldTime = 5
Expand All @@ -63,15 +71,15 @@ exports('isDead', function()
end)

exports('setIsDeadDeprecated', function(isDead)
DeathState = isDead and Config.DeathState.DEAD or Config.DeathState.ALIVE
SetDeathState(isDead and Config.DeathState.DEAD or Config.DeathState.ALIVE)
end)

exports('getLaststand', function()
return DeathState == Config.DeathState.LAST_STAND
end)

exports('setLaststand', function(inLaststand)
DeathState = inLaststand and Config.DeathState.LAST_STAND or Config.DeathState.ALIVE
SetDeathState(inLaststand and Config.DeathState.LAST_STAND or Config.DeathState.ALIVE)
end)

exports('getDeathTime', function()
Expand Down Expand Up @@ -244,7 +252,7 @@ RegisterNetEvent('qbx_medical:client:playerRevived', function()
if DeathState ~= Config.DeathState.ALIVE then
local pos = GetEntityCoords(ped, true)
NetworkResurrectLocalPlayer(pos.x, pos.y, pos.z, GetEntityHeading(ped), true, false)
DeathState = Config.DeathState.ALIVE
SetDeathState(Config.DeathState.ALIVE)
SetEntityInvincible(ped, false)
EndLastStand()
end
Expand Down
37 changes: 15 additions & 22 deletions server/main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,29 @@ local triggerEventHooks = require 'modules.hooks.server'

local playerState

local function getDeathState(src)
local player = exports.qbx_core:GetPlayer(src)
return player.PlayerData.metadata.isdead and Config.DeathState.DEAD
or player.PlayerData.metadata.inlaststand and Config.DeathState.LAST_STAND
or Config.DeathState.ALIVE
end

AddEventHandler('QBCore:Server:OnPlayerLoaded', function()
playerState = Player(source).state
playerState:set(DEATH_STATE_STATE_BAG, getDeathState(source), true)
playerState:set(BLEED_LEVEL_STATE_BAG, 0, true)
for bodyPartKey in pairs(Config.BodyParts) do
playerState:set(BODY_PART_STATE_BAG_PREFIX .. bodyPartKey, nil, true)
end
end)

AddStateBagChangeHandler(DEATH_STATE_STATE_BAG, nil, function(bagName, _, value)
local playerId = GetPlayerFromStateBagName(bagName)
local player = exports.qbx_core:GetPlayer(playerId)
player.Functions.SetMetaData("isdead", value == Config.DeathState.DEAD)
player.Functions.SetMetaData("inlaststand", value == Config.DeathState.LAST_STAND)
end)

RegisterNetEvent('qbx_medical:server:playerDamagedByWeapon', function(hash)
if WeaponsThatDamagedPlayers[source][hash] then return end
WeaponsThatDamagedPlayers[source][hash] = true
Expand All @@ -32,8 +47,6 @@ local function revivePlayer(player)
if type(player) == "number" then
player = exports.qbx_core:GetPlayer(player)
end
player.Functions.SetMetaData("isdead", false)
player.Functions.SetMetaData("inlaststand", false)
WeaponsThatDamagedPlayers[player.PlayerData.source] = nil
TriggerClientEvent('qbx_medical:client:playerRevived', player.PlayerData.source)
end
Expand Down Expand Up @@ -82,26 +95,6 @@ lib.callback.register('hospital:GetPlayerStatus', function(_, playerId)
return damage
end)

RegisterNetEvent('qbx_medical:server:playerDied', function()
if GetInvokingResource() then return end
local src = source
local player = exports.qbx_core:GetPlayer(src)
if not player then return end
player.Functions.SetMetaData("isdead", true)
end)

RegisterNetEvent('qbx_medical:server:onPlayerLaststand', function()
if GetInvokingResource() then return end
local player = exports.qbx_core:GetPlayer(source)
player.Functions.SetMetaData("inlaststand", true)
end)

RegisterNetEvent('qbx_medical:server:onPlayerLaststandEnd', function()
if GetInvokingResource() then return end
local player = exports.qbx_core:GetPlayer(source)
player.Functions.SetMetaData("inlaststand", false)
end)

---@param amount number
lib.callback.register('qbx_medical:server:setArmor', function(source, amount)
local player = exports.qbx_core:GetPlayer(source)
Expand Down
3 changes: 2 additions & 1 deletion shared/main.lua
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
BODY_PART_STATE_BAG_PREFIX = 'qbx_medical:injuries:'
BLEED_LEVEL_STATE_BAG = 'qbx_medical:bleedLevel'
BLEED_LEVEL_STATE_BAG = 'qbx_medical:bleedLevel'
DEATH_STATE_STATE_BAG = 'qbx_medical:deathState'
Loading