Class: Grid

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

Overview

The Grid class represents the grid. It is composed of nodes.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(window, cols, rows, size) ⇒ Grid

Creates the grid with the nodes.



33
34
35
36
37
38
39
40
41
# File 'lib/astar_visualizer/grid.rb', line 33

def initialize(window, cols, rows, size)
  @window = window
  @cols = cols
  @rows = rows
  @width = size / cols    # width of a node's square
  @height = size / rows   # height of a node's square
  @nodes = Grid.build_nodes(window, cols, rows, @width, @height)
  @color = Gosu::Color.argb(0xaad3d3d3)
end

Class Method Details

.build_nodes(window, cols, rows, width, height) ⇒ Object

Builds the nodes of the grid. It returns the list of nodes.



19
20
21
22
23
24
25
26
27
28
# File 'lib/astar_visualizer/grid.rb', line 19

def self.build_nodes(window, cols, rows, width, height)
  nodes = []
  rows.times do |y|
    nodes << []
    cols.times do |x|
      nodes[y] << Node.new(window, x, y, width, height)
    end
  end
  nodes
end

Instance Method Details

#drawObject

Draws the nodes and the grid.



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

def draw
  each_node(&:draw)
  draw_grid
end

#draw_gridObject

Draws the lines of the grid.



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/astar_visualizer/grid.rb', line 100

def draw_grid
  @rows.times do |i|  # horizontal
    x1 = 0
    y1 = @height * i
    x2 = @width * @cols
    y2 = @height * i
    @window.draw_line(x1, y1, @color, x2, y2, @color)
  end
  @cols.times do |i|  # vertical
    x1 = @width * i
    y1 = 0
    x2 = @width * i
    y2 = @height * @rows
    @window.draw_line(x1, y1, @color, x2, y2, @color)
  end
end

#each_nodeObject

Yields all nodes of the grid.



53
54
55
56
57
58
59
# File 'lib/astar_visualizer/grid.rb', line 53

def each_node
  @rows.times do |y|
    @cols.times do |x|
      yield node(x, y)
    end
  end
end

#find_node(mouse_x, mouse_y) ⇒ Object

Finds a node in the grid using mouse position.



128
129
130
131
132
133
# File 'lib/astar_visualizer/grid.rb', line 128

def find_node(mouse_x, mouse_y)
  each_node do |node|
    return node if node.inside?(mouse_x, mouse_y)
  end
  nil
end

#inside?(x, y) ⇒ Boolean

Returns if the (x, y) position is in the grid.

Returns:

  • (Boolean)


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

def inside?(x, y)
  x >= 0 && x < @cols && y >= 0 && y < @rows
end

#node(x, y) ⇒ Object

Returns a node from the grid.



46
47
48
# File 'lib/astar_visualizer/grid.rb', line 46

def node(x, y)
  @nodes[y][x]
end

#reset!Object

Resets all the nodes.



93
94
95
# File 'lib/astar_visualizer/grid.rb', line 93

def reset!
  each_node(&:reset!)
end

#update_neighborsObject

Updates the neighbors of all nodes.



78
79
80
81
82
83
84
85
86
87
88
# File 'lib/astar_visualizer/grid.rb', line 78

def update_neighbors
  each_node do |node|
    x = node.x
    y = node.y
    node.neighbors.clear
    node.add_to_neighbors(node(x, y - 1)) if walkable?(x, y - 1)  # ↑
    node.add_to_neighbors(node(x + 1, y)) if walkable?(x + 1, y)  # →
    node.add_to_neighbors(node(x, y + 1)) if walkable?(x, y + 1)  # ↓
    node.add_to_neighbors(node(x - 1, y)) if walkable?(x - 1, y)  # ←
  end
end

#walkable?(x, y) ⇒ Boolean

Returns if a node is walkable.

Returns:

  • (Boolean)


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

def walkable?(x, y)
  inside?(x, y) && !node(x, y).obstacle?
end