From 90ebc7df9e348f652c8c9183a9c520ce45810d8d Mon Sep 17 00:00:00 2001 From: Hagen Date: Tue, 1 Aug 2023 15:34:53 +0100 Subject: [PATCH] fix: sounds still playing outside of vehicle --- client.lua | 47 ++++++++++++++---------------- static/index.js | 76 +++++++++++++++++++++++++++++++------------------ 2 files changed, 70 insertions(+), 53 deletions(-) diff --git a/client.lua b/client.lua index 9910372..babeccb 100644 --- a/client.lua +++ b/client.lua @@ -26,7 +26,6 @@ function ActivateSeatbelt() if activated == true then return error('seatbelt attempted to activate when already active.') end - SetWarning(false) -- compat for other resources like carhud TriggerEvent('seatbelt:stateChange', true) @@ -68,29 +67,6 @@ function DeactivateSeatbelt() end TriggerEvent('seatbelt:stateChange', false) - -- HUD - Citizen.CreateThread(function() - while not activated do - local ped = PlayerPedId() - if IsPedInAnyVehicle(ped) and hasSeatbelt and not IsHudHidden() then - local vehicle = GetVehiclePedIsIn(ped) - local speed = GetEntitySpeed(vehicle) * 3.6 - - if speed > 20 and not (IsPlayerDead(PlayerId()) or IsPauseMenuActive()) then - SetWarning(true) - showHelp = true - else - SetWarning(false) - showHelp = false - end - else - SetWarning(false) - showHelp = false - end - Citizen.Wait(2e3) - end - end) - -- help text separate from hud Citizen.CreateThread(function() while not activated do @@ -107,7 +83,7 @@ function DeactivateSeatbelt() end end) - -- handling + -- handling and HUD Citizen.CreateThread(function() while not activated do local ped = PlayerPedId() @@ -121,17 +97,31 @@ function DeactivateSeatbelt() if speed > (50 / 3.6) and (lastSpeed - speed) > (speed * .2) then local coords = GetEntityCoords(ped) local fw = Fwv(ped) + ForceStopWarning() + showHelp = false SetEntityCoords(ped, coords.x + fw.x, coords.y + fw.y, coords.z - .47, true, true, true) SetEntityVelocity(ped, lastVelocity.x, lastVelocity.y, lastVelocity.z) SetPedToRagdoll(ped, 1e3, 1e3, 0, false, false, false) + elseif speed > (20 / 3.6) then + SetWarning(true) + showHelp = not (IsPauseMenuActive() or IsHudHidden() or IsPlayerDead(PlayerId())) + else + SetWarning(false) + showHelp = false end lastSpeed = speed lastVelocity = GetEntityVelocity(vehicle) end + else + ForceStopWarning() + showHelp = false end Citizen.Wait(50) end + + SetWarning(false) + showHelp = false end) -- notification @@ -156,6 +146,13 @@ function SetWarning(bool) end end +function ForceStopWarning() + if showingWarning then + SendNuiMessage('{"t":1,"d":3}') + showingWarning = false + end +end + RegisterNetEvent('seatbelt:ClientNotify', function(serverId) local ped = PlayerPedId() local player = GetPlayerFromServerId(serverId) -- onesync notice: returns -1 if not loaded diff --git a/static/index.js b/static/index.js index 1f71c53..add434c 100644 --- a/static/index.js +++ b/static/index.js @@ -12,6 +12,39 @@ const buckle = [sounds.unbluckle, sounds.buckle]; $(() => { const top = $('#seatbelt').css('top'); + + const enableIcon = () => { + const ui = $('#ui'); + ui.stop(true, true); + ui.css('display', 'flex'); + $('#seatbelt').animate( + { + top, + opacity: '1.0', + }, + 700, + ); + } + + const disableIcon = () => { + const ui = $('#ui'); + ui.stop(true, true); + $('#seatbelt').animate( + { + top: '100vw', + opacity: '0.0', + }, + 700, + () => ui.css('display', 'none'), + ); + } + + sounds.chime.on('play', enableIcon); + sounds.chime.on('stop', disableIcon); + sounds.chime.on('end', () => { + if (!sounds.chime.loop()) disableIcon(); + }); + window.addEventListener('message', event => { const payload = event.data; @@ -32,34 +65,21 @@ $(() => { } case 1: { - const ui = $('#ui'); - ui.stop(true, true); - if (payload.d === 1) { - ui.css('display', 'flex'); - $('#seatbelt').animate( - { - top, - opacity: '1.0', - }, - 700, - ); - - if (!sounds.chime.playing()) { - sounds.chime.play(); - } - sounds.chime.loop(true); - } else { - $('#seatbelt').animate( - { - top: '100vw', - opacity: '0.0', - }, - 700, - () => ui.css('display', 'none'), - ); - sounds.chime.loop(false); - } - break; + switch (payload.d) { + case 1: + sounds.chime.loop(true); + if (!sounds.chime.playing()) + sounds.chime.play(); + break; + + case 3: + sounds.chime.stop(); + sounds.unbluckle.stop(); + + case 0: + sounds.chime.loop(false); + break; + } break; } } });