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

fix(client/vehicle/main.lua): rewrite code and fix known bugs #93

Merged
merged 3 commits into from
Sep 16, 2024
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
109 changes: 38 additions & 71 deletions client/vehicle/main.lua
Original file line number Diff line number Diff line change
@@ -1,25 +1,21 @@
local cruiseControlStatus = false
local isPassenger = false
local isSeatbeltOn = false
local p = promise:new()

local function SetCruiseControlState(state)
cruiseControlStatus = state
end

exports("CruiseControlState", function(...)
SetCruiseControlState(...)
end)

local function SetSeatbeltState(state)
isSeatbeltOn = state
end

exports("SeatbeltState", function(...)
SetSeatbeltState(...)
end)
exports("CruiseControlState", SetCruiseControlState)
exports("SeatbeltState", SetSeatbeltState)

if not Config.Disable.Vehicle then
local inVehicle, vehicleType, playerPos = false, nil, nil
local vehicleType, playerPos
local currentMileage = 0

HUD.Data.Driver = false
Expand All @@ -30,18 +26,13 @@ if not Config.Disable.Vehicle then
}

local function driverCheck(currentVehicle)
if not DoesEntityExist(currentVehicle) then
return false
end
if GetPedInVehicleSeat(currentVehicle, -1) == PlayerPedId() then
return true
end
return false
return DoesEntityExist(currentVehicle) and (GetPedInVehicleSeat(currentVehicle, -1) == PlayerPedId())
end

local function driverCheckThread(currentVehicle)
CreateThread(function()
while inVehicle do
CreateThread(function()
while true do
local currentVehicle = Citizen.Await(p)
if currentVehicle then
HUD.Data.Driver = driverCheck(currentVehicle)
playerPos = GetEntityCoords(PlayerPedId()).xy

Expand All @@ -53,46 +44,29 @@ if not Config.Disable.Vehicle then
isPassenger = true
end
end
Wait(1000)
end
end)
end

local function slowInfoThread(currentVehicle)
CreateThread(function()
local oldPos = nil
Wait(1000)
end
end)

while inVehicle and DoesEntityExist(currentVehicle) do
CreateThread(function()
local oldPos
while true do
local currentVehicle = Citizen.Await(p)
if currentVehicle and DoesEntityExist(currentVehicle) then
local engineHealth = math.floor(GetVehicleEngineHealth(currentVehicle) / 10)
local _, lowBeam, highBeam = GetVehicleLightsState(currentVehicle)
local lightState = false
local indicator = GetVehicleIndicatorLights(currentVehicle)
local indicatorLeft, indicatorRight = false, false
local doorLockStatus = false
local tempDoorLockStatus = GetVehicleDoorLockStatus(currentVehicle)

-- Make sure engine health not going to minus
if engineHealth < 0 then
engineHealth = 0
end

-- Set light state
if lowBeam == 1 or highBeam == 1 then
lightState = true
end

-- Set indicator state
if indicator == 1 or indicator == 3 then
indicatorLeft = true
end
if indicator == 2 or indicator == 3 then
indicatorRight = true
end

-- Set lock state
if tempDoorLockStatus == 2 or tempDoorLockStatus == 3 then
doorLockStatus = true
end
local lightState = lowBeam == 1 or highBeam == 1
local indicatorLeft = indicator == 1 or indicator == 3
local indicatorRight = indicator == 2 or indicator == 3
local doorLockStatus = tempDoorLockStatus == 2 or tempDoorLockStatus == 3

if IsVehicleOnAllWheels(currentVehicle) then
if oldPos then
Expand All @@ -119,15 +93,15 @@ if not Config.Disable.Vehicle then
values.defaultIndicators.light = lightState
values.defaultIndicators.leftIndex = indicatorLeft
values.defaultIndicators.rightIndex = indicatorRight

Wait(200)
end
end)
end
Wait(200)
end
end)

