------------------------------
-- Creates the list of rockets
------------------------------
function newRockets()
   local o = {}
   
   
   -------------------------------------
   -- Constructor of the list of rockets
   -------------------------------------
   function o:construct()
      self.sprite = newSprite("images/sprites.png", 114, 114, 13, 14)
      self.sprite:setCenter(0.5, 0.5)
      self.sound = newSound("sounds/rocket.wav")
      self.rockets = {}
      return self
   end
   
   
   -----------------------------
   -- Resets the list of rockets
   -----------------------------
   function o:reset()
      self.rockets = {}
   end
   
   
   ----------------
   -- Adds a rocket
   ----------------
   function o:addRocket(posX, posY, dir, power, owner)
      local rocket = {}
      rocket.x, rocket.y = convertTileToPixels(posX, posY)
      rocket.dir = dir
      rocket.power = power
      rocket.flame = boombastic.particleSystems:addRocketFlame(rocket.x, rocket.y)
      rocket.owner = owner
      
      if (rocket.dir == TankDirection.LEFT) then
         rocket.angle = 180
      elseif (rocket.dir == TankDirection.RIGHT) then
         rocket.angle = 0
      elseif (rocket.dir == TankDirection.UP) then
         rocket.angle = 270
      else
         rocket.angle = 90
      end
      
      self.sound:play()
      
      table.insert(self.rockets, rocket)
   end
   
   
   ----------------------
   -- Updates the rockets
   ----------------------
   function o:update(dt)
      local i = 1
      while (i <= #self.rockets) do
         local rocket = self.rockets[i]
         local prevPosX, prevPosY = convertPixelsToTile(rocket.x, rocket.y)
         
         -- Move the rocket
         if (rocket.dir == TankDirection.LEFT) then
            rocket.x = rocket.x - dt * boombastic.rocketSpeed
         elseif (rocket.dir == TankDirection.RIGHT) then
            rocket.x = rocket.x + dt * boombastic.rocketSpeed
         elseif (rocket.dir == TankDirection.UP) then
            rocket.y = rocket.y - dt * boombastic.rocketSpeed
         else
            rocket.y = rocket.y + dt * boombastic.rocketSpeed
         end
         
         -- Update the flame of the rocket
         rocket.flame:setPosition(rocket.x, rocket.y)
         rocket.flame:start()
         rocket.flame:update(dt)
         
         -- Make the rocket explodes if it hits an obstacle
         local posX, posY = convertPixelsToTile(rocket.x, rocket.y)
         if (tileIsBlocking(posX, posY)) then
            boombastic.explosions:addExplosion(prevPosX, prevPosY, rocket.power)
            table.remove(self.rockets, i)
         else
            i = i + 1
         end
      end
   end
   
   
   --------------------
   -- Draws the rockets
   --------------------
   function o:draw()
      for k, rocket in ipairs(self.rockets) do
         self.sprite:draw(rocket.x, rocket.y, rocket.angle)
      end
   end
   

   return o:construct()
end
