Skip to content

Commit

Permalink
fix: sounds still playing outside of vehicle
Browse files Browse the repository at this point in the history
  • Loading branch information
MrGriefs committed Aug 1, 2023
1 parent d00138b commit 90ebc7d
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 53 deletions.
47 changes: 22 additions & 25 deletions client.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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
Expand All @@ -107,7 +83,7 @@ function DeactivateSeatbelt()
end
end)

-- handling
-- handling and HUD
Citizen.CreateThread(function()
while not activated do
local ped = PlayerPedId()
Expand All @@ -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
Expand All @@ -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
Expand Down
76 changes: 48 additions & 28 deletions static/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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;
}
}
});
Expand Down

0 comments on commit 90ebc7d

Please sign in to comment.