-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathmap.rb
More file actions
43 lines (38 loc) · 1.08 KB
/
map.rb
File metadata and controls
43 lines (38 loc) · 1.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
class Map
attr_reader :width, :height, :gems
def initialize(window, filename)
# Load 60x60 tiles, 5px overlap in all four directions.
@tileset = Image.load_tiles(window, "images/tileset.png", 60, 60, true)
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::Earth
end
end
end
end
def draw
# Very primitive drawing function:
# 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, 0)
end
end
end
end
# Solid at a given pixel position?
def solid?(x, y)
y < 0 || @tiles[x / 50][y / 50]
end
end