Class: FloraGenerator

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

Overview

Generates flora for the given tiles

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(tiles) ⇒ FloraGenerator

Returns a new instance of FloraGenerator.



9
10
11
# File 'lib/flora_generator.rb', line 9

def initialize(tiles)
  @tiles = tiles
end

Instance Attribute Details

#tilesObject (readonly)

Returns the value of attribute tiles.



7
8
9
# File 'lib/flora_generator.rb', line 7

def tiles
  @tiles
end

Instance Method Details

#generate(config) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/flora_generator.rb', line 13

def generate(config)
  return unless config.generate_flora

  puts 'generating flora...' if config.verbose
  tiles.each do |row|
    row.each do |tile|
      next unless tile.biome.flora_available

      range_max_value = tiles[(tile.y - tile.biome.flora_range)...(tile.y + tile.biome.flora_range)]&.map do |r|
        r[(tile.x - tile.biome.flora_range)...(tile.x + tile.biome.flora_range)]
      end&.flatten&.map(&:height)&.max
      tile.add_flora if range_max_value == tile.height
    end
  end
end