Class: Astar::Node
- Inherits:
-
Object
- Object
- Astar::Node
- Defined in:
- lib/astar/node.rb
Instance Attribute Summary collapse
-
#parent ⇒ Object
readonly
Returns the value of attribute parent.
-
#tile ⇒ Object
readonly
Returns the value of attribute tile.
Instance Method Summary collapse
-
#initialize(tile, destination, parent = nil) ⇒ Node
constructor
A new instance of Node.
- #manhatten_distance ⇒ Object
- #relative_to_parent ⇒ Object
- #score ⇒ Object
- #walkable_neighbours ⇒ Object
- #x ⇒ Object
- #y ⇒ Object
Constructor Details
#initialize(tile, destination, parent = nil) ⇒ Node
Returns a new instance of Node.
4 5 6 7 8 |
# File 'lib/astar/node.rb', line 4 def initialize(tile, destination, parent=nil) @tile = tile @parent = parent @destination = destination end |
Instance Attribute Details
#parent ⇒ Object (readonly)
Returns the value of attribute parent.
3 4 5 |
# File 'lib/astar/node.rb', line 3 def parent @parent end |
#tile ⇒ Object (readonly)
Returns the value of attribute tile.
3 4 5 |
# File 'lib/astar/node.rb', line 3 def tile @tile end |
Instance Method Details
#manhatten_distance ⇒ Object
26 27 28 |
# File 'lib/astar/node.rb', line 26 def manhatten_distance (@destination.x - @tile.x) + (@destination.y - @tile.y) end |
#relative_to_parent ⇒ Object
30 31 32 33 34 35 36 |
# File 'lib/astar/node.rb', line 30 def relative_to_parent return 10 if (@tile.x > @parent.x && @tile.y == @parent.y) return 10 if (@tile.x < @parent.x && @tile.y == @parent.y) return 10 if (@tile.y > @parent.y && @tile.x == @parent.x) return 10 if (@tile.y < @parent.y && @tile.x == @parent.x) 14 end |
#score ⇒ Object
22 23 24 |
# File 'lib/astar/node.rb', line 22 def score relative_to_parent + manhatten_distance end |
#walkable_neighbours ⇒ Object
10 11 12 |
# File 'lib/astar/node.rb', line 10 def walkable_neighbours @tile.walkable_neighbours end |
#x ⇒ Object
14 15 16 |
# File 'lib/astar/node.rb', line 14 def x @tile.x end |
#y ⇒ Object
18 19 20 |
# File 'lib/astar/node.rb', line 18 def y @tile.y end |