Class: TownGenerator

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

Overview

Generates building tile items using Poisson Disk Sampling for the given tiles Roads are generated between the buildings and between towns using A* pathfinding and a minimum tree spanning algorithm

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(tiles, seed: MapConfig::DEFAULT_TOWN_SEED) ⇒ TownGenerator

Returns a new instance of TownGenerator.



16
17
18
19
20
21
# File 'lib/town_generator.rb', line 16

def initialize(tiles, seed: MapConfig::DEFAULT_TOWN_SEED)
  @sample_area = PoissonDiskSampling::SampleArea.new(grid: tiles)
  @road_generator = RoadGenerator.new(tiles)
  @seed = seed
  @all_town_points = []
end

Instance Attribute Details

#road_generatorObject (readonly)

Returns the value of attribute road_generator.



14
15
16
# File 'lib/town_generator.rb', line 14

def road_generator
  @road_generator
end

#sample_areaObject (readonly)

Returns the value of attribute sample_area.



14
15
16
# File 'lib/town_generator.rb', line 14

def sample_area
  @sample_area
end

Instance Method Details

#generate_random_towns(config) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
# File 'lib/town_generator.rb', line 23

def generate_random_towns(config)
  return if config.towns <= 0

  puts "generating #{config.towns} random towns..." if config.verbose

  @all_town_points.concat(iterate_through_towns(config.towns) do |n|
    generate_random_town(n, config.verbose)
  end)

  generate_roads_between_towns(config.verbose)
end

#generate_towns_from_coordinate_list(config) ⇒ Object



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

def generate_towns_from_coordinate_list(config)
  return unless (config.towns_to_make.length % 4).zero?

  puts "generating #{config.towns_to_make.length / 4} coordinate towns..." if config.verbose

  @all_town_points.concat(iterate_through_towns((config.towns_to_make.length / 4)) do |n|
    town_values = config.towns_to_make[(n - 1) * 4..].take(4)
    generate_town(n, town_values[2], town_values[3], [sample_area[town_values[0], town_values[1]]], config.verbose)
  end)

  generate_roads_between_towns(config.verbose)
end