Jump to content

[AJUDA] Desligar motor ao entrar no veiculo


Recommended Posts

Olá galera, junto a um mod de velocimetro que tenho fica a função de ligar e desligar o motor do veiculo, no entanto, queria adicionar a função de quando o player entrar no veiculo o motor do carro estar como ele deixou, ou seja, se ele sair do carro com ele ligado ele permanecer ligado, se saiu do carro desligado, permanecer desligado, alguem poderia me ajudar?

 

--Client-side

local scx,scy = guiGetScreenSize()
local px = scx/1920

local sizeX,sizeY = 350*px,350*px
local posX,posY = scx-sizeX,scy-sizeY

local font1 = dxCreateFont( "files/AEROMATICSBOLD.ttf",17*px )
local font2 = dxCreateFont( "files/AEROMATICSBOLDITALIC.ttf" )
local font3 = dxCreateFont( "files/AEROMATICSITALIC.ttf",50*px )
local font4 = dxCreateFont( "files/AEROMATICSITALIC.ttf",18*px )

function math.lerp(a, b, k)
	local result = a * (1-k) + b * k
	if result >= b then
		result = b
	elseif result <= a then
		result = a
	end
	return result
end

local alpha = 255
local side = true
local pulsing = true

function drawSpeedometer()
	local veh = getPedOccupiedVehicle(localPlayer)
	if veh then
		local speedx, speedy, speedz = getElementVelocity ( veh )
		local actualspeed = (speedx^2 + speedy^2 + speedz^2)^(0.5)
		local kmh = math.floor(actualspeed * 180)
		local rotation = math.lerp(-152,90,kmh/300)
		if rotation >= 90 then rotation = math.random(88,92) end
--	--	local fuel = math.floor(getElementData(veh,"Gasolina")) or 100 -- Переменная топлива
	--	local fuelmax = 100 -- Объём бака
	--	local rotation2 = math.lerp(-150,90,fuel/fuelmax)
	--	if rotation2 <= -125 then pulsing = true else pulsing = false end

		local gear = getVehicleCurrentGear( veh )
		if gear == 0 then
			if kmh <= 1 then
				gear = "N"
			else
				gear = "R"
			end 
		elseif gear == 1 then
			if kmh <= 2 then
				gear = "N"
			end
		end

		dxDrawText(gear,posX,posY,posX+sizeX,posY+sizeY,tocolor(0,100,100),1,font1,"center","center")
		dxDrawText(kmh,posX+60*px,posY+250*px,posX+sizeX,posY+250*px,tocolor(255,255,255),1,font3,"center","center")

		dxDrawImage(posX,posY,sizeX,sizeY,"files/Spedo.png")
		dxDrawImage(posX,posY,sizeX,sizeY,"files/strelkaspedo.png",rotation) -- -150 to 90

		if pulsing then
			if side then alpha = alpha + 5 else alpha = alpha -5 end
			if alpha <= 0 then side = true elseif alpha >= 255 then side = false end
		--	dxDrawImage(posX-200*px,posY+100*px,sizeX,sizeY,"files/benz2.png",0,0,0,tocolor(200,0,0,alpha))
		end

		--dxDrawImage(posX-200*px,posY+100*px,sizeX,sizeY,"files/benz.png")
		--dxDrawImage(posX-200*px,posY+100*px,sizeX,sizeY,"files/strelkabenz.png",rotation2) -- -150 to 90
		--dxDrawText(fuel.."/"..fuelmax,posX,posY+310*px,posX+30*px,posY+310*px,tocolor(255,255,255),1,font4,"center","center")

		if getVehicleEngineState( veh ) then
			dxDrawImage(posX-100*px,posY-140*px,512*px,512*px,"files/engine.png",0,0,0,tocolor(200,0,0))
		else
			dxDrawImage(posX-100*px,posY-140*px,512*px,512*px,"files/engine.png",0,0,0,tocolor(255,255,255))
		end

		if getVehicleOverrideLights( veh ) == 2 then
			dxDrawImage(posX-60*px,posY-140*px,512*px,512*px,"files/light.png",0,0,0,tocolor(200,0,0))
		elseif getVehicleOverrideLights( veh ) == 1 then
			dxDrawImage(posX-60*px,posY-140*px,512*px,512*px,"files/light.png",0,0,0,tocolor(255,255,255))
		else
			local h,m = getTime()
			if h >= 7 and h <= 21 then
				dxDrawImage(posX-60*px,posY-140*px,512*px,512*px,"files/light.png",0,0,0,tocolor(255,255,255))
			else
				dxDrawImage(posX-60*px,posY-140*px,512*px,512*px,"files/light.png",0,0,0,tocolor(200,0,0))
			end
		end

		if isVehicleLocked(veh) then
			dxDrawImage(posX-20*px,posY-120*px,512*px,512*px,"files/lock.png",0,0,0,tocolor(200,0,0))
		else
			dxDrawImage(posX-20*px,posY-120*px,512*px,512*px,"files/lock.png",0,0,0,tocolor(255,255,255))
		end

		if ccEnabled then
			dxDrawImage(posX-140*px,posY-120*px,512*px,512*px,"files/kruiz.png",0,0,0,tocolor(200,0,0))
		else
			dxDrawImage(posX-140*px,posY-120*px,512*px,512*px,"files/kruiz.png",0,0,0,tocolor(255,255,255))
		end
	end
