-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
625 lines (572 loc) · 19 KB
/
main.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
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
const EMPTY_CELL = {
type: "EMPTY_CELL",
color: "rgb(0,0,0)"
};
const YELLOW_CELL = {
type: "YELLOW_CELL",
color: "rgb(200, 200, 0)"
};
const BLUE_CELL = {
type: "BLUE_CELL",
color: "rgb(0,0,200)"
};
const RED_CELL = {
type: "RED_CELL",
color: "rgb(200, 0, 0)"
};
const GREEN_CELL = {
type: "GREEN_CELL",
color: "rgb(0, 200, 0)"
};
const ORANGE_CELL = {
type: "ORANGE_CELL",
color: "rgb(200, 100, 0)"
};
const PURPLE_CELL = {
type: "PURPLE_CELL",
color: "rgb(200, 0, 200)"
};
const PINK_CELL = {
type: "PINK_CELL",
color: "rgb(200, 100, 100)"
};
const BROWN_CELL = {
type: "BROWN_CELL",
color: "rgb(120, 120, 0)"
};
/*
e,y,y,
y,y,e
*/
const SMASK = [
[EMPTY_CELL, YELLOW_CELL, YELLOW_CELL],
[YELLOW_CELL, YELLOW_CELL, EMPTY_CELL]
];
const SINFO = {
mask: SMASK,
rot_pivot_point: {x: 1.5, y: 0.5}
};
/*
b,b,e
e,b,b
*/
const ZMASK = [
[GREEN_CELL, GREEN_CELL, EMPTY_CELL],
[EMPTY_CELL, GREEN_CELL, GREEN_CELL]
];
const ZINFO = {
mask: ZMASK,
rot_pivot_point: {x: 1.5, y: 0.5}
};
/*
r,r
r,r
*/
const SQUAREMASK = [
[RED_CELL, RED_CELL],
[RED_CELL, RED_CELL]
];
const SQUAREINFO = {
mask: SQUAREMASK,
rot_pivot_point: {x: 1, y:1}
};
/*
b,b,b
e,b,e
*/
const TMASK = [
[BLUE_CELL, BLUE_CELL, BLUE_CELL],
[EMPTY_CELL, BLUE_CELL, EMPTY_CELL]
];
const TINFO = {
mask: TMASK,
rot_pivot_point: {x: 1.5, y: 0.5}
};
/*
o,o,o,o
*/
const IMASK = [
[ORANGE_CELL, ORANGE_CELL, ORANGE_CELL, ORANGE_CELL]
];
const IINFO = {
mask: IMASK,
rot_pivot_point: {x: 1.5, y: 0.5}
};
/*
p,p,p
p,e,e
*/
const LMASK = [
[PURPLE_CELL, PURPLE_CELL, PURPLE_CELL],
[PURPLE_CELL, EMPTY_CELL, EMPTY_CELL]
];
const LINFO = {
mask: LMASK,
rot_pivot_point: {x: 1.5, y: 0.5}
};
/*
p,p,p
e,e,p
*/
const JMASK = [
[PINK_CELL, PINK_CELL, PINK_CELL],
[EMPTY_CELL, EMPTY_CELL, PINK_CELL]
];
const JINFO = {
mask: JMASK,
rot_pivot_point: {x: 1.5, y: 0.5}
};
function Grid(w, h) {
this.w = w;
this.h = h;
this.cells = [];
for (var i = 0; i < h; i++) {
var tmp = [];
for (var j = 0; j < w; j++) {
tmp.push(EMPTY_CELL);
}
this.cells.push(tmp);
}
}
Grid.prototype.inBounds = function(x, y) {
return (x < this.w && x >= 0 && y < this.h && y >= 0);
};
Grid.prototype.aboveGrid = function(x, y) {
return (x < this.w && x >= 0 && y < 0);
}
Grid.prototype.getCell = function(x, y) {
if (!this.inBounds(x, y)) throw Error("OUT OF BOUNDS -- getCell");
return this.cells[y][x];
};
Grid.prototype.setCell = function(x, y, cell) {
if (!this.inBounds(x, y)) throw Error("OUT OF BOUNDS -- setCell");
this.cells[y][x] = cell;
};
Grid.prototype.checkRowForTetris = function(row) {
if (row < 0 || row >= this.h) throw Error("ROW OUT OF BOUNDS -- checkRowForTetris");
for (var j = 0; j < this.cells[row].length; j++) {
var cell = this.getCell(j, row);
if (cell === EMPTY_CELL) return false;
}
return true;
}
Grid.prototype.collapseRowDown = function(row) {
for (var i = row; i > 0; i--) {
for (var j = 0; j < this.w; j++) {
var aboveCell = this.getCell(j, i-1);
this.setCell(j, i, aboveCell);
}
}
for (var j = 0; j < this.w; j++) this.setCell(j, 0, EMPTY_CELL);
}
function getUnTetris(grid) {
var rowsCollapsed = 0;
for (var row = 0; row < grid.h; row++) {
if (grid.checkRowForTetris(row)) {
grid.collapseRowDown(row);
rowsCollapsed++;
}
}
if (rowsCollapsed === 4) return 10000;
else return 1000 * rowsCollapsed;
}
function Piece(x, y, rot, info) {
this.x = x;
this.y = y;
this.info = info;
this.rot = rot;
}
Piece.prototype.copy = function() {
return new Piece(this.x, this.y, this.rot, this.info);
};
Piece.prototype.getAnchorPoint = function() {
var info = this.info;
return {
x: info.rot_pivot_point.x,
y: info.rot_pivot_point.y
};
};
Piece.prototype.getPointRotated = function(point) {
var x = point.x;
var y = point.y;
for (var k = 0; k < this.rot; k++) {
var tmpx = x;
var tmpy = y;
x = -tmpy;
y = tmpx;
}
return {
x: x,
y: y
};
}
// returns array of cell coordinates and cell value
Piece.prototype.getCellArray = function() {
var arr = [];
var mask = this.info.mask;
var anchorPoint = this.getAnchorPoint();
var ra = this.getPointRotated(anchorPoint);
for (var i = 0; i < mask.length; i++) {
for (var j = 0; j < mask[i].length; j++) {
var cell = mask[i][j];
var p = this.getPointRotated({x: j + 0.5, y: i + 0.5});
arr.push({
x: Math.floor(p.x + (this.x - ra.x)),
y: Math.floor(p.y + (this.y - ra.y)),
cell: cell
});
}
}
return arr;
}
// returns true if all non-empty cells of piece are within
function canPlacePiece(grid, piece) {
// check mask in grid boundaries
var maskArray = piece.getCellArray();
for (var i = 0; i < maskArray.length; i++) {
var cellInfo = maskArray[i];
// check bounds of grid
if (!grid.inBounds(cellInfo.x, cellInfo.y) && !grid.aboveGrid(cellInfo.x, cellInfo.y)) return false;
// check non-empty cell intersections
if (!grid.aboveGrid(cellInfo.x, cellInfo.y)) {
var gcell = grid.getCell(cellInfo.x, cellInfo.y);
if (gcell !== EMPTY_CELL && cellInfo.cell !== EMPTY_CELL) return false;
}
}
return true;
}
function transferPieceToGrid(grid, piece) {
var maskArray = piece.getCellArray();
for (var i = 0; i < maskArray.length; i++) {
var cellInfo = maskArray[i];
if (grid.aboveGrid(cellInfo.x, cellInfo.y)) continue;
if (!grid.inBounds(cellInfo.x, cellInfo.y) ) throw Error("CELL OUT OF BOUNDS -- transferPieceToGrid");
if (cellInfo.cell !== EMPTY_CELL) grid.setCell(cellInfo.x, cellInfo.y, cellInfo.cell);
}
}
function getNextPiece() {
var index = Math.floor(Math.random() * pieces.length);
return new Piece(Math.floor(grid.w / 2), -2, 0, pieces[index]);
}
function initializeGameState() {
grid = new Grid(gw, gh);
pieces = [SINFO, ZINFO, SQUAREINFO, TINFO, IINFO, LINFO, JINFO];
activePiece = getNextPiece();
nextPiece = getNextPiece();
level = 0; // index used for levelCounterTime and levelThreshold
downInterval = levelCounterTime[level];
downCounter = 0;
score = 0;
};
// set up all globals
function initialize() {
canvas = document.getElementById("c");
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
ctx = canvas.getContext("2d");
gw = 10;
gh = 20;
pendingInput = null;
levelCounterTime = [18, 15, 12, 10, 8, 7, 6, 5, 4, 3];
levelThreshold = [0, 2000, 3000, 4000, 5000, 6000, 7000, 8000, 9000, Infinity];
gameState = 'gameStart';
}
// gameStates = 'gameStart' | 'gameRunning' | 'gameOver'
// -> -> ->
function getGameState() {
return gameState;
}
function nextGameState() {
if (gameState === 'gameStart') gameState = 'gameRunning';
else if (gameState === 'gameRunning') gameState = 'gameOver';
else if (gameState === 'gameOver') gameState = 'gameStart';
}
/*
*/
function incrementCounter() {
downCounter += 1;
}
function resetCounter() {
downCounter = 0;
}
function setDownInterval() {
// set level based on score
for (var i = level; i < levelThreshold.length; i++) {
if (score > levelThreshold[i]) level = i;
else break;
}
downInterval = levelCounterTime[level];
}
function setActivePiece(piece) {
activePiece = piece;
}
function gridConditionViolated() {
// check game over
var cellArray = activePiece.getCellArray();
for (var i = 0; i < cellArray.length; i++) {
var cellInfo = cellArray[i];
if (grid.aboveGrid(cellInfo.x, cellInfo.y)) {
return true;
}
}
return false;
}
function activePieceLocked() {
transferPieceToGrid(grid, activePiece);
if (gridConditionViolated()) {
nextGameState();
}
score += getUnTetris(grid);
setDownInterval();
resetCounter();
}
function activePieceMoved() {
}
function activePieceUnmoved() {
}
function gameLoop() {
var testPiece = null;
if (pendingInput) {
var testPiece = activePiece.copy();
if (pendingInput.dir === "left") testPiece.x -= 1;
else if (pendingInput.dir === "right") testPiece.x += 1;
else if (pendingInput.dir === "down") testPiece.y += 1;
else if (pendingInput.dir === "cc") testPiece.rot = (testPiece.rot + 3) % 4;
else if (pendingInput.dir === "cw") testPiece.rot = (testPiece.rot + 1) % 4;
else throw Error("INVALID INPUT -- update");
}
// if testPiece apply input
if (testPiece) {
if (pendingInput.dir === 'down' && !canPlacePiece(grid, testPiece)) {
activePieceLocked();
setActivePiece(nextPiece);
nextPiece = getNextPiece();
} else if (!canPlacePiece(grid, testPiece)) {
activePieceUnmoved();
} else if (canPlacePiece(grid, testPiece)) {
setActivePiece(testPiece);
activePieceMoved();
}
}
// apply down if necessary
if (downCounter >= downInterval) {
resetCounter();
var testPiece = activePiece.copy();
testPiece.y += 1;
if (canPlacePiece(grid, testPiece)){
setActivePiece(testPiece);
activePieceMoved();
} else {
activePieceLocked();
setActivePiece(nextPiece);
nextPiece = getNextPiece();
}
}
incrementCounter();
}
function update() {
let state = getGameState();
if (state === 'gameRunning') {
gameLoop();
} else if (state === 'gameOver') {
if (pendingInput) nextGameState();
} else if (state === 'gameStart') {
if (pendingInput) nextGameState();
initializeGameState();
}
pendingInput = null;
}
function drawCell(cell, posx, posy, dx, dy) {
ctx.fillStyle = cell.color;
ctx.strokeStyle = "white";
if (cell === EMPTY_CELL) ctx.lineWidth = 0.5;
else ctx.lineWidth = 0.15;
ctx.fillRect(posx, posy, dx, dy);
ctx.strokeRect(posx, posy, dx, dy);
}
function drawCell(cell, posx, posy, dx, dy) {
ctx.fillStyle = cell.color;
ctx.strokeStyle = "white";
if (cell === EMPTY_CELL) ctx.lineWidth = 0.5;
else ctx.lineWidth = 0.15;
ctx.fillRect(posx, posy, dx, dy);
ctx.strokeRect(posx, posy, dx, dy);
}
function drawGhostCell(cell, posx, posy, dx, dy) {
var ghostColor = "rgba(20, 20, 20, 20)";
ctx.fillStyle = ghostColor;
ctx.strokeStyle = ghostColor;
if (cell === EMPTY_CELL) ctx.lineWidth = 0.5;
else ctx.lineWidth = 0.15;
ctx.fillRect(posx, posy, dx, dy);
ctx.strokeRect(posx, posy, dx, dy);
}
function drawGrid(grid, anchor, cellSize) {
for (var i = 0; i < grid.h; i++) {
for (var j = 0; j < grid.w; j++) {
var cell = grid.getCell(j, i);
drawCell(cell, anchor.x + j * cellSize, anchor.y + i * cellSize, cellSize, cellSize);
}
}
}
function drawDeadGrid(grid, anchor, cellSize) {
for (var i = 0; i < grid.h; i++) {
for (var j = 0; j < grid.w; j++) {
var cell = grid.getCell(j, i);
if (cell === EMPTY_CELL) drawCell(cell, anchor.x + j * cellSize, anchor.y + i * cellSize, cellSize, cellSize);
else drawCell(BROWN_CELL, anchor.x + j * cellSize, anchor.y + i * cellSize, cellSize, cellSize);
}
}
}
function drawPieceOnGrid(piece, grid, anchor, blockSize) {
var blockArray = piece.getCellArray();
for (var i = 0; i < blockArray.length; i++) {
var cellInfo = blockArray[i];
if (cellInfo.cell === EMPTY_CELL || grid.aboveGrid(cellInfo.x, cellInfo.y)) continue;
drawCell(cellInfo.cell, anchor.x + (cellInfo.x) * blockSize, anchor.y + (cellInfo.y) * blockSize, blockSize, blockSize);
}
}
function drawGhostPieceOnGrid(piece, grid, anchor, blockSize) {
var ghostPiece = piece.copy();
while (canPlacePiece(grid, ghostPiece)) {
ghostPiece.y += 1;
}
ghostPiece.y -= 1;
var blockArray = ghostPiece.getCellArray();
for (var i = 0; i < blockArray.length; i++) {
var cellInfo = blockArray[i];
if (cellInfo.cell === EMPTY_CELL || grid.aboveGrid(cellInfo.x, cellInfo.y)) continue;
drawGhostCell(cellInfo.cell, anchor.x + (cellInfo.x) * blockSize, anchor.y + (cellInfo.y) * blockSize, blockSize, blockSize);
}
}
function drawNextPiece(piece, anchor, blockSize) {
var blockArray = piece.getCellArray();
for (var i = 0; i < blockArray.length; i++) {
var cellInfo = blockArray[i];
if (cellInfo.cell === EMPTY_CELL) continue;
drawCell(cellInfo.cell, anchor.x + (cellInfo.x) * blockSize, anchor.y + (cellInfo.y) * blockSize, blockSize, blockSize);
}
}
function drawText(text, anchor, font, color) {
ctx.font = font;
ctx.fillStyle = color;
ctx.strokeStyle = color;
ctx.fillText(text, anchor.x, anchor.y);
}
function runningRender() {
// clear screen
ctx.clearRect(0, 0, canvas.width, canvas.height);
const gridAnchor = {x: canvas.width * 1/3, y: canvas.height * 1/20} // tells where grid is on canvas,
// calculate block size
const cellSize = Math.floor(((canvas.width > canvas.height ? canvas.height : canvas.width) - gridAnchor.y) / grid.h);
// draw grid
drawGrid(grid, gridAnchor, cellSize);
// draw ghost piece
drawGhostPieceOnGrid(activePiece, grid, gridAnchor, cellSize);
// draw piece
drawPieceOnGrid(activePiece, grid, gridAnchor, cellSize);
// draw next piece
const nextPieceSize = cellSize * 0.7;
const nextPieceAnchor = {x: gridAnchor.x + cellSize * grid.w + canvas.width * 1/30, y: gridAnchor.y + canvas.height * 1/30};
drawNextPiece(nextPiece, nextPieceAnchor, nextPieceSize);
const fontSize = Math.floor(canvas.width * 1/30);
// draw level text
const levelAnchor = {x: canvas.width/10, y: canvas.height * 1/20};
const levelFont = fontSize + "px serif";
const levelColor = "white";
drawText("Level: " + level, levelAnchor, levelFont, levelColor);
// draw score text
const scoreAnchor = {x: levelAnchor.x, y: levelAnchor.y + 2*fontSize};
const scoreFont = fontSize + "px serif";
const scoreColor = "white";
drawText("Score: " + score, scoreAnchor, scoreFont, scoreColor);
}
function gameOverRender() {
// clear screen
ctx.clearRect(0, 0, canvas.width, canvas.height);
const gridAnchor = {x: canvas.width * 1/3, y: canvas.height * 1/20} // tells where grid is on canvas,
// calculate block size
const cellSize = Math.floor(((canvas.width > canvas.height ? canvas.height : canvas.width) - gridAnchor.y) / grid.h);
// draw grid
drawDeadGrid(grid, gridAnchor, cellSize);
// draw next piece
const nextPieceSize = cellSize * 0.7;
const nextPieceAnchor = {x: gridAnchor.x + cellSize * grid.w + canvas.width * 1/30, y: gridAnchor.y + canvas.height * 1/30};
drawNextPiece(nextPiece, nextPieceAnchor, nextPieceSize);
const fontSize = Math.floor(canvas.width * 1/30);
// draw level text
const levelAnchor = {x: canvas.width/10, y: canvas.height * 1/20};
const levelFont = fontSize + "px serif";
const levelColor = "white";
drawText("Level: " + level, levelAnchor, levelFont, levelColor);
// draw score text
const scoreAnchor = {x: levelAnchor.x, y: levelAnchor.y + 2*fontSize};
const scoreFont = fontSize + "px serif";
const scoreColor = "white";
drawText("Score: " + score, scoreAnchor, scoreFont, scoreColor);
// draw game over text
const gameOverAnchor = {x: canvas.width/2, y: canvas.height/2};
const gameOverFont = (2 * fontSize) + 'px serif';
const gameOverColor = "red";
drawText("Game Over", gameOverAnchor, gameOverFont, gameOverColor);
}
function startRender() {
// clear screen
ctx.clearRect(0, 0, canvas.width, canvas.height);
const gridAnchor = {x: canvas.width * 1/3, y: canvas.height * 1/20} // tells where grid is on canvas,
// calculate block size
const cellSize = Math.floor(((canvas.width > canvas.height ? canvas.height : canvas.width) - gridAnchor.y) / gh);
// draw grid
drawGrid(new Grid(gw, gh), gridAnchor, cellSize);
const fontSize = Math.floor(canvas.width * 1/30)
// draw level text
const levelAnchor = {x: canvas.width/10, y: canvas.height * 1/20};
const levelFont = fontSize + "px serif";
const levelColor = "white";
drawText("Level: 0", levelAnchor, levelFont, levelColor);
// draw score text
const scoreAnchor = {x: levelAnchor.x, y: levelAnchor.y + 2*fontSize};
const scoreFont = fontSize + "px serif";
const scoreColor = "white";
drawText("Score: 0", scoreAnchor, scoreFont, scoreColor);
// start game text
const gameStartAnchor = {x: canvas.width/3, y: canvas.height/2};
const gameStartFont = (1 * fontSize) + 'px serif';
const gameStartColor = "white";
drawText("Press button to start", gameStartAnchor, gameStartFont, gameStartColor);
}
function render() {
let state = getGameState();
if (state === 'gameRunning') {
runningRender();
} else if (state === "gameOver") {
// game over screen
gameOverRender();
} else if (state === "gameStart"){
startRender();
}
}
function loopAnimation() {
window.requestAnimationFrame(loopAnimation);
render();
}
initialize();
loopAnimation();
window.setInterval(update, 1000/30);
window.addEventListener("keydown", function(e) {
let state = getGameState();
if (state === 'gameStart' || state === 'gameOver') {
pendingInput = true;
return;
}
if (pendingInput) return;
if (e.key === 'a') pendingInput = {type: "move", dir: "left"};
else if (e.key === 'd') pendingInput = {type: "move", dir: "right"};
else if (e.key === 's') pendingInput = {type: "move", dir: "down"};
else if (e.key === 'q') pendingInput = {type: "rotate", dir: "cc"};
else if (e.key === 'e') pendingInput = {type: "rotate", dir: "cw"};
});
window.addEventListener('resize', function(e) {
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
})