Class: Tile
- Inherits:
-
Object
- Object
- Tile
- Defined in:
- lib/tile.rb
Constant Summary collapse
- TYPES =
%i[ terrain road ].freeze
Instance Attribute Summary collapse
-
#height ⇒ Object
readonly
Returns the value of attribute height.
-
#map ⇒ Object
readonly
Returns the value of attribute map.
-
#moist ⇒ Object
readonly
Returns the value of attribute moist.
-
#temp ⇒ Object
readonly
Returns the value of attribute temp.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
-
#x ⇒ Object
readonly
Returns the value of attribute x.
-
#y ⇒ Object
readonly
Returns the value of attribute y.
Instance Method Summary collapse
- #add_flora ⇒ Object
- #add_item(tile_item) ⇒ Object
- #add_town_item(seed) ⇒ Object
- #biome ⇒ Object
- #can_haz_road? ⇒ Boolean
- #can_haz_town? ⇒ Boolean
-
#initialize(map:, x:, y:, height: 0, moist: 0, temp: 0, type: :terrain) ⇒ Tile
constructor
A new instance of Tile.
- #item_with_highest_priority ⇒ Object
- #items ⇒ Object
- #items_contain?(item_class) ⇒ Boolean
- #items_contain_building? ⇒ Boolean
- #items_contain_flora? ⇒ Boolean
- #make_road ⇒ Object
- #path_heuristic ⇒ Object
- #render_to_standard_output ⇒ Object
- #road? ⇒ Boolean
- #surrounding_tiles(distance = 1) ⇒ Object
- #to_h ⇒ Object
Constructor Details
#initialize(map:, x:, y:, height: 0, moist: 0, temp: 0, type: :terrain) ⇒ Tile
Returns a new instance of Tile.
17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/tile.rb', line 17 def initialize(map:, x:, y:, height: 0, moist: 0, temp: 0, type: :terrain) @x = x @y = y @height = height @moist = moist @temp = temp @map = map raise ArgumentError, 'invalid tile type' unless TYPES.include?(type) @type = type end |
Instance Attribute Details
#height ⇒ Object (readonly)
Returns the value of attribute height.
10 11 12 |
# File 'lib/tile.rb', line 10 def height @height end |
#map ⇒ Object (readonly)
Returns the value of attribute map.
10 11 12 |
# File 'lib/tile.rb', line 10 def map @map end |
#moist ⇒ Object (readonly)
Returns the value of attribute moist.
10 11 12 |
# File 'lib/tile.rb', line 10 def moist @moist end |
#temp ⇒ Object (readonly)
Returns the value of attribute temp.
10 11 12 |
# File 'lib/tile.rb', line 10 def temp @temp end |
#type ⇒ Object (readonly)
Returns the value of attribute type.
10 11 12 |
# File 'lib/tile.rb', line 10 def type @type end |
#x ⇒ Object (readonly)
Returns the value of attribute x.
10 11 12 |
# File 'lib/tile.rb', line 10 def x @x end |
#y ⇒ Object (readonly)
Returns the value of attribute y.
10 11 12 |
# File 'lib/tile.rb', line 10 def y @y end |
Instance Method Details
#add_flora ⇒ Object
113 114 115 |
# File 'lib/tile.rb', line 113 def add_flora add_item(biome.flora) end |
#add_item(tile_item) ⇒ Object
54 55 56 57 58 |
# File 'lib/tile.rb', line 54 def add_item(tile_item) raise ArgumentError, 'item should be a tile' unless tile_item.is_a?(TileItem) items.push(tile_item) end |
#add_town_item(seed) ⇒ Object
89 90 91 |
# File 'lib/tile.rb', line 89 def add_town_item(seed) add_item(Building.random_town_building(seed)) end |
#biome ⇒ Object
41 42 43 |
# File 'lib/tile.rb', line 41 def biome @biome ||= Biome.from(height, moist, temp) end |
#can_haz_road? ⇒ Boolean
109 110 111 |
# File 'lib/tile.rb', line 109 def can_haz_road? true unless biome_is_water_and_is_excluded? || biome_is_high_mountain_and_is_excluded? || tile_contains_flora_and_is_excluded? || items_contain_building? end |
#can_haz_town? ⇒ Boolean
105 106 107 |
# File 'lib/tile.rb', line 105 def can_haz_town? !road? && !biome.water? && !biome.high_mountain? && !items_contain_flora? end |
#item_with_highest_priority ⇒ Object
60 61 62 |
# File 'lib/tile.rb', line 60 def item_with_highest_priority items.max_by(&:render_priority) end |
#items ⇒ Object
45 46 47 |
# File 'lib/tile.rb', line 45 def items @items ||= [] end |
#items_contain?(item_class) ⇒ Boolean
72 73 74 |
# File 'lib/tile.rb', line 72 def items_contain?(item_class) items.any? { |i| i.is_a?(item_class) } end |
#items_contain_building? ⇒ Boolean
68 69 70 |
# File 'lib/tile.rb', line 68 def items_contain_building? items_contain?(Building) end |
#items_contain_flora? ⇒ Boolean
64 65 66 |
# File 'lib/tile.rb', line 64 def items_contain_flora? items_contain?(Flora) end |
#make_road ⇒ Object
93 94 95 |
# File 'lib/tile.rb', line 93 def make_road @type = :road end |
#path_heuristic ⇒ Object
101 102 103 |
# File 'lib/tile.rb', line 101 def path_heuristic height end |
#render_to_standard_output ⇒ Object
49 50 51 52 |
# File 'lib/tile.rb', line 49 def render_to_standard_output print render_color_by_type + (!items.empty? ? item_with_highest_priority.render_symbol : ' ') print AnsiColours::Background::ANSI_RESET end |
#road? ⇒ Boolean
97 98 99 |
# File 'lib/tile.rb', line 97 def road? @type == :road end |
#surrounding_tiles(distance = 1) ⇒ Object
29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/tile.rb', line 29 def surrounding_tiles(distance = 1) @surround_cache ||= {} @surround_cache[distance] ||= begin left_limit = [0, x - distance].max top_limit = [0, y - distance].max map.tiles[left_limit..(x + distance)].map do |r| r[top_limit..(y + distance)] end.flatten end end |
#to_h ⇒ Object
76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/tile.rb', line 76 def to_h { x: x, y: y, height: height, moist: moist, temp: temp, biome: biome.to_h, items: items.map(&:to_h), type: type } end |