Class: Map

Inherits:
Object
  • Object
show all
Defined in:
lib/map.rb

Constant Summary collapse

SOLID =
[1, 2, 3, 4, 5, 10]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ Map

Returns a new instance of Map.



5
6
7
8
9
10
# File 'lib/map.rb', line 5

def initialize(path)
  @path   = path
  @t_size = Processor::TileSize
  @set    = Gosu::Image.load_tiles(Processor.window, BLAST_IMG_PATH + 'tileset.png', @t_size, @t_size, true)
  register_solid_tiles
end

Instance Attribute Details

#solid_tilesObject

Returns the value of attribute solid_tiles.



2
3
4
# File 'lib/map.rb', line 2

def solid_tiles
  @solid_tiles
end

Instance Method Details

#draw(screen_x, screen_y) ⇒ Object



12
13
14
15
16
17
18
# File 'lib/map.rb', line 12

def draw(screen_x, screen_y)
  tiles.each_with_index do |row_array, y|
    row_array.each_with_index do |col, x|
      @set[col].draw(x * @t_size, y * @t_size, 0) #z-order is 0
    end
  end
end

#legendObject



44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/map.rb', line 44

def legend
  {
    '.'  => 0,
    'x'  => 10,
    ']'  => 2,
    '-'  => 3,
    '='  => 1,
    '['  => 5,
    '/'  => 6,
    '\\' => 7,
    '<'  => 8,
    '>'  => 9
    }
end

#register_solid_tilesObject



31
32
33
34
35
36
37
38
# File 'lib/map.rb', line 31

def register_solid_tiles
  @solid_tiles = []
  tiles.each_with_index do |row, y|
    row.each_with_index do |col, x|
      @solid_tiles << SolidTile.new(x*@t_size, y*@t_size) if SOLID.include?(col)
    end
  end
end

#solid_at?(x, y) ⇒ Boolean

Returns:

  • (Boolean)


40
41
42
# File 'lib/map.rb', line 40

def solid_at?(x,y)
  @solid_tiles.detect {|tile| tile.at?(x,y) } || x < 0 || y < 0
end

#tilesObject



20
21
22
23
24
25
26
27
28
29
# File 'lib/map.rb', line 20

def tiles
  @tiles ||= File.readlines(@path).inject([]) do |acc, line|
    tile = []
    line.strip.each_char do |char|
      tile << legend[char]
    end
    acc << tile
    acc
  end
end