end
addEventHandler("onClientRender",root,drawSpeedometer)

bindKey("o","down",function()
	triggerServerEvent( "triggerVehicleSystem", localPlayer, "engine" )
end)

bindKey("lalt","down",function()
	triggerServerEvent( "triggerVehicleSystem", localPlayer, "lights" )
end)

bindKey("","down",function()
	triggerServerEvent( "triggerVehicleSystem", localPlayer, "lock" )
end)

 

-- Server-side

function triggerVehicleSystem(sys)
	local veh = getPedOccupiedVehicle(source)
	if veh and getVehicleController(veh) == source then
		if sys == "engine" then
			if not getElementData(veh,"Gasolina") then setVehicleEngineState(veh, not getVehicleEngineState(veh))  return end
			if getElementData(veh,"Gasolina") > 0 then
				setVehicleEngineState(veh, not getVehicleEngineState(veh))
			else
				outputChatBox("Нет бензина!",source,200,0,0)
			end
		elseif sys == "lights" then
			if getVehicleOverrideLights( veh ) ~= 2 then
				setVehicleOverrideLights( veh, 2 )
			else
				setVehicleOverrideLights( veh, 1 )
			end
		elseif sys == "lock" then
			setVehicleLocked(veh,not isVehicleLocked(veh))
		end
	end
end
addEvent("triggerVehicleSystem",true)
addEventHandler("triggerVehicleSystem",root,triggerVehicleSystem)

-- DESLIGANDO O MOTOR QUANDO ENTRAR NO VEICULO

function turnEngineOff ( theVehicle, leftSeat, jackerPlayer )
    if leftSeat == 0 and not jackerPlayer then
        setVehicleEngineState ( theVehicle, false )
    end
end
addEventHandler ( "onPlayerVehicleEnter", getRootElement ( ), turnEngineOff )

 

Link to comment

É padrão do GTA ligar o carro quando você entrar nele, então uma coisa que pode fazer é desligar o carro depois de entrar. Você vai precisar ir na função de ligar o carro e colocar um setElementData para definir se ele está ligado ou não. E crie uma função que ao entrar no carro vai checar o elementData do veículo e se tiver com motor desligado ele vai desligar, e se tiver com motor ligado ele não vai fazer nada

 

Link to comment

Teste lá :

function triggerVehicleSystem(sys)
   local veh = getPedOccupiedVehicle(source)
   if veh and getVehicleController(veh) == source then
      if sys == "engine" then
         if not getElementData(veh,"Gasolina") then setVehicleEngineState(veh, not getVehicleEngineState(veh))  return end
         if getElementData(veh,"Gasolina") > 0 then
            setVehicleEngineState(veh, not getVehicleEngineState(veh))
         else
            outputChatBox("Нет бензина!",source,200,0,0)
         end
      elseif sys == "lights" then
         if getVehicleOverrideLights( veh ) ~= 2 then
            setVehicleOverrideLights( veh, 2 )
         else
            setVehicleOverrideLights( veh, 1 )
         end
      elseif sys == "lock" then
         setVehicleLocked(veh,not isVehicleLocked(veh))
      end
   end
