Class: Node

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

Overview

The Node class represents the squares of the grid.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(window, x, y, width, height) ⇒ Node

Creates a node. It is composed of the (x, y) position in the grid and the neighbors (list of nodes).



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/astar_visualizer/node.rb', line 28

def initialize(window, x, y, width, height)
  @@colors ||= {
    green: Gosu::Color.argb(0xff00ff00),
    red: Gosu::Color.argb(0xffff0000),
    grey: Gosu::Color.argb(0xff808080),
    lightcyan: Gosu::Color.argb(0xffe0ffff),
    cyan: Gosu::Color.argb(0xff00ffff),
    white: Gosu::Color.argb(0xffffffff),
    yellow: Gosu::Color.argb(0xffffff00),
  }
  @@window ||= window
  @x = x
  @y = y
  @width = width
  @height = height
  @color = @@colors[:white]
  @neighbors = []
end

Instance Attribute Details

#neighborsObject (readonly)

Gets the neighbors (list of nodes).



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

def neighbors
  @neighbors
end

#xObject (readonly)

Gets the x position in the grid.



16
17
18
# File 'lib/astar_visualizer/node.rb', line 16

def x
  @x
end

#yObject (readonly)

Gets the y position in the grid.



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

def y
  @y
end

Instance Method Details

#add_to_neighbors(node) ⇒ Object

Adds a node to the neighbors list.



151
152
153
# File 'lib/astar_visualizer/node.rb', line 151

def add_to_neighbors(node)
  @neighbors << node
end

#closed!Object

Makes a node like in the closed list (lightcyan color).



113
114
115
# File 'lib/astar_visualizer/node.rb', line 113

def closed!
  @color = @@colors[:lightcyan]
end

#closed?Boolean

Returns if the node is in the closed list (lightcyan color).

Returns:

  • (Boolean)


106
107
108
# File 'lib/astar_visualizer/node.rb', line 106

def closed?
  @color == @@colors[:lightcyan]
end

#drawObject

Draws the square.



134
135
136
# File 'lib/astar_visualizer/node.rb', line 134

def draw
  @@window.draw_rect(@x * @width, @y * @height, @width, @height, @color)
end

#end!Object

Makes a node the end point (red color).



71
72
73
# File 'lib/astar_visualizer/node.rb', line 71

def end!
  @color = @@colors[:red]
end

#end?Boolean

Returns if the node is the end point (red color).

Returns:

  • (Boolean)


64
65
66
# File 'lib/astar_visualizer/node.rb', line 64

def end?
  @color == @@colors[:red]
end

#inside?(mouse_x, mouse_y) ⇒ Boolean

Returns if the mouse position is in the square.

Returns:

  • (Boolean)


141
142
143
144
145
146
# File 'lib/astar_visualizer/node.rb', line 141

def inside?(mouse_x, mouse_y)
  pos_x = @x * @width
  pos_y = @y * @height
  mouse_x >= pos_x && mouse_x <= pos_x + @width && \
    mouse_y >= pos_y && mouse_y <= pos_y + @height
end

#obstacle!Object

Makes a node an obstacle (grey color).



85
86
87
# File 'lib/astar_visualizer/node.rb', line 85

def obstacle!
  @color = @@colors[:grey]
end

#obstacle?Boolean

Returns if the node is an obstacle (grey color).

Returns:

  • (Boolean)


78
79
80
# File 'lib/astar_visualizer/node.rb', line 78

def obstacle?
  @color == @@colors[:grey]
end

#open!Object

Makes a node like in the open list (cyan color).



99
100
101
# File 'lib/astar_visualizer/node.rb', line 99

def open!
  @color = @@colors[:cyan]
end

#open?Boolean

Returns if the node is in the open list (cyan color).

Returns:

  • (Boolean)


92
93
94
# File 'lib/astar_visualizer/node.rb', line 92

def open?
  @color == @@colors[:cyan]
end

#path!Object

Makes a node in the found path (yellow color).



127
128
129
# File 'lib/astar_visualizer/node.rb', line 127

def path!
  @color = @@colors[:yellow]
end

#reset!Object

Resets a node (white color).



120
121
122
# File 'lib/astar_visualizer/node.rb', line 120

def reset!
  @color = @@colors[:white]
end

#start!Object

Makes a node the start point (green color).



57
58
59
# File 'lib/astar_visualizer/node.rb', line 57

def start!
  @color = @@colors[:green]
end

#start?Boolean

Returns if the node is the start point (green color).

Returns:

  • (Boolean)


50
51
52
# File 'lib/astar_visualizer/node.rb', line 50

def start?
  @color == @@colors[:green]
end