Class: Astar::Node

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

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#parentObject (readonly)

Returns the value of attribute parent.



3
4
5
# File 'lib/astar/node.rb', line 3

def parent
  @parent
end

#tileObject (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_distanceObject



26
27
28
# File 'lib/astar/node.rb', line 26

def manhatten_distance
  (@destination.x - @tile.x) + (@destination.y - @tile.y)
end

#relative_to_parentObject



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

#scoreObject



22
23
24
# File 'lib/astar/node.rb', line 22

def score
  relative_to_parent + manhatten_distance
end

#walkable_neighboursObject



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

def walkable_neighbours
  @tile.walkable_neighbours
end

#xObject



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

def x
  @tile.x
end

#yObject



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

def y
  @tile.y
end