Class: Map

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(map_config: MapConfig.new) ⇒ Map

Returns a new instance of Map.



12
13
14
# File 'lib/map.rb', line 12

def initialize(map_config: MapConfig.new)
  @config = map_config
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



10
11
12
# File 'lib/map.rb', line 10

def config
  @config
end

Instance Method Details

#[](x, y) ⇒ Object

rubocop:disable Naming/MethodParameterName:

Raises:

  • (ArgumentError)


28
29
30
31
32
# File 'lib/map.rb', line 28

def [](x, y)
  raise ArgumentError, 'coordinates out of bounds' if y.nil? || y >= tiles.size || x.nil? || x >= tiles[y].size

  tiles[y][x]
end

#describeObject



16
17
18
# File 'lib/map.rb', line 16

def describe
  tiles.map { |row| row.map(&:to_h) }
end

#renderObject



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

def render
  tiles.each do |row|
    row.each(&:render_to_standard_output)
    puts
  end
end

#tilesObject

rubocop:enable Naming/MethodParameterName:



35
36
37
38
39
40
41
42
43
44
# File 'lib/map.rb', line 35

def tiles
  return @tiles if @tiles

  generate_tiles
  generate_flora
  generate_roads
  generate_towns

  @tiles
end