-
Notifications
You must be signed in to change notification settings - Fork 0
/
maze.js
442 lines (442 loc) · 14.7 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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
"use strict";
var __assign = (this && this.__assign) || Object.assign || function(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
t[p] = s[p];
}
return t;
};
var reverseDir = new Map([
[1, 4],
[2, 8],
[4, 1],
[8, 2]
]);
var dirDX = new Map([
[1, 0],
[2, 1],
[4, 0],
[8, -1]
]);
var dirDY = new Map([
[1, -1],
[2, 0],
[4, 1],
[8, 0]
]);
function popcnt8(b) {
var bi3b = 0xE994;
var c;
c = 3 & (bi3b >> ((b << 1) & 14));
c += 3 & (bi3b >> ((b >> 2) & 14));
c += 3 & (bi3b >> ((b >> 5) & 6));
return c;
}
function scalePos(p, s) {
return {
x: p.x * s,
y: p.y * s
};
}
function addPos(a, b) {
return {
x: a.x + b.x,
y: a.y + b.y
};
}
function mulPos(a, b) {
return {
x: a.x * b.x,
y: a.y * b.y
};
}
function equalPos(a, b) {
return a.x === b.x && a.y === b.y;
}
function posDistance(a, b) {
return Math.abs(a.x - b.x) + Math.abs(a.y - b.y);
}
function randInt(min, max) {
var range = max - min;
var n = range * Math.random();
return min + Math.round(n);
}
function randSort(arr) {
arr.sort(function (_a, _b) { return Math.random() > 0.5 ? -1 : 1; });
return arr;
}
var Grid = (function () {
function Grid(width, height) {
this.width_ = width;
this.height_ = height;
this.data_ = new Array(width * height).fill(0);
}
Grid.prototype.offset = function (x, y) {
return (y * this.width_) + x;
};
Grid.prototype.get = function (x, y) {
return this.data_[(y * this.width_) + x];
};
Grid.prototype.set = function (x, y, v) {
this.data_[(y * this.width_) + x] = v;
};
Grid.prototype.addDir = function (x, y, d) {
this.set(x, y, this.get(x, y) | d);
};
Grid.prototype.build = function (picker) {
var _c = this, w = _c.width_, h = _c.height_;
var stack = [];
this.data_.fill(0);
var _d = [w >> 1, h >> 1], ix = _d[0], iy = _d[1];
stack.push({ x: ix, y: iy });
while (stack.length) {
var index = picker(stack.length - 1);
var deadend = true;
var _e = stack[index], x = _e.x, y = _e.y;
for (var _i = 0, _f = randSort([1, 2, 4, 8]); _i < _f.length; _i++) {
var dir = _f[_i];
var _g = [x + dirDX.get(dir), y + dirDY.get(dir)], nx = _g[0], ny = _g[1];
if (nx >= 0 && ny >= 0 && nx < w && ny < h && this.get(nx, ny) === 0) {
this.addDir(x, y, dir);
this.addDir(nx, ny, reverseDir.get(dir));
stack.push({ x: nx, y: ny });
deadend = false;
break;
}
}
if (deadend) {
stack.splice(index, 1);
}
}
};
Grid.prototype.nextStop = function (from, dir) {
var x = from.x, y = from.y;
var dirs = this.get(x, y);
while (dirs & dir) {
x += dirDX.get(dir);
y += dirDY.get(dir);
dirs = this.get(x, y);
if (popcnt8(dirs) > 2) {
break;
}
}
return { x: x, y: y };
};
Grid.prototype.minDistance = function (pa, pb) {
if (equalPos(pa, pb)) {
return 0;
}
var _c = this, w = _c.width_, h = _c.height_;
var stack = [];
var visited = new Set();
stack.push(pa);
visited.add(this.offset(pa.x, pa.y));
var bestDist = w * h;
while (stack.length) {
var deadend = true;
var _d = stack[stack.length - 1], x = _d.x, y = _d.y;
var exits = this.get(x, y);
for (var _i = 0, _e = randSort([1, 2, 4, 8]); _i < _e.length; _i++) {
var dir = _e[_i];
if (0 === (exits & dir)) {
continue;
}
var _f = [x + dirDX.get(dir), y + dirDY.get(dir)], nx = _f[0], ny = _f[1];
if (nx === pb.x && ny === pb.y) {
if (stack.length < bestDist) {
bestDist = stack.length;
}
}
if (nx >= 0 && ny >= 0 && nx < w && ny < h && !visited.has(this.offset(nx, ny))) {
stack.push({ x: nx, y: ny });
visited.add(this.offset(nx, ny));
deadend = false;
break;
}
}
if (deadend) {
stack.pop();
}
}
return bestDist;
};
Grid.prototype.each = function (fn) {
for (var y = 0; y < this.height_; ++y) {
for (var x = 0; x < this.width_; ++x) {
fn({ x: x, y: y }, this.get(x, y));
}
}
};
Object.defineProperty(Grid.prototype, "deadends", {
get: function () {
var w = this.width_;
var cells = this.data_.map(function (c, off) { return ({ dirs: c, x: off % w, y: Math.floor(off / w) }); });
return cells.filter(function (c) {
return (c.dirs === 1 || c.dirs === 2 || c.dirs === 4 || c.dirs === 8);
});
},
enumerable: true,
configurable: true
});
Object.defineProperty(Grid.prototype, "width", {
get: function () { return this.width_; },
enumerable: true,
configurable: true
});
Object.defineProperty(Grid.prototype, "height", {
get: function () { return this.height_; },
enumerable: true,
configurable: true
});
return Grid;
}());
var recursiveBacktrack = function (max) { return max; };
var random = function (max) { return randInt(0, max); };
var threshold = function (fa, fb, lim) { return function (max) {
if (Math.random() > lim) {
return fb(max);
}
return fa(max);
}; };
var GridView = (function () {
function GridView(grid, ctx) {
this.grid = grid;
this.ctx = ctx;
this.WD = 10;
}
Object.defineProperty(GridView.prototype, "wallDim", {
get: function () {
return { x: this.WD, y: this.WD };
},
enumerable: true,
configurable: true
});
Object.defineProperty(GridView.prototype, "cellWidth", {
get: function () {
return (this.ctx.canvas.width - ((this.grid.width + 1) * this.WD)) / this.grid.width;
},
enumerable: true,
configurable: true
});
Object.defineProperty(GridView.prototype, "cellHeight", {
get: function () {
return (this.ctx.canvas.height - ((this.grid.height + 1) * this.WD)) / this.grid.height;
},
enumerable: true,
configurable: true
});
Object.defineProperty(GridView.prototype, "cellDim", {
get: function () {
return {
x: this.cellWidth,
y: this.cellHeight
};
},
enumerable: true,
configurable: true
});
Object.defineProperty(GridView.prototype, "cellEffectiveDim", {
get: function () {
return addPos(this.cellDim, this.wallDim);
},
enumerable: true,
configurable: true
});
GridView.prototype.cellTopLeft = function (p) {
return mulPos(this.cellEffectiveDim, p);
};
GridView.prototype.cellCentre = function (p) {
return addPos(addPos(this.cellTopLeft(p), scalePos(this.cellDim, .5)), this.wallDim);
};
return GridView;
}());
function render(gv, img) {
var ctx = gv.ctx;
var WDH = scalePos(gv.wallDim, .5);
ctx.strokeStyle = ctx.createPattern(img, "repeat");
ctx.lineCap = "round";
ctx.lineWidth = gv.WD;
ctx.clearRect(0, 0, 500, 500);
gv.grid.each(function (pos, dirs) {
var line = function (a, b) {
var pa = addPos(gv.cellTopLeft(a), WDH);
var pb = addPos(gv.cellTopLeft(b), WDH);
ctx.beginPath();
ctx.moveTo(pa.x, pa.y);
ctx.lineTo(pb.x, pb.y);
ctx.stroke();
};
if ((dirs & 1) === 0) {
line(pos, addPos(pos, { x: 1, y: 0 }));
}
if ((dirs & 2) === 0) {
line(addPos(pos, { x: 1, y: 0 }), addPos(pos, { x: 1, y: 1 }));
}
if ((dirs & 4) === 0) {
line(addPos(pos, { x: 0, y: 1 }), addPos(pos, { x: 1, y: 1 }));
}
if ((dirs & 8) === 0) {
line(pos, addPos(pos, { x: 0, y: 1 }));
}
});
}
var SECS_PER_TILE = .5;
var state;
function loadImage(src) {
return new Promise(function (resolve, reject) {
var img = new Image();
img.onload = function () { return resolve(img); };
img.onerror = function () { return reject("fail"); };
img.src = src;
});
}
function checkWin() {
if (equalPos(state.positions[0], state.target)) {
state.playerState = 2;
document.querySelector("#winner").style.display = "block";
}
}
function move(dir) {
if (state.playerState !== 0) {
return;
}
var rot = (_a = {}, _a[1] = 180, _a[2] = -90, _a[4] = 0, _a[8] = 90, _a)[dir];
state.mouse.style.transform = state.mouseBaseTransform + ("rotate(" + rot + "deg)");
state.playerDir = dir;
state.playerTarget = state.grid.nextStop(state.positions[0], dir);
if (equalPos(state.playerTarget, state.positions[0])) {
return;
}
state.playerState = 1;
state.playerT0 = Date.now() / 1000;
var placeMouse = function (p) {
state.mouse.style.left = p.x + "px";
state.mouse.style.top = p.y + "px";
};
var interpolatePos = function (a, b, t) {
return {
x: a.x + (b.x - a.x) * t,
y: a.y + (b.y - a.y) * t
};
};
var step = function () {
var curPos = state.positions[0];
var nextPos = addPos(curPos, { x: dirDX.get(dir), y: dirDY.get(dir) });
var dt = (Date.now() / 1000) - state.playerT0;
if (dt >= SECS_PER_TILE) {
state.positions.unshift(nextPos);
curPos = nextPos;
nextPos = addPos(curPos, { x: dirDX.get(dir), y: dirDY.get(dir) });
if (equalPos(curPos, state.playerTarget)) {
state.playerState = 0;
placeMouse(state.view.cellCentre(curPos));
checkWin();
return;
}
else {
dt -= SECS_PER_TILE;
state.playerT0 += SECS_PER_TILE;
}
}
placeMouse(interpolatePos(state.view.cellCentre(curPos), state.view.cellCentre(nextPos), dt / SECS_PER_TILE));
requestAnimationFrame(step);
};
step();
var _a;
}
function main(sizeArg, woodImage) {
var GRID_SIZE;
switch (sizeArg) {
case "s":
GRID_SIZE = 5;
break;
case "m":
GRID_SIZE = 7;
break;
case "l":
GRID_SIZE = 9;
break;
case "xl":
GRID_SIZE = 11;
break;
default:
GRID_SIZE = 7;
break;
}
var grid = new Grid(GRID_SIZE, GRID_SIZE);
grid.build(threshold(recursiveBacktrack, random, 0.85));
var starts = grid.deadends.map(function (p) { return (__assign({}, p, { dist: grid.minDistance({ x: 0, y: grid.height - 1 }, p) })); });
starts.sort(function (a, b) { return a.dist - b.dist; });
var start = starts.shift();
var exits = grid.deadends.map(function (p) { return (__assign({}, p, { dist: grid.minDistance(start, p) })); });
exits.sort(function (a, b) { return a.dist - b.dist; });
var target = exits.pop();
var ctx = document.querySelector("canvas").getContext("2d");
var view = new GridView(grid, ctx);
state = {
ctx: ctx,
mouse: document.querySelector("#mouse"),
cheese: document.querySelector("#cheese"),
mouseBaseTransform: "",
grid: grid,
view: view,
target: target,
positions: [start],
playerDir: 4,
playerState: 0,
playerTarget: start,
playerT0: 0
};
render(view, woodImage);
var mousePos = view.cellCentre(start);
state.mouse.className = "obj obj" + GRID_SIZE;
state.mouse.style.left = mousePos.x + "px";
state.mouse.style.top = mousePos.y + "px";
var exitPos = view.cellCentre(target);
state.cheese.className = "obj obj" + GRID_SIZE;
state.cheese.style.left = exitPos.x + "px";
state.cheese.style.top = exitPos.y + "px";
state.mouse.style.transform = "";
state.mouseBaseTransform = (getComputedStyle(state.mouse).transform || "") + " ";
state.mouse.classList.add("animated");
document.querySelector("#winner").style.display = "none";
}
window.onload = function () {
var sizeArg = location.search.substr(1).toLowerCase() || "m";
loadImage("img/wood.png").then(function (woodImage) {
document.querySelector("#up").addEventListener("mousedown", function () { return move(1); });
document.querySelector("#up").addEventListener("touchstart", function () { return move(1); });
document.querySelector("#right").addEventListener("mousedown", function () { return move(2); });
document.querySelector("#right").addEventListener("touchstart", function () { return move(2); });
document.querySelector("#down").addEventListener("mousedown", function () { return move(4); });
document.querySelector("#down").addEventListener("touchstart", function () { return move(4); });
document.querySelector("#left").addEventListener("mousedown", function () { return move(8); });
document.querySelector("#left").addEventListener("touchstart", function () { return move(8); });
document.querySelector("#small").addEventListener("click", function () { return main("s", woodImage); });
document.querySelector("#medium").addEventListener("click", function () { return main("m", woodImage); });
document.querySelector("#large").addEventListener("click", function () { return main("l", woodImage); });
document.querySelector("#xlarge").addEventListener("click", function () { return main("xl", woodImage); });
document.body.addEventListener("keydown", function (evt) {
if (!evt.metaKey) {
evt.preventDefault();
}
if (evt.repeat) {
return;
}
if (evt.keyCode === 38) {
move(1);
}
else if (evt.keyCode === 39) {
move(2);
}
else if (evt.keyCode === 40) {
move(4);
}
else if (evt.keyCode === 37) {
move(8);
}
});
main(sizeArg, woodImage);
});
};