------------------
-- Creates the HUD
------------------
function newHud()
   local o = {}
   
   
   -------------------------
   -- Constructor of the HUD
   -------------------------
   function o:construct()
      self.hudImg = newSprite("images/hud.png", 0, 0, 480, 272)
      self.boxImg = newSprite("images/hud.png", 0, 272, 49, 55)
      self.boxCrossImg = newSprite("images/hud.png", 49, 272, 49, 55)
      self.veraBI = newFont("fonts/veraBI.ttf", 10)
      
      self.totalTime = 0
      self.infos = {}
      self.fps = 0
      self.fps2 = 0
      
      return self
   end
   
   
   -----------------
   -- Resets the HUD
   -----------------
   function o:reset()
      self.totalTime = 0
   end
   
   
   ------------------
   -- Updates the HUD
   ------------------
   function o:update(dt)
      self.totalTime = self.totalTime + dt
      self.fps2 = self.fps2 + 1
      if (math.floor(self.totalTime) ~= math.floor(self.totalTime - dt)) then
         self.fps = self.fps2
         self.fps2 = 0
      end
      
      -- Retrieve the infos to display from the tanks
      self.infos = {}
      for k, tank in ipairs(boombastic.tanks) do
         local info = {}
         info.name = tank.name
         info.numVictories = tank.victories
         info.numRockets = tank.numRockets
         info.alive = tank:isAlive()
         table.insert(self.infos, info)
      end
   end
   
   
   ----------------
   -- Draws the HUD
   ----------------
   function o:draw()
      self.hudImg:draw(0, 0)
      
      -- Draw the info-boxes for each tank
      for k, info in ipairs(self.infos) do
         local x = 426
         local y = 24 + (k - 1) * 62
         self.boxImg:draw(x, y)
         
         if (not info.alive) then
            self.boxCrossImg:draw(x, y)
         end
      end
      
      -- Print the elapsed time
      local currentTime = math.floor(self.totalTime)
      local timeString = string.format("%d:%02d",
            math.floor(currentTime / 60.0),
            math.floor(currentTime % 60))
      self.veraBI:draw(timeString, 438, 17, 212, 212, 212)
      
      -- Print the infos in each info-box
      for k, info in ipairs(self.infos) do
         local x = 426
         local y = 24 + (k - 1) * 62
         
         self.veraBI:draw(info.name, x + 7, y + 14, 212, 212, 212)
         self.veraBI:draw(info.numVictories, x + 30, y + 32, 212, 212, 212)
         self.veraBI:draw(info.numRockets, x + 30, y + 47, 212, 212, 212)
      end
      
      -- Print the FPS
      --self.veraBI:draw(string.format("FPS: %.1f", self.fps), 425, 268)
   end
   
   
   return o:construct()
end