local function fastInfoThread(currentVehicle)
CreateThread(function()
while inVehicle do
CreateThread(function()
while true do
local currentVehicle = Citizen.Await(p)
if currentVehicle and DoesEntityExist(currentVehicle) then
local currentSpeed = GetEntitySpeed(currentVehicle)
local engineRunning = GetIsVehicleEngineRunning(currentVehicle)
local rpm
Expand All @@ -138,30 +112,24 @@ if not Config.Disable.Vehicle then
rpm = math.ceil(ESX.PlayerData.coords.z)
end

values.speed = Config.Default.Kmh and math.floor(currentSpeed * 3.6) or math.floor(currentSpeed * 2.236936)
values.speed = math.floor(currentSpeed * (Config.Default.Kmh and 3.6 or 2.236936))
values.rpm = rpm
values.defaultIndicators.engine = engineRunning

if not isPassenger then
SendNUIMessage({ type = "VEH_HUD", value = values })
end
Wait(50)
end
end)
end

local function activateVehicleHud(currentVehicle)
values.show = true
slowInfoThread(currentVehicle)
fastInfoThread(currentVehicle)
end
Wait(50)
end
end)

AddEventHandler("esx:enteredVehicle", function(currentVehicle, currentPlate, currentSeat, displayName, netId)
local vehicleClass = GetVehicleClass(currentVehicle)
if vehicleClass == 13 then
return
end

inVehicle = true
HUD.Data.Driver = currentSeat == -1 or false
HUD.Data.Vehicle = currentVehicle
vehicleType = "LAND"
Expand All @@ -172,29 +140,28 @@ if not Config.Disable.Vehicle then
vehicleType = "MOTO"
end

-- We have to check if he changed seat meantime
driverCheckThread(currentVehicle)

if Config.Disable.MinimapOnFoot then
DisplayRadar(true)
end

activateVehicleHud(currentVehicle)

if HUD.Data.Driver then
TriggerServerEvent("esx_hud:EnteredVehicle", currentPlate, Config.Default.Kmh)
end
values.show = true
p:resolve(currentVehicle)
end)

AddEventHandler("esx:exitedVehicle", function(currentVehicle, currentPlate, currentSeat, displayName, netId)
inVehicle = false
p = promise:new()
HUD.Data.Driver = false
HUD.Data.Vehicle = nil
vehicleType = nil

values = {
show = false,
defaultIndicators = {},
}

SendNUIMessage({ type = "VEH_HUD", value = { show = false } })

if Config.Disable.MinimapOnFoot then
Expand Down
62 changes: 31 additions & 31 deletions fxmanifest.lua
Original file line number Diff line number Diff line change
@@ -1,38 +1,38 @@
fx_version 'cerulean'
game 'gta5'
lua54 'yes'
fx_version("cerulean")
game("gta5")
lua54("yes")

description 'The default HUD resource for ESX-Legacy.'
version '1.8.0'
description("The default HUD resource for ESX-Legacy.")
version("1.8.0")

shared_scripts {
'@es_extended/imports.lua',
'@es_extended/locale.lua',
'shared/config.lua',
'shared/main.lua',
'locales/*.lua'
}
shared_scripts({
"@es_extended/imports.lua",
"@es_extended/locale.lua",
"shared/config.lua",
"shared/main.lua",
"locales/*.lua",
})

server_scripts {
'@oxmysql/lib/MySQL.lua',
'server/*.lua'
}
server_scripts({
"@oxmysql/lib/MySQL.lua",
"server/*.lua",
})

client_scripts {
'client/*.lua',
'client/player/*.lua',
'client/vehicle/*.lua'
}
client_scripts({
"client/*.lua",
"client/player/*.lua",
"client/vehicle/*.lua",
})

ui_page 'web/dist/index.html'
ui_page("web/dist/index.html")

files {
'web/dist/**',
'web/dist/assets/**',
}
files({
"web/dist/**",
"web/dist/assets/**",
})

dependencies {
'es_extended',
'esx_status',
'oxmysql'
}
dependencies({
"es_extended",
"esx_status",
"oxmysql",
})
Loading