----------------------------
-- Creates the grid of rocks
----------------------------
function newRocks()
   local o = {}
   
   
   -----------------------------------
   -- Constructor of the grid of rocks
   -----------------------------------
   function o:construct()
      
      -- Load the sprite
      self.sprite = newSprite("images/sprites.png", 0, 38, 38, 38)
      
      -- Build the tile-map (we make sure there is no rock in the corner)
      self.map = {}
      for i = 0, boombastic.numTilesX - 1 do
         self.map[i] = {}
         for j = 0, boombastic.numTilesY - 1 do
            local onEdgeX = (i <= 1 or i >= boombastic.numTilesX - 2)
            local onEdgeY = (j <= 1 or j >= boombastic.numTilesY - 2)
            local inCorner = onEdgeX and onEdgeY
            
            if (not inCorner and (i % 2 == 0 or j % 2 == 0)) then
               self.map[i][j] = (math.random() <= 0.8)
            else
               self.map[i][j] = false
            end
         end
      end
      
      return self
   end
   
   
   ---------------------------
   -- Resets the grid of rocks
   ---------------------------
   function o:reset()
      for i = 0, boombastic.numTilesX - 1 do
         for j = 0, boombastic.numTilesY - 1 do
            local onEdgeX = (i <= 1 or i >= boombastic.numTilesX - 2)
            local onEdgeY = (j <= 1 or j >= boombastic.numTilesY - 2)
            local inCorner = onEdgeX and onEdgeY
            
            if (not inCorner and (i % 2 == 0 or j % 2 == 0)) then
               self.map[i][j] = (math.random() <= 0.8)
            else
               self.map[i][j] = false
            end
         end
      end
   end
   
   
   -------------------------------------------------
   -- Checks whether or not the given tile is a rock
   -------------------------------------------------
   function o:tileIsRock(tileX, tileY)
      return self.map[tileX][tileY]
   end
   
   
   ------------------------------------------------------------
   -- Destroys the rock at the given position (if there is one)
   ------------------------------------------------------------
   function o:destroyRock(tileX, tileY)
      if (self.map[tileX][tileY]) then
         self.map[tileX][tileY] = false
         
         -- Add a bonus of a random type
         if (math.random() <= 0.4) then
            boombastic.bonuses:addBonus(tileX, tileY)
         end
      end
   end
   
   
   --------------------------
   -- Draws the grid of rocks
   --------------------------
   function o:draw()
      for i = 0, boombastic.numTilesX - 1 do
         for j = 0, boombastic.numTilesY - 1 do
            if (self.map[i][j]) then
               self.sprite:draw(boombastic.origX + i * boombastic.tileSize,
                                boombastic.origY + j * boombastic.tileSize)
            end
         end
      end
   end
   

   return o:construct()
end
