Skip to content

Commit

Permalink
refactor: split diving suit code into separate files
Browse files Browse the repository at this point in the history
  • Loading branch information
Manason authored Nov 8, 2023
1 parent 839c8fd commit 454b1b9
Show file tree
Hide file tree
Showing 5 changed files with 161 additions and 160 deletions.
145 changes: 1 addition & 144 deletions client/main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,13 @@ local coralZones = {}
---@type table<integer, number> coralIndex to zoneId
local coralTargetZones = {}

local isWearingSuit = false
local oxygenLevel = 0
local currentDivingLocation = {
area = 0,
blip = {
radius = nil,
label = nil
}
}
local currentGear = {
mask = 0,
tank = 0,
oxygen = 0,
enabled = false
}

-- Functions
local function callCops()
Expand All @@ -33,20 +25,6 @@ local function callCops()
TriggerServerEvent('qb-diving:server:CallCops', coords)
end

local function deleteGear()
if currentGear.mask ~= 0 then
DetachEntity(currentGear.mask, false, true)
DeleteEntity(currentGear.mask)
currentGear.mask = 0
end

if currentGear.tank ~= 0 then
DetachEntity(currentGear.tank, false, true)
DeleteEntity(currentGear.tank)
currentGear.tank = 0
end
end

local function takeCoral(coralIndex)
if math.random() > Config.CopsChance then callCops() end

Expand Down Expand Up @@ -235,19 +213,6 @@ local function createSeller()
end
end

local function DrawText(text)
SetTextFont(4)
SetTextProportional(true)
SetTextScale(0.0, 0.45)
SetTextDropshadow(1, 0, 0, 0, 255)
SetTextEdge(1, 0, 0, 0, 255)
SetTextDropShadow()
SetTextOutline()
BeginTextCommandDisplayText("STRING")
AddTextComponentSubstringPlayerName(text)
EndTextCommandDisplayText(0.45, 0.90)
end

local function init()
local areaIndex, pickedUpCoralIndexes = lib.callback.await('qbx_diving:server:getCurrentDivingArea', false)
setDivingLocation(areaIndex, pickedUpCoralIndexes)
Expand Down Expand Up @@ -302,115 +267,7 @@ RegisterNetEvent('qb-diving:client:CallCops', function(coords, msg)
RemoveBlip(blip)
end)

RegisterNetEvent("qb-diving:client:setoxygenlevel", function()
if oxygenLevel == 0 then
oxygenLevel = Config.OxygenLevel
exports.qbx_core:Notify(Lang:t("success.tube_filled"), 'success')
TriggerServerEvent('qb-diving:server:removeItemAfterFill')
return
end

exports.qbx_core:Notify(Lang:t("error.oxygenlevel", {oxygenlevel = oxygenLevel}), 'error')
end)

RegisterNetEvent('qb-diving:client:UseGear', function()
if isWearingSuit then
if lib.progressBar({
duration = 5000,
label = Lang:t("info.pullout_suit"),
useWhileDead = false,
canCancel = true,
anim = {
dict = "clothingshirt",
clip = "try_shirt_positive_d",
blendIn = 8.0
}
}) then
SetEnableScuba(cache.ped, false)
SetPedMaxTimeUnderwater(cache.ped, 50.00)
currentGear.enabled = false
deleteGear()
exports.qbx_core:Notify(Lang:t("success.took_out"))
TriggerServerEvent("InteractSound_SV:PlayOnSource", nil, 0.25)
isWearingSuit = false
oxygenLevel = oxygenLevel
end

ClearPedTasks(cache.ped)
else
if oxygenLevel <= 0 then
exports.qbx_core:Notify(Lang:t("error.need_otube"), 'error')
return
end

isWearingSuit = true

if IsPedSwimming(cache.ped) or cache.vehicle then
exports.qbx_core:Notify(Lang:t("error.not_standing_up"), 'error')
return
end

if lib.progressBar({
duration = 5000,
label = Lang:t("info.put_suit"),
useWhileDead = false,
canCancel = true,
anim = {
dict = "clothingshirt",
clip = "try_shirt_positive_d",
blendIn = 8.0
}
}) then
deleteGear()
local maskModel = `p_d_scuba_mask_s`
local tankModel = `p_s_scuba_tank_s`
lib.requestModel(maskModel)
lib.requestModel(tankModel)
currentGear.tank = CreateObject(tankModel, 1.0, 1.0, 1.0, true, true, false)
local bone1 = GetPedBoneIndex(cache.ped, 24818)
AttachEntityToEntity(currentGear.tank, cache.ped, bone1, -0.25, -0.25, 0.0, 180.0, 90.0, 0.0, true, true, false, false, 2, true)
currentGear.mask = CreateObject(maskModel, 1.0, 1.0, 1.0, true, true, false)
local bone2 = GetPedBoneIndex(cache.ped, 12844)
AttachEntityToEntity(currentGear.mask, cache.ped, bone2, 0.0, 0.0, 0.0, 180.0, 90.0, 0.0, true, true, false, false, 2, true)
SetEnableScuba(cache.ped, true)
SetPedMaxTimeUnderwater(cache.ped, 2000.00)
currentGear.enabled = true
TriggerServerEvent("InteractSound_SV:PlayOnSource", "breathdivingsuit", 0.25)
CreateThread(function()
while currentGear.enabled do
if IsPedSwimmingUnderWater(cache.ped) and oxygenLevel > 0 then
oxygenLevel -= 1
if oxygenLevel % 10 == 0 and oxygenLevel ~= Config.OxygenLevel then
TriggerServerEvent("InteractSound_SV:PlayOnSource", "breathdivingsuit", 0.25)
elseif oxygenLevel == 0 then
SetEnableScuba(cache.ped, false)
SetPedMaxTimeUnderwater(cache.ped, 1.00)
currentGear.enabled = false
isWearingSuit = false
TriggerServerEvent("InteractSound_SV:PlayOnSource", nil, 0.25)
end
end
Wait(1000)
end
end)
end

ClearPedTasks(cache.ped)
end
end)

