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
-- 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

View File

@@ -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

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
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();