-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.js
254 lines (186 loc) · 5.44 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
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
var player;
var stage;
var victoryScreen = new Image;
var defeatScreen = new Image;
var retryButton;
var menuButton;
//Control the canvas size
function resizeCanvas()
{
Global.canvas.width = window.innerWidth;
Global.canvas.height = window.innerHeight;
}
//Init all variables and objects
function gameStart(stageCode)
{
//Game is not on menu anymore
Global.mainMenu = false;
//define ingame menu screens
victoryScreen.src = 'imgs/screens/victoryScreen.png';
defeatScreen.src = 'imgs/screens/defeatScreen.png';
//set retry button
retryButton = new Button(473,383,170,50,
function(){
setGame(stageCode);
});
//set menu button
menuButton = new Button(709,383,170,50,
function(){
console.log("crico");
Global.gameIsPaused = true;
Global.mainMenu = true;
//reload main menu sound
audio.load();
audio.play();
frameLoop();
});
window.addEventListener("keydown", getKeyDown);
window.addEventListener("mousemove", onMouseMove);
window.addEventListener("click", onMouseClick);
setGame(stageCode);
}
//Control input listener
function getKeyDown(e){
//Should work only when game is running
if(!Global.gameIsPaused)
{
//Up arrow -> JUMP \o/
if (e.keyCode == '38') {
player.jump();
}
//Q buttom
else if (e.keyCode == '81') {
Global.onOffTiles.forEach(function(obj){
obj.on = !obj.on;
});
}
}
//P pause
if (e.keyCode == '80'){
if(Global.gameIsPaused && (!Global.gameIsWon) && (!Global.gameIsLost))
{
Global.gameIsPaused = false;
gameLoop();
}
else
{
Global.gameIsPaused = true;
pauseLoop();
}
}
if(e.keyCode == '82')
{
if(retryButton.isActive){
retryButton.clickFunction();
}
}
}
function startObjs()
{
//Call all "gameObjs" start function
Global.gameObjs.forEach(function(obj){
obj.start();
});
}
function setGame(stageCode)
{
//everytime game is set, the block sizes are aswell, based on the canvas size. that makes it run in any window size
Global.blockSize = Global.canvas.height / 21;
//clean all arrays from the game
Global.gameObjs.length = 0;
Global.onOffTiles.length = 0;
//instantiate player again
player = new Player(Global.canvas.width/2 - 32 - 8*Global.blockSize,Global.blockSize * 10, 2*Global.blockSize, 2*Global.blockSize);
//instantiate stage again
stage = new Stage(stageCode,player);
//clean all tiles
stage.tile.length = 0;
//unable retry button
retryButton.isActive = false;
menuButton.isActive = false;
//set all stage and game objs again
stage.setStage();
startObjs();
//unpause the game
Global.gameIsPaused = Global.gameIsWon = Global.gameIsLost = false;
Global.lastFrameTime = new Date();
gameLoop();
}
//Main game loop
function gameLoop()
{
var requestAnimationFrame = window.requestAnimationFrame || window.mozRequestAnimationFram || window.msRequestAnimationFrame;
if(stage.music.paused){
stage.music.play();
}
//Clear canvas
Global.ctx.drawImage(stage.background,0,0,Global.canvas.width,Global.canvas.height);
Global.deltaTime = new Date() - Global.lastFrameTime;
Global.lastFrameTime = new Date();
//Call all "gameObjs" update function
Global.gameObjs.forEach(function(obj){
obj.update();
});
//Game continues if it isn't paused
if(!Global.gameIsPaused)
{
//Request game loop
requestAnimationFrame(gameLoop);
}
}
//Paused game loop
function pauseLoop()
{
var requestAnimationFrame = window.requestAnimationFrame || window.mozRequestAnimationFram || window.msRequestAnimationFrame;
Global.deltaTime = new Date() - Global.lastFrameTime;
Global.lastFrameTime = new Date();
if(!stage.music.paused){
stage.music.pause();
}
if(Global.gameIsWon || Global.gameIsLost){
//call endGameLoop
retryButton.isActive = true;
menuButton.isActive = true;
endGameLoop();
}else if(Global.gameIsPaused){
//Request pause loop
requestAnimationFrame(pauseLoop);
}
}
function endGameLoop()
{
var requestAnimationFrame = window.requestAnimationFrame || window.mozRequestAnimationFram || window.msRequestAnimationFrame;
Global.deltaTime = new Date() - Global.lastFrameTime;
Global.lastFrameTime = new Date();
if(Global.gameIsWon){
Global.ctx.drawImage(victoryScreen,Global.canvas.width/2 - victoryScreen.width/2,Global.canvas.height/2 - victoryScreen.height/2);
}else if (Global.gameIsLost) {
Global.ctx.drawImage(defeatScreen,Global.canvas.width/2 - defeatScreen.width/2,Global.canvas.height/2 - victoryScreen.height/2);
}
//Request endGameLoop
if(Global.gameIsLost || Global.gameIsWon){
requestAnimationFrame(endGameLoop);
}
}
function onMouseMove (ev) {
//Pega a posição do mouse na tela
if (ev.layerX || ev.layerX == 0) //Tratamento para o Firefox
{
Global.mousePositionX = ev.layerX;
Global.mousePositionY = ev.layerY;
}
Global.mousePositionX -= Global.canvas.offsetLeft;
Global.mousePositionY -= Global.canvas.offsetTop;
//DEBUG -> console.log(Global.mousePositionX + " " + Global.mousePositionY);
}
function onMouseClick(ev){
//DEBUG ->
console.log(Global.mousePositionX + " " + Global.mousePositionY)
Global.buttons.forEach(function(obj){
if(obj.isActive){
if(obj.mouseIsOver(Global.mousePositionX, Global.mousePositionY)){
obj.clickFunction();
}
}
});
}