-
Notifications
You must be signed in to change notification settings - Fork 0
/
Game.js
268 lines (213 loc) · 7.42 KB
/
Game.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
/**
* Created by Brayd on 4/4/2017.
*/
const randomProperty = function (obj) {
const keys = Object.keys(obj);
return obj[keys[keys.length * Math.random() << 0]];
};
window.NEXT_SHAPE = null;
window.CURRENT_SHAPE = null;
GAMEDATA = [];
window.interval = null;
ticks = 0;
cleared = 0;
extraScore = 0;
function haveBlocks(start, end) {
for(let i = start; i <= end && i < GAMEDATA.length; ++i) {
if(i < 0 || GAMEDATA[i] === null) continue;
if(GAMEDATA[i].Block === null) return false;
}
return true;
}
function tick() {
if(window.NEXT_SHAPE === null) return;
++ticks;
let move = false;
const toMove = [];
for(let i = 0; i < GAMEDATA.length; ++i) { // decide whether blocks are rigid or not
if(GAMEDATA[i] === null || GAMEDATA[i].Block === null) {
GAMEDATA[i].blocked = false;
} else {
GAMEDATA[i].connectedToBottom = false;
if (GAMEDATA[i].onBottom()) { // last row
let connected = GAMEDATA[i].connectedBlocks();
for(let j = 0; j < connected.length; ++j) {
GAMEDATA[connected[j]].connectedToBottom = true;
}
}
}
}
for (let i = GAMEDATA.length - 1; i >= 0; --i) {
let cell = (new TableIndex(i)).cell;
if(typeof cell === 'undefined' || cell === null || cell.Block === null) continue;
if(cell.Block.controllable) { // short circuit and prevent moving
let below = cell.below();
//console.log(below);
if(below === null || (below.Block !== null && !below.Block.controllable) ) {
console.log("breaking");
console.log(below);
move = false;
break;
}
}
if((!cell.connectedToBottom) || cell.Block.controllable) {
move = true;
toMove.push(i);
}
}
if(move) { // if we are moving the shape down
for(let i = 0; i < toMove.length; ++i)
moveDown(toMove[i]);
} else {
// clear blocks
for(let i = GAMEDATA.length - BOARD_X; i >= 0; i-=BOARD_X) { //iterate from begining of the last line to beginnig of first (one line at a time)
const end = (i + BOARD_X) - 1;
if(haveBlocks(i, end)) { // if this whole row has blocks
cleared++;
for(let j = GAMEDATA.length - 1; j >= 0; --j) {
GAMEDATA[j].blocked = false;
if(GAMEDATA[j].Block !== null) GAMEDATA[j].Block.controllable = false;
}
for(let j = end; j >= i; --j) {
GAMEDATA[j].Block = null; // clear blocks
}
draw();
clearInterval(interval);
window.setTimeout(tick, 100);
return;
}
}
for(let i = 0; i < GAMEDATA.length; ++i) { // reset blocked status. TODO change to if stuck only
GAMEDATA[i].blocked = GAMEDATA[i].Block !== null;
if(GAMEDATA[i].Block !== null) GAMEDATA[i].Block.controllable = false;
}
addShape();
}
window.INPUT_PAUSED = false;
if(window.NEXT_SHAPE !== null) startTimer();
draw();
}
function moveableBlock(idx) {
if(idx < 0 || idx >= GAMEDATA.length) return false;
if(GAMEDATA[idx] === null) return false;
return !GAMEDATA[idx].blocked;
}
function addShape() {
let cont = forceAddShapeAt(window.NEXT_SHAPE, 0, 0);
if(cont) window.NEXT_SHAPE = randomProperty(SHAPES);
//draw();
}
function checkAddShapeAt(shape, startIdx) {
if(startIdx < 0) return false;
const tableIdx1 = new TableIndex(startIdx);
const beginCol = tableIdx1.x;
const tableIdx2 = new TableIndex(startIdx + shape.length());
let endCol = tableIdx2.x;
if(endCol < beginCol) {
return false;
} // cannot cross left/right edges
for(let i = 0; i < shape.blocks.length; ++i) {
const idx = startIdx + shape.blocks[i].Point.x + (shape.blocks[i].Point.y * BOARD_X) - 0;
if(!moveableBlock(idx)) {
//let tableIndex = new TableIndex(idx);
//alert('cant move to '+idx.x + ","+idx.y)
return false;
}
}
return true;
}
function forceAddShapeAt(shape, startIdx) {
window.CURRENT_SHAPE = shape;
for(let i = 0; i < shape.blocks.length; ++i)
{
const block = shape.blocks[i];
block.controllable = true;
const idx = startIdx + block.Point.x + (block.Point.y * BOARD_X);
if (!moveableBlock(idx)) {
window.clearInterval(window.interval);
window.INPUT_PAUSED = true;
window.NEXT_SHAPE = null;
alert('Game Over!');
return false;
}
GAMEDATA[idx].Block = block;
}
return true;
}
function replaceChildren(parent, child) {
while(parent.lastChild) {
parent.removeChild(parent.lastChild);
}
if(child !== null) parent.appendChild(child);
}
function draw() {
if(!window.NEXT_SHAPE) return false;
for (let i = GAMEDATA.length - 1; i >= 0; --i) {
let elem = document.querySelector('#gameTable #cell_'+i);
if(GAMEDATA[i].Block !== null)
replaceChildren(elem, GAMEDATA[i].Block.element());
else
replaceChildren(elem, null);
}
// update score
let score = (ticks * 10) + (cleared * 1000) + extraScore + "";
replaceChildren(document.querySelector('#score'), document.createTextNode(score));
// update next shape
replaceChildren(document.querySelector('#nextShape'), window.NEXT_SHAPE.element());
}
function startTimer() {
window.clearInterval(window.interval);
if(!window.NEXT_SHAPE) return;
window.interval = window.setInterval(tick, TICK_DELAY);
}
initTable = function() {
const table = document.createElement("table");
table.id = "gameTable";
for(let i = 0; i < BOARD_Y; ++i) {
const row = document.createElement("tr");
for(let j = 0; j < BOARD_X; ++j) {
GAMEDATA.push(new Cell(j,i));
const cell = document.createElement("td");
cell.id = "cell_"+(GAMEDATA.length-1);
cell.width = BLOCK_SIZE + 2;
cell.height = BLOCK_SIZE + 2;
cell.border=1;
row.appendChild(cell)
}
table.appendChild(row);
}
replaceChildren(document.querySelector('#gameboard'), table);
};
window.onload = function() {
initTable();
draw();
};
window.start = function(){
window.paused = false;
replaceChildren(document.querySelector("#pause"), document.createTextNode("PAUSE"));
replaceChildren(document.querySelector("#start"), document.createTextNode("RESTART"));
window.clearInterval(interval);
window.NEXT_SHAPE = randomProperty(SHAPES);
window.CURRENT_SHAPE = null;
window.GAMEDATA = [];
window.interval = null;
window.ticks = 0;
window.cleared = 0;
window.extraScore = 0;
initTable();
//document.querySelector('#nextShape').innerHTML = window.NEXT_SHAPE.html();
draw();
startTimer();
};
window.paused = false;
window.togglePause = function(elem) {
if(window.paused) {
window.paused = false;
replaceChildren(elem, document.createTextNode("PAUSE"));
startTimer();
} else {
window.paused = true;
replaceChildren(elem, document.createTextNode("UNPAUSE"));
clearInterval(window.interval);
}
};