melonjs 10.7.1 → 10.8.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 (62) hide show
  1. package/dist/melonjs.js +1131 -652
  2. package/dist/melonjs.min.js +4 -4
  3. package/dist/melonjs.module.d.ts +1038 -198
  4. package/dist/melonjs.module.js +1234 -763
  5. package/package.json +7 -7
  6. package/src/camera/camera2d.js +1 -1
  7. package/src/entity/entity.js +6 -7
  8. package/src/geometries/ellipse.js +10 -11
  9. package/src/geometries/line.js +3 -3
  10. package/src/geometries/path2d.js +319 -0
  11. package/src/geometries/poly.js +11 -11
  12. package/src/geometries/rectangle.js +15 -15
  13. package/src/geometries/roundrect.js +67 -0
  14. package/src/index.js +5 -1
  15. package/src/input/pointerevent.js +1 -1
  16. package/src/lang/deprecated.js +1 -1
  17. package/src/level/tiled/TMXLayer.js +1 -1
  18. package/src/level/tiled/TMXObject.js +9 -12
  19. package/src/level/tiled/TMXTileMap.js +23 -4
  20. package/src/level/tiled/renderer/TMXHexagonalRenderer.js +1 -1
  21. package/src/level/tiled/renderer/TMXIsometricRenderer.js +1 -1
  22. package/src/level/tiled/renderer/TMXOrthogonalRenderer.js +1 -1
  23. package/src/level/tiled/renderer/TMXRenderer.js +1 -1
  24. package/src/level/tiled/renderer/TMXStaggeredRenderer.js +1 -1
  25. package/src/loader/loader.js +4 -4
  26. package/src/loader/loadingscreen.js +1 -1
  27. package/src/math/color.js +1 -1
  28. package/src/math/matrix2.js +1 -1
  29. package/src/math/matrix3.js +1 -1
  30. package/src/math/observable_vector2.js +1 -1
  31. package/src/math/observable_vector3.js +1 -1
  32. package/src/math/vector2.js +1 -1
  33. package/src/math/vector3.js +1 -1
  34. package/src/particles/emitter.js +23 -14
  35. package/src/particles/particle.js +3 -2
  36. package/src/physics/body.js +67 -51
  37. package/src/physics/bounds.js +8 -9
  38. package/src/physics/world.js +1 -1
  39. package/src/renderable/collectable.js +9 -2
  40. package/src/renderable/colorlayer.js +1 -1
  41. package/src/renderable/container.js +1 -1
  42. package/src/renderable/imagelayer.js +1 -1
  43. package/src/renderable/renderable.js +1 -1
  44. package/src/renderable/sprite.js +2 -3
  45. package/src/renderable/trigger.js +10 -4
  46. package/src/state/stage.js +1 -1
  47. package/src/state/state.js +1 -1
  48. package/src/system/device.js +10 -8
  49. package/src/system/pooling.js +156 -149
  50. package/src/text/bitmaptext.js +1 -1
  51. package/src/text/text.js +1 -1
  52. package/src/video/canvas/canvas_renderer.js +89 -34
  53. package/src/video/renderer.js +26 -14
  54. package/src/video/texture.js +1 -1
  55. package/src/video/webgl/glshader.js +29 -193
  56. package/src/video/webgl/utils/attributes.js +16 -0
  57. package/src/video/webgl/utils/precision.js +11 -0
  58. package/src/video/webgl/utils/program.js +58 -0
  59. package/src/video/webgl/utils/string.js +16 -0
  60. package/src/video/webgl/utils/uniforms.js +87 -0
  61. package/src/video/webgl/webgl_compositor.js +1 -14
  62. package/src/video/webgl/webgl_renderer.js +123 -181
