forked from anhvu0911/Match3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmatch3.js
679 lines (566 loc) · 19 KB
/
match3.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
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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
/*
* a match 3 game, practice on using HTML5 canvas
* https://github.com/anhvu0911/Match3
*
* Author: Nguyen Huu Anh Vu (https://github.com/anhvu0911)
*
* Date: 2013-04-09
*/
//========================================================
// MAIN METHODS
//========================================================
function main(){
gameCanvas = document.getElementById("gameCanvas");
context = gameCanvas.getContext("2d");
slashImg = document.getElementById("slash");
shineImg = document.getElementById("shine");
sunImg = document.getElementById("sun");
requestAnimationFrame = window.requestAnimationFrame ||
window.webkitRequestAnimationFrame ||
window.mozRequestAnimationFrame ||
window.msRequestAnimationFrame ||
window.oRequestAnimationFrame ||
function(callback) {
return setTimeout(callback, 1);
};
// Initialize the board, randomize tokens
// Generate by board[col][row] -> better dropdown management
for(var i = 0; i < TOKEN_PER_COL; i++){
board[i] = [];
for(var j = 0; j < TOKEN_PER_ROW; j++){
board[i][j] = new Token(i, j);
board[i][j].setType(Math.round(Math.random()*NUMBER_OF_TOKEN_TYPE));
}
}
// Draw ONCE, fill the board, loop two times because changing canvas state in loop costs performance
gridCanvas = document.getElementById("gridCanvas");
gridContext = gridCanvas.getContext("2d");
gridContext.fillStyle = "#222";
board.forEach(function(boardCol, i){
boardCol.forEach(function (token, j){
if((i+j) % 2 != 0){
gridContext.fillRect(token.col*CELL_SIZE, token.row*CELL_SIZE, CELL_SIZE, CELL_SIZE);
}
});
});
gridContext.fillStyle = "#111";
board.forEach(function(boardCol, i){
boardCol.forEach(function (token, j){
if((i+j) % 2 == 0){
gridContext.fillRect(token.col*CELL_SIZE, token.row*CELL_SIZE, CELL_SIZE, CELL_SIZE);
}
});
});
toggleClickEvent(true);
toggleMouseMoveEvent(true);
gameCanvas.addEventListener("keydown", hint, false);
checkMatches();
draw();
// board[3][3].setType(SPECIAL);
// board[2][2].special = new MoonToken();
board[3][3].special = new SunToken();
// board[3][4].special = new SunToken();
board[3][4].special = new MoonToken();
}
// register mouse event
function toggleClickEvent(on){
if(on){
gameCanvas.addEventListener("click", selectToken, false);
}else{
gameCanvas.removeEventListener("click", selectToken, false);
}
}
function toggleMouseMoveEvent(on){
if(on){
gameCanvas.addEventListener("mousemove", onMouseMove, false);
}else{
gameCanvas.removeEventListener("mousemove", onMouseMove, false);
gameCanvas.removeEventListener("click", selectToken, false);
}
}
// TODO: Optimize, get token from pool, create = garbage collector
// Factory method, create random Token for col, row
function createToken(col, row){
switch(parseInt(Math.random()*NUMBER_OF_TOKEN_TYPE)){
case RED: return new Token(col, row, RED, "red.png");break;
case ORANGE: return new Token(col, row, ORANGE,"orange.png");break;
case YELLOW: return new Token(col, row, YELLOW,"yellow.png");break;
case GREEN: return new Token(col, row, GREEN,"green.png");break;
case BLUE: return new Token(col, row, BLUE,"blue.png");break;
case MAGENTA:return new Token(col, row, MAGENTA,"magenta.png");break;
case PURPLE:return new Token(col, row, PURPLE,"purple.png");break;
default: return new Token(col, row);
}
}
// Draw the board, tokens
function draw(){
// Don't use context.clearRect(0, 0, gameCanvas.width, gameCanvas.height); // Performance hack
gameCanvas.width = gameCanvas.width;
// draw each Token
board.forEach(function(boardCol, i){
boardCol.forEach(function (token, j){
if (token.special){
token.special.draw(token);
}
token.draw();
});
});
requestAnimationFrame(draw);
}
// Return the row and col of the selected token.
// To be used by selectToken(), onMouseMove()
// e: mouseEvent
function getCell(e){
// Offset from document body
var x = -gameCanvas.offsetLeft;
var y = -gameCanvas.offsetTop;
// Offset by page scroll
if(e.pageX != undefined && e.pageY != undefined){
x += e.pageX;
y += e.pageY;
}else{
x += e.clientX + document.body.scrollLeft + document.documentElement.scrollLeft;
y += e.clientY + document.body.scrollTop + document.documentElement.scrollTop;
}
return new Cell(Math.floor(x / (TOKEN_SIZE+SPACE)), Math.floor(y / (TOKEN_SIZE+SPACE)));
}
// Find the selected Token
var isSelectingFirst = true;
var firstSelectedToken = null;
var lastSelectedToken = null;
function selectToken(e){
var selectedCell = getCell(e);
// Out of bound?
if(selectedCell.col >= TOKEN_PER_COL || selectedCell.row >= TOKEN_PER_ROW){
return;
}
if (isSelectingFirst) {
firstSelectedToken = board[selectedCell.col][selectedCell.row];
firstSelectedToken.setState(SELECT_STATE);
} else {
lastSelectedToken = board[selectedCell.col][selectedCell.row];
// If the same token => Deselect
if (firstSelectedToken.isOnTheSameCellWith(lastSelectedToken)) {
firstSelectedToken.setState();
// If they are next to each other, swap
} else if (lastSelectedToken.isAdjacentTo(firstSelectedToken)) {
firstSelectedToken.setState();
swap(firstSelectedToken, lastSelectedToken);
// If they are far away, re-select the first token
} else {
// Deselect first token
firstSelectedToken.setState();
// set the first token as the newly selected
firstSelectedToken = board[selectedCell.col][selectedCell.row];
firstSelectedToken.setState(SELECT_STATE);
isSelectingFirst = !isSelectingFirst;
}
}
isSelectingFirst = !isSelectingFirst;
}
// TODO: Find the selected Token by dragging
var firstDraggedToken = null;
var lastDraggedToken = null;
var oldHoverCell = new Cell(0,0);
function onMouseMove(e){
var hoverCell = getCell(e);
// Drag
if(e.buttons){
/*if(firstDraggedToken == null || !firstDraggedToken.isOnTheSameCellWith(hoverCell)){
if (isSelectingFirst) {
firstDraggedToken = board[hoverCell.col][hoverCell.row];
firstDraggedToken.setState(SELECT_STATE);
} else {
firstDraggedToken.setState();
lastDraggedToken = board[hoverCell.col][hoverCell.row];
if (lastDraggedToken.isAdjacentTo(firstDraggedToken)) {
swap(firstDraggedToken, lastDraggedToken);
firstDraggedToken = null;
// Find a way to force mouseup
} else {
firstDraggedToken = board[hoverCell.col][hoverCell.row];
firstDraggedToken.setState(SELECT_STATE);
isSelectingFirst = !isSelectingFirst;
}
}
isSelectingFirst = !isSelectingFirst;
}*/
// Hover
} else{
if(oldHoverCell.row != hoverCell.row || oldHoverCell.col != hoverCell.col){
if(hoverCell.col < TOKEN_PER_COL && hoverCell.row < TOKEN_PER_ROW){
if(board[oldHoverCell.col][oldHoverCell.row].state != SELECT_STATE){
board[oldHoverCell.col][oldHoverCell.row].setState(NORMAL_STATE);
}
oldHoverCell = hoverCell;
board[hoverCell.col][hoverCell.row].setState(HOVER_STATE);
} else {
if(board[oldHoverCell.col][oldHoverCell.row].state != SELECT_STATE){
board[oldHoverCell.col][oldHoverCell.row].setState(NORMAL_STATE);
}
}
}
}
}
// Swap token a and b
// swapBack: a boolean - if after swap, no match found, swap back
function swap(a, b, swapBack){
toggleClickEvent(false);
toggleMouseMoveEvent(false);
// If one is special token, destroy all tokens of the same type as the other
if(a.type == SPECIAL){
a.special.explode([a], a, null, b.type);
}else if (b.type == SPECIAL){
b.special.explode([b], b, null, a.type);
}else{
board[oldHoverCell.col][oldHoverCell.row].setState(NORMAL_STATE);
var frame = 0;
var deltaX = (b.x - a.x) / TOTAL_FRAME;
var deltaY = (b.y - a.y) / TOTAL_FRAME;
(function moveSwappedToken(){
a.x += deltaX;
a.y += deltaY;
b.x -= deltaX;
b.y -= deltaY;
frame++;
if(frame == TOTAL_FRAME){
toggleClickEvent(true);
toggleMouseMoveEvent(true);
a.swapWith(b);
if(!swapBack){
checkMatches(function(){
swap(a,b, true);
}); // No match found, swap again
}
}else{
requestAnimationFrame(moveSwappedToken);
}
})();
}
}
// Find the list of matches, auto-call explode
function checkMatches(callback){
var matchLists = findMatches();
// Turn all hint tokens into normal
// board.forEach(function(boardCol, i){
// boardCol.forEach(function (token, j){
// if (token.state == HINT_STATE){
// token.setState(NORMAL_STATE);
// }
// });
// });
// Found some matches, explode them!
if(matchLists.length > 0){
explode(deduplicateInMatchList(matchLists));
} else {
if (typeof(callback) == 'function') callback();
}
}
// TODO: Optimization, some token does not need to search again
// Search the matching pair, using trace algorithm
function findMatches(){
var matchLists = [];
board.forEach(function(boardCol, i){
boardCol.forEach(function (token, j){
traceAndAddToMatchList([token], token, 0, 1, false); // down
traceAndAddToMatchList([token], token, 1, 0, false); // right
});
});
// Starting on a token, trace similar tokens
// matchLists: a list of all matches
// tempMatchLists: temporary list to construct match
// token,rowIndent,colIndent: next token position from current token
// added: Has this tempMatchLists been added to matchLists?
function traceAndAddToMatchList(tempMatchLists, token, rowIndent, colIndent, added){
// prevent out of bound
if(board[token.col + colIndent] == undefined) return;
var nextToken = board[token.col + colIndent][token.row + rowIndent];
var inMatchlist;
// Found a match
if(nextToken != undefined && nextToken.type == token.type){
// Check if that match exist, if yes, no need to create new matchlist
inMatchlist = matchLists.some(function(match){
return match.indexOf(token) > 0 && match.indexOf(nextToken) > 0;
});
// If not, add to the match list
if(!inMatchlist){
tempMatchLists.push(nextToken);
if(tempMatchLists.length > 2 && !added) {
matchLists.push(tempMatchLists);
added = true;
}
traceAndAddToMatchList(tempMatchLists, nextToken, rowIndent, colIndent, added);
}
}
}
return matchLists;
}
// Merge match lists that have the same tokens
function deduplicateInMatchList(matchLists){
var commonToken = null;
// var commonTokenList = [];
for(i = 0; i < matchLists.length - 1; i++){
for(j = i+1; j < matchLists.length; j++){
if(commonToken = matchLists[i].intersectWith(matchLists[j])){
// commonTokenList.push(commonToken);
matchLists[i].merge(matchLists[j]);
matchLists.splice(j,1);
matchLists[i].push(commonToken);
break;
}
}
}
return matchLists;
}
// TODO: Animation Explosion effect
function explode(matchLists) {
var waitTime = TOTAL_FRAME;
matchLists.forEach(function(match){
var boom = false;
match.forEach(function(token) {
if(token.special){
waitTime = token.special.explode(match, token);
boom = true;
return;
}
});
if(!boom){
var specialToken = decideStarToken(match);
// Gather tokens to create special token
if(specialToken){
match.forEach(function(token) {
moveToken(token, token.x, token.y, specialToken.x, specialToken.y, TOTAL_FRAME/2);
});
// Other match explodes normally
} else {
match.forEach(function(token) {
token.setState(EXPLODE_STATE);
});
}
}
});
waitForAnimationFinish(waitTime, function(){
dropDown(matchLists);
});
// 4 matches = black hole
// >=5 matches I = same color
// >=5 matches L,T = shuriken
function decideStarToken(match){
var specialToken = null;
// create black hole token at selected token, or the 2nd in match
if (match.length == 4){
var index = 1;
match.forEach(function(token, i){
if(firstSelectedToken != null && lastSelectedToken != null &&
(token.isOnTheSameCellWith(firstSelectedToken) || token.isOnTheSameCellWith(lastSelectedToken))) {
index = i;
firstSelectedToken = null;
lastSelectedToken = null;
}
});
specialToken = match[index];
match.splice(index,1);
specialToken.special = new SunToken();
console.log("4 match: " + specialToken);
// 5-in-a-row match on a straight line
} else if (match.length == 5){
specialToken = match[2];
match.splice(2,1);
specialToken.setType(SPECIAL);
console.log("5 I straight line: " + specialToken);
// 5-in-a-row match on a zig zag line = shuriken
} else if (match.length > 5){
specialToken = match.pop();
console.log(match.indexOf(specialToken) + " 5 L: " + specialToken);
match.splice(match.indexOf(specialToken),1);
specialToken.special = new MoonToken();
}
return specialToken;
}
}
// TODO: Make animation of dropping
// Temp: Replace with above appropriate tokens
function dropDown(matchLists){
// Merge all matches into An array of array
// each is a token with same column, sort row from low to high
var matches = [];
matchLists.forEach(function(match){
match.forEach(function(token){
if(matches[token.col] == undefined){
matches[token.col] = [];
}
matches[token.col].addToken(token);
});
});
// Start shift board[column] based on matches
for(var i = 0; i < matches.length; i++){
if(matches[i] == undefined) continue;
var currentRow = TOKEN_PER_ROW - 1;
var currentMatchIndex = matches[i].length - 1;
var currentBlankRow = matches[i][currentMatchIndex].row;
var token = null;
while (currentBlankRow >= 0){
// Found a match, this item will be removed
if (currentMatchIndex >= 0 && currentRow == matches[i][currentMatchIndex].row){
currentMatchIndex--;
// Else, drop the above tokens to fill the blank
}else if (currentRow < currentBlankRow && currentRow >= 0){
token = board[i][currentRow];
moveToken(token, token.x, token.y, token.x, token.y + (currentBlankRow - currentRow)*CELL_SIZE);
board[i][currentBlankRow] = token;
token.row = currentBlankRow;
currentBlankRow--;
}
// Move all stock tokens? fill columns with new tokens
if (currentRow < 0){
token = createToken(i, currentBlankRow);
moveToken(token, token.x, token.y - matches[i].length*CELL_SIZE, token.x, token.y);
board[i][currentBlankRow] = token;
currentBlankRow--;
}
currentRow--;
}
}
waitForAnimationFinish(TOTAL_FRAME*1.5, checkMatches);
}
function waitForAnimationFinish(frame, callback){
toggleClickEvent(false);
toggleMouseMoveEvent(false);
var f = 0;
(function wait(){
f++;
if (f < frame){
requestAnimationFrame(wait);
} else {
toggleClickEvent(true);
toggleMouseMoveEvent(true);
if (typeof(callback) == 'function'){
callback();
}
}
})();
}
// Move a token from start to end position
function moveToken(token, startX, startY, endX, endY, totalFrame){
totalFrame = totalFrame == null ? TOTAL_FRAME : totalFrame;
var frame = 0;
var deltaX = (endX - startX) / totalFrame;
var deltaY = (endY - startY) / totalFrame;
token.x = startX;
token.y = startY;
(function move(){
// Change it into new position
token.x += deltaX;
token.y += deltaY;
frame++;
if (frame < totalFrame){
requestAnimationFrame(move);
} else {
token.x = endX;
token.y = endY;
}
})();
}
// TODO: remove hint after 10 sec, or after they match
// Find all possible matches
function hint(e){
var hintList = getHintList();
if (hintList.length == 0){
scamble(checkMatches);
}
// console.log("================Check Hint===================");
// hintList.forEach(function (hint){
// hint.forEach(function(token){
// token.setState(HINT_STATE);
// console.log(token);
// });
// console.log("---");
// });
// Randomly display a hint
var randomIndex = Math.round(Math.random()*(hintList.length-1));
hintList[randomIndex].forEach(function(token){
token.setState(HINT_STATE);
});
}
// TODO: remove duplicate in hintList
// Find all possible matches [[pair1, pair1], [pair2, pair2]...]
function getHintList(){
var hintList = [];
board.forEach(function(boardCol, i){
boardCol.forEach(function (token, j){
traceAndAddToPotentialList([token], token, 1, 0, token.type); // down
traceAndAddToPotentialList([token], token, 0, 1, token.type); // right
});
});
function traceAndAddToPotentialList(tempMatchLists, token, rowIndent, colIndent, type){
var nextToken = undefined;
var inPotentialList;
// Is next token out of bound?
if(board[token.col + colIndent] != undefined){
nextToken = board[token.col + colIndent][token.row + rowIndent];
}
if (nextToken != undefined){
// Found a match
if(nextToken.type == type){
// Add to match lists and continue
tempMatchLists.push(nextToken);
traceAndAddToPotentialList(tempMatchLists, nextToken, rowIndent, colIndent, token.type);
// Found a two pair (o o), add token at the head (x o o) and tail (o o x) to potential list
}else if (tempMatchLists.length > 1){
// tail (o o x)
evaluateNeighbors(nextToken, rowIndent == 0, true, colIndent == 0, true);
// head (x o o)
if(rowIndent == 1){
evaluateNeighbors(board[nextToken.col][nextToken.row-3], true, rowIndent == 0, true, colIndent == 0);
} else if (colIndent == 1){
if(board[nextToken.col-3] != undefined){
evaluateNeighbors(board[nextToken.col-3][nextToken.row], true, rowIndent == 0, true, colIndent == 0);
}
}
// Search for middle potential token (o x o)
} else if (tempMatchLists.length == 1){
if(board[nextToken.col + colIndent] == undefined) return;
var nextnextToken = board[nextToken.col + colIndent][nextToken.row + rowIndent];
if(nextnextToken != undefined && nextnextToken.type == type){
evaluateNeighbors(nextToken, colIndent == 1, colIndent == 1, rowIndent == 1, rowIndent == 1);
}
}
// In case, the tail (o o |x) is out-of-bound, search the head (x o o)
} else if (tempMatchLists.length > 1){
if(rowIndent == 1){
evaluateNeighbors(board[token.col][token.row-2], true, rowIndent == 0, true, colIndent == 0);
} else if (colIndent == 1){
if(board[token.col-2] != undefined){
evaluateNeighbors(board[token.col-2][token.row], true, rowIndent == 0, true, colIndent == 0);
}
}
}
// Search in 4 directions
function evaluateNeighbors(hintToken, searchUp, searchDown, searchLeft, searchRight){
if(hintToken == undefined) return;
if(searchUp && board[hintToken.col][hintToken.row-1] != undefined){
addTokenToHintList(board[hintToken.col][hintToken.row-1], hintToken);
}
if(searchDown && board[hintToken.col][hintToken.row+1] != undefined){
addTokenToHintList(board[hintToken.col][hintToken.row+1], hintToken);
}
if(searchLeft && board[hintToken.col-1] != undefined){
addTokenToHintList(board[hintToken.col-1][hintToken.row], hintToken);
}
if(searchRight && board[hintToken.col+1] != undefined){
addTokenToHintList(board[hintToken.col+1][hintToken.row], hintToken);
}
}
function addTokenToHintList(hintToken, tokenToSwap){
if (hintToken.type == type){
hintList.push([tokenToSwap, hintToken]);
}
}
}
return hintList;
}
// Rearrange the board
function scamble(callback){
console.log("No Match found SCRAMBLEEEEEEEEEEEEEEEEEEEEEE");
//if (typeof(callback) == 'function') callback();
}