-
Notifications
You must be signed in to change notification settings - Fork 77
/
stack.js
223 lines (214 loc) · 6.8 KB
/
stack.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
function Stack() {
//this.grid;
}
/**
* Creates a matrix for the playfield.
*/
Stack.prototype.new = function(x, y) {
var cells = new Array(x);
for (var i = 0; i < x; i++) {
cells[i] = new Array(y);
}
this.grid = cells;
};
/**
* Adds tetro to the stack, and clears lines if they fill up.
*/
Stack.prototype.addPiece = function(tetro) {
var once = false;
// Add the piece to the stack.
var range = [];
var valid = false;
for (var x = 0; x < tetro.length; x++) {
for (var y = 0; y < tetro[x].length; y++) {
if (tetro[x][y]) {
this.grid[x + piece.x][y + piece.y] = tetro[x][y];
// Get column for finesse
if (!once || x + piece.x < column) {
column = x + piece.x;
once = true;
}
// Check which lines get modified
if (range.indexOf(y + piece.y) === -1) {
range.push(y + piece.y);
// This checks if any cell is in the play field. If there
// isn't any this is called a lock out and the game ends.
if (y + piece.y > 1) valid = true;
}
}
}
}
// Lock out
if (!valid) {
gameState = 9;
msg.innerHTML = 'LOCK OUT!';
menu(3);
return;
}
// Check modified lines for full lines.
range = range.sort(function(a, b) {
return a - b;
});
for (var row = range[0], len = row + range.length; row < len; row++) {
var count = 0;
for (var x = 0; x < 10; x++) {
if (this.grid[x][row]) count++;
}
// Clear the line. This basically just moves down the stack.
// TODO Ponder during the day and see if there is a more elegant solution.
if (count === 10) {
lines++; // NOTE stats
if (gametype === 3) {
if (digLines.indexOf(row) !== -1) {
digLines.splice(digLines.indexOf(row), 1);
}
}
for (var y = row; y >= -1; y--) {
for (var x = 0; x < 10; x++) {
this.grid[x][y] = this.grid[x][y - 1];
}
}
}
}
statsFinesse += piece.finesse - finesse[piece.index][piece.pos][column];
piecesSet++; // NOTE Stats
// TODO Might not need this (same for in init)
column = 0;
statsPiece.innerHTML = piecesSet;
if (gametype !== 3) statsLines.innerHTML = lineLimit - lines;
else statsLines.innerHTML = digLines.length;
this.draw();
};
/**
* Draws the stack.
*/
Stack.prototype.draw = function() {
clear(stackCtx);
draw(this.grid, 0, 0, stackCtx);
// Darken Stack
// TODO wrap this with an option.
stackCtx.globalCompositeOperation = 'source-atop';
stackCtx.fillStyle = 'rgba(0,0,0,0.3)';
stackCtx.fillRect(0, 0, stackCanvas.width, stackCanvas.height);
stackCtx.globalCompositeOperation = 'source-over';
if (settings.Outline) {
var b = ~~(cellSize / 8);
var c = cellSize;
var lineCanvas = document.createElement('canvas');
lineCanvas.width = stackCanvas.width;
lineCanvas.height = stackCanvas.height;
var lineCtx = lineCanvas.getContext('2d');
lineCtx.fillStyle = 'rgba(255,255,255,0.5)';
lineCtx.beginPath();
for (var x = 0, len = this.grid.length; x < len; x++) {
for (var y = 0, wid = this.grid[x].length; y < wid; y++) {
if (this.grid[x][y]) {
if (x < 9 && !this.grid[x + 1][y]) {
lineCtx.fillRect(x * c + c - b, y * c - 2 * c, b, c);
}
if (x > 0 && !this.grid[x - 1][y]) {
lineCtx.fillRect(x * c, y * c - 2 * c, b, c);
}
if (y < 21 && !this.grid[x][y + 1]) {
lineCtx.fillRect(x * c, y * c - 2 * c + c - b, c, b);
}
if (!this.grid[x][y - 1]) {
lineCtx.fillRect(x * c, y * c - 2 * c, c, b);
}
// Diags
if (x < 9 && y < 21) {
if (!this.grid[x + 1][y] && !this.grid[x][y + 1]) {
lineCtx.clearRect(x * c + c - b, y * c - 2 * c + c - b, b, b);
lineCtx.fillRect(x * c + c - b, y * c - 2 * c + c - b, b, b);
} else if (
!this.grid[x + 1][y + 1] &&
this.grid[x + 1][y] &&
this.grid[x][y + 1]
) {
lineCtx.moveTo(x * c + c, y * c - 2 * c + c - b);
lineCtx.lineTo(x * c + c, y * c - 2 * c + c);
lineCtx.lineTo(x * c + c - b, y * c - 2 * c + c);
lineCtx.arc(
x * c + c,
y * c - 2 * c + c,
b,
(3 * Math.PI) / 2,
Math.PI,
true,
);
}
}
if (x < 9) {
if (!this.grid[x + 1][y] && !this.grid[x][y - 1]) {
lineCtx.clearRect(x * c + c - b, y * c - 2 * c, b, b);
lineCtx.fillRect(x * c + c - b, y * c - 2 * c, b, b);
} else if (
!this.grid[x + 1][y - 1] &&
this.grid[x + 1][y] &&
this.grid[x][y - 1]
) {
lineCtx.moveTo(x * c + c - b, y * c - 2 * c);
lineCtx.lineTo(x * c + c, y * c - 2 * c);
lineCtx.lineTo(x * c + c, y * c - 2 * c + b);
lineCtx.arc(
x * c + c,
y * c - 2 * c,
b,
Math.PI / 2,
Math.PI,
false,
);
}
}
if (x > 0 && y < 21) {
if (!this.grid[x - 1][y] && !this.grid[x][y + 1]) {
lineCtx.clearRect(x * c, y * c - 2 * c + c - b, b, b);
lineCtx.fillRect(x * c, y * c - 2 * c + c - b, b, b);
} else if (
!this.grid[x - 1][y + 1] &&
this.grid[x - 1][y] &&
this.grid[x][y + 1]
) {
lineCtx.moveTo(x * c, y * c - 2 * c + c - b);
lineCtx.lineTo(x * c, y * c - 2 * c + c);
lineCtx.lineTo(x * c + b, y * c - 2 * c + c);
lineCtx.arc(
x * c,
y * c - 2 * c + c,
b,
Math.PI * 2,
(3 * Math.PI) / 2,
true,
);
}
}
if (x > 0) {
if (!this.grid[x - 1][y] && !this.grid[x][y - 1]) {
lineCtx.clearRect(x * c, y * c - 2 * c, b, b);
lineCtx.fillRect(x * c, y * c - 2 * c, b, b);
} else if (
!this.grid[x - 1][y - 1] &&
this.grid[x - 1][y] &&
this.grid[x][y - 1]
) {
lineCtx.moveTo(x * c + b, y * c - 2 * c);
lineCtx.lineTo(x * c, y * c - 2 * c);
lineCtx.lineTo(x * c, y * c - 2 * c + b);
lineCtx.arc(
x * c,
y * c - 2 * c,
b,
Math.PI / 2,
Math.PI * 2,
true,
);
}
}
}
}
}
lineCtx.fill();
stackCtx.drawImage(lineCanvas, 0, 0);
}
};
var stack = new Stack();