melonjs 10.7.1 → 10.10.0

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 (74) hide show
  1. package/LICENSE.md +1 -1
  2. package/README.md +29 -23
  3. package/dist/melonjs.js +2220 -1070
  4. package/dist/melonjs.min.js +4 -4
  5. package/dist/melonjs.module.d.ts +1395 -485
  6. package/dist/melonjs.module.js +2244 -1131
  7. package/package.json +17 -14
  8. package/src/camera/camera2d.js +1 -1
  9. package/src/entity/entity.js +6 -7
  10. package/src/game.js +2 -2
  11. package/src/geometries/ellipse.js +20 -21
  12. package/src/geometries/line.js +7 -7
  13. package/src/geometries/path2d.js +319 -0
  14. package/src/geometries/poly.js +27 -27
  15. package/src/geometries/rectangle.js +19 -19
  16. package/src/geometries/roundrect.js +164 -0
  17. package/src/index.js +12 -2
  18. package/src/input/gamepad.js +2 -2
  19. package/src/input/pointerevent.js +1 -1
  20. package/src/lang/deprecated.js +8 -6
  21. package/src/level/tiled/TMXLayer.js +1 -1
  22. package/src/level/tiled/TMXObject.js +9 -12
  23. package/src/level/tiled/TMXTileMap.js +23 -4
  24. package/src/level/tiled/TMXUtils.js +1 -1
  25. package/src/level/tiled/renderer/TMXHexagonalRenderer.js +1 -1
  26. package/src/level/tiled/renderer/TMXIsometricRenderer.js +1 -1
  27. package/src/level/tiled/renderer/TMXOrthogonalRenderer.js +1 -1
  28. package/src/level/tiled/renderer/TMXRenderer.js +1 -1
  29. package/src/level/tiled/renderer/TMXStaggeredRenderer.js +1 -1
  30. package/src/loader/loader.js +4 -4
  31. package/src/loader/loadingscreen.js +17 -6
  32. package/src/math/color.js +6 -5
  33. package/src/math/matrix2.js +1 -1
  34. package/src/math/matrix3.js +1 -1
  35. package/src/math/observable_vector2.js +1 -1
  36. package/src/math/observable_vector3.js +1 -1
  37. package/src/math/vector2.js +1 -1
  38. package/src/math/vector3.js +1 -1
  39. package/src/particles/emitter.js +34 -26
  40. package/src/particles/particle.js +3 -2
  41. package/src/physics/body.js +67 -51
  42. package/src/physics/bounds.js +8 -9
  43. package/src/physics/world.js +1 -1
  44. package/src/polyfill/index.js +1 -0
  45. package/src/polyfill/roundrect.js +235 -0
  46. package/src/renderable/GUI.js +5 -5
  47. package/src/renderable/collectable.js +9 -2
  48. package/src/renderable/colorlayer.js +1 -1
  49. package/src/renderable/container.js +27 -27
  50. package/src/renderable/imagelayer.js +3 -3
  51. package/src/renderable/light2d.js +115 -0
  52. package/src/renderable/renderable.js +23 -22
  53. package/src/renderable/sprite.js +15 -16
  54. package/src/renderable/trigger.js +10 -4
  55. package/src/state/stage.js +73 -3
  56. package/src/state/state.js +1 -1
  57. package/src/system/device.js +10 -8
  58. package/src/system/pooling.js +156 -149
  59. package/src/text/bitmaptext.js +1 -1
  60. package/src/text/text.js +14 -18
  61. package/src/utils/utils.js +2 -2
  62. package/src/video/canvas/canvas_renderer.js +144 -81
  63. package/src/video/renderer.js +64 -37
  64. package/src/video/{texture.js → texture/atlas.js} +8 -8
  65. package/src/video/{texture_cache.js → texture/cache.js} +4 -4
  66. package/src/video/texture/canvas_texture.js +87 -0
  67. package/src/video/webgl/glshader.js +29 -193
  68. package/src/video/webgl/utils/attributes.js +16 -0
  69. package/src/video/webgl/utils/precision.js +11 -0
  70. package/src/video/webgl/utils/program.js +58 -0
  71. package/src/video/webgl/utils/string.js +16 -0
  72. package/src/video/webgl/utils/uniforms.js +87 -0
  73. package/src/video/webgl/webgl_compositor.js +1 -14
  74. package/src/video/webgl/webgl_renderer.js +191 -231
