radioAlive = true
radioOn = false
songToPlay = 1
volume = 0.5

textcooldown = 300
counter = 0
--                                            JUST ADD "SONG.OGG" FILES BELOW AND COPY THE FILE IN THE "RADIO" FOLDER THE SCRIPT HANDLES THE REST
songs ={"PortalRadio.ogg",
        "RadioGAGA.ogg",
        "soulja_boy_crank_that_4K_ULTRA_SUPERIOR_QUALITY.ogg"}

function init()
  channels = {}
  for i=1, #songs do
    channels[i] = LoadLoop(songs[i])
  end
  --songToPlay = math.random(#songs)
  breakSound = LoadSound("radioBreaksSound.ogg")
  nextSongSound = LoadSound("RadioNextSongSound.ogg")
  OnOffSound = LoadSound("RadioOnOffSound.ogg")
  
  myRadio = FindShape("myradio", true)
  myButtonRed = FindShape("radioButtonRed", true)
  myButtonGreen = FindShape("radioButtonGreen", true)
  myVolUp = FindShape("radioButtonVolUp", true)
  myVolDown = FindShape("radioButtonVolDown", true)
  myRandom = FindShape("radioButtonRandom", true)
  mylast = FindShape("radioButtonUndo", true)
  songToPlay = math.random(#songs)
end



function tick()
  if radioAlive then
    t = GetShapeWorldTransform(myRadio)
    if InputPressed("interact") and GetPlayerInteractShape() == myButtonGreen then
      PlaySound(nextSongSound, t.pos, 0.2)
      if radioOn then
        DrawShapeOutline(myRadio, 1, 1, 1, 1)
        songToPlay = songToPlay + 1
        StopMusic()
        
        if songToPlay > #songs then
          songToPlay = 1
        end
        schreib(songs[songToPlay])
      end
    end
    
    if InputPressed("interact") and GetPlayerInteractShape() == myButtonRed then
      PlaySound(OnOffSound, t.pos, 0.2)
      DrawShapeOutline(myRadio, 1, 1, 1, 1)
      if radioOn then
        radioOn = false
        StopMusic()
      else
        radioOn = true
        schreib(songs[songToPlay])
      end
    end
    
    if InputPressed("interact") and GetPlayerInteractShape() == myVolUp then
      PlaySound(nextSongSound, t.pos, 0.2)
      volume = volume + 0.1
    end
    
    if InputPressed("interact") and GetPlayerInteractShape() == myVolDown then
      PlaySound(nextSongSound, t.pos, 0.2)
      volume = volume - 0.1
    end
    
    if volume > 1 then
      volume = 1
    end
    if volume < 0.1 then
      volume = 0.1
    end
    
    if InputPressed("interact") and GetPlayerInteractShape() == mylast then
      PlaySound(nextSongSound, t.pos, 0.2)
      if radioOn then
        DrawShapeOutline(myRadio, 1, 1, 1, 1)
        songToPlay = songToPlay - 1
        StopMusic()
        if songToPlay < 1 then
          songToPlay = #songs
        end
        schreib(songs[songToPlay])
      end
    end
    
    if InputPressed("interact") and GetPlayerInteractShape() == myRandom then
      if radioOn then
        playRandom()
        DrawShapeOutline(myRadio, 1, 1, 1, 1)
      end
      PlaySound(nextSongSound, t.pos, 0.2)
    end
  
    if radioOn then
      PlayLoop(channels[songToPlay], t.pos, volume)
    end
    
    if IsShapeBroken(myRadio) then
        killRadio()
        radioAlive = false
    end
  end
  
  if counter == 0 then
    for i=0 , 50 do
      DebugPrint(" ")
    end
  end
    
  
  if counter >= 0 then
    counter = counter - 1
  end
end  
  

function killRadio()
  StopMusic()
  PlaySound(breakSound, t.pos, 0.4)
  for i=0, 50 do
    spawnParticles()
  end
end


function spawnParticles()

  local factor = 5
  local v = Vec(math.random() * factor, math.random() * factor, math.random() * factor)
  math.random()
  local life = math.random()
  life = life*life * 5
  
  ParticleReset()
  ParticleEmissive(5, 0, "easeout")
  ParticleGravity(-10)
  ParticleRadius(0.03, 0.0, "easein")
  ParticleColor(1, 0.4, 0.3)
  ParticleTile(4)
  SpawnParticle(t.pos, v, life)
end
function playRandom()
  local rand = math.random(#songs)
  while rand == songToPlay do
    rand = math.random(#songs)
  end
  songToPlay = rand
  schreib(songs[songToPlay])
end

function schreib(text)
  counter = textcooldown
  for i=0 , 50 do
    DebugPrint(" ")
  end
  DebugPrint("Now Playing:  " .. text)
end