end
addEvent("triggerVehicleSystem",true)
addEventHandler("triggerVehicleSystem",root,triggerVehicleSystem)

--# Adicionado :

local estado_do_motor = { }

addEventHandler ( "onPlayerVehicleExit", getRootElement ( ), function ( veh, seat, jacked )
   if seat == 0 then
      if estado_do_motor[veh] == "true" then
         setVehicleEngineState ( veh, true )
      else
         setVehicleEngineState ( veh, false )
      end
   end
end)

--# Verifica o Motor, ao Começa a Sair do Veiculo.
addEventHandler ( "onVehicleStartExit", getRootElement ( ), function ( thePlayer, seat, jacked )
   if seat == 0 then
      local veh = getPedOccupiedVehicle(thePlayer)
      estado_do_motor[veh] = tostring(getVehicleEngineState(veh))
   end
end)

addEventHandler ( "onPlayerVehicleEnter", getRootElement ( ), function ( veh, seat, jacked )
   if seat == 0 then
      if estado_do_motor[veh] == "true" then
         setVehicleEngineState ( veh, true )
      else
         setVehicleEngineState ( veh, false )
      end
   end
end)

--# Verifica o Motor, ao Começar a Entrar no Veiculo.
addEventHandler ( "onVehicleStartEnter", getRootElement ( ), function ( thePlayer, seat, jacked ) 
   if seat == 0 then
      local veh = getPedOccupiedVehicle(thePlayer)
      estado_do_motor[veh] = tostring(getVehicleEngineState(veh))
   end
end) 

 

  • Thanks 1
Link to comment
On 19/07/2020 at 20:56, Angelo Pereira said:

Teste lá :


function triggerVehicleSystem(sys)
   local veh = getPedOccupiedVehicle(source)
   if veh and getVehicleController(veh) == source then
      if sys == "engine" then
         if not getElementData(veh,"Gasolina") then setVehicleEngineState(veh, not getVehicleEngineState(veh))  return end
         if getElementData(veh,"Gasolina") > 0 then
            setVehicleEngineState(veh, not getVehicleEngineState(veh))
         else
            outputChatBox("Нет бензина!",source,200,0,0)
         end
      elseif sys == "lights" then
         if getVehicleOverrideLights( veh ) ~= 2 then
            setVehicleOverrideLights( veh, 2 )
         else
            setVehicleOverrideLights( veh, 1 )
         end
      elseif sys == "lock" then
         setVehicleLocked(veh,not isVehicleLocked(veh))
      end
   end
end
addEvent("triggerVehicleSystem",true)
addEventHandler("triggerVehicleSystem",root,triggerVehicleSystem)

--# Adicionado :

local estado_do_motor = { }

addEventHandler ( "onPlayerVehicleExit", getRootElement ( ), function ( veh, seat, jacked )
   if seat == 0 then
      if estado_do_motor[veh] == "true" then
         setVehicleEngineState ( veh, true )
      else
         setVehicleEngineState ( veh, false )
      end
   end
end)

--# Verifica o Motor, ao Começa a Sair do Veiculo.
addEventHandler ( "onVehicleStartExit", getRootElement ( ), function ( thePlayer, seat, jacked )
   if seat == 0 then
      local veh = getPedOccupiedVehicle(thePlayer)
      estado_do_motor[veh] = tostring(getVehicleEngineState(veh))
   end
end)

addEventHandler ( "onPlayerVehicleEnter", getRootElement ( ), function ( veh, seat, jacked )
   if seat == 0 then
      if estado_do_motor[veh] == "true" then
         setVehicleEngineState ( veh, true )
      else
         setVehicleEngineState ( veh, false )
      end
   end
end)

--# Verifica o Motor, ao Começar a Entrar no Veiculo.
addEventHandler ( "onVehicleStartEnter", getRootElement ( ), function ( thePlayer, seat, jacked ) 
   if seat == 0 then
      local veh = getPedOccupiedVehicle(thePlayer)
      estado_do_motor[veh] = tostring(getVehicleEngineState(veh))
   end
end) 

 

Funcionou perfeitamente! muito obrigado!

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...