Class: Astar::FindPath
- Inherits:
-
Object
- Object
- Astar::FindPath
- Defined in:
- lib/astar.rb
Class Method Summary collapse
Instance Method Summary collapse
- #add_neighbours_to_list(current_node, neighbours) ⇒ Object
- #calculate_fastest ⇒ Object
- #calculate_route ⇒ Object
- #destination_reached? ⇒ Boolean
- #to(node) ⇒ Object
Class Method Details
.from(node) ⇒ Object
6 7 8 |
# File 'lib/astar.rb', line 6 def self.from(node) new(node) end |
Instance Method Details
#add_neighbours_to_list(current_node, neighbours) ⇒ Object
27 28 29 30 31 |
# File 'lib/astar.rb', line 27 def add_neighbours_to_list(current_node, neighbours) nodes = neighbours.map {|n| Node.new(n, @to_node, current_node) } @open_list << nodes @open_list.flatten! end |
#calculate_fastest ⇒ Object
33 34 35 |
# File 'lib/astar.rb', line 33 def calculate_fastest @open_list.sort_by! {|node| node.score }.reverse end |
#calculate_route ⇒ Object
16 17 18 19 20 21 22 23 24 25 |
# File 'lib/astar.rb', line 16 def calculate_route until destination_reached? || @open_list.empty? current_node = @open_list.pop next if @closed_list.include?(current_node) add_neighbours_to_list(current_node, current_node.walkable_neighbours) calculate_fastest @closed_list << current_node end @closed_list end |
#destination_reached? ⇒ Boolean
37 38 39 |
# File 'lib/astar.rb', line 37 def destination_reached? @closed_list.any? { |node| node.x == @to_node.x && node.y == @to_node.y } end |