Class: Tile

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

Constant Summary collapse

TYPES =
%i[
  terrain
  road
].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(map:, x:, y:, height: 0, moist: 0, temp: 0, type: :terrain) ⇒ Tile

Returns a new instance of Tile.

Raises:

  • (ArgumentError)


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

#heightObject (readonly)

Returns the value of attribute height.



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

def height
  @height
end

#mapObject (readonly)

Returns the value of attribute map.



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

def map
  @map
end

#moistObject (readonly)

Returns the value of attribute moist.



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

def moist
  @moist
end

#tempObject (readonly)

Returns the value of attribute temp.



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

def temp
  @temp
end

#typeObject (readonly)

Returns the value of attribute type.



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

def type
  @type
end

#xObject (readonly)

Returns the value of attribute x.



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

def x
  @x
end

#yObject (readonly)

Returns the value of attribute y.



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

def y
  @y
end

Instance Method Details

#add_floraObject



113
114
115
# File 'lib/tile.rb', line 113

def add_flora
  add_item(biome.flora)
end

#add_item(tile_item) ⇒ Object

Raises:

  • (ArgumentError)


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

#biomeObject



41
42
43
# File 'lib/tile.rb', line 41

def biome
  @biome ||= Biome.from(height, moist, temp)
end

#can_haz_road?Boolean

Returns:

  • (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

Returns:

  • (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_priorityObject



60
61
62
# File 'lib/tile.rb', line 60

def item_with_highest_priority
  items.max_by(&:render_priority)
end

#itemsObject



45
46
47
# File 'lib/tile.rb', line 45

def items
  @items ||= []
end

#items_contain?(item_class) ⇒ Boolean

Returns:

  • (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

Returns:

  • (Boolean)


68
69
70
# File 'lib/tile.rb', line 68

def items_contain_building?
  items_contain?(Building)
end

#items_contain_flora?Boolean

Returns:

  • (Boolean)


64
65
66
# File 'lib/tile.rb', line 64

def items_contain_flora?
  items_contain?(Flora)
end

#make_roadObject



93
94
95
# File 'lib/tile.rb', line 93

def make_road
  @type = :road
end

#path_heuristicObject



101
102
103
# File 'lib/tile.rb', line 101

def path_heuristic
  height
end

#render_to_standard_outputObject



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

Returns:

  • (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_hObject



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