

local car = ac.getCar()
local incrementSpeed = 0.9
local interiorstate = 0.0
local launchstate = 0.0
local fartstate = 1.0
local fartTimer = 0.0
local fartDuration = 0.40 
local downshift = 0.0
local downshiftTimer = 0.0
local downshiftDuration = 0.40
local launchTimer = 0.0
local launchDuration = 6.0
local windows = 0.0
local incrementSpeed2 = 0.6
local turbo = 0.0

local carSounds1 = ui.MediaPlayer()
carSounds1:setSource('./sound/MK8STARTUP.wav')
carSounds1:setLooping(false)
carSounds1:setVolume(ac.getAudioVolume('main') * 1.0)
local flag = 0

local carSounds2 = ui.MediaPlayer()
carSounds2:setSource('./sound/INTMK8STARTUP.wav')
carSounds2:setLooping(false)
carSounds2:setVolume(ac.getAudioVolume('main') * 2.0)
local flag3 = 0

local carSounds3 = ui.MediaPlayer()
carSounds3:setSource('./sound/windowopen.wav')
carSounds3:setLooping(false)
carSounds3:setVolume(ac.getAudioVolume('main') * 2.0)
local flag2 = 0

local carSounds4 = ui.MediaPlayer()
carSounds4:setSource('./sound/windowclose.wav')
carSounds4:setLooping(false)
carSounds4:setVolume(ac.getAudioVolume('main') * 2.0)
local flag4 = 1

function script.update(dt)

	if car.extraA and flag == 0 and not ac.isInteriorView() then
		carSounds1:play()
		flag = 1
   
	end

	if car.extraA and flag3 == 0 and ac.isInteriorView() then
		carSounds2:play()
		flag3 = 1
	end

	if car.extraE and ac.isInteriorView() and flag2 == 0 then
		carSounds3:play()
		flag2 = 1
	end

	if not car.extraE and flag2 == 1 then
		carSounds4:play()
		flag2 = 0
		flag4 = 0
	end
	
	if not car.extraA then
    	flag = 0
  	end

	if ac.isControllerGearDownPressed() then
		downshift = 1.0
		downshiftTimer = downshiftDuration
	end

	if downshiftTimer > 0 then
		downshiftTimer = downshiftTimer - dt
		else 
			downshift = 0.0
	end


	if  car.rpm >= 3000 and car.brake >= 1.0 and car.gas >= 0.1 and downshift == 0.0 and car.gear == 0 then
		launchstate = 1.0
        launchTimer = launchDuration  -- Reset launch timer
    elseif launchTimer > 0 then
        launchTimer = launchTimer - dt  -- Decrease launch timer
    else
        launchstate = 0.0
    end
    ac.CarAudioTweak.setParameter(ac.CarAudioEventID.EngineExt, 'launch', launchstate)
	ac.CarAudioTweak.setParameter(ac.CarAudioEventID.EngineInt, 'launch', launchstate)
	
	if launchstate == 1 then
		if ac.isControllerGearUpPressed() then
			fartstate = 0.0
			fartTimer = fartDuration  -- Reset timer when button is pressed
		end
	
		if fartTimer > 0 then
			fartTimer = fartTimer - dt  -- Decrease timer by delta time
		else
			fartstate = 1.0  -- Reset fartstate when timer expires
		end

	elseif car.gear == 1 then
		fartstate = 0.0
	else
		fartstate = 1.0
	end
	ac.CarAudioTweak.setParameter(ac.CarAudioEventID.EngineExt, 'fart', fartstate)
	ac.CarAudioTweak.setParameter(ac.CarAudioEventID.EngineInt, 'fart', fartstate)
	

	if ac.isInteriorView() then
		interiorstate = 1
		ac.CarAudioTweak.setParameter(ac.CarAudioEventID.Turbo, 'interior', interiorstate)
	end

	if not ac.isInteriorView() then
		interiorstate = 0
		ac.CarAudioTweak.setParameter(ac.CarAudioEventID.Turbo, 'interior', interiorstate)
	end

	if car.extraE and ac.isInteriorView() then
		windows = math.min(windows + incrementSpeed2 * dt, 1.0)
		ac.CarAudioTweak.setParameter(ac.CarAudioEventID.EngineInt, 'windows', windows)
		ac.CarAudioTweak.setParameter(ac.CarAudioEventID.Wind, 'windows', windows)

	end

	if not car.extraE and ac.isInteriorView() then
		windows = math.max(windows - incrementSpeed2 * dt, 0.0)
		ac.CarAudioTweak.setParameter(ac.CarAudioEventID.EngineInt, 'windows', windows)
		ac.CarAudioTweak.setParameter(ac.CarAudioEventID.Wind, 'windows', windows)
	end

	ac.debug('launchstate',launchstate)
	ac.debug('fartstate',fartstate)
	ac.debug('gear',ac.isControllerGearUpPressed())
	ac.debug('downshiftstate',downshift)
	ac.debug('actualgear', car.gear)

  

end