CreateThread(function()
if not isLoggedIn then return end
init()
end)

CreateThread(function()
while true do
if currentGear.enabled and isWearingSuit then
if IsPedSwimmingUnderWater(cache.ped) then
DrawText(oxygenLevel..'')
end
end
Wait(0)
end
end)
end)
144 changes: 144 additions & 0 deletions client/suit.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
local currentGear = {
mask = 0,
tank = 0,
oxygen = 0,
enabled = false
}

local isWearingSuit = false
local oxygenLevel = 0

RegisterNetEvent("qb-diving:client:setoxygenlevel", function()
if oxygenLevel == 0 then
oxygenLevel = Config.OxygenLevel
exports.qbx_core:Notify(Lang:t("success.tube_filled"), 'success')
TriggerServerEvent('qb-diving:server:removeItemAfterFill')
return
end

exports.qbx_core:Notify(Lang:t("error.oxygenlevel", {oxygenlevel = oxygenLevel}), 'error')
end)

local function deleteGear()
if currentGear.mask ~= 0 then
DetachEntity(currentGear.mask, false, true)
DeleteEntity(currentGear.mask)
currentGear.mask = 0
end

if currentGear.tank ~= 0 then
DetachEntity(currentGear.tank, false, true)
DeleteEntity(currentGear.tank)
currentGear.tank = 0
end
end

RegisterNetEvent('qb-diving:client:UseGear', function()
if isWearingSuit then
if lib.progressBar({
duration = 5000,
label = Lang:t("info.pullout_suit"),
useWhileDead = false,
canCancel = true,
anim = {
dict = "clothingshirt",
clip = "try_shirt_positive_d",
blendIn = 8.0
}
}) then
SetEnableScuba(cache.ped, false)
SetPedMaxTimeUnderwater(cache.ped, 50.00)
currentGear.enabled = false
deleteGear()
exports.qbx_core:Notify(Lang:t("success.took_out"))
TriggerServerEvent("InteractSound_SV:PlayOnSource", nil, 0.25)
isWearingSuit = false
oxygenLevel = oxygenLevel
end

ClearPedTasks(cache.ped)
else
if oxygenLevel <= 0 then
exports.qbx_core:Notify(Lang:t("error.need_otube"), 'error')
return
end

isWearingSuit = true

if IsPedSwimming(cache.ped) or cache.vehicle then
exports.qbx_core:Notify(Lang:t("error.not_standing_up"), 'error')
return
end

if lib.progressBar({
duration = 5000,
label = Lang:t("info.put_suit"),
useWhileDead = false,
canCancel = true,
anim = {
dict = "clothingshirt",
clip = "try_shirt_positive_d",
blendIn = 8.0
}
}) then
deleteGear()
local maskModel = `p_d_scuba_mask_s`
local tankModel = `p_s_scuba_tank_s`
lib.requestModel(maskModel)
lib.requestModel(tankModel)
currentGear.tank = CreateObject(tankModel, 1.0, 1.0, 1.0, true, true, false)
local bone1 = GetPedBoneIndex(cache.ped, 24818)
AttachEntityToEntity(currentGear.tank, cache.ped, bone1, -0.25, -0.25, 0.0, 180.0, 90.0, 0.0, true, true, false, false, 2, true)
currentGear.mask = CreateObject(maskModel, 1.0, 1.0, 1.0, true, true, false)
local bone2 = GetPedBoneIndex(cache.ped, 12844)
AttachEntityToEntity(currentGear.mask, cache.ped, bone2, 0.0, 0.0, 0.0, 180.0, 90.0, 0.0, true, true, false, false, 2, true)
SetEnableScuba(cache.ped, true)
SetPedMaxTimeUnderwater(cache.ped, 2000.00)
currentGear.enabled = true
TriggerServerEvent("InteractSound_SV:PlayOnSource", "breathdivingsuit", 0.25)
CreateThread(function()
while currentGear.enabled do
if IsPedSwimmingUnderWater(cache.ped) and oxygenLevel > 0 then
oxygenLevel -= 1
if oxygenLevel % 10 == 0 and oxygenLevel ~= Config.OxygenLevel then
TriggerServerEvent("InteractSound_SV:PlayOnSource", "breathdivingsuit", 0.25)
elseif oxygenLevel == 0 then
SetEnableScuba(cache.ped, false)
SetPedMaxTimeUnderwater(cache.ped, 1.00)
currentGear.enabled = false
isWearingSuit = false
TriggerServerEvent("InteractSound_SV:PlayOnSource", nil, 0.25)
end
end
Wait(1000)
end
end)
end

ClearPedTasks(cache.ped)
end
end)

