-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
300 lines (243 loc) · 5.98 KB
/
script.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
import pieces from './pieces.js';
const rows = 20;
const columns = 10;
const tileSize = 24;
const startLevel = 1;
const board = createEmptyBoard(rows, columns);
const canvas = document.getElementById('board');
const context = canvas.getContext('2d');
const defaultState = {
linesCompleted: 0,
piece: null,
gameOver: false,
paused: false,
countdown: 0,
prevLoop: null,
get level() {
const { linesCompleted } = this;
if (linesCompleted <= 0) return 1;
if (linesCompleted >= 91) return 10;
return 1 + ((linesCompleted - 1) / 10);
},
};
let state = defaultState;
class Piece {
constructor({ shapes, color }) {
this.shapeIndex = 0;
this.shapes = shapes;
this.currentShape = shapes[this.shapeIndex];
this.color = color;
this.x = 6;
this.y = -2;
}
_fill(color) {
const { fillStyle } = context;
context.fillStyle = color;
this.currentShape.forEach((row, y) => {
row.forEach((column, x) => {
if (Boolean(column)) {
drawSquare(this.x + x, this.y + y);
}
});
});
context.fillStyle = fillStyle;
}
_collides(dx, dy, shape) {
return shape.some((row, y) =>
row.some((column, x) => {
if (Boolean(column) === false) return false;
const newX = this.x + x + dx;
const newY = this.y + y + dy;
if (newY < 0) return false;
if (newY >= rows || newX < 0 || newX >= columns) return true;
if (board[newY][newX]) return true;
})
);
}
draw() {
this._fill(this.color);
}
undraw() {
this._fill('white');
}
rotate() {
const nextShape = this.shapes[(this.shapeIndex + 1) % this.shapes.length];
let nudge = 0;
if (this._collides(0, 0, nextShape)) {
nudge = (this.x > (columns / 2)) ? -1 : 1;
}
if (this._collides(nudge, 0, nextShape) === false) {
this.undraw();
this.x += nudge;
this.shapeIndex = (this.shapeIndex + 1) % this.shapes.length;
this.currentShape = this.shapes[this.shapeIndex];
this.draw();
}
}
down() {
if (this._collides(0, 1, this.currentShape)) {
this.lock();
state.piece = newPiece();
return true;
} else {
this.undraw();
this.y++;
this.draw();
return false;
}
}
right() {
if (this._collides(1, 0, this.currentShape) === false) {
this.undraw();
this.x++;
this.draw();
}
}
left() {
if (this._collides(-1, 0, this.currentShape) === false) {
this.undraw();
this.x--;
this.draw();
}
}
drop() {
while(this.down() === false);
}
lock() {
this.currentShape.forEach((row, y) => {
row.forEach((column, x) => {
if (!column) return;
if (this.y + y < 0) return gameOver()
board[this.y + y][this.x + x] = this.color;
});
});
board
.map((row, y) => {
if(row.every((column) => Boolean(column) === true)) {
return y;
}
})
.filter(val => val !== undefined)
.forEach(line => {
for (let x = line; x > 1; x--) {
board[x] = board[x].map((_, i) => board[x - 1][i]);
}
state.linesCompleted++;
});
drawBoard();
}
}
start();
/**
* Start the game.
* - Adds button listeners to window
* - Resets the state
*/
function start() {
window.addEventListener('keydown', mapButtonInput, {
passive: true,
useCapture: false
});
canvas.width = columns * tileSize;
canvas.height = rows * tileSize;
drawBoard();
state = defaultState;
loop(true);
}
/**
* Main game loop
*
* Will run as long as state.gameOver is false
*/
function loop(first = false) {
if (state.gameOver) return;
if (state.paused) return;
if (first === true) {
state.prevLoop = Date.now();
state.piece = newPiece();
}
const now = Date.now();
const delta = now - state.prevLoop;
state.prevLoop = now;
state.countdown -= delta;
if (state.countdown <= 0) {
state.countdown = 50 * (11 - state.level);
state.piece.down();
}
requestAnimationFrame(loop);
}
function gameOver() {
state.gameOver = true;
alert('Game over!');
window.removeEventListener('keydown', mapButtonInput);
}
/**
* Maps button input to piece modifiers
*/
function mapButtonInput({ key }) {
const { piece } = state;
switch (key) {
case 'ArrowUp': return piece.rotate();
case 'ArrowDown': return piece.drop();
case 'ArrowRight': return piece.right();
case 'ArrowLeft': return piece.left();
}
}
/**
* Creates array of arrays for each row and column
* respectively. Each field on the board is set to false
*
* @param {number} rows The amount of rows to create
* @param {number} columns The amount of columns to create
* @returns {array} Array of arrays of arrays containing all false
*
* @example
* var board = createEmptyBoard(20, 10);
* board[5][8]; // => false
*/
function createEmptyBoard(rows, columns) {
var board = [];
for (var i = 0; i < rows; i++) {
board[i] = [];
for (var x = 0; x < columns; x++) {
board[i][x] = false;
}
}
return board;
}
function newPiece() {
const rand = Math.floor(Math.random() * pieces.length);
return new Piece(pieces[rand]);
}
/**
* Draws a square on the provided canvas
* @param {number} x The X position of the square
* @param {number} y The Y position of the square
*/
function drawSquare(x, y) {
const { strokeStyle } = context;
context.fillRect(
x * tileSize,
y * tileSize,
tileSize,
tileSize
);
context.strokeStyle = '#555';
context.strokeRect(x * tileSize, y * tileSize, tileSize, tileSize);
context.strokeStyle = strokeStyle;
}
/**
* Draw the provided board to the provided context
* @param {array} board The gameboard to draw
* @param {object} context The canvas context to draw to
*/
function drawBoard() {
const { fillStyle } = context;
board.forEach((row, y) => {
row.forEach((column, x) => {
context.fillStyle = board[y][x] || 'white';
drawSquare(x, y);
});
});
context.fillStyle = fillStyle;
}