Class: Natural20::MapRenderer

Inherits:
Object
  • Object
show all
Defined in:
lib/natural_20/cli/map_renderer.rb

Constant Summary collapse

DEFAULT_TOKEN_COLOR =
:cyan

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(map, battle = nil) ⇒ MapRenderer



7
8
9
10
# File 'lib/natural_20/cli/map_renderer.rb', line 7

def initialize(map, battle = nil)
  @map = map
  @battle = battle
end

Instance Attribute Details

#battleObject (readonly)

Returns the value of attribute battle.



4
5
6
# File 'lib/natural_20/cli/map_renderer.rb', line 4

def battle
  @battle
end

#mapObject (readonly)

Returns the value of attribute map.



4
5
6
# File 'lib/natural_20/cli/map_renderer.rb', line 4

def map
  @map
end

Instance Method Details

#render(entity: nil, line_of_sight: nil, path: [], acrobatics_checks: [], athletics_checks: [], select_pos: nil, update_on_drop: true, path_char: nil, highlight: {}, viewport_size: nil, top_position: [ 0, 0 ]) ⇒ String

Renders the current map to a string



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
44
45
46
47
48
# File 'lib/natural_20/cli/map_renderer.rb', line 18

def render(entity: nil, line_of_sight: nil, path: [], acrobatics_checks: [], athletics_checks: [], select_pos: nil, update_on_drop: true, path_char: nil, highlight: {}, viewport_size: nil, top_position: [
  0, 0
])
  highlight_positions = highlight.keys.map { |entity| @map.entity_squares(entity) }.flatten(1)

  viewport_size ||= map.size

  top_x, top_y = top_position
  top_x = map.size[0] - top_x < viewport_size[0] ? map.size[0] - viewport_size[0] : top_x if viewport_size
  top_y = map.size[0] - top_y < viewport_size[1] ? map.size[1] - viewport_size[1] : top_y if viewport_size

  right_x = top_x + viewport_size[0] >= map.size[1] ? map.size[1] - 1 : top_x + viewport_size[0]
  right_y = top_y + viewport_size[1] >= map.size[0] ? map.size[0] - 1 : top_y + viewport_size[1]

  (top_x..right_x).map do |row_index|
    (top_y..right_y).map do |col_index|
      c = map.base_map[col_index][row_index]
      display = render_position(c, col_index, row_index, path: path, override_path_char: path_char, entity: entity, line_of_sight: line_of_sight,
                                                         update_on_drop: update_on_drop, acrobatics_checks: acrobatics_checks,
                                                         athletics_checks: athletics_checks)

      display = display.colorize(background: :red) if highlight_positions.include?([col_index, row_index])

      if select_pos && select_pos == [col_index, row_index]
        display.colorize(background: :white)
      else
        display
      end
    end.join
  end.join("\n") + "\n"
end