# To change this template, choose Tools | Templates
# and open the template in the editor.

class Map4
attr_reader :width, :height, :finish

  def initialize(window, filename)
    # Load 60x60 tiles, 5px overlap in all four directions.
    @tileset = Gosu::Image.load_tiles(window, "media/Tiles2.png", 60, 60, true)   
    finish_img = Gosu::Image.new(window, "media/Finish.png", false)       
    @finish = []
    
    lines = File.readlines(filename).map { |line| line.chomp }
    @height = lines.size
    @width = lines[0].size
    @tiles = Array.new(@width) do |x|
      Array.new(@height) do |y|
        case lines[y][x, 1]
        when '"'
          Tiles::Grass
        when '^'         
          Tiles::Pit
        when '<'          
          Tiles::EarthF
        when '>'
          Tiles::EarthB
        when '#'
          Tiles::Earth
        when '*'
          Tiles::Solid
        when 'o'
          @finish.push(Finish.new(finish_img, x * 50 + 25, y * 50 + 25))          
          nil #Tiles::Finish        
        else
          nil
        end
      end
    end
  end

  def draw    
    # Draws all the tiles, some off-screen, some on-screen.
    @height.times do |y|
      @width.times do |x|
        tile = @tiles[x][y]
        if tile
          # Draw the tile with an offset (tile images have some overlap)
          # Scrolling is implemented here just as in the game objects.
          @tileset[tile].draw(x * 50 - 5, y * 50 - 5, 3)
        end
      end
    end       
    @finish.each { |c| c.draw } 
  end
  
  # Solid at a given pixel position?
  def solid?(x, y)
    y < 0 || @tiles[x / 50][y / 50]
  end  

end