Class: Astar::FindPath

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

Class Method Summary collapse

Instance Method Summary collapse

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_fastestObject



33
34
35
# File 'lib/astar.rb', line 33

def calculate_fastest
  @open_list.sort_by! {|node| node.score }.reverse
end

#calculate_routeObject



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

Returns:

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

#to(node) ⇒ Object



10
11
12
13
14
# File 'lib/astar.rb', line 10

def to(node)
  @to_node = Node.new(node,nil)
  @open_list << Node.new(@from_node, @to_node)
  calculate_route
end