melonjs 9.1.0 → 10.0.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.
Files changed (78) hide show
  1. package/{LICENSE → LICENSE.md} +0 -0
  2. package/README.md +93 -57
  3. package/dist/melonjs.js +10334 -11179
  4. package/dist/melonjs.min.js +4 -10
  5. package/dist/melonjs.module.d.ts +13206 -0
  6. package/dist/melonjs.module.js +9913 -10872
  7. package/package.json +19 -14
  8. package/src/audio/audio.js +477 -553
  9. package/src/camera/camera2d.js +67 -65
  10. package/src/entity/draggable.js +26 -35
  11. package/src/entity/droptarget.js +17 -14
  12. package/src/entity/entity.js +59 -79
  13. package/src/game.js +194 -204
  14. package/src/index.js +12 -30
  15. package/src/input/gamepad.js +8 -19
  16. package/src/input/keyboard.js +4 -4
  17. package/src/input/pointer.js +14 -12
  18. package/src/input/pointerevent.js +15 -13
  19. package/src/lang/deprecated.js +2 -887
  20. package/src/level/level.js +3 -3
  21. package/src/level/tiled/TMXGroup.js +7 -11
  22. package/src/level/tiled/TMXLayer.js +33 -32
  23. package/src/level/tiled/TMXTileMap.js +15 -19
  24. package/src/level/tiled/TMXTileset.js +5 -5
  25. package/src/level/tiled/TMXUtils.js +3 -3
  26. package/src/level/tiled/renderer/TMXRenderer.js +4 -0
  27. package/src/loader/loader.js +8 -23
  28. package/src/loader/loadingscreen.js +51 -60
  29. package/src/math/matrix3.js +1 -1
  30. package/src/particles/emitter.js +36 -39
  31. package/src/particles/particle.js +27 -12
  32. package/src/particles/particlecontainer.js +17 -16
  33. package/src/physics/body.js +80 -118
  34. package/src/physics/collision.js +5 -235
  35. package/src/physics/detector.js +235 -0
  36. package/src/physics/quadtree.js +14 -14
  37. package/src/physics/world.js +84 -18
  38. package/src/plugin/plugin.js +26 -24
  39. package/src/polyfill/console.js +9 -14
  40. package/src/renderable/GUI.js +48 -62
  41. package/src/renderable/collectable.js +11 -4
  42. package/src/renderable/colorlayer.js +28 -26
  43. package/src/renderable/container.js +120 -96
  44. package/src/renderable/imagelayer.js +94 -93
  45. package/src/renderable/renderable.js +164 -138
  46. package/src/renderable/sprite.js +42 -44
  47. package/src/renderable/trigger.js +24 -17
  48. package/src/shapes/ellipse.js +27 -27
  49. package/src/shapes/line.js +12 -8
  50. package/src/shapes/poly.js +77 -49
  51. package/src/shapes/rectangle.js +193 -268
  52. package/src/state/stage.js +23 -25
  53. package/src/state/state.js +35 -86
  54. package/src/system/device.js +233 -285
  55. package/src/system/event.js +485 -432
  56. package/src/system/pooling.js +61 -54
  57. package/src/system/save.js +17 -16
  58. package/src/system/timer.js +34 -38
  59. package/src/text/bitmaptext.js +44 -46
  60. package/src/text/text.js +39 -34
  61. package/src/tweens/easing.js +0 -2
  62. package/src/tweens/interpolation.js +3 -8
  63. package/src/tweens/tween.js +332 -351
  64. package/src/utils/function.js +6 -8
  65. package/src/utils/utils.js +34 -30
  66. package/src/video/canvas/canvas_renderer.js +13 -8
  67. package/src/video/renderer.js +8 -7
  68. package/src/video/texture.js +8 -8
  69. package/src/video/texture_cache.js +5 -5
  70. package/src/video/video.js +373 -403
  71. package/src/video/webgl/glshader.js +2 -2
  72. package/src/video/webgl/webgl_compositor.js +14 -8
  73. package/src/video/webgl/webgl_renderer.js +21 -19
  74. package/plugins/debug/debugPanel.js +0 -770
  75. package/plugins/debug/font/PressStart2P.fnt +0 -100
  76. package/plugins/debug/font/PressStart2P.ltr +0 -1
  77. package/plugins/debug/font/PressStart2P.png +0 -0
  78. package/plugins/debug/particleDebugPanel.js +0 -303
