From 6881f08bf0974f8462fd83ebbbe714dde3aa1292 Mon Sep 17 00:00:00 2001 From: Matthew <22198949+MafewTM@users.noreply.github.com> Date: Thu, 12 Dec 2024 22:16:29 -0500 Subject: [PATCH] refactor: spotlight --- client/main.lua | 49 ++++++++++++++++++++++++++++++----------------- config/client.lua | 4 +++- fxmanifest.lua | 1 - server/main.lua | 10 ++++++++-- 4 files changed, 42 insertions(+), 22 deletions(-) diff --git a/client/main.lua b/client/main.lua index 71b8857..3829653 100644 --- a/client/main.lua +++ b/client/main.lua @@ -7,9 +7,7 @@ local UD_SPEED = 3.0 -- speed by which the camera pans up-down local toggleHeliCam = 51 -- control id of the button by which to toggle the heliCam mode. Default: INPUT_CONTEXT (E) local toggleVision = 25 -- control id to toggle vision mode. Default: INPUT_AIM (Right mouse btn) local toggleRappel = 154 -- control id to rappel out of the heli. Default: INPUT_DUCK (X) -local toggleSpotlight = 74 -- control id to toggle the front spotlight Default: INPUT_VEH_HEADLIGHT (H) local toggleLockOn = 22 -- control id to lock onto a vehicle with the camera. Default is INPUT_SPRINT (spacebar) -local spotlightState = false local heliCam = false local fov = (FOV_MAX + FOV_MIN) * 0.5 @@ -34,10 +32,6 @@ local vehicleLockState = VEHICLE_LOCK_STATE.dormant local vehicleDetected = nil local lockedOnVehicle = nil -local function isPlayerInPoliceHeli() - return GetEntityModel(cache.vehicle) == joaat(config.policeHelicopter) -end - local function isHeliHighEnough(heli) return GetEntityHeightAboveGround(heli) > 1.5 end @@ -132,10 +126,6 @@ local function renderVehicleInfo(vehicle) }) end -RegisterNetEvent('qbx_helicam:client:toggledSpotlight', function(serverId, state) - SetVehicleSearchlight(GetVehiclePedIsIn(GetPlayerPed(GetPlayerFromServerId(serverId)), false), state, false) -end) - local function heliCamThread() CreateThread(function() local sleep @@ -220,12 +210,6 @@ local function handleInVehicle() end end - if IsControlJustPressed(0, toggleSpotlight) and (cache.seat == -1 or cache.seat == 0) then - spotlightState = not spotlightState - TriggerServerEvent('qbx_helicam:server:toggleSpotlight', spotlightState) - PlaySoundFrontend(-1, 'SELECT', 'HUD_FRONTEND_DEFAULT_SOUNDSET', false) - end - if heliCam then SetTimecycleModifier('heliGunCam') SetTimecycleModifierStrength(0.3) @@ -288,11 +272,40 @@ local function handleInVehicle() end end -AddEventHandler('ox_lib:cache:vehicle', function() +local spotlight = lib.addKeybind({ + name = 'helicam', + description = 'Toggle Helicopter Spotlight', + defaultKey = '7', + dsiabled = true, + onPressed = function() + PlaySoundFrontend(-1, 'SELECT', 'HUD_FRONTEND_DEFAULT_SOUNDSET', false) + + local netId = NetworkGetNetworkIdFromEntity(cache.vehicle) + + TriggerServerEvent('qbx_helicam:server:toggleSpotlightState', netId) + end, +}) + +---@diagnostic disable-next-line: param-type-mismatch +AddStateBagChangeHandler('spotlight', nil, function(bagName, key, value) + local entity = GetEntityFromStateBagName(bagName) + + SetVehicleSearchlight(entity, value, false) +end) + +lib.onCache('vehicle', function(vehicle) + local model = GetEntityModel(vehicle) + + if not config.policeHelicopters[model] or (cache.seat ~= -1 and cache.seat ~= 0) then return end + + if DoesVehicleHaveSearchlight(vehicle) then + spotlight:disable(not vehicle) + end + CreateThread(function() - if not isPlayerInPoliceHeli() then return end while cache.vehicle do handleInVehicle() + Wait(0) end end) diff --git a/config/client.lua b/config/client.lua index 1040184..6fde126 100644 --- a/config/client.lua +++ b/config/client.lua @@ -1,3 +1,5 @@ return { - policeHelicopter = 'polmav', + policeHelicopters = { + `polmav`, + }, } \ No newline at end of file diff --git a/fxmanifest.lua b/fxmanifest.lua index bbf2bfe..a47bcc2 100644 --- a/fxmanifest.lua +++ b/fxmanifest.lua @@ -27,7 +27,6 @@ files { 'html/script.js', 'html/main.css', 'config/client.lua', - 'locales/*.json', } lua54 'yes' diff --git a/server/main.lua b/server/main.lua index 6ed174b..56b4966 100644 --- a/server/main.lua +++ b/server/main.lua @@ -1,5 +1,11 @@ lib.versionCheck('Qbox-project/qbx_helicam') -RegisterNetEvent('qbx_helicam:server:toggleSpotlight', function(toggle) - TriggerClientEvent('qbx_helicam:client:toggledSpotlight', -1, toggle) +RegisterNetEvent('qbx_helicam:server:toggleSpotlightState', function(netId) + local vehicle = NetworkGetEntityFromNetworkId(netId) + + if not DoesEntityExist(vehicle) or GetVehicleType(vehicle) ~= 'heli' then return end + + local spotlightStatus = Entity(vehicle).state.spotlight + + Entity(vehicle).state:set('spotlight', not spotlightStatus, true) end) \ No newline at end of file