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
@@ -21,28 +21,27 @@ var pool = {
21
21
  /**
22
22
  * register an object to the pool. <br>
23
23
  * Pooling must be set to true if more than one such objects will be created. <br>
24
- * (note) If pooling is enabled, you shouldn't instantiate objects with `new`.
25
- * See examples in {@link me.pool#pull}
26
- * @name register
27
- * @memberOf me.pool
28
- * @public
29
- * @function
24
+ * (Note: for an object to be poolable, it must implements a `onResetEvent` method)
25
+ * @function me.pool.register
30
26
  * @param {String} className as defined in the Name field of the Object Properties (in Tiled)
31
27
  * @param {Object} class corresponding Class to be instantiated
32
- * @param {Boolean} [objectPooling=false] enables object pooling for the specified class
33
- * - speeds up the game by reusing existing objects
28
+ * @param {Boolean} [recycling=false] enables object recycling for the specified class
34
29
  * @example
35
- * // add our users defined entities in the object pool
36
- * me.pool.register("playerspawnpoint", PlayerEntity);
30
+ * // implement CherryEntity
31
+ * class CherryEntity extends Spritesheet {
32
+ * onResetEvent() {
33
+ * // reset object mutable properties
34
+ * this.lifeBar = 100;
35
+ * }
36
+ * };
37
+ * // add our users defined entities in the object pool and enable object recycling
37
38
  * me.pool.register("cherryentity", CherryEntity, true);
38
- * me.pool.register("heartentity", HeartEntity, true);
39
- * me.pool.register("starentity", StarEntity, true);
40
39
  */
41
- register(className, classObj, pooling) {
40
+ register(className, classObj, recycling = false) {
42
41
  if (typeof (classObj) !== "undefined") {
43
42
  objectClass[className] = {
44
43
  "class" : classObj,
45
- "pool" : (pooling ? [] : undefined)
44
+ "pool" : (recycling ? [] : undefined)
46
45
  };
47
46
  } else {
48
47
  throw new Error("Cannot register object '" + className + "', invalid class");
@@ -51,17 +50,11 @@ var pool = {
51
50
 
52
51
  /**
53
52
  * Pull a new instance of the requested object (if added into the object pool)
54
- * @name pull
55
- * @memberOf me.pool
56
- * @public
57
- * @function
53
+ * @function me.pool.pull
58
54
  * @param {String} className as used in {@link me.pool.register}
59
55
  * @param {} [arguments...] arguments to be passed when instantiating/reinitializing the object
60
56
  * @return {Object} the instance of the requested object
61
57
  * @example
62
- * me.pool.register("player", PlayerEntity);
63
- * var player = me.pool.pull("player");
64
- * @example
65
58
  * me.pool.register("bullet", BulletEntity, true);
66
59
  * me.pool.register("enemy", EnemyEntity, true);
67
60
  * // ...
@@ -82,25 +75,23 @@ var pool = {
82
75
  for (var i = 0; i < arguments.length; i++) {
83
76
  args[i] = arguments[i];
84
77
  }
85
- var entity = objectClass[name];
86
- if (entity) {
87
- var proto = entity["class"],
88
- poolArray = entity.pool,
78
+ var className = objectClass[name];
79
+ if (className) {
80
+ var proto = className["class"],
81
+ poolArray = className.pool,
89
82
  obj;
90
83
 
91
84
  if (poolArray && ((obj = poolArray.pop()))) {
85
+ // pull an existing instance from the pool
92
86
  args.shift();
93
87
  // call the object onResetEvent function if defined
94
88
  if (typeof(obj.onResetEvent) === "function") {
95
89
  obj.onResetEvent.apply(obj, args);
96
90
  }
97
- else if (typeof(obj.init) === "function") {
98
- // backward compatibility with Jay Inheritance
99
- obj.init.apply(obj, args);
100
- }
101
91
  instance_counter--;
102
92
  }
103
93
  else {
94
+ // create a new instance
104
95
  args[0] = proto;
105
96
  obj = new (proto.bind.apply(proto, args))();
106
97
  if (poolArray) {
@@ -109,7 +100,6 @@ var pool = {
109
100
  }
110
101
  return obj;
111
102
  }
112
-
113
103
  throw new Error("Cannot instantiate object of type '" + name + "'");
114
104
  },
115
105
 
@@ -117,10 +107,7 @@ var pool = {
117
107
  * purge the object pool from any inactive object <br>
118
108
  * Object pooling must be enabled for this function to work<br>
119
109
  * note: this will trigger the garbage collector
120
- * @name purge
121
- * @memberOf me.pool
122
- * @public
123
- * @function
110
+ * @function me.pool.purge
124
111
  */
125
112
  purge() {
126
113
  for (var className in objectClass) {
@@ -136,45 +123,65 @@ var pool = {
136
123
  * Object pooling for the object class must be enabled,
137
124
  * and object must have been instantiated using {@link me.pool#pull},
138
125
  * otherwise this function won't work
139
- * @name push
140
- * @memberOf me.pool
141
- * @public
142
- * @function
126
+ * @function me.pool.push
127
+ * @throws will throw an error if the object cannot be recycled
143
128
  * @param {Object} instance to be recycled
129
+ * @param {Boolean} [throwOnError=true] throw an exception if the object cannot be recycled
130
+ * @return {Boolean} true if the object was successfully recycled in the object pool
144
131
  */
145
- push(obj) {
146
- var name = obj.className;
147
- if (typeof(name) === "undefined" || !objectClass[name]) {
148
- // object is not registered, don't do anything
149
- return;
132
+ push(obj, throwOnError = true) {
133
+ if (!this.poolable(obj)) {
134
+ if (throwOnError === true ) {
135
+ throw new Error("me.pool: object " + obj + " cannot be recycled");
136
+ } else {
137
+ return false;
138
+ }
150
139
  }
140
+
151
141
  // store back the object instance for later recycling
152
- objectClass[name].pool.push(obj);
142
+ objectClass[obj.className].pool.push(obj);
153
143
  instance_counter++;
144
+
145
+ return true;
154
146
  },
155
147
 
156
148
  /**
157
149
  * Check if an object with the provided name is registered
158
- * @name exists
159
- * @memberOf me.pool
160
- * @public
161
- * @function
162
- * @param {String} name of the registered object
150
+ * @function me.pool.exists
151
+ * @param {String} name of the registered object class
163
152
  * @return {Boolean} true if the classname is registered
164
153
  */
165
154
  exists(name) {
166
155
  return name in objectClass;
167
156
  },
168
157
 
158
+ /**
159
+ * Check if an object is poolable
160
+ * (was properly registered with the recycling feature enable)
161
+ * @function me.pool.poolable
162
+ * @see me.pool.register
163
+ * @param {Object} object
164
+ * @return {Boolean} true if the object is poolable
165
+ * @example
166
+ * if (!me.pool.poolable(myCherryEntity)) {
167
+ * // object was not properly registered
168
+ * }
169
+ */
170
+ poolable(obj) {
171
+ var className = obj.className;
172
+ return (typeof className !== "undefined") &&
173
+ (typeof obj.onResetEvent === "function") &&
174
+ (className in objectClass) &&
175
+ (objectClass[className].pool !== "undefined");
176
+
177
+ },
178
+
169
179
  /**
170
180
  * returns the amount of object instance currently in the pool
171
- * @name getInstanceCount
172
- * @memberOf me.pool
173
- * @public
174
- * @function
181
+ * @function me.pool.getInstanceCount
175
182
  * @return {Number} amount of object instance
176
183
  */
177
- getInstanceCount(name) {
184
+ getInstanceCount() {
178
185
  return instance_counter;
179
186
  }
180
187
  };
@@ -1,4 +1,6 @@
1
1
  import device from "./../system/device.js";
2
+ import * as event from "./../system/event.js";
3
+
2
4
  /**
3
5
  * allow to access and manage the device localStorage
4
6
  * @example
@@ -37,26 +39,25 @@ import device from "./../system/device.js";
37
39
  // a function to check if the given key is a reserved word
38
40
  function isReserved(key) {
39
41
  return (key === "add" || key === "remove");
40
- };
42
+ }
41
43
 
42
- var save = {
43
44
 
44
- /**
45
- * @ignore
46
- */
47
- init() {
48
- // Load previous data if local Storage is supported
49
- if (device.localStorage === true) {
50
- var me_save_content = localStorage.getItem("me.save");
45
+ // Initialize me.save on Boot event
46
+ event.on(event.BOOT, () => {
47
+ // Load previous data if local Storage is supported
48
+ if (device.localStorage === true) {
49
+ var me_save_content = localStorage.getItem("me.save");
51
50
 
52
- if (typeof me_save_content === "string" && me_save_content.length > 0) {
53
- var keys = JSON.parse(me_save_content) || [];
54
- keys.forEach(function (key) {
55
- data[key] = JSON.parse(localStorage.getItem("me.save." + key));
56
- });
57
- }
51
+ if (typeof me_save_content === "string" && me_save_content.length > 0) {
52
+ var keys = JSON.parse(me_save_content) || [];
53
+ keys.forEach(function (key) {
54
+ data[key] = JSON.parse(localStorage.getItem("me.save." + key));
55
+ });
58
56
  }
59
- },
57
+ }
58
+ });
59
+
60
+ var save = {
60
61
 
61
62
  /**
62
63
  * Add new keys to localStorage and set them to the given default values if they do not exist
@@ -1,5 +1,5 @@
1
1
  import utils from "./../utils/utils.js";
2
- import event from "./../system/event.js";
2
+ import * as event from "./../system/event.js";
3
3
  import state from "./../state/state.js";
4
4
  import { clamp } from "./../math/math.js";
5
5
 
@@ -20,6 +20,28 @@ var timers = [];
20
20
  var timerId = 0;
21
21
 
22
22
  /**
23
+ * update
24
+ * @ignore
25
+ */
26
+ function update(time) {
27
+ last = now;
28
+ now = time;
29
+ delta = (now - last);
30
+
31
+ // fix for negative timestamp returned by wechat or chrome on startup
32
+ if (delta < 0) {
33
+ delta = 0;
34
+ }
35
+
36
+ // get the game tick
37
+ timer.tick = (delta > minstep && timer.interpolation) ? delta / step : 1;
38
+
39
+
40
+ updateTimers();
41
+ };
42
+
43
+ /**
44
+ * clear Timers
23
45
  * @ignore
24
46
  */
25
47
  function clearTimer(timerId) {
@@ -31,23 +53,12 @@ function clearTimer(timerId) {
31
53
  }
32
54
  };
33
55
 
56
+
34
57
  /**
35
58
  * update timers
36
59
  * @ignore
37
60
  */
38
- function updateTimers(time) {
39
- last = now;
40
- now = time;
41
- delta = (now - last);
42
-
43
- // fix for negative timestamp returned by wechat or chrome on startup
44
- if (delta < 0) {
45
- delta = 0;
46
- }
47
-
48
- // get the game tick
49
- timer.tick = (delta > minstep && timer.interpolation) ? delta / step : 1;
50
-
61
+ function updateTimers() {
51
62
  for (var i = 0, len = timers.length; i < len; i++) {
52
63
  var _timer = timers[i];
53
64
  if (!(_timer.pauseable && state.isPaused())) {
@@ -64,6 +75,15 @@ function updateTimers(time) {
64
75
  }
65
76
  };
66
77
 
78
+ // Initialize me.timer on Boot event
79
+ event.on(event.BOOT, () => {
80
+ // reset variables to initial state
81
+ timer.reset();
82
+ now = last = 0;
83
+ // register to the game before update event
84
+ event.on(event.GAME_BEFORE_UPDATE, update);
85
+ });
86
+
67
87
 
68
88
  /**
69
89
  * a Timer class to manage timing related function (FPS, Game Tick, Time...)
@@ -117,30 +137,6 @@ var timer = {
117
137
  */
118
138
  interpolation : false,
119
139
 
120
- /**
121
- * Last update time.<br/>
122
- * Use this value to implement frame prediction in drawing events,
123
- * for creating smooth motion while running game update logic at
124
- * a lower fps.
125
- * @public
126
- * @type Date
127
- * @name lastUpdate
128
- * @memberOf me.timer
129
- */
130
- lastUpdate : window.performance.now(),
131
-
132
- /**
133
- * init the timer
134
- * @ignore
135
- */
136
- init() {
137
- // reset variables to initial state
138
- this.reset();
139
- now = last = 0;
140
- // register to the game update event
141
- event.subscribe(event.GAME_UPDATE, updateTimers);
142
- },
143
-
144
140
  /**
145
141
  * reset time (e.g. usefull in case of pause)
146
142
  * @name reset
@@ -1,5 +1,5 @@
1
1
  import Color from "./../math/color.js";
2
- import utils from "./../utils/utils.js";
2
+ import * as stringUtil from "./../utils/string.js";
3
3
  import pool from "./../system/pooling.js";
4
4
  import loader from "./../loader/loader.js";
5
5
  import Renderable from "./../renderable/renderable.js";
@@ -63,12 +63,13 @@ var measureTextHeight = function(font) {
63
63
  * // or just add it to the word container
64
64
  * me.game.world.addChild(myFont);
65
65
  */
66
- var BitmapText = Renderable.extend({
66
+
67
+ class BitmapText extends Renderable {
67
68
 
68
69
  /** @ignore */
69
- init : function (x, y, settings) {
70
+ constructor(x, y, settings) {
70
71
  // call the parent constructor
71
- this._super(Renderable, "init", [x, y, settings.width || 0, settings.height || 0]);
72
+ super(x, y, settings.width || 0, settings.height || 0);
72
73
 
73
74
  /**
74
75
  * Set the default text alignment (or justification),<br>
@@ -158,7 +159,7 @@ var BitmapText = Renderable.extend({
158
159
 
159
160
  // set the text
160
161
  this.setText(settings.text);
161
- },
162
+ }
162
163
 
163
164
  /**
164
165
  * change the font settings
@@ -169,7 +170,7 @@ var BitmapText = Renderable.extend({
169
170
  * @param {Number} [scale]
170
171
  * @return this object for chaining
171
172
  */
172
- set : function (textAlign, scale) {
173
+ set(textAlign, scale) {
173
174
  this.textAlign = textAlign;
174
175
  // updated scaled Size
175
176
  if (scale) {
@@ -178,7 +179,7 @@ var BitmapText = Renderable.extend({
178
179
  this.isDirty = true;
179
180
 
180
181
  return this;
181
- },
182
+ }
182
183
 
183
184
  /**
184
185
  * change the text to be displayed
@@ -188,7 +189,7 @@ var BitmapText = Renderable.extend({
188
189
  * @param {Number|String|String[]} value a string, or an array of strings
189
190
  * @return this object for chaining
190
191
  */
191
- setText : function (value) {
192
+ setText(value) {
192
193
  if (typeof value === "undefined") {
193
194
  value = "";
194
195
  }
@@ -203,8 +204,29 @@ var BitmapText = Renderable.extend({
203
204
  }
204
205
 
205
206
  return this;
206
- },
207
+ }
207
208
 
209
+ /**
210
+ * defines the color used to tint the bitmap text
211
+ * @public
212
+ * @type {me.Color}
213
+ * @name fillStyle
214
+ * @see me.Renderable#tint
215
+ * @memberOf me.BitmapText
216
+ */
217
+
218
+ /**
219
+ * @ignore
220
+ */
221
+ get fillStyle() {
222
+ return this.tint;
223
+ }
224
+ /**
225
+ * @ignore
226
+ */
227
+ set fillStyle(value) {
228
+ this.tint = value;
229
+ }
208
230
 
209
231
  /**
210
232
  * change the font display size
@@ -214,14 +236,13 @@ var BitmapText = Renderable.extend({
214
236
  * @param {Number} scale ratio
215
237
  * @return this object for chaining
216
238
  */
217
- resize : function (scale) {
239
+ resize(scale) {
218
240
  this.fontScale.set(scale, scale);
219
241
  // clear the cache text to recalculate bounds
220
242
  this.isDirty = true;
221
243
 
222
244
  return this;
223
- },
224
-
245
+ }
225
246
 
226
247
  /**
227
248
  * measure the given text size in pixels
@@ -232,7 +253,7 @@ var BitmapText = Renderable.extend({
232
253
  * @param {me.Rect} [ret] a object in which to store the text metrics
233
254
  * @returns {TextMetrics} a TextMetrics object with two properties: `width` and `height`, defining the output dimensions
234
255
  */
235
- measureText : function (text, ret) {
256
+ measureText(text, ret) {
236
257
  text = text || this._text;
237
258
 
238
259
  var stringHeight = measureTextHeight(this);
@@ -246,17 +267,17 @@ var BitmapText = Renderable.extend({
246
267
  textMetrics.height += stringHeight;
247
268
  }
248
269
  return textMetrics;
249
- },
270
+ }
250
271
 
251
272
  /**
252
273
  * @ignore
253
274
  */
254
- update : function (/* dt */) {
275
+ update(/* dt */) {
255
276
  if (this.isDirty === true) {
256
277
  this.measureText();
257
278
  }
258
279
  return this.isDirty;
259
- },
280
+ }
260
281
 
261
282
  /**
262
283
  * draw the bitmap font
@@ -268,7 +289,7 @@ var BitmapText = Renderable.extend({
268
289
  * @param {Number} [x]
269
290
  * @param {Number} [y]
270
291
  */
271
- draw : function (renderer, text, x, y) {
292
+ draw(renderer, text, x, y) {
272
293
  // save the previous global alpha value
273
294
  var _alpha = renderer.globalAlpha();
274
295
 
@@ -292,7 +313,7 @@ var BitmapText = Renderable.extend({
292
313
 
293
314
  for (var i = 0; i < this._text.length; i++) {
294
315
  x = lX;
295
- var string = utils.string.trimRight(this._text[i]);
316
+ var string = stringUtil.trimRight(this._text[i]);
296
317
  // adjust x pos based on alignment value
297
318
  var stringWidth = measureTextWidth(this, string);
298
319
  switch (this.textAlign) {
@@ -373,45 +394,22 @@ var BitmapText = Renderable.extend({
373
394
  // clear the dirty flag here for
374
395
  // backward compatibility
375
396
  this.isDirty = false;
376
- },
377
-
397
+ }
378
398
 
379
399
  /**
380
400
  * Destroy function
381
401
  * @ignore
382
402
  */
383
- destroy : function () {
403
+ destroy() {
384
404
  pool.push(this.fontScale);
385
405
  this.fontScale = undefined;
386
406
  pool.push(this.fontData);
387
407
  this.fontData = undefined;
388
408
  this._text.length = 0;
389
- this._super(Renderable, "destroy");
409
+ super.destroy();
390
410
  }
391
- });
392
411
 
393
- /**
394
- * defines the color used to tint the bitmap text
395
- * @public
396
- * @type {me.Color}
397
- * @name fillStyle
398
- * @see me.Renderable#tint
399
- * @memberOf me.BitmapText
400
- */
401
- Object.defineProperty(BitmapText.prototype, "fillStyle", {
402
- /**
403
- * @ignore
404
- */
405
- get : function () {
406
- return this.tint;
407
- },
408
- /**
409
- * @ignore
410
- */
411
- set : function (value) {
412
- this.tint = value;
413
- },
414
- configurable : true
415
- });
412
+ };
413
+
416
414
 
417
415
  export default BitmapText;