-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathblock.js
More file actions
345 lines (315 loc) · 6.07 KB
/
Copy pathblock.js
File metadata and controls
345 lines (315 loc) · 6.07 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
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
/**
* the different blocks
* @enum {number}
*/
var TILES = {
I: 0,
J: 1,
L: 2,
O: 3,
S: 4,
T: 5,
Z: 6
};
/**
* @enum {number}
*/
var COLORS = {
EMPTY: 0,
CYAN: 1,
BLUE: 2,
ORANGE: 3,
YELLOW: 4,
GREEN: 5,
MAGENTA: 6,
RED: 7
};
/**
* @type {Array.<string>}
*/
var COLOR_NAMES = [
"",
"cyan.png",
"blue.png",
"orange.png",
"yellow.png",
"green.png",
"magenta.png",
"red.png"
];
/**
* @type {Array.<Object>}
*/
var TILE_SPECS = [
{
name: "I",
color: COLORS.CYAN,
cols: 4,
grid: [
0, 0, 0, 0,
1, 1, 1, 1,
0, 0, 0, 0,
0, 0, 0, 0
]
},
{
name: "J",
color: COLORS.BLUE,
cols: 3,
grid: [
1, 0, 0,
1, 1, 1,
0, 0, 0
]
},
{
name: "L",
color: COLORS.ORANGE,
cols: 3,
grid: [
0, 0, 1,
1, 1, 1,
0, 0, 0
]
},
{
name: "O",
color: COLORS.YELLOW,
cols: 2,
grid: [
1, 1,
1, 1
]
},
{
name: "S",
color: COLORS.GREEN,
cols: 3,
grid: [
0, 1, 1,
1, 1, 0,
0, 0, 0
]
},
{
name: "T",
color: COLORS.MAGENTA,
cols: 3,
grid: [
0, 1, 0,
1, 1, 1,
0, 0, 0
]
},
{
name: "Z",
color: COLORS.RED,
cols: 3,
grid: [
1, 1, 0,
0, 1, 1,
0, 0, 0
]
}
];
/**
* @constructor
* @param {CCSpriteBatchNode} batchNode
*/
var Block = function () {
// every block is a new batch node
this.batchNode = new CCSpriteBatchNode("tiles.png");
this.batchNode.setAnchorPoint(0, 0);
this.matrix = [];
this.cols = 0;
this.color = -1;
};
/**
* @type {?Array.<CCSprite>}
*/
Block.prototype.blocks = null;
/**
* @type {?Array.<number>}
*/
Block.prototype.matrix = null;
/**
* @type {?CCSpriteBatchNode}
*/
Block.prototype.batchNode = null;
/**
* @type {number}
*/
Block.prototype.cols = 0;
/**
* @type {number}
*/
Block.prototype.color = 0;
/**
* @type {?CCPoint}
*/
Block.prototype.position = null;
/**
* lowest position in Y that contains a block
* @type {number}
*/
Block.prototype.lowestY = 0;
/**
* max position in X that contains a block
* @type {number}
*/
Block.prototype.maxX = 0;
/**
* min position in X that contains a block
* @type {number}
*/
Block.prototype.minX = 0;
/**
* sets the block matrix. It should be a square matrix
* @param {Array.<number>} matrix
* @param {number} cols
* @param {number} color
*/
Block.prototype.setMatrix = function (matrix, cols, color) {
this.cols = cols || this.cols;
this.color = color || this.color;
this.matrix = [].concat(matrix);
cols = this.cols;
this.minX = 100;
this.maxX = 0;
// go through the matrix and add as many blocks as we need
var count = 0;
for (var j=0; j < cols; j++) {
for (var i=0; i < cols; i++) {
if (this.matrix[j * cols + i] > 0) {
this.lowestY = (cols - j) - 1;
this.maxX = Math.max(this.maxX, i);
this.minX = Math.min(this.minX, i);
// we assume the matrix is never changed, only rotated
// this means the number of nodes is always the same
var sp = this.batchNode.getChildByTag(count);
if (!sp) {
sp = new CCSprite(COLOR_NAMES[this.color], 1);
this.batchNode.addChild(sp);
sp.setTag(count);
sp.setAnchorPoint(new CCPoint(0, 0));
}
// rows == cols
sp.setPosition(new CCPoint(i * Game.TILE_SIZE, (cols - j) * Game.TILE_SIZE));
count++;
}
}
}
// debug.log("min,max x: " + this.minX + "," + this.maxX);
};
/**
* @param {number} x
* @param {number} y
*/
Block.prototype.setPosition = function (x, y) {
this.position = new CCPoint(x, y);
this.batchNode.setPosition(new CCPoint(x * Game.TILE_SIZE, y * Game.TILE_SIZE));
};
/**
* only rotate if we can actually rotate the matrix
*/
Block.prototype.rotate = function () {
var rotatedMatrix = this.rotateMatrix();
if (this.canMoveDown(rotatedMatrix, Game.matrix, 0)) {
this.setMatrix(rotatedMatrix);
}
};
Block.prototype.rotateMatrix = function () {
var cols = this.cols,
mat = [].concat(this.matrix);
for (var i=0; i < cols; i++) {
for (var j=0; j < cols; j++) {
mat[j * cols + i] = this.matrix[(cols - 1 - i) * cols + j];
}
}
return mat;
};
/**
* just debugs the matrix
*/
Block.prototype.debugMatrix = function (matrix) {
matrix = matrix || this.matrix;
for (var j=this.cols-1; j >= 0; j--) {
var arr = [];
for (var i=0; i < this.cols; i++) {
if (matrix[j*this.cols + i] > 0) {
arr.push(1);
} else {
arr.push(0);
}
}
debug.log("" + arr.join(" "));
}
};
/**
* can this block be moved one line down?
* @param {Array.<number>} matrix
* @return {boolean}
*/
Block.prototype.canMoveDown = function (myMatrix, globalMatrix, dy) {
var cols = this.cols;
if (arguments.length == 1) {
globalMatrix = myMatrix;
myMatrix = this.matrix;
dy = -1;
} else if (arguments == 2) {
dy = -1;
}
for (var j=0; j < cols; j++) {
for (var i=0; i < cols; i++) {
var pos = new CCPoint(this.position.x + i, this.position.y + (cols - j) - 1);
if ((myMatrix[j * cols + i] > 0 && globalMatrix[(pos.y + dy) * Game.COLS + pos.x] > 0)) {
// debug.log(this.position.x + "," + this.position.y + ": " + i + "," + j);
// this.debugMatrix(myMatrix);
// Game.debugMatrix();
return false;
}
}
}
if (this.position.y + this.lowestY > 0) {
return true;
}
return false;
};
/**
* will move the block one row down
*/
Block.prototype.moveDown = function () {
this.setPosition(this.position.x, this.position.y - 1);
};
Block.prototype.moveHorizontally = function (dx) {
if (dx > 0 && (this.position.x + this.maxX) < Game.COLS) {
this.setPosition(this.position.x + 1, this.position.y);
} else if (this.position.x + this.minX > 0) {
this.setPosition(this.position.x - 1, this.position.y);
}
};
/**
* will add the block to the scene
* @param {CCScene} scene
*/
Block.prototype.addToScene = function (scene) {
scene.addChild(this.batchNode);
};
/**
* will remove the block from the scene
* @param {CCScene} scene
*/
Block.prototype.removeFromScene = function (scene) {
scene.removeChild(this.batchNode);
};
/**
* create a new random block
* @return {Block}
*/
Block.random = function () {
var idx = Math.floor(Math.random() * 7);
var spec = TILE_SPECS[idx];
var block = new Block();
block.setMatrix(spec.grid, spec.cols, spec.color);
return block;
};