Skip to content

Commit

Permalink
refactor: spotlight
Browse files Browse the repository at this point in the history
  • Loading branch information
mafewtm committed Dec 13, 2024
1 parent 9df2e06 commit 6881f08
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 22 deletions.
49 changes: 31 additions & 18 deletions client/main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
4 changes: 3 additions & 1 deletion config/client.lua
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
return {
policeHelicopter = 'polmav',
policeHelicopters = {
`polmav`,
},
}
1 change: 0 additions & 1 deletion fxmanifest.lua
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ files {
'html/script.js',
'html/main.css',
'config/client.lua',
'locales/*.json',
}

lua54 'yes'
Expand Down
10 changes: 8 additions & 2 deletions server/main.lua
Original file line number Diff line number Diff line change
@@ -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)

0 comments on commit 6881f08

Please sign in to comment.