-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmaze.js
304 lines (271 loc) · 7.35 KB
/
maze.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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
// CONFIG - Play
var MULT = 6; //mini tile size
var scl = 20; //box size
var TILE_WIDTH = 5; //number of tiles
var initalFR = 15; //framerate
var xyProb = 100; //probability to move only x/y
// -------
var cols, rows;
var cellGrid = [];
var currentCell;
var pathStack = [];
var gridSize = scl * MULT;
var hOffset = gridSize + 5;
var vOffset = gridSize + 5;
var zaxis = TILE_WIDTH;
var qaxis = TILE_WIDTH;
var fSlider;
function setup(){
//add canvas
var canvas = createCanvas(hOffset*zaxis, vOffset*qaxis);
canvas.parent(gameContainer);
//framerate slider
fSlider = createSlider(1, 55, initalFR);
fSlider.position(90, height + 28);
textSize(15);
fill(0);
noStroke();
frameRate(fSlider.value());
cols = gridSize / scl//floor(width / scl);
rows = gridSize / scl //floor(height / scl);
//setup cells
for (var q = 0; q < qaxis; q++){
var qGrid = [];
for (var z = 0; z < zaxis; z++){
var tempGrid = [];
for (var j = 0; j < rows; j++){
for (var i = 0; i < cols; i++){
var cell = new Cell(i, j, z, q);
tempGrid.push(cell);
}
}
qGrid.push(tempGrid);
}
cellGrid.push(qGrid);
}
//initalise current random start cell
// var rq = floor(random(0,qaxis-1));
// var rz = floor(random(0,zaxis-1));
// var rindex = floor(random(0,24));
//console.log(rq,rz,rindex);
//currentCell = cellGrid[rq][rz][rindex];
//fixed start position
currentCell = cellGrid[3][3][13];
}
function draw(){
background(88);
//update framerate to slider value
var fr = fSlider.value();
frameRate(fr);
//render cells
for (var q = 0; q < qaxis; q++){
for (var z = 0; z < zaxis; z++){
for (var i = 0; i < cellGrid[q][z].length; i++){
cellGrid[q][z][i].render();
}
}
}
//flag cell as visited
currentCell.visited = true;
//next cell is an available neighbour cell
var nextCell = currentCell.checkNeighbours();
//if nextcell possible
if (nextCell){
pathStack.push(currentCell);
//remove lines between current and next
removeLines(currentCell, nextCell);
//add marker for z or q movement
addMarker(currentCell, nextCell);
//update current cell to be next cell
currentCell = nextCell;
}
//if no neightbour trace stack back until neighbour
if (!nextCell && pathStack.length > 0){
var poppedCell = pathStack.pop();
currentCell = poppedCell;
}
}
function removeLines(a,b){
var x = a.i - b.i;
if (x == 1){ // to left of current
a.walls[3] = false;
b.walls[1] = false;
}
if (x == -1){ // to right of current
a.walls[1] = false;
b.walls[3] = false;
}
var y = a.j - b.j;
if (y == 1){ // to top of current
a.walls[0] = false;
b.walls[2] = false;
}
if (y == -1){ // to bottom of current
a.walls[2] = false;
b.walls[0] = false;
}
}
//add markers showing z and q movements
function addMarker(a,b){
if (a.z != b.z){
a.zmarker = true;
b.zmarker = true;
var col = getRandomColor();
a.zfill = col;
b.zfill = col;
var dir = b.z - a.z;
if (dir == 1){ // moved right
b.dir = 'L';
a.dir = 'R';
} else { // moved left
b.dir = 'R';
a.dir = 'L';
}
}
if (a.q != b.q){
a.qmarker = true;
b.qmarker = true;
var col = getRandomColor();
a.qfill = col;
b.qfill = col;
var dir = b.q - a.q;
if (dir == 1){ // moved down
b.dir = 'U';
a.dir = 'D';
} else { // moved up
b.dir = 'D';
a.dir = 'U';
}
}
}
//main cell object
function Cell(i, j, z, q){
this.i = i;
this.j = j;
this.z = z;
this.q = q;
this.visited = false;
this.zmarker = false;
this.qmarker = false;
this.dir = '';
//top, right, bottom, left
this.walls = [true, true, true, true];
this.checkNeighbours = function(){
var neighbours = [];
//2D check through neighbours
var above = cellGrid[q][z][calcIndex(i,j-1)];
var right = cellGrid[q][z][calcIndex(i+1,j)];
var below = cellGrid[q][z][calcIndex(i,j+1)];
var left = cellGrid[q][z][calcIndex(i-1,j)];
//3D check
if (z != 0) var infront = cellGrid[q][z-1][calcIndex(i,j)];
if(calcZ(z+1)) var behind = cellGrid[q][z+1][calcIndex(i,j)];
//4d check
if (q !=0) var qUp = cellGrid[q-1][z][calcIndex(i,j)];
if(calcZ(q+1)) var qDown = cellGrid[q+1][z][calcIndex(i,j)];
//push into array if valid neighbour
for (var d = 0; d < xyProb; d++){
//add multiple times to array to increase chance of picking x,y direction
if (above && !above.visited) neighbours.push(above);
if (right && !right.visited) neighbours.push(right);
if (below && !below.visited) neighbours.push(below);
if (left && !left.visited) neighbours.push(left);
}
if (infront && !infront.visited) neighbours.push(infront);
if (behind && !behind.visited) neighbours.push(behind);
if (qUp && !qUp.visited) neighbours.push(qUp);
if (qDown && !qDown.visited) neighbours.push(qDown);
//pick a random neighbour
if (neighbours.length > 0){
//console.log('Found ' + neighbours.length + " neighbours");
var r = floor(random(0,neighbours.length));
return neighbours[r]
}
}
this.render = function() {
var x = (this.i * scl) + (z * hOffset);
var y = (this.j * scl) + (q * vOffset);
stroke(255);
//draw walls
for (var i = 0; i < this.walls.length; i++){
if (this.walls[i] == true){
switch(i) {
case 0:
line(x,y,x+scl,y); //top
break;
case 1:
line(x+scl,y,x+scl,y+scl); //right
break;
case 2:
line(x+scl,y+scl,x,y+scl); //bottom
break;
case 3:
line(x,y+scl,x,y); //left
break;
}
}
}
//sets marker for z movement
if (this.zmarker){
noStroke();
fill(this.zfill);
this.drawMarker(this.zmarker.dir);
}
//sets marker for q axis movement
if (this.qmarker){
noStroke();
fill(this.qfill);
this.drawMarker(this.qmarker.dir);
}
//colour current cell
if (this == currentCell){
noStroke();
fill(0,255,0,50);
rect(x,y,scl,scl);
}
//if visted change colour
if (this.visited){
noStroke();
fill(225,0,0,50);
rect(x,y,scl,scl);
}
this.drawMarker = function(dir){
var offset = scl / 4;
if (this.dir == 'D') triangle(x+offset,y+offset,x+scl-offset,y+offset,x+scl/2,y+scl-offset);
if (this.dir == 'U') triangle(x+scl/2,y+offset,x+scl-offset,y+scl-offset,x+offset,y+scl-offset);
if (this.dir == 'R') triangle(x+offset,y+offset,x+scl-offset,y+scl/2,x+offset,y+scl-offset);
if (this.dir == 'L') triangle(x+offset,y+scl/2,x+scl-offset,y+offset,x+scl-offset,y+scl-offset);
}
}
}
//calculates index of 1d array based on grid layout
function calcIndex(i,j){
//check for valid index
if (i < 0 || j < 0 || i > cols-1 || j > rows-1){
return -1
}
return i + j * rows
}
//stops invalid z movement
function calcZ(a){
//check for valid index
if (a == 0){
return a
}
if (a == -1){
return undefined
}
if (a > zaxis-1){
return undefined
}
return a
}
// https://stackoverflow.com/questions/1484506/random-color-generator
function getRandomColor() {
var letters = '0123456789ABCDEF';
var color = '#';
for (var i = 0; i < 6; i++) {
color += letters[Math.floor(Math.random() * 16)];
}
return color;
}