-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgame.js
540 lines (540 loc) · 16 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
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
"use strict";
window.addEventListener('load', function () {
var game = new Phaser.Game({
width: 1280,
height: 720,
type: Phaser.AUTO,
backgroundColor: "#242424",
scale: {
mode: Phaser.Scale.FIT,
autoCenter: Phaser.Scale.CENTER_BOTH
}
});
game.scene.add("Preload", Preload);
game.scene.add("Level", Level);
game.scene.add("Boot", Boot, true);
});
class Boot extends Phaser.Scene {
preload() {
this.load.pack("pack", "assets/preload-asset-pack.json");
}
create() {
this.scene.start("Preload");
}
}
class UserComponent {
/**
* @param gameObject The entity.
*/
constructor(gameObject) {
this.scene = gameObject.scene;
const listenAwake = this.awake !== UserComponent.prototype.awake;
const listenStart = this.start !== UserComponent.prototype.start;
const listenUpdate = this.update !== UserComponent.prototype.update;
const listenDestroy = this.destroy !== UserComponent.prototype.destroy;
if (listenAwake) {
this.scene.events.once("scene-awake", this.awake, this);
}
if (listenStart) {
this.scene.events.once(Phaser.Scenes.Events.UPDATE, this.start, this);
}
if (listenUpdate) {
this.scene.events.on(Phaser.Scenes.Events.UPDATE, this.update, this);
}
if (listenStart || listenUpdate || listenDestroy) {
gameObject.on(Phaser.GameObjects.Events.DESTROY, () => {
this.scene.events.off(Phaser.Scenes.Events.UPDATE, this.start, this);
this.scene.events.off(Phaser.Scenes.Events.UPDATE, this.update, this);
if (listenDestroy) {
this.destroy();
}
});
}
}
scene;
awake() {
// override this
}
start() {
// override this
}
update() {
// override this
}
destroy() {
// override this
}
}
// You can write more code here
/* START OF COMPILED CODE */
class Level extends Phaser.Scene {
constructor() {
super("Level");
/* START-USER-CTR-CODE */
// Write your code here.
/* END-USER-CTR-CODE */
}
editorCreate() {
// dino
const dino = this.add.image(640, 302, "dino");
// onPointerDownScript
const onPointerDownScript = new OnPointerDownScript(dino);
// pushActionScript
new PushActionScript(onPointerDownScript);
// text_1
const text_1 = this.add.text(640, 462, "", {});
text_1.setOrigin(0.5, 0);
text_1.text = "Phaser 3 + Phaser Editor 2D + TypeScript";
text_1.setStyle({ "fontFamily": "arial", "fontSize": "3em" });
this.events.emit("scene-awake");
}
/* START-USER-CODE */
// Write your code here.
create() {
this.editorCreate();
}
}
/* END OF COMPILED CODE */
// You can write more code here
// You can write more code here
/* START OF COMPILED CODE */
class Preload extends Phaser.Scene {
constructor() {
super("Preload");
/* START-USER-CTR-CODE */
// Write your code here.
/* END-USER-CTR-CODE */
}
editorCreate() {
// guapen
const guapen = this.add.image(505, 360, "guapen");
guapen.scaleX = 0.4;
guapen.scaleY = 0.4;
// progressBar
const progressBar = this.add.rectangle(553, 361, 256, 20);
progressBar.setOrigin(0, 0);
progressBar.isFilled = true;
progressBar.fillColor = 14737632;
// preloadUpdater
new PreloadBarUpdaterScript(progressBar);
// progressBarBg
const progressBarBg = this.add.rectangle(553, 361, 256, 20);
progressBarBg.setOrigin(0, 0);
progressBarBg.fillColor = 14737632;
progressBarBg.isStroked = true;
// loadingText
const loadingText = this.add.text(552, 329, "", {});
loadingText.text = "Loading...";
loadingText.setStyle({ "color": "#e0e0e0", "fontFamily": "arial", "fontSize": "20px" });
this.events.emit("scene-awake");
}
/* START-USER-CODE */
// Write your code here
preload() {
this.editorCreate();
this.load.pack("asset-pack", "assets/asset-pack.json");
}
create() {
this.scene.start("Level");
}
}
/* END OF COMPILED CODE */
// You can write more code here
class ScriptNode {
_scene;
_gameObject;
_parent;
_children;
constructor(parent) {
this._parent = parent;
if (parent instanceof ScriptNode) {
this._scene = parent.scene;
this._gameObject = parent.gameObject;
parent.add(this);
}
else if (parent instanceof Phaser.GameObjects.GameObject) {
this._scene = parent.scene;
this._gameObject = parent;
}
else {
this._scene = parent;
}
const listenAwake = this.awake !== ScriptNode.prototype.awake;
const listenStart = this.start !== ScriptNode.prototype.start;
const listenUpdate = this.update !== ScriptNode.prototype.update;
const listenDestroy = this.destroy !== ScriptNode.prototype.destroy;
if (listenAwake) {
this.scene.events.once("scene-awake", this.awake, this);
}
if (listenStart) {
this.scene.events.once(Phaser.Scenes.Events.UPDATE, this.start, this);
}
if (listenUpdate) {
this.scene.events.on(Phaser.Scenes.Events.UPDATE, this.update, this);
}
if (listenAwake || listenStart || listenUpdate || listenDestroy) {
const destroyCallback = () => {
this.scene.events.off("scene-awake", this.awake, this);
this.scene.events.off(Phaser.Scenes.Events.UPDATE, this.start, this);
this.scene.events.off(Phaser.Scenes.Events.UPDATE, this.update, this);
if (listenDestroy) {
this.destroy();
}
};
if (this.gameObject) {
this.gameObject.on(Phaser.GameObjects.Events.DESTROY, destroyCallback);
}
else {
this.scene.events.on(Phaser.Scenes.Events.SHUTDOWN, destroyCallback);
}
}
}
get scene() {
return this._scene;
}
get gameObject() {
return this._gameObject;
}
get parent() {
return this._parent;
}
get children() {
if (!this._children) {
this._children = [];
}
return this._children;
}
add(child) {
this.children.push(child);
}
executeChildren(...args) {
if (this._children) {
for (const child of this._children) {
child.execute(...args);
}
}
}
execute(...args) {
// override this on executable nodes
}
awake() {
// override this
}
start() {
// override this
}
update() {
// override this
}
destroy() {
// override this
}
}
/// <reference path="../script-nodes-basic-ts/ScriptNode.ts"/>
// You can write more code here
/* START OF COMPILED CODE */
class PreloadBarUpdaterScript extends ScriptNode {
constructor(parent) {
super(parent);
/* START-USER-CTR-CODE */
// Write your code here.
/* END-USER-CTR-CODE */
}
/* START-USER-CODE */
get gameObject() {
return super.gameObject;
}
awake() {
const fullWidth = this.gameObject.width;
this.scene.load.on(Phaser.Loader.Events.PROGRESS, (p) => {
this.gameObject.width = fullWidth * p;
});
}
}
/* END OF COMPILED CODE */
// You can write more code here
/// <reference path="../script-nodes-basic-ts/ScriptNode.ts"/>
// You can write more code here
/* START OF COMPILED CODE */
class PushActionScript extends ScriptNode {
constructor(parent) {
super(parent);
/* START-USER-CTR-CODE */
// Write your code here.
/* END-USER-CTR-CODE */
}
/* START-USER-CODE */
execute(args) {
this.scene.add.tween({
targets: this.gameObject,
scaleX: "*=0.8",
scaleY: "*=0.8",
duration: 80,
yoyo: true,
onYoyo: () => {
this.executeChildren(args);
}
});
}
}
/* END OF COMPILED CODE */
// You can write more code here
/// <reference path="./ScriptNode.ts"/>
/* START OF COMPILED CODE */
class CallbackActionScript extends ScriptNode {
constructor(parent) {
super(parent);
/* START-USER-CTR-CODE */
// Write your code here.
/* END-USER-CTR-CODE */
}
callback;
/* START-USER-CODE */
execute(...args) {
if (this.callback) {
this.callback(...args);
}
}
}
/* END OF COMPILED CODE */
// You can write more code here
/// <reference path="./ScriptNode.ts"/>
// You can write more code here
/* START OF COMPILED CODE */
class EmitEventActionScript extends ScriptNode {
constructor(parent) {
super(parent);
/* START-USER-CTR-CODE */
// Write your code here.
/* END-USER-CTR-CODE */
}
eventName = "";
eventEmitter = "gameObject";
/* START-USER-CODE */
execute(...args) {
let emitter;
switch (this.eventEmitter) {
case "game.events":
emitter = this.scene.game.events;
break;
case "scene.events":
emitter = this.scene.events;
break;
case "scene.loader":
emitter = this.scene.load;
break;
case "scene.input":
emitter = this.scene.input;
break;
case "scene.input.keyboard":
emitter = this.scene.input.keyboard;
break;
case "scene.anims":
emitter = this.scene.anims;
break;
case "gameObject":
emitter = this.gameObject;
break;
}
if (emitter) {
emitter.emit(this.eventName, ...args);
}
}
}
/* END OF COMPILED CODE */
// You can write more code here
/// <reference path="./ScriptNode.ts"/>
// You can write more code here
/* START OF COMPILED CODE */
class ExecActionScript extends ScriptNode {
constructor(parent) {
super(parent);
/* START-USER-CTR-CODE */
// Write your code here.
/* END-USER-CTR-CODE */
}
targetAction;
/* START-USER-CODE */
execute(...args) {
if (this.targetAction) {
this.targetAction.execute(...args);
}
}
}
/* END OF COMPILED CODE */
// You can write more code here
/// <reference path="./ScriptNode.ts"/>
// You can write more code here
/* START OF COMPILED CODE */
class OnEventScript extends ScriptNode {
constructor(parent) {
super(parent);
/* START-USER-CTR-CODE */
/* END-USER-CTR-CODE */
}
eventName = "";
eventEmitter = "gameObject";
once = false;
/* START-USER-CODE */
awake() {
let emitter;
switch (this.eventEmitter) {
case "game.events":
emitter = this.scene.game.events;
break;
case "scene.events":
emitter = this.scene.events;
break;
case "scene.loader":
emitter = this.scene.load;
break;
case "scene.input":
emitter = this.scene.input;
break;
case "scene.input.keyboard":
emitter = this.scene.input.keyboard;
break;
case "scene.anims":
emitter = this.scene.anims;
break;
case "gameObject":
emitter = this.gameObject;
break;
}
if (emitter) {
if (this.once) {
emitter.once(this.eventName, this.executeChildren, this);
}
else {
emitter.on(this.eventName, this.executeChildren, this);
}
switch (this.eventEmitter) {
case "scene.anims":
case "scene.events":
case "scene.input":
case "scene.input.keyboard":
case "scene.loader":
this.scene.events.on(Phaser.Scenes.Events.SHUTDOWN, () => {
emitter?.off(this.eventName, this.executeChildren, this);
});
break;
}
}
}
}
/* END OF COMPILED CODE */
// You can write more code here
/// <reference path="./OnEventScript.ts"/>
// You can write more code here
/* START OF COMPILED CODE */
class OnPointerDownScript extends OnEventScript {
constructor(parent) {
super(parent);
// this (prefab fields)
this.eventName = "pointerdown";
/* START-USER-CTR-CODE */
// Write your code here.
/* END-USER-CTR-CODE */
}
/* START-USER-CODE */
awake() {
if (!this.gameObject) {
return;
}
if (!this.gameObject.input) {
this.gameObject.setInteractive();
}
super.awake();
}
}
/* END OF COMPILED CODE */
// You can write more code here
/// <reference path="./ScriptNode.ts"/>
// You can write more code here
/* START OF COMPILED CODE */
class RootScriptNode extends ScriptNode {
constructor(parent) {
super(parent);
/* START-USER-CTR-CODE */
// Write your code here.
/* END-USER-CTR-CODE */
}
key = "scripts";
/* START-USER-CODE */
/**
* Gets the RootScript object set into the game object.
* It lookups the script node in using the `key` parameter as attribute of the game object.
*
* @param gameObject The game object where the root script is set.
* @param key The key used to set root script into the game object. It is `"scripts"` by default.
* @returns The root script.
*/
static getRoot(gameObject, key = "scripts") {
return gameObject[`RootScript__${key}`];
}
/**
* Gets the children of the root script registered in the given game object, using the given key.
*
* @param gameObject The game object containing the root script.
* @param key The key used to register the root script in the game object.
* @returns The chidlren of the root script.
*/
static getChildren(gameObject, key = "scripts") {
const root = this.getRoot(gameObject, key);
if (root) {
return root.children;
}
return [];
}
/**
* Gets the root script associated to the game object, using the given key.
*
* @param gameObject The game object where the root script is set.
* @param key The key used for registering the root script in the game object.
* @returns The root script.
*/
static hasRoot(gameObject, key = "scripts") {
const script = this.getRoot(gameObject, key);
return script !== undefined;
}
awake() {
this.gameObject[`RootScript__${this.key}`] = this;
}
}
/* END OF COMPILED CODE */
// You can write more code here
/// <reference path="./ScriptNode.ts"/>
// You can write more code here
/* START OF COMPILED CODE */
class SpriteScriptNode extends ScriptNode {
constructor(parent) {
super(parent);
/* START-USER-CTR-CODE */
// Write your code here.
/* END-USER-CTR-CODE */
}
/* START-USER-CODE */
get gameObject() {
return super.gameObject;
}
}
/* END OF COMPILED CODE */
// You can write more code here
/// <reference path="./ScriptNode.ts"/>
// You can write more code here
/* START OF COMPILED CODE */
class StartSceneActionScript extends ScriptNode {
constructor(parent) {
super(parent);
/* START-USER-CTR-CODE */
// Write your code here.
/* END-USER-CTR-CODE */
}
sceneKey = "";
/* START-USER-CODE */
execute(...args) {
this.scene.scene.start(this.sceneKey, ...args);
}
}
/* END OF COMPILED CODE */
// You can write more code here