@vpmedia/phaser 1.0.1 → 1.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (107) hide show
  1. package/dist/phaser.cjs.LICENSE.txt +1 -1
  2. package/dist/phaser.js.LICENSE.txt +1 -1
  3. package/package.json +2 -3
  4. package/src/index.js +99 -0
  5. package/src/phaser/core/animation.js +355 -0
  6. package/src/phaser/core/animation_manager.js +238 -0
  7. package/src/phaser/core/animation_parser.js +130 -0
  8. package/src/phaser/core/array_set.js +108 -0
  9. package/src/phaser/core/cache.js +558 -0
  10. package/src/phaser/core/const.js +106 -0
  11. package/src/phaser/core/device.js +67 -0
  12. package/src/phaser/core/device_util.js +386 -0
  13. package/src/phaser/core/dom.js +207 -0
  14. package/src/phaser/core/event_manager.js +243 -0
  15. package/src/phaser/core/factory.js +74 -0
  16. package/src/phaser/core/frame.js +75 -0
  17. package/src/phaser/core/frame_data.js +84 -0
  18. package/src/phaser/core/frame_util.js +31 -0
  19. package/src/phaser/core/game.js +412 -0
  20. package/src/phaser/core/input.js +401 -0
  21. package/src/phaser/core/input_button.js +102 -0
  22. package/src/phaser/core/input_handler.js +687 -0
  23. package/src/phaser/core/input_mouse.js +289 -0
  24. package/src/phaser/core/input_mspointer.js +197 -0
  25. package/src/phaser/core/input_pointer.js +427 -0
  26. package/src/phaser/core/input_touch.js +157 -0
  27. package/src/phaser/core/loader.js +946 -0
  28. package/src/phaser/core/loader_parser.js +105 -0
  29. package/src/phaser/core/raf.js +46 -0
  30. package/src/phaser/core/raf_fb.js +75 -0
  31. package/src/phaser/core/raf_to.js +34 -0
  32. package/src/phaser/core/scale_manager.js +806 -0
  33. package/src/phaser/core/scene.js +66 -0
  34. package/src/phaser/core/scene_manager.js +310 -0
  35. package/src/phaser/core/signal.js +175 -0
  36. package/src/phaser/core/signal_binding.js +69 -0
  37. package/src/phaser/core/sound.js +538 -0
  38. package/src/phaser/core/sound_manager.js +365 -0
  39. package/src/phaser/core/stage.js +108 -0
  40. package/src/phaser/core/time.js +203 -0
  41. package/src/phaser/core/timer.js +276 -0
  42. package/src/phaser/core/timer_event.js +21 -0
  43. package/src/phaser/core/tween.js +329 -0
  44. package/src/phaser/core/tween_data.js +258 -0
  45. package/src/phaser/core/tween_easing.js +316 -0
  46. package/src/phaser/core/tween_manager.js +185 -0
  47. package/src/phaser/core/world.js +18 -0
  48. package/src/phaser/display/bitmap_text.js +322 -0
  49. package/src/phaser/display/button.js +194 -0
  50. package/src/phaser/display/canvas/buffer.js +36 -0
  51. package/src/phaser/display/canvas/graphics.js +227 -0
  52. package/src/phaser/display/canvas/masker.js +39 -0
  53. package/src/phaser/display/canvas/pool.js +121 -0
  54. package/src/phaser/display/canvas/renderer.js +123 -0
  55. package/src/phaser/display/canvas/tinter.js +141 -0
  56. package/src/phaser/display/canvas/util.js +151 -0
  57. package/src/phaser/display/display_object.js +597 -0
  58. package/src/phaser/display/graphics.js +723 -0
  59. package/src/phaser/display/graphics_data.js +27 -0
  60. package/src/phaser/display/graphics_data_util.js +14 -0
  61. package/src/phaser/display/group.js +227 -0
  62. package/src/phaser/display/image.js +288 -0
  63. package/src/phaser/display/sprite_batch.js +15 -0
  64. package/src/phaser/display/sprite_util.js +248 -0
  65. package/src/phaser/display/text.js +1089 -0
  66. package/src/phaser/display/webgl/abstract_filter.js +25 -0
  67. package/src/phaser/display/webgl/base_texture.js +68 -0
  68. package/src/phaser/display/webgl/blend_manager.js +35 -0
  69. package/src/phaser/display/webgl/earcut.js +647 -0
  70. package/src/phaser/display/webgl/earcut_node.js +28 -0
  71. package/src/phaser/display/webgl/fast_sprite_batch.js +242 -0
  72. package/src/phaser/display/webgl/filter_manager.js +46 -0
  73. package/src/phaser/display/webgl/filter_texture.js +61 -0
  74. package/src/phaser/display/webgl/graphics.js +618 -0
  75. package/src/phaser/display/webgl/graphics_data.js +42 -0
  76. package/src/phaser/display/webgl/mask_manager.js +36 -0
  77. package/src/phaser/display/webgl/render_texture.js +81 -0
  78. package/src/phaser/display/webgl/renderer.js +234 -0
  79. package/src/phaser/display/webgl/shader/complex.js +74 -0
  80. package/src/phaser/display/webgl/shader/fast.js +97 -0
  81. package/src/phaser/display/webgl/shader/normal.js +225 -0
  82. package/src/phaser/display/webgl/shader/primitive.js +72 -0
  83. package/src/phaser/display/webgl/shader/strip.js +77 -0
  84. package/src/phaser/display/webgl/shader_manager.js +89 -0
  85. package/src/phaser/display/webgl/sprite_batch.js +320 -0
  86. package/src/phaser/display/webgl/stencil_manager.js +170 -0
  87. package/src/phaser/display/webgl/texture.js +117 -0
  88. package/src/phaser/display/webgl/texture_util.js +32 -0
  89. package/src/phaser/display/webgl/util.js +74 -0
  90. package/src/phaser/geom/circle.js +186 -0
  91. package/src/phaser/geom/ellipse.js +65 -0
  92. package/src/phaser/geom/line.js +190 -0
  93. package/src/phaser/geom/matrix.js +147 -0
  94. package/src/phaser/geom/point.js +164 -0
  95. package/src/phaser/geom/polygon.js +141 -0
  96. package/src/phaser/geom/rectangle.js +306 -0
  97. package/src/phaser/geom/rounded_rectangle.js +36 -0
  98. package/src/phaser/geom/util/circle.js +115 -0
  99. package/src/phaser/geom/util/ellipse.js +30 -0
  100. package/src/phaser/geom/util/line.js +130 -0
  101. package/src/phaser/geom/util/matrix.js +48 -0
  102. package/src/phaser/geom/util/point.js +276 -0
  103. package/src/phaser/geom/util/polygon.js +24 -0
  104. package/src/phaser/geom/util/rectangle.js +212 -0
  105. package/src/phaser/geom/util/rounded_rectangle.js +28 -0
  106. package/src/phaser/util/math.js +279 -0
  107. package/src/phaser/util/string.js +26 -0
