JS switch/case, xmit light, speed display setting, reduced player vehicle check rate

This commit is contained in:
Dan
2019-11-27 20:41:46 +00:00
parent 3b0061bdf0
commit 7872f6453f
6 changed files with 150 additions and 68 deletions

View File

@@ -24,10 +24,19 @@ Citizen.SetTimeout( 1000, function()
print( "WK_WARS2X: Sending resource name (" .. name .. ") to JavaScript side." ) print( "WK_WARS2X: Sending resource name (" .. name .. ") to JavaScript side." )
-- Send a message through the NUI system to the JavaScript file to give the name of the resource -- Send a message through the NUI system to the JavaScript file to give the name of the resource
SendNUIMessage( { pathName = name } ) SendNUIMessage( { _type = "updatePathName", pathName = name } )
end ) end )
--[[------------------------------------------------------------------------
Player info variables
------------------------------------------------------------------------]]--
PLY = {}
PLY.ped = PlayerPedId()
PLY.veh = nil
PLY.inDriverSeat = false
--[[------------------------------------------------------------------------ --[[------------------------------------------------------------------------
Radar variables Radar variables
------------------------------------------------------------------------]]-- ------------------------------------------------------------------------]]--
@@ -202,7 +211,7 @@ end
function RADAR:SetPatrolSpeed( speed ) function RADAR:SetPatrolSpeed( speed )
if ( type( speed ) == "number" ) then if ( type( speed ) == "number" ) then
self.vars.patrolSpeed = speed self.vars.patrolSpeed = self:GetVehSpeedFormatted( speed )
end end
end end
@@ -530,9 +539,9 @@ end
------------------------------------------------------------------------]]-- ------------------------------------------------------------------------]]--
function RADAR:GetVehSpeedFormatted( speed ) function RADAR:GetVehSpeedFormatted( speed )
if ( self.vars.speedType == "mph" ) then if ( self.vars.speedType == "mph" ) then
return math.ceil( speed * 2.236936 ) return UTIL:Round( math.ceil( speed * 2.236936 ), 0 )
else else
return math.ceil( speed * 3.6 ) return UTIL:Round( math.ceil( speed * 3.6 ), 0 )
end end
end end
@@ -602,7 +611,7 @@ function RADAR:GetVehiclesForAntenna()
end end
end end
return { results["front"][1], results["front"][2], results["rear"][1], results["rear"][2] } return { ["front"] = { results["front"][1], results["front"][2] }, ["rear"] = { results["rear"][1], results["rear"][2] } }
end end
-- Num4 = 108 - INPUT_VEH_FLY_ROLL_LEFT_ONLY -- Num4 = 108 - INPUT_VEH_FLY_ROLL_LEFT_ONLY
@@ -619,7 +628,7 @@ function RADAR:RunControlManager()
end end
if ( IsDisabledControlJustPressed( 1, 166 ) ) then if ( IsDisabledControlJustPressed( 1, 166 ) ) then
SendNUIMessage( { activateRemote = true } ) SendNUIMessage( { _type = "openRemote" } )
SetNuiFocus( true, true ) SetNuiFocus( true, true )
end end
@@ -655,25 +664,27 @@ RegisterNUICallback( "closeRemote", function( data )
end ) end )
RegisterNUICallback( "setAntennaMode", function( data ) RegisterNUICallback( "setAntennaMode", function( data )
RADAR:SetAntennaMode( data.value, tostring( data.mode ) ) RADAR:SetAntennaMode( data.value, tonumber( data.mode ) )
print( "Set antenna: " .. data.value .. " to mode " .. tostring( data.mode ) ) print( "Set antenna: " .. data.value .. " to mode " .. tostring( data.mode ) )
end ) end )
RegisterNUICallback( "toggleAntenna", function( data )
RADAR:ToggleAntenna( data.value )
SendNUIMessage( { _type = "antennaXmit", ant = data.value, on = RADAR:IsAntennaTransmitting( data.value ) } )
print( "Toggled " .. data.value .. " antenna" )
end )
--[[------------------------------------------------------------------------ --[[------------------------------------------------------------------------
Main function Main function
------------------------------------------------------------------------]]-- ------------------------------------------------------------------------]]--
function RADAR:Main() function RADAR:Main()
-- Get the local player's ped and store it in a variable
local ped = PlayerPedId()
-- Get the vehicle the player is sitting in
local plyVeh = GetVehiclePedIsIn( ped, false )
-- Check to make sure the player is in the driver's seat, and also that the vehicle has a class of VC_EMERGENCY (18) -- Check to make sure the player is in the driver's seat, and also that the vehicle has a class of VC_EMERGENCY (18)
if ( DoesEntityExist( plyVeh ) and GetPedInVehicleSeat( plyVeh, -1 ) == ped and GetVehicleClass( plyVeh ) == 18 and self:IsPowerOn() ) then if ( DoesEntityExist( PLY.veh ) and PLY.inDriverSeat and GetVehicleClass( PLY.veh ) == 18 and self:IsPowerOn() ) then
local plyVehPos = GetEntityCoords( plyVeh ) local plyVehPos = GetEntityCoords( PLY.veh )
-- First stage of the radar - get all of the vehicles hit by the radar -- First stage of the radar - get all of the vehicles hit by the radar
if ( self:GetRadarStage() == 0 --[[ and self:IsEitherAntennaOn() ]] ) then if ( self:GetRadarStage() == 0 --[[ and self:IsEitherAntennaOn() ]] ) then
@@ -682,28 +693,59 @@ function RADAR:Main()
self:ResetCapturedVehicles() self:ResetCapturedVehicles()
self:ResetRayTraceState() self:ResetRayTraceState()
self:CreateRayThreads( plyVeh, vehs ) self:CreateRayThreads( PLY.veh, vehs )
elseif ( self:GetRayTraceState() == self:GetNumOfRays() ) then elseif ( self:GetRayTraceState() == self:GetNumOfRays() ) then
self:IncreaseRadarStage() self:IncreaseRadarStage()
end end
elseif ( self:GetRadarStage() == 1 ) then elseif ( self:GetRadarStage() == 1 ) then
-- self:RemoveDuplicateCapturedVehicles() local data = {}
-- Get the player's vehicle speed
local speed = self:GetVehSpeedFormatted( GetEntitySpeed( PLY.veh ) )
data.patrolSpeed = UTIL:FormatSpeed( speed )
-- Only grab data to send if there have actually been vehicles captured by the radar -- Only grab data to send if there have actually been vehicles captured by the radar
if ( not UTIL:IsTableEmpty( self:GetCapturedVehicles() ) ) then if ( not UTIL:IsTableEmpty( self:GetCapturedVehicles() ) ) then
local vehsForDisplay = self:GetVehiclesForAntenna() local vehsForDisplay = self:GetVehiclesForAntenna()
-- self:SetActiveVehicles( vehsForDisplay ) -- not really any point in setting this self:SetActiveVehicles( vehsForDisplay ) -- not really any point in setting this
else else
-- self:SetActiveVehicles( { nil, nil, nil, nil } ) self:SetActiveVehicles( { ["front"] = { nil, nil }, ["rear"] = { nil, nil } } )
end end
-- Work out what has to be sent
local av = self:GetActiveVehicles()
local test = { ["front"] = {}, ["rear"] = {} }
if ( av["front"][1] ~= nil ) then
test["front"].speed = UTIL:FormatSpeed( self:GetVehSpeedFormatted( av["front"][1].speed ) )
end
if ( av["front"][2] ~= nil ) then
test["front"].fast = UTIL:FormatSpeed( self:GetVehSpeedFormatted( av["front"][2].speed ) )
end
-- Send the update to the NUI side
SendNUIMessage( { _type = "update", speed = data.patrolSpeed, antennas = test } )
self:ResetRadarStage() self:ResetRadarStage()
self:ResetRayTraceState() self:ResetRayTraceState()
end end
end end
end end
-- Updates the local player information
Citizen.CreateThread( function()
while ( true ) do
PLY.ped = PlayerPedId()
PLY.veh = GetVehiclePedIsIn( PLY.ped, false )
PLY.inDriverSeat = GetPedInVehicleSeat( PLY.veh, -1 ) == PLY.ped
Citizen.Wait( 250 )
end
end )
-- Update the vehicle pool every 3 seconds -- Update the vehicle pool every 3 seconds
Citizen.CreateThread( function() Citizen.CreateThread( function()
while ( true ) do while ( true ) do
@@ -722,7 +764,7 @@ Citizen.CreateThread( function()
while ( true ) do while ( true ) do
RADAR:Main() RADAR:Main()
Citizen.Wait( 100 ) Citizen.Wait( 50 )
end end
end ) end )
@@ -739,9 +781,9 @@ end )
------------------------------ DEBUG ------------------------------ ------------------------------ DEBUG ------------------------------
local types = { "FRONT", "FRONT FAST", "REAR", "REAR FAST" } --[[ local types = { "FRONT", "FRONT FAST", "REAR", "REAR FAST" }
--[[ Citizen.CreateThread( function() Citizen.CreateThread( function()
while ( true ) do while ( true ) do
-- Caught veh debug printing -- Caught veh debug printing
local av = RADAR:GetActiveVehicles() local av = RADAR:GetActiveVehicles()
@@ -756,9 +798,9 @@ local types = { "FRONT", "FRONT FAST", "REAR", "REAR FAST" }
local speed = RADAR:GetVehSpeedFormatted( GetEntitySpeed( av[i].veh ) ) local speed = RADAR:GetVehSpeedFormatted( GetEntitySpeed( av[i].veh ) )
local veh = av[i].veh local veh = av[i].veh
local rt = av[i].rayType local rt = av[i].rayType
local dir = UTIL:IsEntityInMyHeading( GetEntityHeading( GetVehiclePedIsIn( PlayerPedId(), false ) ), GetEntityHeading( veh ), 100 ) local dir = UTIL:GetEntityRelativeDirection( GetEntityHeading( GetVehiclePedIsIn( PlayerPedId(), false ) ), GetEntityHeading( veh ), 100 )
if ( av[i].relPos == -1 and dir ~= nil ) then dir = not dir end
if ( dir == true ) then dir = "Away" elseif ( dir == false ) then dir = "Closing" else dir = "nil" end if ( dir == 1 ) then dir = "/\\" elseif ( dir == 2 ) then dir = "\\/" else dir = "none" end
DrawMarker( 2, pos.x, pos.y, pos.z + 3, 0.0, 0.0, 0.0, 0.0, 180.0, 0.0, 1.0, 1.0, 1.0, 255, 255, 0, 70, false, true, 2, nil, nil, false ) DrawMarker( 2, pos.x, pos.y, pos.z + 3, 0.0, 0.0, 0.0, 0.0, 180.0, 0.0, 1.0, 1.0, 1.0, 255, 255, 0, 70, false, true, 2, nil, nil, false )

View File

@@ -66,7 +66,7 @@ function UTIL:OppositeAngle( ang )
return ( ang + 180 ) % 360 return ( ang + 180 ) % 360
end end
function UTIL:IsEntityInMyHeading( myAng, tarAng, angToCheck ) function UTIL:GetEntityRelativeDirection( myAng, tarAng, angToCheck )
local rangeStartFront = myAng - ( angToCheck / 2 ) local rangeStartFront = myAng - ( angToCheck / 2 )
local rangeEndFront = myAng + ( angToCheck / 2 ) local rangeEndFront = myAng + ( angToCheck / 2 )
@@ -76,12 +76,12 @@ function UTIL:IsEntityInMyHeading( myAng, tarAng, angToCheck )
local rangeEndBack = opp + ( angToCheck / 2 ) local rangeEndBack = opp + ( angToCheck / 2 )
if ( ( tarAng > rangeStartFront ) and ( tarAng < rangeEndFront ) ) then if ( ( tarAng > rangeStartFront ) and ( tarAng < rangeEndFront ) ) then
return true return 1
elseif ( ( tarAng > rangeStartBack ) and ( tarAng < rangeEndBack ) ) then elseif ( ( tarAng > rangeStartBack ) and ( tarAng < rangeEndBack ) ) then
return false return 2
else
return nil
end end
return 0
end end
function UTIL:Notify( text ) function UTIL:Notify( text )

View File

@@ -23,4 +23,4 @@ RADAR.config.reset_key = 244
RADAR.config.fast_lock_blips = true RADAR.config.fast_lock_blips = true
-- Debug mode -- Debug mode
RADAR.config.debug_mode = true RADAR.config.debug_mode = false

View File

@@ -29,9 +29,9 @@ button:focus { outline: none; }
position: absolute; position: absolute;
margin: auto; margin: auto;
/* top: 0; */ /* top: 0; */
right: 0; right: 10px;
bottom: 15px; bottom: 10px;
left: 0; /* left: 0; */
background-image: url( "frame.png" ); background-image: url( "frame.png" );
} }

View File

@@ -119,31 +119,31 @@
<div class="antenna_btns_container"> <div class="antenna_btns_container">
<div class="btns btns_top"> <div class="btns btns_top">
<button id="frontOppMode" data-action="setAntennaMode" data-value="front" data-mode="2" class="zone_btn top_left">OPP LK/REL</button> <button id="frontOppMode" data-nuitype="setAntennaMode" data-value="front" data-mode="2" class="zone_btn top_left">OPP LK/REL</button>
<div class="xmit_wrap"> <div class="xmit_wrap">
<div class="xmit_btn xmit_top"> <div class="xmit_btn xmit_top">
<div class="arrow"></div> <div class="arrow"></div>
<button id="frontXmitToggle" class="top_middle">XMIT HOLD</button> <button id="frontXmitToggle" data-nuitype="toggleAntenna" data-value="front" class="top_middle">XMIT HOLD</button>
</div> </div>
</div> </div>
<button id="frontSameMode" data-action="setAntennaMode" data-value="front" data-mode="1" class="zone_btn top_right">SAME LK/REL</button> <button id="frontSameMode" data-nuitype="setAntennaMode" data-value="front" data-mode="1" class="zone_btn top_right">SAME LK/REL</button>
</div> </div>
<div class="breaker"></div> <div class="breaker"></div>
<div class="btns btns_bottom"> <div class="btns btns_bottom">
<button id="rearOppMode" data-action="setAntennaMode" data-value="rear" data-mode="2" class="zone_btn bottom_left">LK/REL OPP</button> <button id="rearOppMode" data-nuitype="setAntennaMode" data-value="rear" data-mode="2" class="zone_btn bottom_left">LK/REL OPP</button>
<div class="xmit_wrap"> <div class="xmit_wrap">
<div class="xmit_btn xmit_bottom"> <div class="xmit_btn xmit_bottom">
<div class="arrow arrow_bottom"></div> <div class="arrow arrow_bottom"></div>
<button id="rearXmitToggle" class="bottom_middle">HOLD XMIT</button> <button id="rearXmitToggle" data-nuitype="toggleAntenna" data-value="rear" class="bottom_middle">HOLD XMIT</button>
</div> </div>
</div> </div>
<button id="rearSameMode" data-action="setAntennaMode" data-value="rear" data-mode="1" class="zone_btn bottom_right">LK/REL SAME</button> <button id="rearSameMode" data-nuitype="setAntennaMode" data-value="rear" data-mode="1" class="zone_btn bottom_right">LK/REL SAME</button>
</div> </div>
</div> </div>

View File

@@ -90,13 +90,14 @@ const remoteButtons =
const antennaModes = const antennaModes =
{ {
same: 0, off: 0,
opp: 1, same: 1,
both: 2 opp: 2,
both: 3
} }
// Hide the radar and remote, this way we can bypass setting a style of 'display: none;' in the HTML file // Hide the radar and remote, this way we can bypass setting a style of 'display: none;' in the HTML file
elements.radar.hide(); // elements.radar.hide();
elements.remote.hide(); elements.remote.hide();
// Create the onclick event for the toggle display button // Create the onclick event for the toggle display button
@@ -109,7 +110,39 @@ function toggleRemote()
elements.remote.toggle(); elements.remote.toggle();
} }
// function toggleLabel( ) function setLight( ant, cat, item, state )
{
let obj = elements.antennas[ant][cat][item];
if ( state ) {
obj.addClass( "active" );
} else {
obj.removeClass( "active" );
}
}
function setAntennaXmit( ant, state )
{
setLight( ant, "modes", "xmit", state );
if ( !state ) {
elements.antennas[ant].targetSpeed.html( "¦¦¦" );
elements.antennas[ant].fast.speed.html( "HLd" );
}
}
function updateDisplays( ps, ants )
{
elements.patrolSpeed.html( ps );
if ( ants["front"].speed != null ) {
elements.antennas["front"].targetSpeed.html( ants["front"].speed );
}
if ( ants["front"].fast != null ) {
elements.antennas["front"].fast.speed.html( ants["front"].fast );
}
}
// This function is used to send data back through to the LUA side // This function is used to send data back through to the LUA side
function sendData( name, data ) { function sendData( name, data ) {
@@ -121,20 +154,17 @@ function sendData( name, data ) {
} }
// This runs when the JS file is loaded, loops through all of the remote buttons and assigns them an onclick function // This runs when the JS file is loaded, loops through all of the remote buttons and assigns them an onclick function
function remoteInit() elements.remote.find( "button" ).each( function( i, obj ) {
{ if ( $( this ).attr( "data-nuitype" ) && $( this ).attr( "data-value" ) ) {
elements.remote.find( "button" ).each( function( i, obj ) { $( this ).click( function() {
if ( $( this ).attr( "data-action" ) && $( this ).attr( "data-value" ) ) { let type = $( this ).data( "nuitype" );
$( this ).click( function() { let value = $( this ).data( "value" );
let action = $( this ).data( "action" ); let mode = $( this ).attr( "data-mode" ) ? $( this ).data( "mode" ) : null;
let value = $( this ).data( "value" );
let mode = $( this ).data( "mode" );
sendData( action, { value, mode } ); sendData( type, { value, mode } );
} ) } )
} }
} ); } );
}
// Close the remote when the user presses the 'Escape' key // Close the remote when the user presses the 'Escape' key
document.onkeyup = function ( event ) { document.onkeyup = function ( event ) {
@@ -145,16 +175,26 @@ document.onkeyup = function ( event ) {
} }
} }
remoteInit();
// The main event listener, this is what the NUI messages sent by the LUA side arrive at, they are // The main event listener, this is what the NUI messages sent by the LUA side arrive at, they are
// then handled properly via a switch/case that runs the relevant code // then handled properly via a switch/case that runs the relevant code
window.addEventListener( "message", function( event ) { window.addEventListener( "message", function( event ) {
var item = event.data; var item = event.data;
var type = event.data._type;
if ( item.pathName ) { switch ( type ) {
resourceName = item.pathName; case "updatePathName":
} else if ( item.activateRemote ) { resourceName = item.pathName
toggleRemote(); break;
case "openRemote":
toggleRemote();
break;
case "update":
updateDisplays( item.speed, item.antennas );
break;
case "antennaXmit":
setAntennaXmit( item.ant, item.on );
break;
default:
break;
} }
} ); } );