----------------------------
-- Creates the grid of bombs
----------------------------
function newBombs()
   local o = {}
   
   
   -----------------------------------
   -- Constructor of the grid of bombs
   -----------------------------------
   function o:construct()
   
      -- Load the sprite
      self.sprite = newSprite("images/sprites.png", 38, 38, 38, 38)
      self.sprite:setCenter(0.5, 0.5)
      
      -- Build the grid of bombs
      self.map = {}
      for i = 0, boombastic.numTilesX - 1 do
         self.map[i] = {}
         for j = 0, boombastic.numTilesY - 1 do
            self.map[i][j] = nil
         end
      end
      
      return self
   end
   
   
   ---------------------------
   -- Resets the grid of bombs
   ---------------------------
   function o:reset()
      for i = 0, boombastic.numTilesX - 1 do
         for j = 0, boombastic.numTilesY - 1 do
            self.map[i][j] = nil
         end
      end
   end
   
   
   --------------------------------------------------------------
   -- Adds a new bomb to the grid of bombs, at the given position
   --------------------------------------------------------------
   function o:addBomb(tileX, tileY, power, owner)
      if (not self.map[tileX][tileY]) then
         local bomb = {}
         bomb.currentTime = 0.0
         bomb.size = boombastic.tileSize
         bomb.power = power
         bomb.owner = owner
         self.map[tileX][tileY] = bomb
         return true
      else
         return false
      end
   end
   
   
   -------------------------------------------------
   -- Checks whether or not the given tile is a bomb
   -------------------------------------------------
   function o:tileIsBomb(tileX, tileY)
      return (self.map[tileX][tileY] ~= nil)
   end
   
   
   -----------------------------------------------------------------------------------
   -- Gets the power of the bomb at the given position (or 0 if there is no such bomb)
   -----------------------------------------------------------------------------------
   function o:getBombPower(tileX, tileY)
      if (not self.map[tileX][tileY]) then
         return 0
      else
         return self.map[tileX][tileY].power
      end
   end
   
   
   -----------------------------------------------------------------
   -- Makes the bomb explode at the given position (if there is one)
   -----------------------------------------------------------------
   function o:explodeBomb(tileX, tileY)
      if (self.map[tileX][tileY]) then
         local bomb = self.map[tileX][tileY]
         bomb.owner.numBombs = bomb.owner.numBombs + 1
         boombastic.explosions:addExplosion(tileX, tileY, bomb.power)
         boombastic.map:makeDirty(tileX, tileY)
         self.map[tileX][tileY] = nil
      end
   end
   
   
   --------------------
   -- Updates the bombs
   --------------------
   function o:update(dt)
      for i = 0, boombastic.numTilesX - 1 do
         for j = 0, boombastic.numTilesY - 1 do
            if (self.map[i][j]) then
               local bomb = self.map[i][j]
               
               -- Animate the bomb
               bomb.currentTime = bomb.currentTime + dt
               bomb.scale = 1.0 - (0.25 * math.abs(math.sin(bomb.currentTime * 5.0)))
               
               -- Make the bomb explode if needed
               if (bomb.currentTime >= boombastic.bombDuration) then
                  self:explodeBomb(i, j)
               end
            end
         end
      end
   end
   
   
   --------------------------
   -- Draws the grid of bombs
   --------------------------
   function o:draw()
      for i = 0, boombastic.numTilesX - 1 do
         for j = 0, boombastic.numTilesY - 1 do
            if (self.map[i][j]) then
               local bomb = self.map[i][j]
               local x, y = convertTileToPixels(i, j)
               self.sprite:draw(boombastic.origX + x, boombastic.origY + y,
                                0, bomb.scale, bomb.scale)
            end
         end
      end
   end
   
   
   return o:construct()
end
