melonjs 10.6.0 → 10.7.1

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.
@@ -3,101 +3,108 @@ import timer from "./../system/timer.js";
3
3
  import { randomFloat, clamp } from "./../math/math.js";
4
4
  import Renderable from "./../renderable/renderable.js";
5
5
 
6
-
7
6
  /**
8
7
  * @classdesc
9
8
  * Single Particle Object.
10
- * @class Particle
11
9
  * @augments Renderable
12
- * @param {ParticleEmitter} particle emitter
13
10
  */
14
11
  class Particle extends Renderable {
15
12
  /**
16
- * @ignore
13
+ * @param {ParticleEmitter} emitter the particle emitter
17
14
  */
18
15
  constructor(emitter) {
19
16
  // Call the super constructor
20
17
  super(
21
18
  emitter.getRandomPointX(),
22
19
  emitter.getRandomPointY(),
23
- emitter.image.width,
24
- emitter.image.height
20
+ emitter.settings.image.width,
21
+ emitter.settings.image.height
25
22
  );
26
-
27
- // particle velocity
28
- this.vel = new Vector2d();
29
23
  this.onResetEvent(emitter, true);
30
24
  }
31
25
 
26
+ /**
27
+ * @ignore
28
+ */
32
29
  onResetEvent(emitter, newInstance = false) {
33
30
  if (newInstance === false) {
34
- super.onResetEvent(
31
+ this.pos.set(
35
32
  emitter.getRandomPointX(),
36
- emitter.getRandomPointY(),
37
- emitter.image.width,
38
- emitter.image.height
33
+ emitter.getRandomPointY()
39
34
  );
35
+ this.resize(
36
+ emitter.settings.image.width,
37
+ emitter.settings.image.height
38
+ );
39
+ } else {
40
+ // particle velocity
41
+ this.vel = new Vector2d();
40
42
  }
41
43
 
44
+ this.image = emitter.settings.image;
45
+
42
46
  // Particle will always update
43
47
  this.alwaysUpdate = true;
44
48
 
45
- // Cache the image reference
46
- this.image = emitter.image;
49
+ if (typeof emitter.settings.tint === "string") {
50
+ this.tint.parseCSS(emitter.settings.tint);
51
+ }
52
+
53
+ if (emitter.settings.textureAdditive === true) {
54
+ this.blendMode = "additive";
55
+ }
56
+
57
+ if (emitter.settings.blendMode !== "normal") {
58
+ this.blendMode = emitter.settings.blendMode;
59
+ }
47
60
 
48
61
  // Set the start particle Angle and Speed as defined in emitter
49
- var angle = emitter.angle + ((emitter.angleVariation > 0) ? (randomFloat(0, 2) - 1) * emitter.angleVariation : 0);
50
- var speed = emitter.speed + ((emitter.speedVariation > 0) ? (randomFloat(0, 2) - 1) * emitter.speedVariation : 0);
62
+ var angle = emitter.settings.angle + ((emitter.settings.angleVariation > 0) ? (randomFloat(0, 2) - 1) * emitter.settings.angleVariation : 0);
63
+ var speed = emitter.settings.speed + ((emitter.settings.speedVariation > 0) ? (randomFloat(0, 2) - 1) * emitter.settings.speedVariation : 0);
51
64
 
52
65
  // Set the start particle Velocity
53
66
  this.vel.set(speed * Math.cos(angle), -speed * Math.sin(angle));
54
67
 
55
68
  // Set the start particle Time of Life as defined in emitter
56
- this.life = randomFloat(emitter.minLife, emitter.maxLife);
69
+ this.life = randomFloat(emitter.settings.minLife, emitter.settings.maxLife);
57
70
  this.startLife = this.life;
58
71
 
59
72
  // Set the start and end particle Scale as defined in emitter
60
73
  // clamp the values as minimum and maximum scales range
61
74
  this.startScale = clamp(
62
- randomFloat(emitter.minStartScale, emitter.maxStartScale),
63
- emitter.minStartScale,
64
- emitter.maxStartScale
75
+ randomFloat(emitter.settings.minStartScale, emitter.settings.maxStartScale),
76
+ emitter.settings.minStartScale,
77
+ emitter.settings.maxStartScale
65
78
  );
66
79
  this.endScale = clamp(
67
- randomFloat(emitter.minEndScale, emitter.maxEndScale),
68
- emitter.minEndScale,
69
- emitter.maxEndScale
80
+ randomFloat(emitter.settings.minEndScale, emitter.settings.maxEndScale),
81
+ emitter.settings.minEndScale,
82
+ emitter.settings.maxEndScale
70
83
  );
71
84
 
72
85
  // Set the particle Gravity and Wind (horizontal gravity) as defined in emitter
73
- this.gravity = emitter.gravity;
74
- this.wind = emitter.wind;
86
+ this.gravity = emitter.settings.gravity;
87
+ this.wind = emitter.settings.wind;
75
88
 
76
89
  // Set if the particle update the rotation in accordance the trajectory
77
- this.followTrajectory = emitter.followTrajectory;
90
+ this.followTrajectory = emitter.settings.followTrajectory;
78
91
 
79
92
  // Set if the particle update only in Viewport
80
- this.onlyInViewport = emitter.onlyInViewport;
81
-
82
- // Set the particle Z Order
83
- this.pos.z = emitter.z;
93
+ this.onlyInViewport = emitter.settings.onlyInViewport;
84
94
 
85
95
  // cache inverse of the expected delta time
86
96
  this._deltaInv = timer.maxfps / 1000;
87
97
 
88
98
  // Set the start particle rotation as defined in emitter
89
99
  // if the particle not follow trajectory
90
- if (!emitter.followTrajectory) {
91
- this.angle = randomFloat(emitter.minRotation, emitter.maxRotation);
100
+ if (!emitter.settings.followTrajectory) {
101
+ this.angle = randomFloat(emitter.settings.minRotation, emitter.settings.maxRotation);
92
102
  }
93
103
  }
94
104
 
95
105
  /**
96
106
  * Update the Particle <br>
97
107
  * This is automatically called by the game manager {@link game}
98
- * @name update
99
- * @memberof Particle
100
- * @function
101
108
  * @ignore
102
109
  * @param {number} dt time since the last update in milliseconds
103
110
  */
@@ -108,6 +115,11 @@ class Particle extends Renderable {
108
115
  // Decrease particle life
109
116
  this.life = this.life > dt ? this.life - dt : 0;
110
117
 
118
+ if (this.life <= 0) {
119
+ this.ancestor.removeChild(this);
120
+ return false;
121
+ }
122
+
111
123
  // Calculate the particle Age Ratio
112
124
  var ageRatio = this.life / this.startLife;
113
125
 
@@ -142,23 +154,10 @@ class Particle extends Renderable {
142
154
  this.pos.x, this.pos.y, 1
143
155
  ).rotate(angle);
144
156
 
145
- // Return true if the particle is not dead yet
146
- return (this.inViewport || !this.onlyInViewport) && (this.life > 0);
147
- }
148
-
149
- /**
150
- * @ignore
151
- */
152
- preDraw(renderer) {
153
-
154
- // restore is called in postDraw
155
- renderer.save();
156
-
157
- // particle alpha value
158
- renderer.setGlobalAlpha(renderer.globalAlpha() * this.alpha);
157
+ // mark as dirty if the particle is not dead yet
158
+ this.isDirty = this.inViewport || !this.onlyInViewport;
159
159
 
160
- // translate to the defined anchor point and scale it
161
- renderer.transform(this.currentTransform);
160
+ return super.update(dt);
162
161
  }
163
162
 
164
163
  /**
@@ -0,0 +1,310 @@
1
+ /**
2
+ * ParticleEmitterSettings contains the default settings for ParticleEmitter
3
+ * @see ParticleEmitter
4
+ * @namespace ParticleEmitterSettings
5
+ */
6
+ const ParticleEmitterSettings = {
7
+ /**
8
+ * Width of the particle spawn area.
9
+ * @type {number}
10
+ * @name width
11
+ * @memberof ParticleEmitterSettings
12
+ * @default 1
13
+ */
14
+ width : 1,
15
+
16
+ /**
17
+ * Height of the particle spawn area
18
+ * @public
19
+ * @type {number}
20
+ * @name height
21
+ * @memberof ParticleEmitterSettings
22
+ * @default 1
23
+ */
24
+ height : 1,
25
+
26
+ /**
27
+ * image used for particles texture
28
+ * (by default melonJS will create an white 8x8 texture image)
29
+ * @public
30
+ * @type {HTMLCanvasElement}
31
+ * @name image
32
+ * @memberof ParticleEmitterSettings
33
+ * @default undefined
34
+ * @see ParticleEmitterSettings.textureSize
35
+ */
36
+ image : undefined,
37
+
38
+ /**
39
+ * default texture size used for particles if no image is specified
40
+ * (by default melonJS will create an white 8x8 texture image)
41
+ * @public
42
+ * @type {number}
43
+ * @name textureSize
44
+ * @memberof ParticleEmitterSettings
45
+ * @default 8
46
+ * @see ParticleEmitterSettings.image
47
+ */
48
+ textureSize : 8,
49
+
50
+ /**
51
+ * tint to be applied to particles
52
+ * @public
53
+ * @type {string}
54
+ * @name tint
55
+ * @memberof ParticleEmitterSettings
56
+ * @default "#fff"
57
+ */
58
+ tint : "#fff",
59
+
60
+ /**
61
+ * Total number of particles in the emitter
62
+ * @public
63
+ * @type {number}
64
+ * @name totalParticles
65
+ * @default 50
66
+ * @memberof ParticleEmitterSettings
67
+ */
68
+ totalParticles : 50,
69
+
70
+ /**
71
+ * Start angle for particle launch in Radians
72
+ * @public
73
+ * @type {number}
74
+ * @name angle
75
+ * @default Math.PI / 2
76
+ * @memberof ParticleEmitterSettings
77
+ */
78
+ angle : Math.PI / 2,
79
+
80
+ /**
81
+ * Variation in the start angle for particle launch in Radians.
82
+ * @public
83
+ * @type {number}
84
+ * @name angleVariation
85
+ * @default 0
86
+ * @memberof ParticleEmitterSettings
87
+ */
88
+ angleVariation : 0,
89
+
90
+ /**
91
+ * Minimum time each particle lives once it is emitted in ms.
92
+ * @public
93
+ * @type {number}
94
+ * @name minLife
95
+ * @default 1000
96
+ * @memberof ParticleEmitterSettings
97
+ */
98
+ minLife : 1000,
99
+
100
+ /**
101
+ * Maximum time each particle lives once it is emitted in ms.
102
+ * @public
103
+ * @type {number}
104
+ * @name maxLife
105
+ * @default 3000
106
+ * @memberof ParticleEmitterSettings
107
+ */
108
+ maxLife : 3000,
109
+
110
+ /**
111
+ * Start speed of particles.<br>
112
+ * @public
113
+ * @type {number}
114
+ * @name speed
115
+ * @default 2
116
+ * @memberof ParticleEmitterSettings
117
+ */
118
+ speed : 2,
119
+
120
+ /**
121
+ * Variation in the start speed of particles
122
+ * @public
123
+ * @type {number}
124
+ * @name speedVariation
125
+ * @default 1
126
+ * @memberof ParticleEmitterSettings
127
+ */
128
+ speedVariation : 1,
129
+
130
+ /**
131
+ * Minimum start rotation for particles sprites in Radians
132
+ * @public
133
+ * @type {number}
134
+ * @name minRotation
135
+ * @default 0
136
+ * @memberof ParticleEmitterSettings
137
+ */
138
+ minRotation : 0,
139
+
140
+ /**
141
+ * Maximum start rotation for particles sprites in Radians
142
+ * @public
143
+ * @type {number}
144
+ * @name maxRotation
145
+ * @default 0
146
+ * @memberof ParticleEmitterSettings
147
+ */
148
+ maxRotation : 0,
149
+
150
+ /**
151
+ * Minimum start scale ratio for particles (1 = no scaling)
152
+ * @public
153
+ * @type {number}
154
+ * @name minStartScale
155
+ * @default 1
156
+ * @memberof ParticleEmitterSettings
157
+ */
158
+ minStartScale : 1,
159
+
160
+ /**
161
+ * Maximum start scale ratio for particles (1 = no scaling)
162
+ * @public
163
+ * @type {number}
164
+ * @name maxStartScale
165
+ * @default 1
166
+ * @memberof ParticleEmitterSettings
167
+ */
168
+ maxStartScale : 1,
169
+
170
+ /**
171
+ * Minimum end scale ratio for particles
172
+ * @public
173
+ * @type {number}
174
+ * @name minEndScale
175
+ * @default 0
176
+ * @memberof ParticleEmitterSettings
177
+ */
178
+ minEndScale : 0,
179
+
180
+ /**
181
+ * Maximum end scale ratio for particles
182
+ * @public
183
+ * @type {number}
184
+ * @name maxEndScale
185
+ * @default 0
186
+ * @memberof ParticleEmitterSettings
187
+ */
188
+ maxEndScale : 0,
189
+
190
+ /**
191
+ * Vertical force (Gravity) for each particle
192
+ * @public
193
+ * @type {number}
194
+ * @name gravity
195
+ * @default 0
196
+ * @memberof ParticleEmitterSettings
197
+ * @see game.world.gravity
198
+ */
199
+ gravity : 0,
200
+
201
+ /**
202
+ * Horizontal force (like a Wind) for each particle
203
+ * @public
204
+ * @type {number}
205
+ * @name wind
206
+ * @default 0
207
+ * @memberof ParticleEmitterSettings
208
+ */
209
+ wind : 0,
210
+
211
+ /**
212
+ * Update the rotation of particle in accordance the particle trajectory.<br>
213
+ * The particle sprite should aim at zero angle (draw from left to right).<br>
214
+ * Override the particle minRotation and maxRotation.<br>
215
+ * @public
216
+ * @type {boolean}
217
+ * @name followTrajectory
218
+ * @default false
219
+ * @memberof ParticleEmitterSettings
220
+ */
221
+ followTrajectory : false,
222
+
223
+ /**
224
+ * Enable the Texture Additive by composite operation ("additive" blendMode)
225
+ * @public
226
+ * @type {boolean}
227
+ * @name textureAdditive
228
+ * @default false
229
+ * @memberof ParticleEmitterSettings
230
+ * @see ParticleEmitterSettings.blendMode
231
+ */
232
+ textureAdditive : false,
233
+
234
+ /**
235
+ * the blend mode to be applied when rendering particles.
236
+ * (note: this will superseed the `textureAdditive` setting if different than "normal")
237
+ * @public
238
+ * @type {string}
239
+ * @name blendMode
240
+ * @default normal
241
+ * @memberof ParticleEmitterSettings
242
+ * @see CanvasRenderer#setBlendMode
243
+ * @see WebGLRenderer#setBlendMode
244
+ */
245
+ blendMode : "normal",
246
+
247
+ /**
248
+ * Update particles only in the viewport, remove it when out of viewport.
249
+ * @public
250
+ * @type {boolean}
251
+ * @name onlyInViewport
252
+ * @default true
253
+ * @memberof ParticleEmitterSettings
254
+ */
255
+ onlyInViewport : true,
256
+
257
+ /**
258
+ * Render particles in screen space.
259
+ * @public
260
+ * @type {boolean}
261
+ * @name floating
262
+ * @default false
263
+ * @memberof ParticleEmitterSettings
264
+ */
265
+ floating : false,
266
+
267
+ /**
268
+ * Maximum number of particles launched each time in this emitter (used only if emitter is Stream).
269
+ * @public
270
+ * @type {number}
271
+ * @name maxParticles
272
+ * @default 10
273
+ * @memberof ParticleEmitterSettings
274
+ */
275
+ maxParticles : 10,
276
+
277
+ /**
278
+ * How often a particle is emitted in ms (used only if emitter is a Stream).
279
+ * @public
280
+ * @type {number}
281
+ * @name frequency
282
+ * @default 100
283
+ * @memberof ParticleEmitterSettings
284
+ */
285
+ frequency : 100,
286
+
287
+ /**
288
+ * Duration that the emitter releases particles in ms (used only if emitter is Stream).
289
+ * After this period, the emitter stop the launch of particles.
290
+ * @public
291
+ * @type {number}
292
+ * @name duration
293
+ * @default Infinity
294
+ * @memberof ParticleEmitterSettings
295
+ */
296
+ duration : Infinity,
297
+
298
+ /**
299
+ * Skip n frames after updating the particle system once.
300
+ * This can be used to reduce the performance impact of emitters with many particles.
301
+ * @public
302
+ * @type {number}
303
+ * @name framesToSkip
304
+ * @default 0
305
+ * @memberof ParticleEmitterSettings
306
+ */
307
+ framesToSkip : 0
308
+ };
309
+
310
+ export default ParticleEmitterSettings;
@@ -1,10 +1,10 @@
1
- if (typeof window !== "undefined") {
2
- if (typeof window.console === "undefined") {
3
- window.console = {};
4
- window.console.log = function() {};
5
- window.console.assert = function() {};
6
- window.console.warn = function() {};
7
- window.console.error = function() {
1
+ if (typeof globalThis !== "undefined") {
2
+ if (typeof globalThis.console === "undefined") {
3
+ globalThis.console = {};
4
+ globalThis.console.log = function() {};
5
+ globalThis.console.assert = function() {};
6
+ globalThis.console.warn = function() {};
7
+ globalThis.console.error = function() {
8
8
  alert(Array.prototype.slice.call(arguments).join(", "));
9
9
  };
10
10
  }
@@ -0,0 +1,7 @@
1
+ // https://github.com/melonjs/melonJS/issues/1092
2
+ import "core-js/proposals/global-this";
3
+
4
+ // "built-in" polyfills
5
+ import "./console.js";
6
+ import "./performance.js";
7
+ import "./requestAnimationFrame.js";
@@ -0,0 +1,20 @@
1
+ if ("performance" in globalThis === false) {
2
+ globalThis.performance = {};
3
+ }
4
+
5
+ Date.now = (Date.now || function () { // thanks IE8
6
+ return new Date().getTime();
7
+ });
8
+
9
+ if ("now" in globalThis.performance === false) {
10
+
11
+ var nowOffset = Date.now();
12
+
13
+ if (performance.timing && performance.timing.navigationStart) {
14
+ nowOffset = performance.timing.navigationStart;
15
+ }
16
+
17
+ globalThis.performance.now = function now() {
18
+ return Date.now() - nowOffset;
19
+ };
20
+ }
@@ -6,23 +6,23 @@ var x;
6
6
 
7
7
  // standardized functions
8
8
  // https://developer.mozilla.org/fr/docs/Web/API/Window/requestAnimationFrame
9
- var requestAnimationFrame = window.requestAnimationFrame;
10
- var cancelAnimationFrame = window.cancelAnimationFrame;
9
+ var requestAnimationFrame = globalThis.requestAnimationFrame;
10
+ var cancelAnimationFrame = globalThis.cancelAnimationFrame;
11
11
 
12
12
  // get prefixed rAF and cAF is standard one not supported
13
13
  for (x = 0; x < vendors.length && !requestAnimationFrame; ++x) {
14
- requestAnimationFrame = window[vendors[x] + "RequestAnimationFrame"];
14
+ requestAnimationFrame = globalThis[vendors[x] + "RequestAnimationFrame"];
15
15
  }
16
16
  for (x = 0; x < vendors.length && !cancelAnimationFrame; ++x) {
17
- cancelAnimationFrame = window[vendors[x] + "CancelAnimationFrame"] ||
18
- window[vendors[x] + "CancelRequestAnimationFrame"];
17
+ cancelAnimationFrame = globalThis[vendors[x] + "CancelAnimationFrame"] ||
18
+ globalThis[vendors[x] + "CancelRequestAnimationFrame"];
19
19
  }
20
20
 
21
21
  if (!requestAnimationFrame || !cancelAnimationFrame) {
22
22
  requestAnimationFrame = function (callback) {
23
- var currTime = window.performance.now();
23
+ var currTime = globalThis.performance.now();
24
24
  var timeToCall = Math.max(0, (1000 / timer.maxfps) - (currTime - lastTime));
25
- var id = window.setTimeout(function () {
25
+ var id = globalThis.setTimeout(function () {
26
26
  callback(currTime + timeToCall);
27
27
  }, timeToCall);
28
28
  lastTime = currTime + timeToCall;
@@ -30,10 +30,10 @@ if (!requestAnimationFrame || !cancelAnimationFrame) {
30
30
  };
31
31
 
32
32
  cancelAnimationFrame = function (id) {
33
- window.clearTimeout(id);
33
+ globalThis.clearTimeout(id);
34
34
  };
35
35
 
36
36
  // put back in global namespace
37
- window.requestAnimationFrame = requestAnimationFrame;
38
- window.cancelAnimationFrame = cancelAnimationFrame;
37
+ globalThis.requestAnimationFrame = requestAnimationFrame;
38
+ globalThis.cancelAnimationFrame = cancelAnimationFrame;
39
39
  }
@@ -93,7 +93,7 @@ class ImageLayer extends Sprite {
93
93
  this.repeat = settings.repeat || "repeat";
94
94
 
95
95
  // on context lost, all previous textures are destroyed
96
- event.on(event.WEBGL_ONCONTEXT_RESTORED, this.createPattern, this);
96
+ event.on(event.ONCONTEXT_RESTORED, this.createPattern, this);
97
97
  }
98
98
 
99
99
  /**
@@ -294,7 +294,7 @@ class ImageLayer extends Sprite {
294
294
  destroy() {
295
295
  pool.push(this.ratio);
296
296
  this.ratio = undefined;
297
- event.off(event.WEBGL_ONCONTEXT_RESTORED, this.createPattern);
297
+ event.off(event.ONCONTEXT_RESTORED, this.createPattern);
298
298
  super.destroy();
299
299
  }
300
300
 
@@ -45,7 +45,7 @@ function _startRunLoop() {
45
45
  timer.reset();
46
46
 
47
47
  // start the main loop
48
- _animFrameId = window.requestAnimationFrame(_renderFrame);
48
+ _animFrameId = globalThis.requestAnimationFrame(_renderFrame);
49
49
  }
50
50
  }
51
51
 
@@ -85,7 +85,7 @@ function _renderFrame(time) {
85
85
  game.draw(stage);
86
86
  // schedule the next frame update
87
87
  if (_animFrameId !== -1) {
88
- _animFrameId = window.requestAnimationFrame(_renderFrame);
88
+ _animFrameId = globalThis.requestAnimationFrame(_renderFrame);
89
89
  }
90
90
  }
91
91
 
@@ -95,7 +95,7 @@ function _renderFrame(time) {
95
95
  */
96
96
  function _stopRunLoop() {
97
97
  // cancel any previous animationRequestFrame
98
- window.cancelAnimationFrame(_animFrameId);
98
+ globalThis.cancelAnimationFrame(_animFrameId);
99
99
  _animFrameId = -1;
100
100
  }
101
101
 
@@ -268,7 +268,7 @@ var state = {
268
268
  }
269
269
 
270
270
  // store time when stopped
271
- _pauseTime = window.performance.now();
271
+ _pauseTime = globalThis.performance.now();
272
272
 
273
273
  // publish the stop notification
274
274
  event.emit(event.STATE_STOP);
@@ -294,7 +294,7 @@ var state = {
294
294
  }
295
295
 
296
296
  // store time when paused
297
- _pauseTime = window.performance.now();
297
+ _pauseTime = globalThis.performance.now();
298
298
 
299
299
  // publish the pause event
300
300
  event.emit(event.STATE_PAUSE);
@@ -319,7 +319,7 @@ var state = {
319
319
  }
320
320
 
321
321
  // calculate the elpased time
322
- _pauseTime = window.performance.now() - _pauseTime;
322
+ _pauseTime = globalThis.performance.now() - _pauseTime;
323
323
 
324
324
  // force repaint
325
325
  game.repaint();
@@ -347,7 +347,7 @@ var state = {
347
347
  }
348
348
 
349
349
  // calculate the elpased time
350
- _pauseTime = window.performance.now() - _pauseTime;
350
+ _pauseTime = globalThis.performance.now() - _pauseTime;
351
351
 
352
352
  // publish the resume event
353
353
  event.emit(event.STATE_RESUME, _pauseTime);