-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.js
179 lines (170 loc) · 5.58 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
function Game() {
this.board = new Board();
this.keyboard = new Keyboard();
this.level = new Level();
this.score = new Score(this.level);
this.nextTetromino = 0;
this.tetrominoInQueue = 0;
this.tetrominoInHold = 0;
this.keyHandler = 0;
this.timeout = 0;
this.display = new Display();
this.pause = false;
this.hold = false;
this.lastKeyPress = new Date().getTime();
this.gameOver = false;
}
Game.prototype.start = function() {
this.display.load("#boardBoard", "#scoreBoard", "#holdBoard", "#queueBoard", "#helpBoard", this.board, this.score, this.tetrominoInHold, this.tetrominoInQueue);
this.display.showHelp();
this.nextTetromino = this.level.getNextTetromino();
this.tetrominoInQueue = this.level.getNextTetromino();
this.board.addTetromino(this.nextTetromino);
_this = this;
window.addEventListener("blur", function() {
clearTimeout(_this.timeout);
_this.keyboard.remove();
console.log("BLUR");
});
window.addEventListener("focus", function() {
if(!_this.pause) {
_this.downLoop();
_this.keyboard.init(_this.keyHandler);
}
console.log("FOCUS");
});
this.keyHandler = function(event) {
var val = _this.keyboard.onKeyDown(event.keyCode, _this.nextTetromino);
_this.lastKeyPress = new Date().getTime();
switch(val) {
case 0:
// rotate, left, right, down
_this.display.updateBoard();
break;
case 1:
// drop
if(_this.onDrop()) {
clearTimeout(_this.timeout);
_this.downLoop();
_this.display.updateAll(_this.tetrominoInHold, _this.tetrominoInQueue);
}
break;
case 2:
// pause/unpause
clearTimeout(_this.timeout);
_this.keyboard.remove();
// add new keyboard handler to detect "p"
var unpauseMachine = function(event) {
if(_this.keyboard.onKeyDown(event.keyCode, 0) === 2) {
_this.downLoop();
_this.keyboard.init(_this.keyHandler);
_this.pause = false;
document.removeEventListener("keydown", unpauseMachine);
}
};
document.addEventListener("keydown", unpauseMachine);
_this.pause = true;
break;
case 3:
//hold
// if something in hold
// remove nextTetromino from the board
// add tetrominoInHold to board
// swap nextTetromino with tetrominoInHold
// set flag so pressing "c" doesn't let you swap again.
// else nothing in hold
// set tetrominoInhold as nextTetromino
// remove nextTetromino from board
// set nextTetromino as tetrominoInQueue
// set tetrominoInQueue to getNextTetromino
// add nextTetromino
// set flag so pressing "c" doesn't let you swap again.
if(_this.tetrominoInHold) {
// check flag, can I swap again?
if(!_this.hold) {
_this.board.removeTetromino(_this.nextTetromino);
var temp = _this.tetrominoInHold;
_this.tetrominoInHold = _this.nextTetromino;
_this.nextTetromino = temp;
_this.board.addTetromino(_this.nextTetromino);
//set flag
_this.hold = true;
}
} else {
// check flag, can I swap again?
if(!_this.hold) {
_this.tetrominoInHold = _this.nextTetromino;
_this.board.removeTetromino(_this.nextTetromino);
_this.nextTetromino = _this.tetrominoInQueue;
_this.tetrominoInQueue = _this.level.getNextTetromino();
_this.board.addTetromino(_this.nextTetromino);
// set flag
_this.hold = true;
}
}
_this.display.updateBoard();
_this.display.updateHold(_this.tetrominoInHold);
_this.display.updateQueue(_this.tetrominoInQueue);
break;
default:
// something else
break;
}
};
this.keyboard.init(this.keyHandler);
this.downLoop();
this.display.updateAll(this.tetrominoInHold, this.tetrominoInQueue);
};
/**
* Update Display
*/
Game.prototype.updateBoard = function() {
document.getElementById(this.scoreDOM).innerHTML = this.score.print();
document.getElementById(this.scoreDOM).innerHTML += "<br />";
document.getElementById(this.scoreDOM).innerHTML += "QUEUE: " + this.tetrominoInQueue.print();
document.getElementById(this.scoreDOM).innerHTML += "<br />";
document.getElementById(this.scoreDOM).innerHTML += "HOLD: " + (this.tetrominoInHold? this.tetrominoInHold.print(): "NULL");
document.getElementById(this.boardDOM).innerHTML = this.board.print();
};
Game.prototype.downLoop = function() {
var _this = this;
this.timeout = setTimeout(function() {
if(!_this.nextTetromino.down()) {
// tetromino cannot go down anymore
//check lockDelay. Was last keypress less than lockDelay? If so. don't onDrop yet. call this function and return.
if(new Date().getTime() - _this.lastKeyPress >= _this.level.lockDelay) {
_this.onDrop();
_this.display.updateAll(_this.tetrominoInHold, _this.tetrominoInQueue);
}
}
_this.display.updateBoard();
if(!_this.gameOver) _this.downLoop();
console.log(_this.level.lockDelay);
}, _this.level.lockDelay);
};
Game.prototype.onDrop = function() {
// check if row clear?
// add appropriate score
this.score.addScore(this.board.findFullLine());
// stop current tetromino
// get next tetromino
this.nextTetromino = this.tetrominoInQueue;
// add new tetromino
if(this.board.addTetromino(this.nextTetromino)) {
this.tetrominoInQueue = this.level.getNextTetromino();
} else {
this.over();
return false;
}
this.hold = false;
return true;
}
Game.prototype.over = function() {
// stop down loop on current tertromino
// deactivate keyboard
this.keyboard.remove();
this.gameOver = true;
// print appropriate message
console.log("GAME OVER!");
document.getElementById("game_over").style.zIndex = "150";
}