@@ -0,0 +1,67 @@
1
+ import Rect from "./rectangle.js";
2
+
3
+ // https://developer.chrome.com/blog/canvas2d/#round-rect
4
+
5
+ /**
6
+ * @classdesc
7
+ * a rectangle object with rounded corners
8
+ * @augments Rect
9
+ */
10
+ class RoundRect extends Rect {
11
+ /**
12
+ * @param {number} x position of the rounded rectangle
13
+ * @param {number} y position of the rounded rectangle
14
+ * @param {number} width the rectangle width
15
+ * @param {number} height the rectangle height
16
+ * @param {number} [radius=20] the radius of the rounded corner
17
+ */
18
+ constructor(x, y, width, height, radius = 20) {
19
+ // parent constructor
20
+ super(x, y, width, height);
21
+
22
+ // set the corner radius
23
+ this.radius = radius;
24
+ }
25
+
26
+ /** @ignore */
27
+ onResetEvent(x, y, w, h, radius) {
28
+ super.setShape(x, y, w, h);
29
+ this.radius = radius;
30
+ }
31
+
32
+
33
+ /**
34
+ * the radius of the rounded corner
35
+ * @public
36
+ * @type {number}
37
+ * @default 20
38
+ * @name radius
39
+ * @memberof RoundRect.prototype
40
+ */
41
+ get radius() {
42
+ return this._radius;
43
+ }
44
+ set radius(value) {
45
+ // verify the rectangle is at least as wide and tall as the rounded corners.
46
+ if (this.width < 2 * value) {
47
+ value = this.width / 2;
48
+ }
49
+ if (this.height < 2 * value) {
50
+ value = this.height / 2;
51
+ }
52
+ this._radius = value;
53
+ }
54
+
55
+ /**
56
+ * clone this RoundRect
57
+ * @name clone
58
+ * @memberof RoundRect.prototype
59
+ * @function
60
+ * @returns {RoundRect} new RoundRect
61
+ */
62
+ clone() {
63
+ return new RoundRect(this.pos.x, this.pos.y, this.width, this.height, this.radius);
64
+ }
65
+ };
66
+
67
+ export default RoundRect;
package/src/index.js CHANGED
@@ -15,7 +15,7 @@ import { plugin, plugins } from "./plugin/plugin.js";
15
15
  import * as video from "./video/video.js";
16
16
  import save from "./system/save.js";
17
17
  import timer from "./system/timer.js";
18
- import * as pool from "./system/pooling.js";
18
+ import pool from "./system/pooling.js";
19
19
  import state from "./state/state.js";
20
20
  import level from "./level/level.js";
21
21
 
@@ -31,6 +31,7 @@ import Polygon from "./geometries/poly.js";
31
31
  import Line from "./geometries/line.js";
32
32
  import Ellipse from "./geometries/ellipse.js";
33
33
  import Rect from "./geometries/rectangle.js";
34
+ import RoundRect from "./geometries/roundrect.js";
34
35
  import QuadTree from "./physics/quadtree.js";
35
36
  import Body from "./physics/body.js";
36
37
  import Bounds from "./physics/bounds.js";
