refactor: require radar power to use state rather than toggle

This commit is contained in:
Dan
2021-03-12 20:12:03 +00:00
parent 2f9c483d6d
commit 8155fae94c
3 changed files with 24 additions and 26 deletions

View File

@@ -455,31 +455,36 @@ function RADAR:SetPoweringUpState( state )
end end
-- Toggles the radar power -- 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 -- 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 -- Toggle the power variable
self.vars.power = not self.vars.power self.vars.power = state
-- Send the NUI message to toggle the power -- 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 -- Power is now turned on
if ( self:IsPowerOn() ) then if ( self:IsPowerOn() ) then
-- Also make sure the operator menu is inactive -- Also make sure the operator menu is inactive
self:SetMenuState( false ) self:SetMenuState( false )
-- Tell the system the radar is 'powering up' -- Only do the power up simulation if allowed
self:SetPoweringUpState( true ) if ( not instantOverride ) then
-- Tell the system the radar is 'powering up'
self:SetPoweringUpState( true )
-- Set a 2 second countdown -- Set a 2 second countdown
Citizen.SetTimeout( 2000, function() Citizen.SetTimeout( 2000, function()
-- Tell the system the radar has 'powered up' -- Tell the system the radar has 'powered up'
self:SetPoweringUpState( false ) self:SetPoweringUpState( false )
-- Let the UI side know the system has loaded -- Let the UI side know the system has loaded
SendNUIMessage( { _type = "poweredUp" } ) SendNUIMessage( { _type = "poweredUp" } )
end ) end )
end
else else
-- If the system is being turned off, then we reset the antennas -- If the system is being turned off, then we reset the antennas
self:ResetAntenna( "front" ) self:ResetAntenna( "front" )
@@ -1649,7 +1654,7 @@ RegisterNUICallback( "togglePower", function( data, cb )
if ( PLY:CanControlRadar() ) then if ( PLY:CanControlRadar() ) then
if ( not RADAR:IsPoweringUp() ) then if ( not RADAR:IsPoweringUp() ) then
-- Toggle the radar's power -- Toggle the radar's power
RADAR:TogglePower() RADAR:SetPowerState( not RADAR:IsPowerOn(), false )
SYNC:SendPowerState( RADAR:IsPowerOn() ) SYNC:SendPowerState( RADAR:IsPowerOn() )
end end

View File

@@ -108,15 +108,8 @@ end
-- Event for receiving the radar powet state -- Event for receiving the radar powet state
RegisterNetEvent( "wk_wars2x_sync:receivePowerState" ) RegisterNetEvent( "wk_wars2x_sync:receivePowerState" )
AddEventHandler( "wk_wars2x_sync:receivePowerState", function( state ) AddEventHandler( "wk_wars2x_sync:receivePowerState", function( state )
-- Get the current local radar power state -- Set the radar's power
local power = RADAR:IsPowerOn() RADAR:SetPowerState( state, false )
-- 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
end ) end )
-- Event for receiving a power state for the given antenna -- Event for receiving a power state for the given antenna

View File

@@ -510,9 +510,9 @@ function poweredUp()
} }
// Runs the startup process or clears everything, the Lua side calls for the full powered up state // 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 ); setEleVisible( elements.radar, item.state );
break; break;
case "radarPower": case "radarPower":
radarPower( item.state ); radarPower( item.state, item.override );
break; break;
case "poweredUp": case "poweredUp":
poweredUp(); poweredUp();