@@ -1,7 +1,5 @@
1
- var objectClass = {};
2
- var instance_counter = 0;
3
-
4
1
  /**
2
+ * @classdesc
5
3
  * This object is used for object pooling - a technique that might speed up your game if used properly.<br>
6
4
  * If some of your classes will be instantiated and removed a lot at a time, it is a
7
5
  * good idea to add the class to this object pool. A separate pool for that class
@@ -12,172 +10,181 @@ var instance_counter = 0;
12
10
  * which means, that on level loading the engine will try to instantiate every object
13
11
  * found in the map, based on the user defined name in each Object Properties<br>
14
12
  * <img src="images/object_properties.png"/><br>
15
- * @namespace pool
13
+ * @see {@link pool} a default global instance of ObjectPool
16
14
  */
15
+ class ObjectPool {
17
16
 
18
- /**
19
- * register an object to the pool. <br>
20
- * Pooling must be set to true if more than one such objects will be created. <br>
21
- * (Note: for an object to be poolable, it must implements a `onResetEvent` method)
22
- * @function pool.register
23
- * @param {string} className as defined in the Name field of the Object Properties (in Tiled)
24
- * @param {object} classObj corresponding Class to be instantiated
25
- * @param {boolean} [recycling=false] enables object recycling for the specified class
26
- * @example
27
- * // implement CherryEntity
28
- * class CherryEntity extends Spritesheet {
29
- * onResetEvent() {
30
- * // reset object mutable properties
31
- * this.lifeBar = 100;
32
- * }
33
- * };
34
- * // add our users defined entities in the object pool and enable object recycling
35
- * me.pool.register("cherryentity", CherryEntity, true);
36
- */
37
- export function register(className, classObj, recycling = false) {
38
- if (typeof (classObj) !== "undefined") {
39
- objectClass[className] = {
40
- "class" : classObj,
41
- "pool" : (recycling ? [] : undefined)
42
- };
43
- } else {
44
- throw new Error("Cannot register object '" + className + "', invalid class");
17
+ constructor() {
18
+ this.objectClass = {};
19
+ this.instance_counter = 0;
20
+ }
21
+
22
+ /**
23
+ * register an object to the pool. <br>
24
+ * Pooling must be set to true if more than one such objects will be created. <br>
25
+ * (Note: for an object to be poolable, it must implements a `onResetEvent` method)
26
+ * @param {string} className as defined in the Name field of the Object Properties (in Tiled)
27
+ * @param {object} classObj corresponding Class to be instantiated
28
+ * @param {boolean} [recycling=false] enables object recycling for the specified class
29
+ * @example
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
38
+ * me.pool.register("cherryentity", CherryEntity, true);
39
+ */
40
+ register(className, classObj, recycling = false) {
41
+ if (typeof (classObj) !== "undefined") {
42
+ this.objectClass[className] = {
43
+ "class" : classObj,
44
+ "pool" : (recycling ? [] : undefined)
45
+ };
46
+ } else {
47
+ throw new Error("Cannot register object '" + className + "', invalid class");
48
+ }
45
49
  }
46
- };
47
50
 
48
- /**
49
- * Pull a new instance of the requested object (if added into the object pool)
50
- * @function pool.pull
51
- * @param {string} name as used in {@link pool.register}
52
- * @param {object} [...arguments] arguments to be passed when instantiating/reinitializing the object
53
- * @returns {object} the instance of the requested object
54
- * @example
55
- * me.pool.register("bullet", BulletEntity, true);
56
- * me.pool.register("enemy", EnemyEntity, true);
57
- * // ...
58
- * // when we need to manually create a new bullet:
59
- * var bullet = me.pool.pull("bullet", x, y, direction);
60
- * // ...
61
- * // params aren't a fixed number
62
- * // when we need new enemy we can add more params, that the object construct requires:
63
- * var enemy = me.pool.pull("enemy", x, y, direction, speed, power, life);
64
- * // ...
65
- * // when we want to destroy existing object, the remove
66
- * // function will ensure the object can then be reallocated later
67
- * me.game.world.removeChild(enemy);
68
- * me.game.world.removeChild(bullet);
69
- */
70
- export function pull(name) {
71
- var args = new Array(arguments.length);
72
- for (var i = 0; i < arguments.length; i++) {
73
- args[i] = arguments[i];
74
- }
75
- var className = objectClass[name];
76
- if (className) {
77
- var proto = className["class"],
78
- poolArray = className.pool,
79
- obj;
51
+ /**
52
+ * Pull a new instance of the requested object (if added into the object pool)
53
+ * @param {string} name as used in {@link pool.register}
54
+ * @param {object} [...arguments] arguments to be passed when instantiating/reinitializing the object
55
+ * @returns {object} the instance of the requested object
56
+ * @example
57
+ * me.pool.register("bullet", BulletEntity, true);
58
+ * me.pool.register("enemy", EnemyEntity, true);
59
+ * // ...
60
+ * // when we need to manually create a new bullet:
61
+ * var bullet = me.pool.pull("bullet", x, y, direction);
62
+ * // ...
63
+ * // params aren't a fixed number
64
+ * // when we need new enemy we can add more params, that the object construct requires:
65
+ * var enemy = me.pool.pull("enemy", x, y, direction, speed, power, life);
66
+ * // ...
67
+ * // when we want to destroy existing object, the remove
68
+ * // function will ensure the object can then be reallocated later
69
+ * me.game.world.removeChild(enemy);
70
+ * me.game.world.removeChild(bullet);
71
+ */
72
+ pull(name) {
73
+ var args = new Array(arguments.length);
74
+ for (var i = 0; i < arguments.length; i++) {
75
+ args[i] = arguments[i];
76
+ }
77
+ var className = this.objectClass[name];
78
+ if (className) {
79
+ var proto = className["class"],
80
+ poolArray = className.pool,
81
+ obj;
80
82
 
81
- if (poolArray && ((obj = poolArray.pop()))) {
82
- // pull an existing instance from the pool
83
- args.shift();
84
- // call the object onResetEvent function if defined
85
- if (typeof(obj.onResetEvent) === "function") {
86
- obj.onResetEvent.apply(obj, args);
83
+ if (poolArray && ((obj = poolArray.pop()))) {
84
+ // pull an existing instance from the pool
85
+ args.shift();
86
+ // call the object onResetEvent function if defined
87
+ if (typeof(obj.onResetEvent) === "function") {
88
+ obj.onResetEvent.apply(obj, args);
89
+ }
90
+ this.instance_counter--;
87
91
  }
88
- instance_counter--;
89
- }
90
- else {
91
- // create a new instance
92
- args[0] = proto;
93
- obj = new (proto.bind.apply(proto, args))();
94
- if (poolArray) {
95
- obj.className = name;
92
+ else {
93
+ // create a new instance
94
+ args[0] = proto;
95
+ obj = new (proto.bind.apply(proto, args))();
96
+ if (poolArray) {
97
+ obj.className = name;
98
+ }
96
99
  }
100
+ return obj;
97
101
  }
98
- return obj;
102
+ throw new Error("Cannot instantiate object of type '" + name + "'");
99
103
  }
100
- throw new Error("Cannot instantiate object of type '" + name + "'");
101
- };
102
104
 
103
- /**
104
- * purge the object pool from any inactive object <br>
105
- * Object pooling must be enabled for this function to work<br>
106
- * note: this will trigger the garbage collector
107
- * @function pool.purge
108
- */
109
- export function purge() {
110
- for (var className in objectClass) {
111
- if (objectClass[className]) {
112
- objectClass[className].pool = [];
105
+ /**
106
+ * purge the object pool from any inactive object <br>
107
+ * Object pooling must be enabled for this function to work<br>
108
+ * note: this will trigger the garbage collector
109
+ */
110
+ purge() {
111
+ for (var className in this.objectClass) {
112
+ if (this.objectClass[className]) {
113
+ this.objectClass[className].pool = [];
114
+ }
113
115
  }
116
+ this.instance_counter = 0;
114
117
  }
115
- instance_counter = 0;
116
- };
117
118
 
118
- /**
119
- * Push back an object instance into the object pool <br>
120
- * Object pooling for the object class must be enabled,
121
- * and object must have been instantiated using {@link pool#pull},
122
- * otherwise this function won't work
123
- * @function pool.push
124
- * @throws will throw an error if the object cannot be recycled
125
- * @param {object} obj instance to be recycled
126
- * @param {boolean} [throwOnError=true] throw an exception if the object cannot be recycled
127
- * @returns {boolean} true if the object was successfully recycled in the object pool
128
- */
129
- export function push(obj, throwOnError = true) {
130
- if (!poolable(obj)) {
131
- if (throwOnError === true ) {
132
- throw new Error("me.pool: object " + obj + " cannot be recycled");
133
- } else {
134
- return false;
119
+ /**
120
+ * Push back an object instance into the object pool <br>
121
+ * Object pooling for the object class must be enabled,
122
+ * and object must have been instantiated using {@link pool#pull},
123
+ * otherwise this function won't work
124
+ * @throws will throw an error if the object cannot be recycled
125
+ * @param {object} obj instance to be recycled
126
+ * @param {boolean} [throwOnError=true] throw an exception if the object cannot be recycled
127
+ * @returns {boolean} true if the object was successfully recycled in the object pool
128
+ */
129
+ push(obj, throwOnError = true) {
130
+ if (!this.poolable(obj)) {
131
+ if (throwOnError === true ) {
132
+ throw new Error("me.pool: object " + obj + " cannot be recycled");
133
+ } else {
134
+ return false;
135
+ }
135
136
  }
136
- }
137
137
 
138
- // store back the object instance for later recycling
139
- objectClass[obj.className].pool.push(obj);
140
- instance_counter++;
138
+ // store back the object instance for later recycling
139
+ this.objectClass[obj.className].pool.push(obj);
140
+ this.instance_counter++;
141
141
 
142
- return true;
143
- };
142
+ return true;
143
+ }
144
144
 
145
- /**
146
- * Check if an object with the provided name is registered
147
- * @function pool.exists
148
- * @param {string} name of the registered object class
149
- * @returns {boolean} true if the classname is registered
150
- */
151
- export function exists(name) {
152
- return name in objectClass;
153
- };
145
+ /**
146
+ * Check if an object with the provided name is registered
147
+ * @param {string} name of the registered object class
148
+ * @returns {boolean} true if the classname is registered
149
+ */
150
+ exists(name) {
151
+ return name in this.objectClass;
152
+ };
154
153
 
155
- /**
156
- * Check if an object is poolable
157
- * (was properly registered with the recycling feature enable)
158
- * @function pool.poolable
159
- * @see pool.register
160
- * @param {object} obj object to be checked
161
- * @returns {boolean} true if the object is poolable
162
- * @example
163
- * if (!me.pool.poolable(myCherryEntity)) {
164
- * // object was not properly registered
165
- * }
166
- */
167
- export function poolable(obj) {
168
- var className = obj.className;
169
- return (typeof className !== "undefined") &&
170
- (typeof obj.onResetEvent === "function") &&
171
- (className in objectClass) &&
172
- (objectClass[className].pool !== "undefined");
154
+ /**
155
+ * Check if an object is poolable
156
+ * (was properly registered with the recycling feature enable)
157
+ * @see register
158
+ * @param {object} obj object to be checked
159
+ * @returns {boolean} true if the object is poolable
160
+ * @example
161
+ * if (!me.pool.poolable(myCherryEntity)) {
162
+ * // object was not properly registered
163
+ * }
164
+ */
165
+ poolable(obj) {
166
+ var className = obj.className;
167
+ return (typeof className !== "undefined") &&
168
+ (typeof obj.onResetEvent === "function") &&
169
+ (className in this.objectClass) &&
170
+ (this.objectClass[className].pool !== "undefined");
171
+
172
+ }
173
173
 
174
+ /**
175
+ * returns the amount of object instance currently in the pool
176
+ * @returns {number} amount of object instance
177
+ */
178
+ getInstanceCount() {
179
+ return this.instance_counter;
180
+ }
174
181
  };
175
182
 
176
183
  /**
177
- * returns the amount of object instance currently in the pool
178
- * @function pool.getInstanceCount
179
- * @returns {number} amount of object instance
184
+ * a default global object pool instance
185
+ * @public
186
+ * @type {ObjectPool}
180
187
  */
181
- export function getInstanceCount() {
182
- return instance_counter;
183
- };
188
+ var pool = new ObjectPool();
189
+
190
+ export default pool;
@@ -1,6 +1,6 @@
1
1
  import Color from "./../math/color.js";
2
2
  import * as stringUtil from "./../utils/string.js";
3
- import * as pool from "./../system/pooling.js";
3
+ import pool from "./../system/pooling.js";
4
4
  import loader from "./../loader/loader.js";
5
5
  import Renderable from "./../renderable/renderable.js";
6
6
  import TextMetrics from "./textmetrics.js";
package/src/text/text.js CHANGED
@@ -1,8 +1,8 @@
1
1
  import Color from "./../math/color.js";
2
2
  import WebGLRenderer from "./../video/webgl/webgl_renderer.js";
3
- import { renderer as globalRenderer, createCanvas } from "./../video/video.js";
3
+ import { renderer as globalRenderer } from "./../video/video.js";
4
4
  import { trimRight } from "./../utils/string.js";
5
- import * as pool from "./../system/pooling.js";
5
+ import pool from "./../system/pooling.js";
6
6
  import Renderable from "./../renderable/renderable.js";
7
7
  import { nextPowerOfTwo } from "./../math/math.js";
8
8
  import setContextStyle from "./textstyle.js";
@@ -23,7 +23,7 @@ var toPX = [12, 24, 0.75, 1];
23
23
  // return a valid 2d context for Text rendering/styling
24
24
  var getContext2d = function (renderer, text) {
25
25
  if (text.offScreenCanvas === true) {
26
- return text.context;
26
+ return text.canvasTexture.context;
27
27
  } else {
28
28
  return renderer.getFontContext();
29
29
  }
@@ -190,8 +190,7 @@ class Text extends Renderable {
190
190
 
191
191
  if (settings.offScreenCanvas === true) {
192
192
  this.offScreenCanvas = true;
193
- this.canvas = createCanvas(2, 2, true);
194
- this.context = this.canvas.getContext("2d");
193
+ this.canvasTexture = pool.pull("CanvasTexture", 2, 2, true);
195
194
  }
196
195
 
197
196
  // instance to text metrics functions
@@ -206,10 +205,9 @@ class Text extends Renderable {
206
205
  // free the canvas and potential corresponding texture when deactivated
207
206
  if (this.offScreenCanvas === true) {
208
207
  globalRenderer.currentCompositor.deleteTexture2D(globalRenderer.currentCompositor.getTexture2D(this.glTextureUnit));
209
- globalRenderer.cache.delete(this.canvas);
210
- this.canvas.width = this.canvas.height = 0;
211
- this.context = undefined;
212
- this.canvas = undefined;
208
+ globalRenderer.cache.delete(this.canvasTexture.canvas);
209
+ pool.push(this.canvasTexture);
210
+ this.canvasTexture = undefined;
213
211
  this.glTextureUnit = undefined;
214
212
  }
215
213
  }
@@ -307,7 +305,7 @@ class Text extends Renderable {
307
305
 
308
306
  if (globalRenderer instanceof WebGLRenderer) {
309
307
  // invalidate the previous corresponding texture so that it can reuploaded once changed
310
- this.glTextureUnit = globalRenderer.cache.getUnit(globalRenderer.cache.get(this.canvas));
308
+ this.glTextureUnit = globalRenderer.cache.getUnit(globalRenderer.cache.get(this.canvasTexture.canvas));
311
309
  globalRenderer.currentCompositor.unbindTexture2D(null, this.glTextureUnit);
312
310
 
313
311
  if (globalRenderer.WebGLVersion === 1) {
@@ -318,14 +316,12 @@ class Text extends Renderable {
318
316
  }
319
317
 
320
318
  // resize the cache canvas if necessary
321
- if (this.canvas.width < width || this.canvas.height < height) {
322
- this.canvas.width = width;
323
- this.canvas.height = height;
324
- // resizing the canvas will automatically clear its content
325
- } else {
326
- this.context.clearRect(0, 0, this.canvas.width, this.canvas.height);
319
+ if (this.canvasTexture.width < width || this.canvasTexture.height < height) {
320
+ this.canvasTexture.resize(width, height);
327
321
  }
328
- this._drawFont(this.context, this._text, this.pos.x - this.metrics.x, this.pos.y - this.metrics.y, false);
322
+
323
+ this.canvasTexture.clear();
324
+ this._drawFont(this.canvasTexture.context, this._text, this.pos.x - this.metrics.x, this.pos.y - this.metrics.y, false);
329
325
  }
330
326
 
331
327
  this.isDirty = true;
@@ -389,7 +385,7 @@ class Text extends Renderable {
389
385
 
390
386
  // draw the text
391
387
  if (this.offScreenCanvas === true) {
392
- renderer.drawImage(this.canvas, x, y);
388
+ renderer.drawImage(this.canvasTexture.canvas, x, y);
393
389
  } else {
394
390
  renderer.drawFont(this._drawFont(renderer.getFontContext(), this._text, x, y, stroke));
395
391
  }
@@ -120,14 +120,14 @@ var utils = {
120
120
  // never cache if a url is passed as parameter
121
121
  var index = url.indexOf("#");
122
122
  if (index !== -1) {
123
- url = url.substr(index, url.length);
123
+ url = url.slice(index, url.length);
124
124
  } else {
125
125
  return hash;
126
126
  }
127
127
  }
128
128
 
129
129
  // parse the url
130
- url.substr(1).split("&").filter(function (value) {
130
+ url.slice(1).split("&").filter(function (value) {
131
131
  return (value !== "");
132
132
  }).forEach(function (value) {
133
133
  var kv = value.split("=");