From 8155fae94c48fcea6fab5be807d9d10607647976 Mon Sep 17 00:00:00 2001 From: Dan Date: Fri, 12 Mar 2021 20:12:03 +0000 Subject: [PATCH] refactor: require radar power to use state rather than toggle --- cl_radar.lua | 33 +++++++++++++++++++-------------- cl_sync.lua | 11 ++--------- nui/radar.js | 6 +++--- 3 files changed, 24 insertions(+), 26 deletions(-) diff --git a/cl_radar.lua b/cl_radar.lua index 8f8a3ef..781cccb 100644 --- a/cl_radar.lua +++ b/cl_radar.lua @@ -455,31 +455,36 @@ function RADAR:SetPoweringUpState( state ) end -- Toggles the radar power -function RADAR:TogglePower() +function RADAR:SetPowerState( state, instantOverride ) + local currentState = self:IsPowerOn() + -- Only power up if the system is not already powering up - if ( not self:IsPoweringUp() ) then + if ( not self:IsPoweringUp() and currentState ~= state ) then -- Toggle the power variable - self.vars.power = not self.vars.power + self.vars.power = state -- Send the NUI message to toggle the power - SendNUIMessage( { _type = "radarPower", state = self:IsPowerOn() } ) + SendNUIMessage( { _type = "radarPower", state = state, override = instantOverride } ) -- Power is now turned on if ( self:IsPowerOn() ) then -- Also make sure the operator menu is inactive self:SetMenuState( false ) - -- Tell the system the radar is 'powering up' - self:SetPoweringUpState( true ) + -- Only do the power up simulation if allowed + if ( not instantOverride ) then + -- Tell the system the radar is 'powering up' + self:SetPoweringUpState( true ) - -- Set a 2 second countdown - Citizen.SetTimeout( 2000, function() - -- Tell the system the radar has 'powered up' - self:SetPoweringUpState( false ) + -- Set a 2 second countdown + Citizen.SetTimeout( 2000, function() + -- Tell the system the radar has 'powered up' + self:SetPoweringUpState( false ) - -- Let the UI side know the system has loaded - SendNUIMessage( { _type = "poweredUp" } ) - end ) + -- Let the UI side know the system has loaded + SendNUIMessage( { _type = "poweredUp" } ) + end ) + end else -- If the system is being turned off, then we reset the antennas self:ResetAntenna( "front" ) @@ -1649,7 +1654,7 @@ RegisterNUICallback( "togglePower", function( data, cb ) if ( PLY:CanControlRadar() ) then if ( not RADAR:IsPoweringUp() ) then -- Toggle the radar's power - RADAR:TogglePower() + RADAR:SetPowerState( not RADAR:IsPowerOn(), false ) SYNC:SendPowerState( RADAR:IsPowerOn() ) end diff --git a/cl_sync.lua b/cl_sync.lua index 8691211..da30f15 100644 --- a/cl_sync.lua +++ b/cl_sync.lua @@ -108,15 +108,8 @@ end -- Event for receiving the radar powet state RegisterNetEvent( "wk_wars2x_sync:receivePowerState" ) AddEventHandler( "wk_wars2x_sync:receivePowerState", function( state ) - -- Get the current local radar power state - local power = RADAR:IsPowerOn() - - -- If the local power state is not the same as the state sent, toggle the radar power - if ( power ~= state ) then - Citizen.SetTimeout( 100, function() - RADAR:TogglePower() - end ) - end + -- Set the radar's power + RADAR:SetPowerState( state, false ) end ) -- Event for receiving a power state for the given antenna diff --git a/nui/radar.js b/nui/radar.js index 9a08614..98eff93 100644 --- a/nui/radar.js +++ b/nui/radar.js @@ -510,9 +510,9 @@ function poweredUp() } // Runs the startup process or clears everything, the Lua side calls for the full powered up state -function radarPower( state ) +function radarPower( state, override ) { - state ? poweringUp() : clearEverything(); + state ? ( override ? poweredUp() : poweringUp() ) : clearEverything(); } @@ -1114,7 +1114,7 @@ window.addEventListener( "message", function( event ) { setEleVisible( elements.radar, item.state ); break; case "radarPower": - radarPower( item.state ); + radarPower( item.state, item.override ); break; case "poweredUp": poweredUp();