@@ -0,0 +1,412 @@
1
+ /**
2
+ * @author Andras Csizmadia <andras@vpmedia.hu>
3
+ * @author Richard Davey <rich@photonstorm.com>
4
+ * @copyright Copyright (c) 2018-present Richard Davey, Photon Storm Ltd., Andras Csizmadia <andras@vpmedia.hu> (www.vpmedia.hu)
5
+ */
6
+ import CanvasRenderer from '../display/canvas/renderer';
7
+ import WebGLRenderer from '../display/webgl/renderer';
8
+ import Signal from './signal';
9
+ import Loader from './loader';
10
+ import Cache from './cache';
11
+ import Input from './input';
12
+ import Device from './device';
13
+ import GameObjectFactory from './factory';
14
+ import RequestAnimationFrame from './raf';
15
+ import TimeOutAnimationFrame from './raf_to';
16
+ import ScaleManager from './scale_manager';
17
+ import SoundManager from './sound_manager';
18
+ import SceneManager from './scene_manager';
19
+ import Time from './time';
20
+ import TweenManager from './tween_manager';
21
+ import World from './world';
22
+ import Stage from './stage';
23
+ import { RENDER_AUTO, RENDER_WEBGL, RENDER_CANVAS, RENDER_HEADLESS } from './const';
24
+ import { create, removeFromDOM, addToDOM, setTouchAction } from '../display/canvas/util';
25
+ import { initialize, checkOS } from './device_util';
26
+
27
+ export default class {
28
+
29
+ constructor(gameConfig = {}) {
30
+ if (!window.PhaserRegistry) {
31
+ window.PhaserRegistry = {};
32
+ }
33
+ this.config = {};
34
+ this.id = 0;
35
+ this.parent = '';
36
+ this.width = 800;
37
+ this.height = 600;
38
+ this.renderer = null;
39
+ this.state = null;
40
+ this.isBooted = false;
41
+ this.isRunning = false;
42
+ this.raf = null;
43
+ this.add = null;
44
+ this.cache = null;
45
+ this.input = null;
46
+ this.load = null;
47
+ this.scale = null;
48
+ this.sound = null;
49
+ this.stage = null;
50
+ this.time = null;
51
+ this.tweens = null;
52
+ this.world = null;
53
+ this.device = new Device();
54
+ this.canvas = null;
55
+ this.context = null;
56
+ this.isStepping = false;
57
+ this.isPendingStep = false;
58
+ this.stepCount = 0;
59
+ this.onPause = null;
60
+ this.onResume = null;
61
+ this.isPaused = false;
62
+ this.currentUpdateID = 0;
63
+ this.updatesThisFrame = 1;
64
+ this._deltaTime = 0;
65
+ this._lastCount = 0;
66
+ this._spiraling = 0;
67
+ this.isKickStart = true;
68
+ this.fpsProblemNotifier = new Signal();
69
+ this._nextFpsNotification = 0;
70
+ this.parseConfig(gameConfig);
71
+ checkOS(this.device);
72
+ // whenReady(this.device, this.boot, this);
73
+ if (document.readyState === 'complete') {
74
+ initialize(this.device);
75
+ this.boot();
76
+ } else {
77
+ window.addEventListener('load', () => {
78
+ initialize(this.device);
79
+ this.boot();
80
+ }, false);
81
+ }
82
+ }
83
+
84
+ boot() {
85
+ if (this.isBooted) {
86
+ console.warn('Game already booted.');
87
+ return;
88
+ }
89
+ this.isBooted = true;
90
+ this.onPause = new Signal();
91
+ this.onResume = new Signal();
92
+ this.scale = new ScaleManager(this, this.config.width, this.config.height);
93
+ this.stage = new Stage(this);
94
+ this.initRenderer();
95
+ this.world = new World(this);
96
+ this.add = new GameObjectFactory(this);
97
+ this.cache = new Cache(this);
98
+ this.load = new Loader(this);
99
+ this.time = new Time(this);
100
+ this.tweens = new TweenManager(this);
101
+ this.input = new Input(this);
102
+ this.sound = new SoundManager(this);
103
+ this.time.boot();
104
+ this.stage.boot();
105
+ this.world.boot();
106
+ this.scale.boot();
107
+ this.input.boot();
108
+ this.sound.boot();
109
+ this.state.boot();
110
+ this.isRunning = true;
111
+ if (this.config.forceSetTimeOut) {
112
+ this.raf = new TimeOutAnimationFrame(this);
113
+ } else {
114
+ this.raf = new RequestAnimationFrame(this);
115
+ }
116
+ this.isKickStart = true;
117
+ if (window.focus && !this.config.isSkipWindowFocus) {
118
+ window.focus();
119
+ }
120
+ this.raf.start();
121
+ }
122
+
123
+ initRenderer() {
124
+ if (this.config.canvas) {
125
+ this.canvas = this.config.canvas;
126
+ } else {
127
+ this.canvas = create(this, this.width, this.height, this.config.canvasID, true);
128
+ }
129
+ if (this.config.canvasStyle) {
130
+ this.canvas.style = this.config.canvasStyle;
131
+ } else {
132
+ this.canvas.style['-webkit-full-screen'] = 'width: 100%; height: 100%';
133
+ }
134
+ let isWebGlReady = false;
135
+ if (this.config.renderType === RENDER_AUTO || this.config.renderType === RENDER_WEBGL) {
136
+ try {
137
+ this.renderer = new WebGLRenderer(this);
138
+ this.context = null;
139
+ this.contextLostBinded = this.contextLost.bind(this);
140
+ this.contextRestoredBinded = this.contextRestored.bind(this);
141
+ this.canvas.addEventListener('webglcontextlost', this.contextLostBinded, false);
142
+ this.canvas.addEventListener('webglcontextrestored', this.contextRestoredBinded, false);
143
+ isWebGlReady = true;
144
+ } catch (e) {
145
+ isWebGlReady = false;
146
+ }
147
+ /*
148
+ this.renderer = new WebGLRenderer(this);
149
+ this.context = null;
150
+ this.canvas.addEventListener('webglcontextlost', this.contextLost.bind(this), false);
151
+ this.canvas.addEventListener('webglcontextrestored', this.contextRestored.bind(this), false);
152
+ isWebGlReady = true;
153
+ */
154
+ }
155
+ if (!isWebGlReady) {
156
+ if (this.renderer) {
157
+ if (this.contextLostBinded) {
158
+ this.canvas.removeEventListener('webglcontextlost', this.contextLostBinded, false);
159
+ }
160
+ if (this.contextRestoredBinded) {
161
+ this.canvas.removeEventListener('webglcontextlost', this.contextRestoredBinded, false);
162
+ }
163
+ this.renderer.destroy();
164
+ removeFromDOM(this.canvas);
165
+ this.canvas = create(this, this.width, this.height, this.config.canvasID, true);
166
+ }
167
+ this.renderer = new CanvasRenderer(this);
168
+ this.context = this.renderer.context;
169
+ }
170
+ if (this.device && this.device.cocoonJS && this.renderer) {
171
+ this.canvas.screencanvas = this.renderer.type === RENDER_CANVAS;
172
+ }
173
+ if (this.config.renderType !== RENDER_HEADLESS) {
174
+ this.stage.smoothed = this.config.antialias;
175
+ addToDOM(this.canvas, this.parent, false);
176
+ setTouchAction(this.canvas);
177
+ }
178
+ }
179
+
180
+ parseConfigElement(config, key, defaultValue) {
181
+ if (config[key] !== undefined) {
182
+ this.config[key] = config[key];
183
+ } else {
184
+ this.config[key] = defaultValue;
185
+ }
186
+ }
187
+
188
+ parseConfig(config) {
189
+ /* game */
190
+ this.parseConfigElement(config, 'isDestroyUnload', false);
191
+ this.parseConfigElement(config, 'isSkipWindowFocus', false);
192
+ this.parseConfigElement(config, 'forceSetTimeOut', false);
193
+ this.parseConfigElement(config, 'lockRender', false);
194
+ this.parseConfigElement(config, 'width', 800);
195
+ this.parseConfigElement(config, 'height', 600);
196
+ this.parseConfigElement(config, 'backgroundColor', 0x000000);
197
+ // Should the game loop force a logic update, regardless of the delta timer? Set to true if you know you need this. You can toggle it on the fly.
198
+ this.parseConfigElement(config, 'forceSingleUpdate', true);
199
+ /* canvas */
200
+ this.parseConfigElement(config, 'canvasID', '');
201
+ this.parseConfigElement(config, 'canvasStyle', undefined);
202
+ /* renderer */
203
+ // The resolution of your game.
204
+ this.parseConfigElement(config, 'resolution', 1);
205
+ // Use a transparent canvas background or not.
206
+ this.parseConfigElement(config, 'transparent', false);
207
+ // Anti-alias graphics. By default scaled images are smoothed in Canvas and WebGL, set anti-alias to false to disable this globally.
208
+ this.parseConfigElement(config, 'antialias', false);
209
+ // The value of the preserveDrawingBuffer flag affects whether or not the contents of the stencil buffer is retained after rendering.
210
+ this.parseConfigElement(config, 'preserveDrawingBuffer', false);
211
+ // Clear the Canvas each frame before rendering the display list.
212
+ // You can set this to `false` to gain some performance if your game always contains a background that completely fills the display.
213
+ this.parseConfigElement(config, 'clearBeforeRender', true);
214
+ // The Renderer this game will use. Either PowerGamer.Const.RENDER_AUTO, PowerGamer.Const.RENDER_CANVAS, PowerGamer.Const.RENDER_WEBGL, or PowerGamer.Const.RENDER_HEADLESS.
215
+ this.parseConfigElement(config, 'renderType', RENDER_AUTO);
216
+ if (config.renderer) {
217
+ this.config.renderType = config.renderer;
218
+ }
219
+ if (config.parent) {
220
+ this.parent = config.parent;
221
+ }
222
+ let state = null;
223
+ if (config.state) {
224
+ state = config.state;
225
+ }
226
+ this.state = new SceneManager(this, state);
227
+ }
228
+
229
+ contextLost(event) {
230
+ event.preventDefault();
231
+ if (this.renderer) {
232
+ this.renderer.contextLost = true;
233
+ }
234
+ }
235
+
236
+ contextRestored() {
237
+ this.renderer.initContext();
238
+ // this.cache.clearGLTextures();
239
+ if (this.renderer) {
240
+ this.renderer.contextLost = false;
241
+ }
242
+ }
243
+
244
+ update(time) {
245
+ if (this.isPendingDestroy) {
246
+ this.destroyDelayed();
247
+ return;
248
+ } else if (!this.isBooted) {
249
+ return;
250
+ }
251
+ this.time.update(time);
252
+ if (this.isKickStart) {
253
+ this.updateLogic(this.time.desiredFpsMult);
254
+ // call the game render update exactly once every frame
255
+ this.updateRender(this.time.slowMotion * this.time.desiredFps);
256
+ this.isKickStart = false;
257
+ return;
258
+ }
259
+ if (this._spiraling > 1 && !this.config.forceSingleUpdate) {
260
+ // cause an event to warn the program that this CPU can't keep up with the current desiredFps rate
261
+ if (this.time.time > this._nextFpsNotification) {
262
+ // only permit one fps notification per 10 seconds
263
+ this._nextFpsNotification = this.time.time + 10000;
264
+ // dispatch the notification signal
265
+ this.fpsProblemNotifier.dispatch();
266
+ }
267
+ // reset the _deltaTime accumulator which will cause all pending dropped frames to be permanently skipped
268
+ this._deltaTime = 0;
269
+ this._spiraling = 0;
270
+ // call the game render update exactly once every frame
271
+ this.updateRender(this.time.slowMotion * this.time.desiredFps);
272
+ } else {
273
+ // step size taking into account the slow motion speed
274
+ const slowStep = this.time.slowMotion * 1000.0 / this.time.desiredFps;
275
+ // accumulate time until the slowStep threshold is met or exceeded... up to a limit of 3 catch-up frames at slowStep intervals
276
+ this._deltaTime += Math.max(Math.min(slowStep * 3, this.time.elapsed), 0);
277
+ // call the game update logic multiple times if necessary to "catch up" with dropped frames
278
+ // unless forceSingleUpdate is true
279
+ let count = 0;
280
+ this.updatesThisFrame = Math.floor(this._deltaTime / slowStep);
281
+ if (this.config.forceSingleUpdate) {
282
+ this.updatesThisFrame = Math.min(1, this.updatesThisFrame);
283
+ }
284
+ while (this._deltaTime >= slowStep) {
285
+ this._deltaTime -= slowStep;
286
+ this.currentUpdateID = count;
287
+ this.updateLogic(this.time.desiredFpsMult);
288
+ count += 1;
289
+ if (this.config.forceSingleUpdate && count === 1) {
290
+ break;
291
+ } else {
292
+ this.time.refresh();
293
+ }
294
+ }
295
+ // detect spiraling (if the catch-up loop isn't fast enough, the number of iterations will increase constantly)
296
+ if (count > this._lastCount) {
297
+ this._spiraling += 1;
298
+ } else if (count < this._lastCount) {
299
+ // looks like it caught up successfully, reset the spiral alert counter
300
+ this._spiraling = 0;
301
+ }
302
+ this._lastCount = count;
303
+ // call the game render update exactly once every frame unless we're playing catch-up from a spiral condition
304
+ this.updateRender(this._deltaTime / slowStep);
305
+ }
306
+ }
307
+
308
+ updateLogic(timeStep) {
309
+ if (!this.isPaused && !this.isPendingStep) {
310
+ if (this.isStepping) {
311
+ this.isPendingStep = true;
312
+ }
313
+ this.scale.preUpdate();
314
+ this.state.preUpdate(timeStep);
315
+ this.stage.preUpdate();
316
+ this.state.update();
317
+ this.stage.update();
318
+ this.tweens.update();
319
+ this.sound.update();
320
+ this.input.update();
321
+ this.stage.postUpdate();
322
+ } else {
323
+ // Scaling and device orientation changes are still reflected when paused.
324
+ this.scale.pauseUpdate();
325
+ this.state.pauseUpdate();
326
+ }
327
+ this.stage.updateTransform();
328
+ }
329
+
330
+ updateRender(elapsedTime) {
331
+ if (this.config.lockRender) {
332
+ return;
333
+ }
334
+ this.state.preRender(elapsedTime);
335
+ if (this.config.renderType === RENDER_HEADLESS) {
336
+ return;
337
+ }
338
+ this.renderer.render(this.stage);
339
+ this.state.render(elapsedTime);
340
+ }
341
+
342
+ enableStep() {
343
+ this.isStepping = true;
344
+ this.isPendingStep = false;
345
+ this.stepCount = 0;
346
+ }
347
+
348
+ disableStep() {
349
+ this.isStepping = false;
350
+ this.isPendingStep = false;
351
+ }
352
+
353
+ step() {
354
+ this.isPendingStep = false;
355
+ this.stepCount += 1;
356
+ }
357
+
358
+ destroy() {
359
+ if (this.isPendingDestroy) {
360
+ return;
361
+ }
362
+ this.isPendingDestroy = true;
363
+ }
364
+
365
+ destroyDelayed() {
366
+ if (!this.isPendingDestroy) {
367
+ return;
368
+ }
369
+ this.isPendingDestroy = false;
370
+
371
+ this.isPaused = true;
372
+ this.isBooted = false;
373
+ this.isRunning = false;
374
+
375
+ if (!this.raf) {
376
+ return;
377
+ }
378
+
379
+ this.raf.stop();
380
+
381
+ this.time.destroy();
382
+ this.state.destroy();
383
+ this.sound.destroy();
384
+ this.scale.destroy();
385
+ this.stage.destroy();
386
+ this.input.destroy();
387
+
388
+ if (this.canvas) {
389
+ if (this.contextLostBinded) {
390
+ this.canvas.removeEventListener('webglcontextlost', this.contextLostBinded, false);
391
+ }
392
+ if (this.contextRestoredBinded) {
393
+ this.canvas.removeEventListener('webglcontextlost', this.contextRestoredBinded, false);
394
+ }
395
+ }
396
+ this.renderer.destroy(false);
397
+ removeFromDOM(this.canvas);
398
+
399
+ this.cache = null;
400
+ this.load = null;
401
+ this.time = null;
402
+ this.world = null;
403
+ this.state = null;
404
+ this.sound = null;
405
+ this.scale = null;
406
+ this.stage = null;
407
+ this.input = null;
408
+ this.canvas = null;
409
+ this.renderer = null;
410
+ }
411
+
412
+ }