local starter_sound = ui.MediaPlayer()
starter_sound:setSource('./start.mp3')
starter_sound:setLooping(false)
starter_sound:setVolume(0.5) -- Set the volume to 50%

local start_on_sound = ui.MediaPlayer()
start_on_sound:setSource('./startON.mp3')
start_on_sound:setLooping(false)
start_on_sound:setVolume(0.5) -- Set the volume to 50%

local ignition_sound = ui.MediaPlayer()
ignition_sound:setSource('./ignition.mp3')
ignition_sound:setLooping(false)
ignition_sound:setVolume(0.5) -- Set the volume to 50%

local turn_off_sound = ui.MediaPlayer()
turn_off_sound:setSource('./kill.mp3')
turn_off_sound:setLooping(false)
turn_off_sound:setVolume(0.5) -- Set the volume to 50%

local ignitionPlayed = false
local flag = 0
local onProtection = false
local pressTimer = 0

function update(dt)
  if not car.extraB then
    pressTimer = 0
    onProtection = false
  end

  if car.extraB and not car.extraA and flag == 0 then
    starter_sound:play()
  end

  if car.extraB then
    pressTimer = pressTimer + dt
    if pressTimer >= 1.0 and not onProtection and flag == 1 then
      if pressTimer < 6 and pressTimer >= 1 then  
        start_on_sound:play()
      end
      flag = 2
    end
  end

  if car.extraA and not car.extraB and not ignitionPlayed then
    ignition_sound:play()
    ignitionPlayed = true
    flag = 1

    ignition_sound:onCompletion(function()
      ignition_sound:stop()
    end)
  end

  if not car.extraA then
    ignitionPlayed = false
    flag = 0
  end

  if car.extraB then
    pressTimer = pressTimer + dt
    if pressTimer < 0.5 and not onProtection and flag == 2 then
      turn_off_sound:play()
      onProtection = true
      flag = 1
      pressTimer = 7
    end
  end
end