Class: RoadGenerator
- Inherits:
-
Object
- Object
- RoadGenerator
- Defined in:
- lib/road_generator.rb
Overview
Generates roads across map tiles, randomly or given specific coordinates
Instance Attribute Summary collapse
-
#finder ⇒ Object
readonly
Returns the value of attribute finder.
-
#grid ⇒ Object
readonly
Returns the value of attribute grid.
Instance Method Summary collapse
- #generate_num_of_random_roads(config) ⇒ Object
- #generate_path(start_x, start_y, end_x, end_y) ⇒ Object
- #generate_roads_from_coordinate_list(road_paths, verbose) ⇒ Object
-
#initialize(tiles) ⇒ RoadGenerator
constructor
A new instance of RoadGenerator.
Constructor Details
#initialize(tiles) ⇒ RoadGenerator
Returns a new instance of RoadGenerator.
12 13 14 15 |
# File 'lib/road_generator.rb', line 12 def initialize(tiles) @grid = Pathfinding::Grid.new(tiles) @finder = Pathfinding::AStarFinder.new end |
Instance Attribute Details
#finder ⇒ Object (readonly)
Returns the value of attribute finder.
10 11 12 |
# File 'lib/road_generator.rb', line 10 def finder @finder end |
#grid ⇒ Object (readonly)
Returns the value of attribute grid.
10 11 12 |
# File 'lib/road_generator.rb', line 10 def grid @grid end |
Instance Method Details
#generate_num_of_random_roads(config) ⇒ Object
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/road_generator.rb', line 17 def generate_num_of_random_roads(config) return if config.roads <= 0 puts "generating #{config.roads} random roads..." if config.verbose seed = config.road_seed (1..config.roads).each do |n| puts "generating road #{n}..." if config.verbose random_objects_at_edges = random_nodes_not_on_same_edge(seed + n) # add n otherwise each road is the same generate_path( random_objects_at_edges[0].x, random_objects_at_edges[0].y, random_objects_at_edges[1].x, random_objects_at_edges[1].y ).each(&:make_road) end end |
#generate_path(start_x, start_y, end_x, end_y) ⇒ Object
50 51 52 53 54 |
# File 'lib/road_generator.rb', line 50 def generate_path(start_x, start_y, end_x, end_y) start_node = grid.node(start_x, start_y) end_node = grid.node(end_x, end_y) finder.find_path(start_node, end_node, grid) end |
#generate_roads_from_coordinate_list(road_paths, verbose) ⇒ Object
35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/road_generator.rb', line 35 def generate_roads_from_coordinate_list(road_paths, verbose) return unless (road_paths.length % 4).zero? puts "generating #{road_paths.length / 4} coordinate roads..." if verbose road_paths.each_slice(4) do |road_coordinates| generate_path( road_coordinates[0], road_coordinates[1], road_coordinates[2], road_coordinates[3] ).each(&:make_road) end end |