-
Notifications
You must be signed in to change notification settings - Fork 16
/
pause-handler.js
302 lines (251 loc) · 7.25 KB
/
pause-handler.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
/*global me,game*/
/**
* Watches the pause key, effectively pausing the game
* when the user presses it.
*
* It stops everything and shows a text "PAUSED" with
* a Pause Menu.
* It waits until the user presses the "pause" key again
* or selects "RESUME" from the menu.
*
* @note This file has at least 3 ugly hacks I did to
* make the code work.
* I'm very embarrassed.
* Maybe the shame of publicly exposing them will
* make me fix them quicker.
*/
game.pauseHandlerEntity = me.Renderable.extend({
/**
* Initializes the thing that keeps looking for the
* "pause" key.
*/
init : function () {
// Bleh.
this.parent(new me.Vector2d(5, 0),
me.game.viewport.width, me.game.viewport.height);
// This is very important
//
// We're attaching callbacks to know when the game
// loses focus (user clicked elsewhere, changed
// browser tab, changed OS window...)
//
// To do so we MUST remove melonJS's default way
// of handling this.
//
// By default melonJS simply calls `me.state.pause()`
// but since we're showing a Pause Menu and stuff
// we're doing like this:
//
// - When we lose focus, pretend the user has hit the
// "pause" key (on this case, ESC).
//
// Got it?
// First, removing melonJS' handlers.
me.sys.stopOnBlur = false;
me.sys.pauseOnBlur = false;
me.sys.resumeOnFocus = false;
// Then, attaching our own
// for when losing focus...
window.addEventListener("blur", function () {
// Remember to change this if you ever decide to
// NOT use ESC as a "pause" key.
me.input.triggerKeyEvent(me.input.KEY.ESC, true);
}, false);
// Aww yeah, now we're talkin'
this.updateWhenPaused = true;
// Need to draw on top of most things.
this.z = 30;
this.floating = true;
// This will make sure the menu gets initialized
// at the right time.
this.menu = null;
this.enableMenu(false);
// Attaching ourselves to the global game namespace.
// I'm not proud of this, but it's necessary so we can
// use it on the `this.handler`.
// Check below.
game.pauseHandler = this;
// Will keep watching the pause key directly
// (when the user releases the pause key)
this.handler = me.event.subscribe(me.event.KEYDOWN, function (action, keyCode) {
// Only controlling the menu when the game is paused.
//
// Let's grab the user input and control the menu.
// NOTE: HARDCODED KEYS11!!!111
if ((me.state.isPaused()) &&
(game.pauseHandler.menu)) {
if (keyCode === me.input.KEY.DOWN) {
game.pauseHandler.menu.next();
me.audio.play("menu", false, null, me.save.sfxVolume);
}
else if (keyCode === me.input.KEY.UP) {
game.pauseHandler.menu.previous();
me.audio.play("menu", false, null, me.save.sfxVolume);
}
else if (keyCode === me.input.KEY.ENTER) {
game.pauseHandler.menu.activate();
me.audio.play("menu", false, null, me.save.sfxVolume);
// HACK BUG TODO
// I'm really embarassed to place this here.
// I need this `return` because the "pause" key
// is `me.input.KEY_ENTER`.
//
// So to pause the user presses "ENTER".
// The way it was, if he pressed "ENTER" again it
// would unpause.
// But we need it to control the menu option with
// "ENTER".
// So we'll assign a higher priority for "ENTER"
// to control the menu when paused.
//
// Sorry if it seems confusing.
// Please contact me if you need a better explanation
// Alexandre Dantas <eu@alexdantas.net>
return;
}
}
// Now, pausing or unpausing the game!
// This is where we enable/disable the Pause Menu.
if (action === "pause") {
if (me.state.isPaused()) {
me.state.resume();
me.audio.resumeTrack();
game.pauseHandler.enableMenu(false);
}
else {
game.pauseHandler.enableMenu(true);
me.state.pause(true);
me.audio.pauseTrack();
}
}
});
},
update : function(dt) {
// When the game is not paused, do nothing.
// When the game is paused, return `true` so the
// engine can call `draw()`.
return (me.state.isPaused());
},
draw : function(context) {
// Will only show "PAUSED" if the game
// is paused, duh
if (! me.state.isPaused())
return;
game.font_white.draw(
context,
"PAUSED",
this.pos.x,
this.pos.y
);
if (this.menu)
this.menu.draw(context, true);
},
/**
* Enables/Disables the pause menu.
* It's called once the game is paused to create the menu;
* and once the game gets unpaused, it's called to destroy
* it.
*
* I know it's a waste of resources but I couldn't find a
* way to create a menu that only gets drawn when I ask
* it to.
*/
enableMenu : function(option) {
if (! option) {
// Let's destroy the menu!
// (but only if it exists)
if (this.menu === null)
return;
me.game.world.removeChild(this.menu);
this.menu = null;
}
else {
// Creating a sweet sweet pause menu.
// (but only if it doesn't exist)
if (this.menu !== null)
return;
this.menu = new me.Menu(1, 12);
// WHAT THE HELL?
// WHY DOESNT THIS APPLY?
//this.menu.floating = true;
// This way the menu works when paused -- yay!
this.menu.updateWhenPaused = true;
this.menu.addItem(
"RESUME",
function() {
// Let's simulate the user pressing
// the "pause" key to unpause the game
me.input.triggerKeyEvent(me.input.KEY.ESC, true);
}
);
this.menu.addItem(
((me.save.sound)?
"SOUND:Y" :
"SOUND:N"),
function () {
var newLabel = "";
if (me.save.sound) {
newLabel = "SOUND:N";
me.save.sound = false;
me.audio.disable();
}
else {
newLabel = "SOUND:Y";
me.save.sound = true;
me.audio.enable();
}
// That's a VERY HACKISH THING TO DO
// I should NEVER have to directly access
// stuff like this!
game.pauseHandler.menu.children[1].label = newLabel;
}
);
this.menu.addItem(
((me.save.stars) ?
"STARS:Y" :
"STARS:N"),
function() {
var newLabel = "";
if (me.save.stars) {
newLabel = "STARS:N";
me.save.stars = false;
// Dynamically destroying stars
game.enableStars(false);
}
else {
newLabel = "STARS:Y";
me.save.stars = true;
// Dynamically creating stars
game.enableStars(true);
}
// That's a VERY HACKISH THING TO DO
// I should NEVER have to directly access
// stuff like this!
game.pauseHandler.menu.children[2].label = newLabel;
}
);
// I couldn't add an item to go to the menu because
// it required a lot of hacks and was buggy.
// TODO: implement this later
/*
this.menu.addItem(
"QUIT",
function() {
// This is a clever/hackish thing to do.
// Since I can't change states when paused, I
// simulate this keypress and THEN change the state.
me.input.triggerKeyEvent(me.input.KEY.ESC, true);
game.pauseHandler.enableMenu(false);
// It's very ugly to directly
// access a game state...
me.state.change(me.state.STATE_MAIN_MENU);
}
);
*/
// FINALLY, adding the menu to the world
// (meaning it's update() and draw() functions
// will get called from now on)
me.game.world.addChild(this.menu, 20);
}
}
});