@@ -12,19 +12,17 @@
12
12
  * @memberOf me.utils.function
13
13
  * @name defer
14
14
  * @param {Function} fn The function to be deferred.
15
- * @param {Object} scope The execution scope of the deferred function.
16
- * @param {} [arguments...] Optional additional arguments to carry for the
17
- * function.
15
+ * @param {Object} thisArg The value to be passed as the this parameter to the target function when the deferred function is called
16
+ * @param {...*} [args] Optional additional arguments to carry for the function.
18
17
  * @return {Number} id that can be used to clear the deferred function using
19
18
  * clearTimeout
20
19
  * @example
21
20
  * // execute myFunc() when the stack is empty,
22
- * // with the current context and 'myArgument' as parameter
23
- * me.utils.function.defer(fn, this, 'myArgument');
21
+ * // with the current context and [1, 2, 3] as parameter
22
+ * me.utils.function.defer(myFunc, this, 1, 2, 3);
24
23
  */
25
- export function defer(fn, scope) {
26
- var args = Array.prototype.slice.call(arguments, 1);
27
- return setTimeout(fn.bind.apply(fn, args), 0.01);
24
+ export function defer(func, thisArg, ...args) {
25
+ return setTimeout(func.bind(thisArg), 0.01, ...args);
28
26
  };
29
27
 
30
28
  /**
@@ -3,13 +3,13 @@ import * as arrayUtils from "./array.js";
3
3
  import * as fileUtils from "./file.js";
4
4
  import * as stringUtils from "./string.js";
5
5
  import * as fnUtils from "./function.js";
6
- import video from "./../video/video.js";
6
+ import { createCanvas } from "./../video/video.js";
7
7
  import CanvasRenderer from "./../video/canvas/canvas_renderer.js";
8
8
  import { version } from "./../index.js";
9
9
 
10
10
  /**
11
11
  * a collection of utility functions
12
- * @namespace me.utils
12
+ * @namespace utils
13
13
  * @memberOf me
14
14
  */
15
15
 
@@ -37,7 +37,7 @@ var utils = {
37
37
  getPixels : function (arg) {
38
38
  if (arg instanceof HTMLImageElement) {
39
39
  var _context = CanvasRenderer.getContext2d(
40
- video.createCanvas(arg.width, arg.height)
40
+ createCanvas(arg.width, arg.height)
41
41
  );
42
42
  _context.drawImage(arg, 0, 0);
43
43
  return _context.getImageData(0, 0, arg.width, arg.height);
@@ -100,36 +100,40 @@ var utils = {
100
100
  * var UriFragment = me.utils.getUriFragment();
101
101
  * console.log(UriFragment["mytag"]); //> "value"
102
102
  */
103
- getUriFragment : (function (url) {
104
- var UriFragments = {};
105
- var parsed = false;
106
- return function (url) {
107
- var hash;
108
- if (typeof url === "undefined") {
109
- hash = UriFragments;
110
- if (parsed === true) {
111
- return hash;
112
- }
113
- url = document.location;
114
- parsed = true;
103
+ getUriFragment : function (url) {
104
+ var hash = {};
105
+
106
+ if (typeof url === "undefined") {
107
+ var location = document.location;
108
+
109
+ if (location && location.hash) {
110
+ url = location.hash;
115
111
  } else {
116
- // never cache if a url is passed as parameter
117
- hash = {};
112
+ // No "document.location" exist for Wechat mini game platform.
113
+ return hash;
118
114
  }
119
- // No "document.location" exist for Wechat mini game platform.
120
- if (url && url.hash) {
121
- url.hash.substr(1).split("&").filter(function (value) {
122
- return (value !== "");
123
- }).forEach(function (value) {
124
- var kv = value.split("=");
125
- var k = kv.shift();
126
- var v = kv.join("=");
127
- hash[k] = v || true;
128
- });
115
+ } else {
116
+ // never cache if a url is passed as parameter
117
+ var index = url.indexOf("#");
118
+ if (index !== -1) {
119
+ url = url.substr(index, url.length);
120
+ } else {
121
+ return hash;
129
122
  }
130
- return hash;
131
- };
132
- })(),
123
+ }
124
+
125
+ // parse the url
126
+ url.substr(1).split("&").filter(function (value) {
127
+ return (value !== "");
128
+ }).forEach(function (value) {
129
+ var kv = value.split("=");
130
+ var k = kv.shift();
131
+ var v = kv.join("=");
132
+ hash[k] = v || true;
133
+ });
134
+
135
+ return hash;
136
+ },
133
137
 
134
138
  /**
135
139
  * reset the GUID Base Name
@@ -2,7 +2,7 @@ import Color from "./../../math/color.js";
2
2
  import Renderer from "./../renderer.js";
3
3
  import TextureCache from "./../texture_cache.js";
4
4
  import Ellipse from "./../../shapes/ellipse.js";
5
- import video from "./../video.js";
5
+ import { createCanvas } from "./../video.js";
6
6
 
7
7
 
8
8
 
@@ -36,7 +36,7 @@ class CanvasRenderer extends Renderer {
36
36
 
37
37
  // create the back buffer if we use double buffering
38
38
  if (this.settings.doubleBuffering) {
39
- this.backBufferCanvas = video.createCanvas(this.settings.width, this.settings.height, true);
39
+ this.backBufferCanvas = createCanvas(this.settings.width, this.settings.height, true);
40
40
  this.backBufferContext2D = this.getContext2d(this.backBufferCanvas);
41
41
  }
42
42
  else {
@@ -281,8 +281,9 @@ class CanvasRenderer extends Renderer {
281
281
  * @param {Number} start start angle in radians
282
282
  * @param {Number} end end angle in radians
283
283
  * @param {Boolean} [antiClockwise=false] draw arc anti-clockwise
284
+ * @param {Boolean} [fill=false] also fill the shape with the current color if true
284
285
  */
285
- strokeArc(x, y, radius, start, end, antiClockwise, fill) {
286
+ strokeArc(x, y, radius, start, end, antiClockwise, fill = false) {
286
287
  var context = this.backBufferContext2D;
287
288
 
288
289
  if (context.globalAlpha < 1 / 255) {
@@ -321,8 +322,9 @@ class CanvasRenderer extends Renderer {
321
322
  * @param {Number} y ellipse center point y-axis
322
323
  * @param {Number} w horizontal radius of the ellipse
323
324
  * @param {Number} h vertical radius of the ellipse
325
+ * @param {Boolean} [fill=false] also fill the shape with the current color if true
324
326
  */
325
- strokeEllipse(x, y, w, h, fill) {
327
+ strokeEllipse(x, y, w, h, fill = false) {
326
328
  var context = this.backBufferContext2D;
327
329
 
328
330
  if (context.globalAlpha < 1 / 255) {
@@ -412,8 +414,9 @@ class CanvasRenderer extends Renderer {
412
414
  * @memberOf me.CanvasRenderer.prototype
413
415
  * @function
414
416
  * @param {me.Polygon} poly the shape to draw
417
+ * @param {Boolean} [fill=false] also fill the shape with the current color if true
415
418
  */
416
- strokePolygon(poly, fill) {
419
+ strokePolygon(poly, fill = false) {
417
420
  var context = this.backBufferContext2D;
418
421
 
419
422
  if (context.globalAlpha < 1 / 255) {
@@ -455,8 +458,9 @@ class CanvasRenderer extends Renderer {
455
458
  * @param {Number} y
456
459
  * @param {Number} width
457
460
  * @param {Number} height
461
+ * @param {Boolean} [fill=false] also fill the shape with the current color if true
458
462
  */
459
- strokeRect(x, y, width, height, fill) {
463
+ strokeRect(x, y, width, height, fill = false) {
460
464
  if (fill === true ) {
461
465
  this.fillRect(x, y, width, height);
462
466
  } else {
@@ -695,6 +699,8 @@ class CanvasRenderer extends Renderer {
695
699
  var context = this.backBufferContext2D;
696
700
  var _x = mask.pos.x, _y = mask.pos.y;
697
701
 
702
+ context.save();
703
+
698
704
  // https://github.com/melonjs/melonJS/issues/648
699
705
  if (mask instanceof Ellipse) {
700
706
  var hw = mask.radiusV.x,
@@ -718,7 +724,6 @@ class CanvasRenderer extends Renderer {
718
724
  context.bezierCurveTo(xmin, by, lx, ymax, lx, _y);
719
725
  context.bezierCurveTo(lx, ymin, xmin, ty, _x, ty);
720
726
  } else {
721
- context.save();
722
727
  context.beginPath();
723
728
  context.moveTo(_x + mask.points[0].x, _y + mask.points[0].y);
724
729
  var point;
@@ -726,8 +731,8 @@ class CanvasRenderer extends Renderer {
726
731
  point = mask.points[i];
727
732
  context.lineTo(_x + point.x, _y + point.y);
728
733
  }
729
- context.closePath();
730
734
  }
735
+
731
736
  context.clip();
732
737
  }
733
738
 
@@ -1,7 +1,7 @@
1
1
  import Color from "./../math/color.js";
2
2
  import Matrix3d from "./../math/matrix3.js";
3
- import video from "./video.js";
4
- import event from "./../system/event.js";
3
+ import { createCanvas, renderer } from "./video.js";
4
+ import * as event from "./../system/event.js";
5
5
  import device from "./../system/device.js";
6
6
  import { setPrefixed } from "./../utils/agent.js";
7
7
  import { Texture } from "./texture.js";
@@ -72,7 +72,7 @@ class Renderer {
72
72
  } else if (typeof this.settings.canvas !== "undefined") {
73
73
  this.canvas = this.settings.canvas;
74
74
  } else {
75
- this.canvas = video.createCanvas(this.settings.zoomX, this.settings.zoomY);
75
+ this.canvas = createCanvas(this.settings.zoomX, this.settings.zoomY);
76
76
  }
77
77
 
78
78
  // canvas object and context
@@ -94,8 +94,8 @@ class Renderer {
94
94
  this.Texture = Texture;
95
95
 
96
96
  // reset the instantiated renderer on game reset
97
- event.subscribe(event.GAME_RESET, function () {
98
- video.renderer.reset();
97
+ event.on(event.GAME_RESET, () => {
98
+ renderer.reset();
99
99
  });
100
100
 
101
101
  return this;
@@ -288,7 +288,7 @@ class Renderer {
288
288
  this.currentScissor[2] = width;
289
289
  this.currentScissor[3] = height;
290
290
  // publish the corresponding event
291
- event.publish(event.CANVAS_ONRESIZE, [ width, height ]);
291
+ event.emit(event.CANVAS_ONRESIZE, width, height);
292
292
  }
293
293
  }
294
294
 
@@ -367,7 +367,7 @@ class Renderer {
367
367
  * @return {HTMLCanvasElement|OffscreenCanvas} a new canvas element representing the tinted image
368
368
  */
369
369
  tint(src, color, mode) {
370
- var canvas = video.createCanvas(src.width, src.height, true);
370
+ var canvas = createCanvas(src.width, src.height, true);
371
371
  var context = this.getContext2d(canvas);
372
372
 
373
373
  context.save();
@@ -405,6 +405,7 @@ class Renderer {
405
405
  * @function
406
406
  * @param {me.Rect|me.Polygon|me.Line|me.Ellipse} [mask] the shape defining the mask to be applied
407
407
  */
408
+ // eslint-disable-next-line no-unused-vars
408
409
  setMask(mask) {}
409
410
 
410
411
  /**
@@ -2,7 +2,7 @@ import Vector2d from "./../math/vector2.js";
2
2
  import WebGLRenderer from "./webgl/webgl_renderer.js";
3
3
  import TextureCache from "./texture_cache.js";
4
4
  import Sprite from "./../renderable/sprite.js";
5
- import video from "./video.js";
5
+ import { renderer } from "./video.js";
6
6
  import pool from "./../system/pooling.js";
7
7
  import loader from "./../loader/loader.js";
8
8
  import { ETA } from "./../math/math.js";
@@ -161,11 +161,11 @@ export class Texture {
161
161
 
162
162
  // Add self to TextureCache if cache !== false
163
163
  if (cache !== false) {
164
- this.sources.forEach(function (source) {
164
+ this.sources.forEach((source) => {
165
165
  if (cache instanceof TextureCache) {
166
166
  cache.set(source, this);
167
167
  } else {
168
- video.renderer.cache.set(source, this);
168
+ renderer.cache.set(source, this);
169
169
  }
170
170
  });
171
171
  }
@@ -177,8 +177,8 @@ export class Texture {
177
177
  */
178
178
  parse(data) {
179
179
  var atlas = {};
180
- var self = this;
181
- data.frames.forEach(function (frame) {
180
+
181
+ data.frames.forEach((frame) => {
182
182
  // fix wrongly formatted JSON (e.g. last dummy object in ShoeBox)
183
183
  if (frame.hasOwnProperty("filename")) {
184
184
  // Source coordinates
@@ -202,7 +202,7 @@ export class Texture {
202
202
  height : s.h,
203
203
  angle : (frame.rotated === true) ? -ETA : 0
204
204
  };
205
- self.addUvsMap(atlas, frame.filename, data.meta.size.w, data.meta.size.h);
205
+ this.addUvsMap(atlas, frame.filename, data.meta.size.w, data.meta.size.h);
206
206
  }
207
207
  });
208
208
  return atlas;
@@ -275,7 +275,7 @@ export class Texture {
275
275
  */
276
276
  addUvsMap(atlas, frame, w, h) {
277
277
  // ignore if using the Canvas Renderer
278
- if (video.renderer instanceof WebGLRenderer) {
278
+ if (renderer instanceof WebGLRenderer) {
279
279
  // Source coordinates
280
280
  var s = atlas[frame].offset;
281
281
  var sw = atlas[frame].width;
@@ -300,7 +300,7 @@ export class Texture {
300
300
  */
301
301
  addQuadRegion(name, x, y, w, h) {
302
302
  // TODO: Require proper atlas regions instead of caching arbitrary region keys
303
- if (video.renderer.settings.verbose === true) {
303
+ if (renderer.settings.verbose === true) {
304
304
  console.warn("Adding texture region", name, "for texture", this);
305
305
  }
306
306
 
@@ -1,5 +1,5 @@
1
- import video from "./video.js";
2
- import utils from "./../utils/utils.js";
1
+ import { renderer } from "./video.js";
2
+ import * as fileUtil from "./../utils/file.js";
3
3
  import { Texture, createAtlas } from "./texture.js";
4
4
  import { isPowerOfTwo} from "./../math/math.js";
5
5
 
@@ -50,7 +50,7 @@ class TextureCache {
50
50
  get(image, atlas) {
51
51
  if (!this.cache.has(image)) {
52
52
  if (!atlas) {
53
- atlas = createAtlas(image.width, image.height, image.src ? utils.file.getBasename(image.src) : undefined);
53
+ atlas = createAtlas(image.width, image.height, image.src ? fileUtil.getBasename(image.src) : undefined);
54
54
  }
55
55
  this.set(image, new Texture(atlas, image, false));
56
56
  }
@@ -69,7 +69,7 @@ class TextureCache {
69
69
  }
70
70
 
71
71
  if (!image_cache.has(color)) {
72
- image_cache.set(color, video.renderer.tint(src, color, "multiply"));
72
+ image_cache.set(color, renderer.tint(src, color, "multiply"));
73
73
  }
74
74
 
75
75
  return image_cache.get(color);
@@ -83,7 +83,7 @@ class TextureCache {
83
83
  var height = image.height;
84
84
 
85
85
  // warn if a non POT texture is added to the cache when using WebGL1
86
- if (video.renderer.WebGLVersion === 1 && (!isPowerOfTwo(width) || !isPowerOfTwo(height))) {
86
+ if (renderer.WebGLVersion === 1 && (!isPowerOfTwo(width) || !isPowerOfTwo(height))) {
87
87
  var src = typeof image.src !== "undefined" ? image.src : image;
88
88
  console.warn(
89
89
  "[Texture] " + src + " is not a POT texture " +