@@ -117,6 +118,7 @@ export {
117
118
  Line,
118
119
  Ellipse,
119
120
  Rect,
121
+ RoundRect,
120
122
  Tween,
121
123
  QuadTree,
122
124
  GLShader,
@@ -218,6 +220,7 @@ export function boot() {
218
220
  pool.register("me.Matrix2d", Matrix2d, true);
219
221
  pool.register("me.Matrix3d", Matrix3d, true);
220
222
  pool.register("me.Rect", Rect, true);
223
+ pool.register("me.RoundRect", RoundRect, true);
221
224
  pool.register("me.Polygon", Polygon, true);
222
225
  pool.register("me.Line", Line, true);
223
226
  pool.register("me.Ellipse", Ellipse, true);
@@ -245,6 +248,7 @@ export function boot() {
245
248
  pool.register("Matrix2d", Matrix2d, true);
246
249
  pool.register("Matrix3d", Matrix3d, true);
247
250
  pool.register("Rect", Rect, true);
251
+ pool.register("RoundRect", RoundRect, true);
248
252
  pool.register("Polygon", Polygon, true);
249
253
  pool.register("Line", Line, true);
250
254
  pool.register("Ellipse", Ellipse, true);
@@ -5,7 +5,7 @@ import { throttle } from "./../utils/function.js";
5
5
  import { remove } from "./../utils/array.js";
6
6
  import * as event from "./../system/event.js";
7
7
  import timer from "./../system/timer.js";
8
- import * as pool from "./../system/pooling.js";
8
+ import pool from "./../system/pooling.js";
9
9
  import device from "./../system/device.js";
10
10
  import Pointer from "./pointer.js";
11
11
  import Rect from "./../geometries/rectangle.js";
@@ -74,7 +74,7 @@ device.turnOffPointerLock = function () {
74
74
  /**
75
75
  * @public
76
76
  * @name Texture
77
- * @memberof Renderer
77
+ * @memberof Renderer#
78
78
  * @deprecated since 10.4.0
79
79
  * @see TextureAtlas
80
80
  */
@@ -1,5 +1,5 @@
1
1
  import { createCanvas } from "./../../video/video.js";
2
- import * as pool from "./../../system/pooling.js";
2
+ import pool from "./../../system/pooling.js";
3
3
  import * as TMXUtils from "./TMXUtils.js";
4
4
  import Tile from "./TMXTile.js";
5
5
  import Renderable from "./../../renderable/renderable.js";
@@ -1,9 +1,6 @@
1
- import Vector2d from "./../../math/vector2.js";
1
+ import pool from "./../../system/pooling.js";
2
2
  import { applyTMXProperties } from "./TMXUtils.js";
3
3
  import Tile from "./TMXTile.js";
4
- import Ellipse from "./../../geometries/ellipse.js";
5
- import Polygon from "./../../geometries/poly.js";
6
- import Line from "./../../geometries/line.js";
7
4
  import { degToRad } from "./../../math/math.js";
8
5
 
9
6
  /**
@@ -260,7 +257,7 @@ export default class TMXObject {
260
257
  // add an ellipse shape
261
258
  if (this.isEllipse === true) {
262
259
  // ellipse coordinates are the center position, so set default to the corresonding radius
263
- shapes.push((new Ellipse(
260
+ shapes.push((pool.pull("Ellipse",
264
261
  this.width / 2,
265
262
  this.height / 2,
266
263
  this.width,
@@ -270,7 +267,7 @@ export default class TMXObject {
270
267
 
271
268
  // add a polygon
272
269
  if (this.isPolygon === true) {
273
- var _polygon = new Polygon(0, 0, this.points);
270
+ var _polygon = pool.pull("Polygon", 0, 0, this.points);
274
271
  // make sure it's a convex polygon
275
272
  if (_polygon.isConvex() === false ) {
276
273
  throw new Error("collision polygones in Tiled should be defined as Convex");
@@ -286,22 +283,22 @@ export default class TMXObject {
286
283
  for (i = 0; i < segments; i++) {
287
284
  // clone the value before, as [i + 1]
288
285
  // is reused later by the next segment
289
- p1 = new Vector2d(p[i].x, p[i].y);
290
- p2 = new Vector2d(p[i + 1].x, p[i + 1].y);
286
+ p1 = pool.pull("Vector2d", p[i].x, p[i].y);
287
+ p2 = pool.pull("Vector2d", p[i + 1].x, p[i + 1].y);
291
288
  if (this.rotation !== 0) {
292
289
  p1 = p1.rotate(this.rotation);
293
290
  p2 = p2.rotate(this.rotation);
294
291
  }
295
- shapes.push(new Line(0, 0, [ p1, p2 ]));
292
+ shapes.push(pool.pull("Line", 0, 0, [ p1, p2 ]));
296
293
  }
297
294
  }
298
295
 
299
296
  // it's a rectangle, returns a polygon object anyway
300
297
  else {
301
- shapes.push((new Polygon(
298
+ shapes.push((pool.pull("Polygon",
302
299
  0, 0, [
303
- new Vector2d(), new Vector2d(this.width, 0),
304
- new Vector2d(this.width, this.height), new Vector2d(0, this.height)
300
+ pool.pull("Vector2d"), pool.pull("Vector2d", this.width, 0),
301
+ pool.pull("Vector2d", this.width, this.height), pool.pull("Vector2d", 0, this.height)
305
302
  ]
306
303
  )).rotate(this.rotation));
307
304
  }
@@ -1,4 +1,4 @@
1
- import * as pool from "./../../system/pooling.js";
1
+ import pool from "./../../system/pooling.js";
2
2
  import * as event from "./../../system/event.js";
3
3
  import { viewport } from "./../../game.js";
4
4
  import collision from "./../../physics/collision.js";
@@ -14,7 +14,6 @@ import TMXLayer from "./TMXLayer.js";
14
14
  import { applyTMXProperties } from "./TMXUtils.js";
15
15
  import Renderable from "./../../renderable/renderable.js";
16
16
  import Container from "./../../renderable/container.js";
17
- import Rect from "./../../geometries/rectangle.js";
18
17
 
19
18
  // constant to identify the collision object layer
20
19
  var COLLISION_GROUP = "collision";
@@ -474,6 +473,8 @@ class TMXTileMap {
474
473
  var settings = group.objects[o];
475
474
  // reference to the instantiated object
476
475
  var obj;
476
+ // a reference to the default shape
477
+ var shape;
477
478
 
478
479
  // Tiled uses 0,0 by default
479
480
  if (typeof (settings.anchorPoint) === "undefined") {
@@ -506,9 +507,18 @@ class TMXTileMap {
506
507
  // set the obj z order
507
508
  obj.pos.z = settings.z;
508
509
  } else if (typeof settings.tile === "object") {
510
+ // create a default shape if none is specified
511
+ shape = settings.shapes;
512
+ if (typeof shape === "undefined") {
513
+ shape = pool.pull("Polygon", 0, 0, [
514
+ pool.pull("Vector2d", 0, 0),
515
+ pool.pull("Vector2d", this.width, 0),
516
+ pool.pull("Vector2d", this.width, this.height)
517
+ ]);
518
+ }
509
519
  // check if a me.Tile object is embedded
510
520
  obj = settings.tile.getRenderable(settings);
511
- obj.body = new Body(obj, settings.shapes || new Rect(0, 0, this.width, this.height));
521
+ obj.body = new Body(obj, shape);
512
522
  obj.body.setStatic(true);
513
523
  // set the obj z order
514
524
  obj.pos.setMuted(settings.x, settings.y, settings.z);
@@ -527,11 +537,20 @@ class TMXTileMap {
527
537
  settings.x, settings.y,
528
538
  settings.width, settings.height
529
539
  );
540
+ // create a default shape if none is specified
541
+ shape = settings.shapes;
542
+ if (typeof shape === "undefined") {
543
+ shape = pool.pull("Polygon", 0, 0, [
544
+ pool.pull("Vector2d", 0, 0),
545
+ pool.pull("Vector2d", this.width, 0),
546
+ pool.pull("Vector2d", this.width, this.height)
547
+ ]);
548
+ }
530
549
  obj.anchorPoint.set(0, 0);
531
550
  obj.name = settings.name;
532
551
  obj.type = settings.type;
533
552
  obj.id = settings.id;
534
- obj.body = new Body(obj, settings.shapes || new Rect(0, 0, obj.width, obj.height));
553
+ obj.body = new Body(obj, shape);
535
554
  obj.body.setStatic(true);
536
555
  obj.resize(obj.body.getBounds().width, obj.body.getBounds().height);
537
556
  }
@@ -1,5 +1,5 @@
1
1
  import Vector2d from "./../../../math/vector2.js";
2
- import * as pool from "./../../../system/pooling.js";
2
+ import pool from "./../../../system/pooling.js";
3
3
  import TMXRenderer from "./TMXRenderer.js";
4
4
  import TMXLayer from "./../TMXLayer.js";
5
5
 
@@ -1,5 +1,5 @@
1
1
  import Vector2d from "./../../../math/vector2.js";
2
- import * as pool from "./../../../system/pooling.js";
2
+ import pool from "./../../../system/pooling.js";
3
3
  import TMXRenderer from "./TMXRenderer.js";
4
4
  import TMXLayer from "./../TMXLayer.js";
5
5
 
@@ -1,5 +1,5 @@
1
1
  import Vector2d from "./../../../math/vector2.js";
2
- import * as pool from "./../../../system/pooling.js";
2
+ import pool from "./../../../system/pooling.js";
3
3
  import TMXRenderer from "./TMXRenderer.js";
4
4
 
5
5
  /**
@@ -1,6 +1,6 @@
1
1
  /* eslint-disable no-unused-vars */
2
2
 
3
- import * as pool from "./../../../system/pooling.js";
3
+ import pool from "./../../../system/pooling.js";
4
4
  import TMXLayer from "./../TMXLayer.js";
5
5
  import Bounds from "./../../../physics/Bounds.js";
6
6
 
@@ -1,5 +1,5 @@
1
1
  import Vector2d from "./../../../math/vector2.js";
2
- import * as pool from "./../../../system/pooling.js";
2
+ import pool from "./../../../system/pooling.js";
3
3
  import TMXHexagonalRenderer from "./TMXHexagonalRenderer.js";
4
4
  import { degToRad } from "./../../../math/math.js";
5
5
 
@@ -46,7 +46,7 @@ function checkLoadStatus(onload) {
46
46
  }
47
47
  }
48
48
  else {
49
- timerId = setTimeout(function() {
49
+ timerId = setTimeout(() => {
50
50
  checkLoadStatus(onload);
51
51
  }, 100);
52
52
  }
@@ -85,7 +85,7 @@ function preloadImage(img, onload, onerror) {
85
85
  function preloadFontFace(data, onload, onerror) {
86
86
  var font = new FontFace(data.name, data.src);
87
87
  // loading promise
88
- font.load().then(function() {
88
+ font.load().then(() => {
89
89
  // apply the font after the font has finished downloading
90
90
  document.fonts.add(font);
91
91
  document.body.style.fontFamily = data.name;
@@ -278,12 +278,12 @@ function preloadJavascript(data, onload, onerror) {
278
278
  }
279
279
  script.defer = true;
280
280
 
281
- script.onload = function() {
281
+ script.onload = () => {
282
282
  // callback
283
283
  onload();
284
284
  };
285
285
 
286
- script.onerror = function() {
286
+ script.onerror = () => {
287
287
  // callback
288
288
  onerror(data.name);
289
289
  };
@@ -2,7 +2,7 @@ import { world, viewport } from "./../game.js";
2
2
  import { createCanvas, renderer } from "./../video/video.js";
3
3
  import * as event from "./../system/event.js";
4
4
  import {nextPowerOfTwo} from "./../math/math.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 Stage from "./../state/stage.js";
8
8
 
package/src/math/color.js CHANGED
@@ -1,5 +1,5 @@
1
1
  import { clamp, random } from "./math.js";
2
- import * as pool from "./../system/pooling.js";
2
+ import pool from "./../system/pooling.js";
3
3
 
4
4
  // convert a give color component to it hexadecimal value
5
5
  var toHex = function (component) {
@@ -1,4 +1,4 @@
1
- import * as pool from "./../system/pooling.js";
1
+ import pool from "./../system/pooling.js";
2
2
  import Matrix3d from "./matrix3.js";
3
3
 
4
4
  /**
@@ -1,4 +1,4 @@
1
- import * as pool from "./../system/pooling.js";
1
+ import pool from "./../system/pooling.js";
2
2
  import { EPSILON } from "./math.js";
3
3
 
4
4
  /**
@@ -1,4 +1,4 @@
1
- import * as pool from "./../system/pooling.js";
1
+ import pool from "./../system/pooling.js";
2
2
  import Vector2d from "./vector2.js";
3
3
  import {clamp} from "./math.js";
4
4
 
@@ -1,6 +1,6 @@
1
1
  import Vector3d from "./vector3.js";
2
2
  import {clamp} from "./math.js";
3
- import * as pool from "./../system/pooling.js";
3
+ import pool from "./../system/pooling.js";
4
4
 
5
5
  /**
6
6
  * @classdesc
@@ -1,5 +1,5 @@
1
1
  import {clamp} from "./math.js";
2
- import * as pool from "./../system/pooling.js";
2
+ import pool from "./../system/pooling.js";
3
3
 
4
4
  /**
5
5
  * @classdesc
@@ -1,5 +1,5 @@
1
1
  import {clamp} from "./math.js";
2
- import * as pool from "./../system/pooling.js";
2
+ import pool from "./../system/pooling.js";
3
3
 
4
4
  /**
5
5
  * @classdesc
@@ -1,5 +1,5 @@
1
1
  import { createCanvas } from "./../video/video.js";
2
- import * as pool from "./../system/pooling.js";
2
+ import pool from "./../system/pooling.js";
3
3
  import ParticleEmitterSettings from "./settings.js";
4
4
  import { randomFloat } from "./../math/math.js";
5
5
  import Container from "./../renderable/container.js";
@@ -32,14 +32,17 @@ class ParticleEmitter extends Container {
32
32
  * @param {number} y y position of the particle emitter
33
33
  * @param {ParticleEmitterSettings} [settings=ParticleEmitterSettings] the settings for the particle emitter.
34
34
  * @example
35
- * // Create a basic emitter at position 100, 100
36
- * var emitter = new ParticleEmitter(100, 100);
37
- *
38
- * // Adjust the emitter properties
39
- * emitter.totalParticles = 200;
40
- * emitter.minLife = 1000;
41
- * emitter.maxLife = 3000;
42
- * emitter.z = 10;
35
+ * // Create a particle emitter at position 100, 100
36
+ * var emitter = new ParticleEmitter(100, 100, {
37
+ * width: 16,
38
+ * height : 16,
39
+ * tint: "#f00",
40
+ * totalParticles: 32,
41
+ * angle: 0,
42
+ * angleVariation: 6.283185307179586,
43
+ * maxLife: 5,
44
+ * speed: 3
45
+ * });
43
46
  *
44
47
  * // Add the emitter to the game world
45
48
  * me.game.world.addChild(emitter);
@@ -62,6 +65,15 @@ class ParticleEmitter extends Container {
62
65
  settings.height | 1
63
66
  );
64
67
 
68
+ /**
69
+ * the current (active) emitter settings
70
+ * @public
71
+ * @type {ParticleEmitterSettings}
72
+ * @name settings
73
+ * @memberof ParticleEmitter
74
+ */
75
+ this.settings = {};
76
+
65
77
  // center the emitter around the given coordinates
66
78
  this.centerOn(x, y);
67
79
 
@@ -92,9 +104,6 @@ class ParticleEmitter extends Container {
92
104
  // count the updates
93
105
  this._updateCount = 0;
94
106
 
95
- // the emitter settings
96
- this.settings = {};
97
-
98
107
  // internally store how much time was skipped when frames are skipped
99
108
  this._dt = 0;
100
109
 
@@ -106,7 +115,7 @@ class ParticleEmitter extends Container {
106
115
 
107
116
  /**
108
117
  * Reset the emitter with particle emitter settings.
109
- * @param {object} settings [optional] object with emitter settings. See {@link ParticleEmitterSettings}
118
+ * @param {ParticleEmitterSettings} settings [optional] object with emitter settings. See {@link ParticleEmitterSettings}
110
119
  */
111
120
  reset(settings = {}) {
112
121
  Object.assign(this.settings, ParticleEmitterSettings, settings);
@@ -140,7 +149,7 @@ class ParticleEmitter extends Container {
140
149
  // Add count particles in the game world
141
150
  /** @ignore */
142
151
  addParticles(count) {
143
- for (var i = 0; i < ~~count; i++) {
152
+ for (var i = 0; i < count; i++) {
144
153
  // Add particle to the container
145
154
  this.addChild(pool.pull("Particle", this), this.pos.z);
146
155
  }
@@ -1,4 +1,4 @@
1
- import Vector2d from "./../math/vector2.js";
1
+ import pool from "./../system/pooling.js";
2
2
  import timer from "./../system/timer.js";
3
3
  import { randomFloat, clamp } from "./../math/math.js";
4
4
  import Renderable from "./../renderable/renderable.js";
@@ -36,9 +36,10 @@ class Particle extends Renderable {
36
36
  emitter.settings.image.width,
37
37
  emitter.settings.image.height
38
38
  );
39
+ this.currentTransform.identity();
39
40
  } else {
40
41
  // particle velocity
41
- this.vel = new Vector2d();
42
+ this.vel = pool.pull("Vector2d");
42
43
  }
43
44
 
44
45
  this.image = emitter.settings.image;
@@ -1,8 +1,8 @@
1
- import Vector2d from "./../math/vector2.js";
2
1
  import Rect from "./../geometries/rectangle.js";
3
2
  import Ellipse from "./../geometries/ellipse.js";
4
3
  import Polygon from "./../geometries/poly.js";
5
4
  import Bounds from "./bounds.js";
5
+ import pool from "./../system/pooling.js";
6
6
  import collision from "./collision.js";
7
7
  import * as arrayUtil from "./../utils/array.js";
8
8
  import timer from "./../system/timer.js";
@@ -37,7 +37,7 @@ class Body {
37
37
  * @public
38
38
  * @type {Bounds}
39
39
  */
40
- this.bounds = new Bounds();
40
+ this.bounds = pool.pull("Bounds");
41
41
  }
42
42
 
43
43
  if (typeof this.shapes === "undefined") {
@@ -71,55 +71,54 @@ class Body {
71
71
  */
72
72
  this.collisionType = collision.types.ENEMY_OBJECT;
73
73
 
74
- /**
75
- * body velocity
76
- * @public
77
- * @type {Vector2d}
78
- * @default <0,0>
79
- */
80
74
  if (typeof this.vel === "undefined") {
81
- this.vel = new Vector2d();
75
+ /**
76
+ * body velocity
77
+ * @public
78
+ * @type {Vector2d}
79
+ * @default <0,0>
80
+ */
81
+ this.vel = pool.pull("Vector2d");
82
82
  }
83
83
  this.vel.set(0, 0);
84
84
 
85
- /**
86
- * body force or acceleration (automatically) applied to the body.
87
- * when defining a force, user should also define a max velocity
88
- * @public
89
- * @type {Vector2d}
90
- * @default <0,0>
91
- * @see Body.setMaxVelocity
92
- * @example
93
- * // define a default maximum acceleration, initial force and friction
94
- * this.body.force.set(0, 0);
95
- * this.body.friction.set(0.4, 0);
96
- * this.body.setMaxVelocity(3, 15);
97
- *
98
- * // apply a postive or negative force when pressing left of right key
99
- * update(dt) {
100
- * if (me.input.isKeyPressed("left")) {
101
- * this.body.force.x = -this.body.maxVel.x;
102
- * } else if (me.input.isKeyPressed("right")) {
103
- * this.body.force.x = this.body.maxVel.x;
104
- * } else {
105
- * this.body.force.x = 0;
106
- * }
107
- * }
108
- */
109
85
  if (typeof this.force === "undefined") {
110
- this.force = new Vector2d();
86
+ /**
87
+ * body force or acceleration (automatically) applied to the body.
88
+ * when defining a force, user should also define a max velocity
89
+ * @public
90
+ * @type {Vector2d}
91
+ * @default <0,0>
92
+ * @see Body.setMaxVelocity
93
+ * @example
94
+ * // define a default maximum acceleration, initial force and friction
95
+ * this.body.force.set(0, 0);
96
+ * this.body.friction.set(0.4, 0);
97
+ * this.body.setMaxVelocity(3, 15);
98
+ *
99
+ * // apply a postive or negative force when pressing left of right key
100
+ * update(dt) {
101
+ * if (me.input.isKeyPressed("left")) {
102
+ * this.body.force.x = -this.body.maxVel.x;
103
+ * } else if (me.input.isKeyPressed("right")) {
104
+ * this.body.force.x = this.body.maxVel.x;
105
+ * } else {
106
+ * this.body.force.x = 0;
107
+ * }
108
+ * }
109
+ */
110
+ this.force = pool.pull("Vector2d");
111
111
  }
112
112
  this.force.set(0, 0);
113
113
 
114
-
115
- /**
116
- * body friction
117
- * @public
118
- * @type {Vector2d}
119
- * @default <0,0>
120
- */
121
114
  if (typeof this.friction === "undefined") {
122
- this.friction = new Vector2d();
115
+ /**
116
+ * body friction
117
+ * @public
118
+ * @type {Vector2d}
119
+ * @default <0,0>
120
+ */
121
+ this.friction = pool.pull("Vector2d");
123
122
  }
124
123
  this.friction.set(0, 0);
125
124
 
@@ -140,14 +139,14 @@ class Body {
140
139
  */
141
140
  this.mass = 1;
142
141
 
143
- /**
144
- * max velocity (to limit body velocity)
145
- * @public
146
- * @type {Vector2d}
147
- * @default <490,490>
148
- */
149
142
  if (typeof this.maxVel === "undefined") {
150
- this.maxVel = new Vector2d();
143
+ /**
144
+ * max velocity (to limit body velocity)
145
+ * @public
146
+ * @type {Vector2d}
147
+ * @default <490,490>
148
+ */
149
+ this.maxVel = pool.pull("Vector2d");
151
150
  }
152
151
  // cap by default to half the default gravity force
153
152
  this.maxVel.set(490, 490);
@@ -297,7 +296,7 @@ class Body {
297
296
  polygon.setShape(0, 0, vertices);
298
297
  } else {
299
298
  // this will replace any other non polygon shape type if defined
300
- this.shapes[index] = new Polygon(0, 0, vertices);
299
+ this.shapes[index] = pool.pull("Polygon", 0, 0, vertices);
301
300
  }
302
301
 
303
302
  // update the body bounds to take in account the new vertices
@@ -686,11 +685,28 @@ class Body {
686
685
  * @ignore
687
686
  */
688
687
  destroy() {
688
+ // push back instance into object pool
689
+ pool.push(this.bounds);
690
+ pool.push(this.vel);
691
+ pool.push(this.force);
692
+ pool.push(this.friction);
693
+ pool.push(this.maxVel);
694
+ this.shapes.forEach((shape) => {
695
+ pool.push(shape);
696
+ });
697
+
698
+ // set to undefined
689
699
  this.onBodyUpdate = undefined;
690
700
  this.ancestor = undefined;
691
701
  this.bounds = undefined;
692
- this.setStatic(false);
702
+ this.vel = undefined;
703
+ this.force = undefined;
704
+ this.friction = undefined;
705
+ this.maxVel = undefined;
693
706
  this.shapes.length = 0;
707
+
708
+ // reset some variable to default
709
+ this.setStatic(false);
694
710
  }
695
711
  };
696
712