class Pathfinder

Pathfinders provide the shortest route between two locations. The destination needs to be accessible from the origin through portals. Note that Pathfinders do not take into account portals that characters should not be able to traverse, such as locked doors.

Attributes

destination[R]

@return [Room]

origin[R]

@return [Room]

Public Class Methods

new(origin, destination) click to toggle source
# File lib/gamefic-standard/pathfinder.rb, line 12
def initialize origin, destination
  @origin = origin
  @destination = destination
  @path = nil
  @paths = [[@origin]]
  @visited = []
  if @origin == @destination
    @path = []
  else
    embark while @path.nil? && @paths.length > 0
  end
end

Public Instance Methods

path() click to toggle source

@return [Array<Room>]

# File lib/gamefic-standard/pathfinder.rb, line 26
def path
  # @path is nil if the path is invalid, but #path should return an empty
  # array instead.
  @path || []
end
valid?() click to toggle source

@return [Boolean]

# File lib/gamefic-standard/pathfinder.rb, line 33
def valid?
  path.length > 0 || origin == destination
end

Private Instance Methods

embark() click to toggle source
# File lib/gamefic-standard/pathfinder.rb, line 39
def embark
  new_paths = []
  @paths.each { |path|
    last = path.last
    portals = last.children.that_are(Portal)
    portals.each { |portal|
      new_path = path.clone
      if !@visited.include?(portal.destination)
        new_path.push portal.destination
        @visited.push portal.destination
        if portal.destination == @destination
          @path = new_path
          @path.shift
          break
        end
        new_paths.push new_path
      end
    }
    path.push nil
  }
  @paths += new_paths
  @paths.delete_if{|path| path.last.nil?}
end