-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathboard.js
More file actions
275 lines (219 loc) · 6.14 KB
/
board.js
File metadata and controls
275 lines (219 loc) · 6.14 KB
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
function arrayUniq(array) {
return array.reduce((ar, item) => ar.includes(item) ? ar : ar.concat(item), [])
}
function arrayRemove(array, value) {
if(!array.includes(value)) return;
array.splice(array.indexOf(value), 1);
return array;
}
function now() {
return (new Date()).getTime()
}
// countdown to start of gameplay
const BOARD_STATE_COUNTDOWN = 'Countdown'
// player controlling falling mino
const BOARD_STATE_INTERACTIVE = 'Interactive'
// line clear animation
const BOARD_STATE_CLEARING = 'Clearing'
// settle animation - falling block entities
const BOARD_STATE_SETTLING = 'Settling'
class Board {
constructor (minos) {
this.minos = minos;
this.grid = [];
this.blocks = [];
this.updateCells();
this.rowsToClear = [];
this.queue = [];
this.initQueue();
this.newMino = null;
this.startInteractive();
// !!! move me
window.addEventListener('keypress', (e) => {
if(e.keyCode === 32) {
this.hardDrop();
}
})
}
initQueue() {
this.queue.push(Mino.getRandom())
this.queue.push(Mino.getRandom())
}
// Remove and return a mino from the queue
// Add another to the bottom
popMino() {
this.queue.unshift(Mino.getRandom())
return this.queue.pop()
}
nextMino() {
return this.queue[this.queue.length-1]
}
setState(state) {
console.log("state: " + this.state + '->' + state);
this.state = state
this.stateFrame = 0
}
// Update minos to the lowest position they can fall to instantly
// and start updating their entities for settle animation
drop () {
let updatedCount = 1
while(updatedCount > 0) {
updatedCount = 0;
this.minos.forEach((mino) => {
if(this.canFall(mino)) {
updatedCount ++;
mino.moveDown();
board.updateCells();
}
})
}
this.setState(BOARD_STATE_SETTLING)
}
update () {
this.stateFrame ++;
this["update" + this.state]()
}
updateSettling () {
this.updateBlockEntities()
if(this.blocks.every((b) => b.entity.y === b.y)) {
this.startLineClearing()
}
}
updateClearing () {
if(this.stateFrame > 10) {
// removes the filled rows
this.clearLines();
// move blocks down and start the falling animation
this.drop();
}
}
updateBlockEntities () {
this.blocks.forEach((block) => block.entity.update())
}
updateCells () {
this.blocks = this.minos.flatMap((mino) => mino.blocks)
// Populate array of rows of cells for fast lookup of block at location
for(let y = 0; y < BOARD_HEIGHT; y++) {
for(let x = 0; x < BOARD_WIDTH; x++) {
let val = this.blocks.find((block) => block.x === x && block.y === y)
this.grid[y] = this.grid[y] || []
this.grid[y][x] = val
}
}
}
hardDrop () {
console.log("hardDrop");
while(this.canFall(this.newMino)) {
console.log("moving down");
this.newMino.moveDown(true);
this.updateCells()
}
}
canFall (mino) {
return !mino.onBottom() && !this.touchingMinoBelow(mino)
}
canMoveLeft (mino) {
//!! add collision check
return mino.blocks.every((b) => b.x > 0);
}
canMoveRight (mino) {
//!! add collision check
return mino.blocks.every((b) => b.x < BOARD_WIDTH - 1);
}
touchingMinoBelow (mino) {
return mino.cellsBelow().some((cell) => !! this.getCell(cell[0], cell[1]))
}
getCell (x, y) {
return this.grid[y][x]
}
startInteractive () {
this.newMino = this.popMino();
this.setState(BOARD_STATE_INTERACTIVE)
}
updateCountdown () {
}
updateInteractive () {
if(this.canFall(this.newMino)) {
if (!Key.isDown(Key.LEFT)) {
this.holdLeftSince = null
}
if (!Key.isDown(Key.RIGHT)) {
this.holdRightSince = null
}
if (Key.isDown(Key.LEFT) && !this.holdRightSince) {
if(this.canMoveLeft(this.newMino)) {
if(this.holdLeftSince) {
if(now() - this.holdLeftSince > 300) {
this.newMino.moveLeft();
}
} else {
this.holdLeftSince = now()
this.newMino.moveLeft();
}
}
}
if (Key.isDown(Key.RIGHT) && !this.holdLeftSince) {
if(this.canMoveRight(this.newMino)) {
if(this.holdRightSince) {
if(now() - this.holdRightSince > 300) {
this.newMino.moveRight();
}
} else {
this.holdRightSince = now()
this.newMino.moveRight();
}
}
}
if (Key.isDown(Key.UP) && !this.turnKeyDown) {
this.newMino.rotateCW();
this.turnKeyDown = true
}
if (!Key.isDown(Key.UP)) {
this.turnKeyDown = false
}
if(this.stateFrame % 40 === 0) {
this.newMino.moveDown(true)
}
} else {
this.minos.push(this.newMino);
this.updateCells();
this.newMino = null;
this.popMino();
this.startLineClearing()
}
}
startLineClearing () {
// store this.rowsToClear - array of row indeces
this.rowsToClear = this.filledRows();
if(this.rowsToClear.length) {
// starts the clearing animation, at the end of which the rows will be removed
this.setState(BOARD_STATE_CLEARING)
}
else {
// no rows to clear
this.startInteractive()
}
}
clearLines () {
// All blocks that are on one of the rows to be cleared
const blocks = this.blocks.filter((b) => this.rowsToClear.includes(b.y))
// unique minos intersecting these blocks
const minos = arrayUniq(blocks.map((block) => this.minos.find((mino) => mino.blocks.includes(block))))
// new minos resulting from splitting
const newMinos = minos.flatMap((mino) => mino.split(blocks))
// remove the old minos
minos.forEach((mino) => arrayRemove(this.minos, mino));
// replace with new ones
this.minos = this.minos.concat(newMinos)
// Rebuilt the grid of blocks
this.updateCells()
this.rowsToClear = []
}
filledRows () {
const rowIndices = Array.from(this.grid.keys())
return rowIndices.filter((i) => this.isRowFilled(i))
}
isRowFilled (row) {
return this.grid[row].every((b) => b)
}
}