Skip to content

Instantly share code, notes, and snippets.

@apeiros
Last active June 8, 2018 18:04
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save apeiros/5571a69fc5065e2fec4e0582b611c670 to your computer and use it in GitHub Desktop.
Save apeiros/5571a69fc5065e2fec4e0582b611c670 to your computer and use it in GitHub Desktop.
class Map
TileSetPath = "../data/graphics/texturepack_dungeon.png"
TileSetSize = 32
def initialize(f_map)
puts "Loading map from #{f_map}.."
@tileset = Gosu::Image.load_tiles(TileSetPath, TileSetSize, TileSetSize)
@tile_size = TileSetSize
@tiles = File.readlines(f_map, chomp: true).map.with_index { |line, y|
line.chars.each_slice(2).map.with_index { |(char, _), x|
Tile.new(x * @tile_size, y * @tile_size, @tileset, char)
}
}
@width = @tiles.first.size
@height = @tiles.size
puts "\n" + 'width: ' + @width.to_s + ', height: ' + @height.to_s
puts 'Test data: ' + @tiles[0, 1].get_code
end
def tile_at(x, y)
@tiles.dig(y, x)
end
# Render whole map
def draw
@tiles.each do |row|
row.each(&:draw)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment