-
Notifications
You must be signed in to change notification settings - Fork 0
/
hex.js
189 lines (167 loc) · 4.97 KB
/
hex.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
const EMPTY = 0;
const START = 1;
const END = 2;
const OPEN = 3;
const CLOSED = 4;
const PATH = 5;
const WALL = 6;
class Hex {
constructor(x, y, q, r) {
// Defining costs here so they show up first
// when I debug.log the object
this.f_cost = undefined;
this.h_cost = undefined;
this.g_cost = undefined;
this.x = x; // Pixel x
this.y = y; // Pixel y
this.q = q; // Grid column
this.r = r; // Grid row
this.s = -q - r; // Cubic third dimension
this.state = EMPTY;
}
onClick() {
switch (currentTool) {
case 0: // Walls
if (this.state == WALL) {
this.state = EMPTY;
} else {
this.state = WALL;
}
break;
case 1: // Start
this.state = START;
start.state = EMPTY;
start.f_cost = undefined;
start.h_cost = undefined;
start.g_cost = undefined;
openHexes = openHexes.filter((hex) => {
return hex !== start;
});
start = this;
this.update(start, end);
this.open();
break;
case 2: // End
this.state = END;
end.state = EMPTY;
end.f_cost = undefined;
end.h_cost = undefined;
end.g_cost = undefined;
end = this;
break;
}
}
show() {
push();
translate(this.x, this.y);
scale(zoom);
if (this.state == EMPTY) {
// TODO: Figure out why this is more
// saturated on firefox than chrome
fill("yellow");
} else if (this.state == START) {
fill("white");
} else if (this.state == END) {
fill("#FF7A60");
} else if (this.state == OPEN) {
fill("#61B8FF");
} else if (this.state == CLOSED) {
fill("#7AD9FF");
} else if (this.state == PATH) {
fill("green");
} else if (this.state == WALL) {
fill("grey");
}
// Hexagon
beginShape();
vertex(-130, -75);
vertex(-130, 75);
vertex(0, 150);
vertex(130, 75);
vertex(130, -75);
vertex(0, -150);
endShape(CLOSE);
pop();
}
showStats() {
push();
translate(this.x, this.y);
scale(zoom);
// Co-ordinates
textCentered(`${this.q}, ${this.r}, ${this.s}`, 0, 80); // Whitespace is using custom character
// Label
if (this.state == START) {
textCentered("Start", 0, 0);
} else if (this.state == END) {
textCentered("End", 0, 0);
}
// Costs
if (this.g_cost != "undefined") {
textCentered(this.g_cost, -60, -60);
}
if (this.h_cost != "undefined") {
textCentered(this.h_cost, 60, -60);
}
if (this.f_cost != "undefined") {
textCentered(this.f_cost, 0, -80);
}
pop();
}
getNeighbors() {
let neighbors = [];
// Check that column to the left exists
if (grid[this.q + 1] != null) {
neighbors.push(
...[
grid[this.q + 1][this.r + 0], // Right
grid[this.q + 1][this.r - 1], // Top Right
]
);
}
// We already know this column exists
neighbors.push(
...[
grid[this.q + 0][this.r + 1], // Bot Right
grid[this.q + 0][this.r - 1], // Top Left
]
);
// Check that column to the right exists
if (grid[this.q - 1] != null) {
neighbors.push(
...[
grid[this.q - 1][this.r + 0], // Left
grid[this.q - 1][this.r + 1], // Bot Left
]
);
}
// Remove undefineds
neighbors = neighbors.filter(Boolean);
return neighbors;
}
tracePath() {
if (this.state == START) return;
if (this.parent) {
this.parent.state = PATH;
if (this.parent.parent.state != START) this.parent.tracePath();
}
}
update(start, end) {
this.g_cost = this.distance(start);
this.h_cost = this.distance(end);
this.f_cost = this.g_cost + this.h_cost;
}
open() {
if (this.state != START && this.state != END) this.state = OPEN;
openHexes.push(this);
}
close() {
if (this.state != START && this.state != END) this.state = CLOSED;
openHexes = openHexes.filter((hex) => {
return hex !== this;
});
closedHexes.push(this);
}
distance(target) {
return (abs(this.q - target.q) + abs(this.r - target.r) + abs(this.s - target.s)) / 2;
}
}