-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathgame.js
More file actions
306 lines (274 loc) · 6.92 KB
/
Copy pathgame.js
File metadata and controls
306 lines (274 loc) · 6.92 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
/**
* @enum {number}
*/
var GAME_STATES = {
RUNNING: 0,
PAUSED: 1,
REMOVING_ROW: 2,
OVER: 3
};
/**
* returns a simple hash from the point coords.
* Assumes coords are never greater than 0xff (which should be just
* fine for our game)
* @return {number}
*/
var hashFromCoord = function (point) {
return (0xffff & ((point.x << 8) | point.y));
};
/**
* @type {Object}
*/
var Game = {};
/**
* @const
* @type {number}
*/
Game.TILE_SIZE = 24;
/**
* @const
* @type {number}
*/
Game.COLS = 13;
/**
* @const
* @type {number}
*/
Game.ROWS = 20;
/**
* The main game scene
* @type {?CCScene}
*/
Game.scene = null;
/**
* The current game state
* @type {number}
*/
Game.state = GAME_STATES.RUNNING;
/**
* The current speed of the game
*/
Game.speed = 200;
/**
* Internal time accumulator
* @type {number}
* @ignore
*/
Game.__timeAccum = 0.0;
/**
* @type {?Block}
*/
Game.currentBlock = null;
/**
* The matrix for the game
* @type {?Array.<number>}
*/
Game.matrix = null;
/**
* The batch node
* @type {?CCSpriteBatchNode}
*/
Game.batchNode = null;
/**
* adds a new random block at the top of the screen
*/
Game.addNewBlock = function (scene) {
Game.currentBlock = Block.random();
Game.currentBlock.setPosition(5, Game.ROWS - 1);
Game.currentBlock.addToScene(scene);
};
/**
* just debugs the internal matrix
*/
Game.debugMatrix = function () {
for (var j=Game.ROWS - 1; j >= 0; j--) {
var arr = [];
for (var i=0; i < Game.COLS; i++) {
if (this.matrix[j*Game.COLS + i] > 0) {
arr.push(1);
} else {
arr.push(0);
}
}
debug.log("" + arr.join(" "));
}
};
/**
* Will copy a block to the game matrix and add the needed
* sprites to fill in the colors
* @param {Block} block
*/
Game.copyToMatrix = function (block) {
var cols = block.cols;
for (var j=0; j < cols; j++) {
for (var i=0; i < cols; i++) {
if (block.matrix[j * cols + i] > 0) {
var pos = new CCPoint(block.position.x + i, block.position.y + (cols - j) - 1);
// debug.log("creating block for matrix in " + pos.x + "," + pos.y);
this.matrix[pos.y * Game.COLS + pos.x] = 1;
var sprite = new CCSprite(COLOR_NAMES[block.color], 1);
sprite.setTag(hashFromCoord(pos));
sprite.setAnchorPoint(new CCPoint(0, 0));
sprite.setPosition(new CCPoint(pos.x * Game.TILE_SIZE, pos.y * Game.TILE_SIZE));
this.batchNode.addChild(sprite);
}
}
}
// this.debugMatrix();
};
/**
* will check the matrix for full lines, and if so, then remove those lines
*/
Game.checkLines = function () {
for (var j=0; j < Game.ROWS; j++) {
var full = true;
for (var i=0; i < Game.COLS; i++) {
var hasBlock = false;
if (this.matrix[j * Game.COLS + i] > 0) {
hasBlock = true;
if (j == Game.ROWS - 1) {
Game.gameOver();
return;
}
}
full = full && hasBlock;
}
if (full) {
// destroy row j and check the same row
Game.destroyRow(j--);
}
}
};
/**
* destroy a row
* @param {number} row
*/
Game.destroyRow = function (row) {
CCAudioManager.playEffect("check_in.caf");
Game.state = GAME_STATES.REMOVING_ROW;
var i;
for (i=0; i < Game.COLS; i++) {
var point = new CCPoint(i, row);
this.batchNode.removeChildByTag(hashFromCoord(point));
// reset the matrix as well
Game.matrix[row * Game.COLS + i] = 0;
}
// now move all elements from the row after to the next one
for (var j=row+1; j < Game.ROWS; j++) {
for (i=0; i < Game.COLS; i++) {
if (Game.matrix[j * Game.COLS + i] > 0) {
var hash = hashFromCoord(new CCPoint(i, j));
var sprite = this.batchNode.getChildByTag(hash);
sprite.setTag(hashFromCoord(new CCPoint(i, j-1)));
sprite.setPosition(new CCPoint(i * Game.TILE_SIZE, (j-1) * Game.TILE_SIZE));
// and update matrix
Game.matrix[ j * Game.COLS + i] = 0;
Game.matrix[(j-1) * Game.COLS + i] = 1;
} else {
// just copy the blank space
Game.matrix[(j-1) * Game.COLS + i] = 0;
}
}
}
Game.state = GAME_STATES.RUNNING;
};
/**
* should display the game over function
*/
Game.gameOver = function () {
Game.state = GAME_STATES.OVER;
Game.cleanup();
CCAudioManager.stopBackgroundMusic();
debug.log("game over!");
CCDirector.popScene();
};
/**
* clean up the game scene and other things
*/
Game.cleanup = function () {
Game.scene.unregisterAsTouchHandler();
CCScheduler.unschedule(Game.__updateId);
};
/**
* scroll the block one line below, if not possible, then
* move the contents of the block to the matrix
*/
Game.tick = function () {
// debug.log("tick");
if (Game.currentBlock && Game.state == GAME_STATES.RUNNING) {
if (Game.currentBlock.canMoveDown(Game.matrix)) {
// debug.log(" will move block down");
Game.currentBlock.moveDown();
} else {
// debug.log(" need to copy block to matrix");
// debug.log(" copying block from " + Game.currentBlock.position.x + "," + Game.currentBlock.position.y);
Game.copyToMatrix(Game.currentBlock);
Game.currentBlock.removeFromScene(Game.scene);
Game.addNewBlock(Game.scene);
}
// check for full lines
Game.checkLines();
}
};
/**
* start a new game
*/
Game.start = function () {
// load tile frames
CCSpriteFrameCache.addSpriteFramesWithFile("tiles.plist");
Game.matrix = new Array(Game.COLS * Game.ROWS);
Game.batchNode = new CCSpriteBatchNode("tiles.png");
Game.batchNode.setPosition(new CCPoint(0, 0));
Game.batchNode.setAnchorPoint(new CCPoint(0, 0));
// create the scene
var scene = new CCScene();
scene.setPosition(new CCPoint(4, 0));
var background = new CCSprite("background.png");
background.setPosition(new CCPoint(-4, 0));
scene.addChild(background);
scene.addChild(Game.batchNode);
Game.state = GAME_STATES.RUNNING;
Game.addNewBlock(scene);
// schedule every frame
Game.__updateId = CCScheduler.schedule(function (delta) {
Game.__timeAccum += delta;
if (Game.__timeAccum >= Game.speed) {
Game.tick();
Game.__timeAccum = 0.0;
}
}, 0);
scene.registerAsTouchHandler();
scene.touchesBegan = function (points) {
this.initialPoint = points[0];
this.movedBlock = false;
};
scene.touchesMoved = function (points) {
if (Game.currentBlock) {
var distx = points[0].x - this.initialPoint.x;
var disty = points[0].y - this.initialPoint.y;
if (Math.abs(distx) > Game.TILE_SIZE) {
Game.currentBlock.moveHorizontally(distx);
this.initialPoint = points[0];
Game.movedBlock = true;
}
// do not use the drag down/up for now
// but do not allow to rotate the block using that
if (Math.abs(disty) > Game.TILE_SIZE) {
Game.movedBlock = true;
}
}
};
scene.touchesEnded = function (points) {
if (Game.currentBlock && !Game.movedBlock) {
Game.currentBlock.rotate();
}
Game.movedBlock = false;
};
Game.scene = scene;
// play the music (not at full volume, and loop)
CCAudioManager.playBackgroundMusic("music.mp3", true);
CCAudioManager.setBackgroundMusicVolume(0.2);
// run the game scene with a transition
var transitionScene = new CCTransitionTurnOffTiles(1.0, Game.scene);
CCDirector.pushScene(transitionScene);
};