-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathinternal.js
More file actions
287 lines (254 loc) · 9.27 KB
/
internal.js
File metadata and controls
287 lines (254 loc) · 9.27 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
/**
* # internal
* Copyright(c) 2016 Stefano Balietti
* MIT Licensed
*
* Listeners for internal messages.
*
* Internal listeners are not directly associated to messages,
* but they are usually responding to internal nodeGame events,
* such as progressing in the loading chain, or finishing a game stage.
*
* http://nodegame.org
*/
(function(exports, parent) {
"use strict";
var NGC = parent.NodeGameClient;
var GameStage = parent.GameStage,
constants = parent.constants;
var stageLevels = constants.stageLevels,
gcommands = constants.gamecommands;
var CMD = 'NODEGAME_GAMECOMMAND_';
/**
* ## NodeGameClient.addDefaultInternalListeners
*
* Adds a battery of event listeners for internal events
*
* If executed once, it requires a force flag to re-add the listeners.
*
* @param {boolean} force Whether to force re-adding the listeners
* @return {boolean} TRUE on success
*/
NGC.prototype.addDefaultInternalListeners = function(force) {
var node = this;
if (this.conf.internalAdded && !force) {
this.err('Default internal listeners already added once. ' +
'Use the force flag to re-add.');
return false;
}
this.info('node: adding internal listeners.');
function done() {
var res;
node.game.willBeDone = false;
node.game.beDone = false;
node.emit('REALLY_DONE');
res = node.game.shouldStep(stageLevels.DONE);
node.game.setStageLevel(stageLevels.DONE);
// Step forward, if allowed.
if (res) setTimeout(function() { node.game.step(); }, 0);
}
/**
* ## DONE
*
* Registers the stageLevel _DONE_ and eventually steps forward.
*
* If a DONE handler is defined in the game-plot, it executes it.
* In case the handler returns FALSE, the process is stopped.
*
* @emit REALLY_DONE
*/
this.events.ng.on('DONE', function() {
// Execute done handler before updating stage.
// If willBeDone is not set, then PLAYING called DONE earlier.
// Can happen if node.done() is called before PLAYING.
if (!node.game.willBeDone) return;
// TODO check >=.
if (node.game.getStageLevel() >= stageLevels.PLAYING) done();
else node.game.willBeDone = true;
});
/**
* ## STEP_CALLBACK_EXECUTED
*
* @emit LOADED
*/
this.events.ng.on('STEP_CALLBACK_EXECUTED', function() {
if (!node.window || node.window.isReady()) {
node.emit('LOADED');
}
});
/**
* ## LOADED
*
* @emit PLAYING
*/
this.events.ng.on('LOADED', function() {
var frame;
node.game.setStageLevel(constants.stageLevels.LOADED);
if (node.socket.shouldClearBuffer()) {
node.socket.clearBuffer();
}
// Make the frame visibile (if any).
// The Window hides it with every new load, so that if the page
// is manipulated in the step callback, the user still sees it
// appearing all at once.
if (node.window) {
frame = node.window.getFrame();
if (frame) frame.style.visibility = '';
}
if (node.game.shouldEmitPlaying()) {
node.emit('PLAYING');
}
});
/**
* ## PLAYING
*
* @emit BEFORE_PLAYING
*/
this.events.ng.on('PLAYING', function() {
var currentTime;
node.emit('BEFORE_PLAYING');
node.game.setStageLevel(stageLevels.PLAYING);
node.socket.clearBuffer();
// Last thing to do, is to store time:
currentTime = (new Date()).getTime();
node.timer.setTimestamp(node.game.getCurrentGameStage().toString(),
currentTime);
node.timer.setTimestamp('step', currentTime);
// DONE was previously emitted, we just execute done handler.
if (node.game.willBeDone) done();
});
/**
* ## NODEGAME_GAMECOMMAND: start
*/
this.events.ng.on(CMD + gcommands.start, function(options) {
if (!node.game.isStartable()) {
node.warn('"' + CMD + gcommands.start + '": game cannot ' +
'be started now.');
return;
}
node.emit('BEFORE_GAMECOMMAND', gcommands.start, options);
node.game.start(options);
});
/**
* ## NODEGAME_GAMECMD: pause
*/
this.events.ng.on(CMD + gcommands.pause, function(options) {
if (!node.game.isPausable()) {
node.warn('"' + CMD + gcommands.pause + '": game cannot ' +
'be paused now.');
return;
}
node.emit('BEFORE_GAMECOMMAND', gcommands.pause, options);
node.game.pause(options);
});
/**
* ## NODEGAME_GAMECOMMAND: resume
*/
this.events.ng.on(CMD + gcommands.resume, function(options) {
if (!node.game.isResumable()) {
node.warn('"' + CMD + gcommands.resume + '": game cannot ' +
'be resumed now.');
return;
}
node.emit('BEFORE_GAMECOMMAND', gcommands.resume, options);
node.game.resume(options);
});
/**
* ## NODEGAME_GAMECOMMAND: step
*/
this.events.ng.on(CMD + gcommands.step, function(options) {
if (!node.game.isSteppable()) {
node.warn('"' + CMD + gcommands.step + '": game cannot ' +
'be stepped now.');
return;
}
node.emit('BEFORE_GAMECOMMAND', gcommands.step, options);
if (options.breakStage) node.game.breakStage(true);
node.game.step();
});
/**
* ## NODEGAME_GAMECOMMAND: stop
*/
this.events.ng.on(CMD + gcommands.stop, function(options) {
if (!node.game.isStoppable()) {
node.warn('"' + CMD + gcommands.stop + '": game cannot ' +
'be stopped now.');
return;
}
node.emit('BEFORE_GAMECOMMAND', gcommands.stop, options);
node.game.stop();
});
/**
* ## NODEGAME_GAMECOMMAND: goto_step
*/
this.events.ng.on(CMD + gcommands.goto_step, function(options) {
var step;
if (!node.game.isSteppable()) {
node.warn('"' + CMD + gcommands.goto_step + '": game cannot ' +
'be stepped now');
return;
}
// Adjust parameters.
if (options.targetStep) {
step = options.targetStep;
delete options.targetStep;
}
else {
step = options;
options = undefined;
}
node.emit('BEFORE_GAMECOMMAND', gcommands.goto_step, step, options);
if (step !== parent.GamePlot.GAMEOVER) {
step = new GameStage(step);
if (!node.game.plot.getStep(step)) {
node.err('"' + CMD + gcommands.goto_step + '": ' +
'step not found: ' + step);
return;
}
}
node.game.gotoStep(step, options);
});
/**
* ## NODEGAME_GAMECOMMAND: clear_buffer
*/
this.events.ng.on(CMD + gcommands.clear_buffer, function() {
node.emit('BEFORE_GAMECOMMAND', gcommands.clear_buffer);
node.socket.clearBuffer();
});
/**
* ## NODEGAME_GAMECOMMAND: erase_buffer
*/
this.events.ng.on(CMD + gcommands.erase_buffer, function() {
node.emit('BEFORE_GAMECOMMAND', gcommands.clear_buffer);
node.socket.eraseBuffer();
});
/**
* ## NODEGAME_GAMECOMMAND: push_step
*
* If listener is moved to another file, update doc page
*/
node.events.ng.on(CMD + gcommands.push_step, function() {
var res, stageLevel;
node.warn('push_step command. ', node.player.stage);
// Call timeup, if defined.
if (!node.game.timer.isTimeup()) node.game.timer.doTimeup();
// Force node.done.
if (!node.game.willBeDone) {
stageLevel = node.game.getStageLevel();
if (stageLevel !== stageLevels.DONE_CALLED &&
stageLevel !== stageLevels.GETTING_DONE &&
stageLevel !== stageLevels.DONE) {
res = node.done();
if (!res) node.emit('DONE');
}
}
return 'ok!';
});
this.conf.internalAdded = true;
this.silly('node: internal listeners added.');
return true;
};
})(
'undefined' != typeof node ? node : module.exports,
'undefined' != typeof node ? node : module.parent.exports
);