-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwall.js
66 lines (59 loc) · 1.34 KB
/
wall.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
// An object to describe a spot in the grid
function Spot(i, j) {
// Location
this.i = i;
this.j = j;
// f, g, and h values for A*
this.f = 0;
this.g = 0;
this.h = 0;
// Neighbors
this.neighbors = [];
// Where did I come from?
this.previous = undefined;
// Am I a wall?
this.wall = false;
if (random(1) < 0.4) {
this.wall = true;
}
// Display me
this.show = function(col) {
if (this.wall) {
fill(0);
noStroke();
ellipse(this.i * w + w / 2, this.j * h + h / 2, w / 2, h / 2);
} else if (col) {
fill(col);
rect(this.i * w, this.j * h, w, h);
}
}
// Figure out who my neighbors are
this.addNeighbors = function(grid) {
var i = this.i;
var j = this.j;
if (i < cols - 1) {
this.neighbors.push(grid[i + 1][j]);
}
if (i > 0) {
this.neighbors.push(grid[i - 1][j]);
}
if (j < rows - 1) {
this.neighbors.push(grid[i][j + 1]);
}
if (j > 0) {
this.neighbors.push(grid[i][j - 1]);
}
if (i > 0 && j > 0) {
this.neighbors.push(grid[i - 1][j - 1]);
}
if (i < cols - 1 && j > 0) {
this.neighbors.push(grid[i + 1][j - 1]);
}
if (i > 0 && j < rows - 1) {
this.neighbors.push(grid[i - 1][j + 1]);
}
if (i < cols - 1 && j < rows - 1) {
this.neighbors.push(grid[i + 1][j + 1]);
}
}
}