local function DrawText(text)
SetTextFont(4)
SetTextProportional(true)
SetTextScale(0.0, 0.45)
SetTextDropshadow(1, 0, 0, 0, 255)
SetTextEdge(1, 0, 0, 0, 255)
SetTextDropShadow()
SetTextOutline()
BeginTextCommandDisplayText("STRING")
AddTextComponentSubstringPlayerName(text)
EndTextCommandDisplayText(0.45, 0.90)
end

CreateThread(function()
while true do
if currentGear.enabled and isWearingSuit then
if IsPedSwimmingUnderWater(cache.ped) then
DrawText(oxygenLevel..'')
end
end
Wait(0)
end
end)
6 changes: 4 additions & 2 deletions fxmanifest.lua
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,12 @@ shared_script {
'config.lua'
}

server_script 'server/main.lua'
server_scripts {
'server/*',
}

client_scripts {
'client/main.lua'
'client/*'
}

lua54 'yes'
Expand Down
15 changes: 1 addition & 14 deletions server/main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -82,29 +82,16 @@ RegisterNetEvent('qb-diving:server:TakeCoral', function(area, coralIndex)

exports.ox_inventory:AddItem(src, coralType.item, amount)
pickedUpCoralIndexes[coralIndex] = true
TriggerClientEvent('qbx_diving:client:coralTaken', -1, coralIndex)
if #pickedUpCoralIndexes == Config.CoralLocations[area].maxHarvestAmount then
pickedUpCoralIndexes = {}
currentDivingArea = getNewLocation()
TriggerClientEvent('qbx_diving:client:newLocationSet', -1, currentDivingArea)
end

TriggerClientEvent('qbx_diving:client:coralTaken', -1, coralIndex)
end)

RegisterNetEvent('qb-diving:server:removeItemAfterFill', function()
exports.ox_inventory:RemoveItem(source, 'diving_fill', 1)
end)

---@return integer areaIndex
---@return table<integer, true> pickedUpCoralIndexes
lib.callback.register('qbx_diving:server:getCurrentDivingArea', function()
return currentDivingArea, pickedUpCoralIndexes
end)

exports.qbx_core:CreateUseableItem("diving_gear", function(source)
TriggerClientEvent("qb-diving:client:UseGear", source)
end)

exports.qbx_core:CreateUseableItem("diving_fill", function(source)
TriggerClientEvent("qb-diving:client:setoxygenlevel", source)
end)
11 changes: 11 additions & 0 deletions server/suit.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
exports.qbx_core:CreateUseableItem("diving_gear", function(source)
TriggerClientEvent("qb-diving:client:UseGear", source)
end)

exports.qbx_core:CreateUseableItem("diving_fill", function(source)
TriggerClientEvent("qb-diving:client:setoxygenlevel", source)
end)

RegisterNetEvent('qb-diving:server:removeItemAfterFill', function()
exports.ox_inventory:RemoveItem(source, 'diving_fill', 1)
end)

0 comments on commit 454b1b9

Please sign in to comment.