Class: Gamefic::Standard::Pathfinder

Inherits:
Object
  • Object
show all
Defined in:
lib/gamefic/standard/pathfinder.rb

Overview

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(origin, destination) ⇒ Pathfinder

Returns a new instance of Pathfinder.

Parameters:



19
20
21
22
23
24
25
26
27
28
# File 'lib/gamefic/standard/pathfinder.rb', line 19

def initialize(origin, destination)
  @origin = origin
  @destination = destination
  @visited = []
  if origin == destination
    @path = []
  else
    embark [[origin]]
  end
end

Instance Attribute Details

#destinationRoom (readonly)

Returns:



15
16
17
# File 'lib/gamefic/standard/pathfinder.rb', line 15

def destination
  @destination
end

#originRoom (readonly)

Returns:



12
13
14
# File 'lib/gamefic/standard/pathfinder.rb', line 12

def origin
  @origin
end

Instance Method Details

#pathArray<Room>

An array of rooms starting with the first room after the origin and ending with the destination.

The path will be empty if a path could not be found or the origin and destination are the same room.

Returns:



37
38
39
# File 'lib/gamefic/standard/pathfinder.rb', line 37

def path
  @path || []
end

#valid?Boolean

True if there is a valid path from the origin to the destination (or the origin and destination are the same room).

Returns:

  • (Boolean)


44
45
46
# File 'lib/gamefic/standard/pathfinder.rb', line 44

def valid?
  !!@path
end