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



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

def self.from(node)
  new(node)
end

Instance Method Details

#add_neighbours_to_list(current_node, neighbours) ⇒ Object



34
35
36
37
38
# File 'lib/astar.rb', line 34

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



40
41
42
# File 'lib/astar.rb', line 40

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

#calculate_routeObject



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

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)


44
45
46
# File 'lib/astar.rb', line 44

def destination_reached?
  @closed_list.any? { |node| node.x == @to_node.x && node.y == @to_node.y }
end

#to(node) ⇒ Object



13
14
15
16
17
# File 'lib/astar.rb', line 13

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

#use_euclidean_distanceObject



19
20
21
# File 'lib/astar.rb', line 19

def use_euclidean_distance
  @strategy = EuclideanDistance
end