melonjs 13.0.0 → 13.2.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.
@@ -465,10 +465,38 @@ class ObservableVector3d extends Vector3d {
465
465
  * @returns {ObservableVector3d} Reference to this object for method chaining
466
466
  */
467
467
  lerp(v, alpha) {
468
- this._x += ( v.x - this._x ) * alpha;
469
- this._y += ( v.y - this._y ) * alpha;
470
- this._z += ( v.z - this._z ) * alpha;
471
- return this;
468
+ return this._set(
469
+ this._x + ( v.x - this._x ) * alpha,
470
+ this._y + ( v.y - this._y ) * alpha,
471
+ this._z + ( v.z - this._z ) * alpha
472
+ );
473
+ }
474
+
475
+ /**
476
+ * interpolate the position of this vector on the x and y axis towards the given one while ensure that the distance never exceeds the given step.
477
+ * @name moveTowards
478
+ * @memberof ObservableVector3d
479
+ * @param {Vector2d|ObservableVector2d|Vector3d|ObservableVector3d} target
480
+ * @param {number} step the maximum step per iteration (Negative values will push the vector away from the target)
481
+ * @returns {ObservableVector3d} Reference to this object for method chaining
482
+ */
483
+ moveTowards(target, step) {
484
+ var angle = Math.atan2(target.y - this._y, target.x - this._x);
485
+
486
+ var dx = this._x - target.x;
487
+ var dy = this._y - target.y;
488
+
489
+ var distance = Math.sqrt(dx * dx + dy * dy);
490
+
491
+ if (distance === 0 || (step >= 0 && distance <= step * step)) {
492
+ return target;
493
+ }
494
+
495
+ return this._set(
496
+ this._x + Math.cos(angle) * step,
497
+ this._y + Math.sin(angle) * step,
498
+ this._z
499
+ );
472
500
  }
473
501
 
474
502
  /**
@@ -426,6 +426,29 @@ class Vector2d {
426
426
  return this;
427
427
  }
428
428
 
429
+ /**
430
+ * interpolate the position of this vector towards the given one by the given maximum step.
431
+ * @name moveTowards
432
+ * @memberof Vector2d
433
+ * @param {Vector2d} target
434
+ * @param {number} step the maximum step per iteration (Negative values will push the vector away from the target)
435
+ * @returns {Vector2d} Reference to this object for method chaining
436
+ */
437
+ moveTowards(target, step) {
438
+ var angle = Math.atan2(target.y - this.y, target.x - this.x);
439
+
440
+ var distance = this.distance(target);
441
+
442
+ if (distance === 0 || (step >= 0 && distance <= step * step)) {
443
+ return target;
444
+ }
445
+
446
+ this.x += Math.cos(angle) * step;
447
+ this.y += Math.sin(angle) * step;
448
+
449
+ return this;
450
+ }
451
+
429
452
  /**
430
453
  * return the distance between this vector and the passed one
431
454
  * @name distance
@@ -460,6 +460,32 @@ class Vector3d {
460
460
  return this;
461
461
  }
462
462
 
463
+ /**
464
+ * interpolate the position of this vector on the x and y axis towards the given one by the given maximum step.
465
+ * @name moveTowards
466
+ * @memberof Vector3d
467
+ * @param {Vector2d|Vector3d} target
468
+ * @param {number} step the maximum step per iteration (Negative values will push the vector away from the target)
469
+ * @returns {Vector3d} Reference to this object for method chaining
470
+ */
471
+ moveTowards(target, step) {
472
+ var angle = Math.atan2(target.y - this.y, target.x - this.x);
473
+
474
+ var dx = this.x - target.x;
475
+ var dy = this.y - target.y;
476
+
477
+ var distance = Math.sqrt(dx * dx + dy * dy);
478
+
479
+ if (distance === 0 || (step >= 0 && distance <= step * step)) {
480
+ return target;
481
+ }
482
+
483
+ this.x += Math.cos(angle) * step;
484
+ this.y += Math.sin(angle) * step;
485
+
486
+ return this;
487
+ }
488
+
463
489
  /**
464
490
  * return the distance between this vector and the passed one
465
491
  * @name distance
@@ -7,6 +7,7 @@ import collision from "./collision.js";
7
7
  import * as arrayUtil from "./../utils/array.js";
8
8
  import timer from "./../system/timer.js";
9
9
  import { clamp } from "./../math/math.js";
10
+ import Point from "../geometries/point.js";
10
11
 
11
12
  /**
12
13
  * @classdesc
@@ -16,7 +17,7 @@ import { clamp } from "./../math/math.js";
16
17
  class Body {
17
18
  /**
18
19
  * @param {Renderable} ancestor the parent object this body is attached to
19
- * @param {Rect|Rect[]|Polygon|Polygon[]|Line|Line[]|Ellipse|Ellipse[]|Bounds|Bounds[]|object} [shapes] a initial shape, list of shapes, or JSON object defining the body
20
+ * @param {Rect|Rect[]|Polygon|Polygon[]|Line|Line[]|Ellipse|Ellipse[]|Point|Point[]|Bounds|Bounds[]|object} [shapes] a initial shape, list of shapes, or JSON object defining the body
20
21
  * @param {Function} [onBodyUpdate] callback for when the body is updated (e.g. add/remove shapes)
21
22
  */
22
23
  constructor(ancestor, shapes, onBodyUpdate) {
@@ -43,7 +44,7 @@ class Body {
43
44
  /**
44
45
  * The collision shapes of the body
45
46
  * @ignore
46
- * @type {Polygon[]|Line[]|Ellipse[]}
47
+ * @type {Polygon[]|Line[]|Ellipse[]|Point|Point[]}
47
48
  */
48
49
  this.shapes = [];
49
50
  }
@@ -235,7 +236,7 @@ class Body {
235
236
  /**
236
237
  * add a collision shape to this body <br>
237
238
  * (note: me.Rect objects will be converted to me.Polygon before being added)
238
- * @param {Rect|Polygon|Line|Ellipse|Bounds|object} shape a shape or JSON object
239
+ * @param {Rect|Polygon|Line|Ellipse|Point|Point[]|Bounds|object} shape a shape or JSON object
239
240
  * @returns {number} the shape array length
240
241
  * @example
241
242
  * // add a rectangle shape
@@ -270,6 +271,12 @@ class Body {
270
271
  // update the body bounds
271
272
  this.bounds.add(shape.points);
272
273
  this.bounds.translate(shape.pos);
274
+ } else if (shape instanceof Point) {
275
+ if (!this.shapes.includes(shape)) {
276
+ // see removeShape
277
+ this.shapes.push(shape);
278
+ }
279
+ this.bounds.addPoint(shape);
273
280
  } else {
274
281
  // JSON object
275
282
  this.fromJSON(shape);
@@ -251,17 +251,18 @@ class Bounds {
251
251
  * add the given point to the bounds definition.
252
252
  * @name addPoint
253
253
  * @memberof Bounds
254
- * @param {Vector2d} v
255
- * @param {Matrix2d} [m] an optional transform to apply to the given point
254
+ * @param {Vector2d|Point} point the point to be added to the bounds
255
+ * @param {Matrix2d} [m] an optional transform to apply to the given point (only if the given point is a vector)
256
256
  */
257
- addPoint(v, m) {
258
- if (typeof m !== "undefined") {
259
- v = m.apply(v);
257
+ addPoint(point, m) {
258
+ if ((typeof m !== "undefined") && (typeof point.rotate === "function")) {
259
+ // only Vectors object have a rotate function
260
+ point = m.apply(point);
260
261
  }
261
- this.min.x = Math.min(this.min.x, v.x);
262
- this.max.x = Math.max(this.max.x, v.x);
263
- this.min.y = Math.min(this.min.y, v.y);
264
- this.max.y = Math.max(this.max.y, v.y);
262
+ this.min.x = Math.min(this.min.x, point.x);
263
+ this.max.x = Math.max(this.max.x, point.x);
264
+ this.min.y = Math.min(this.min.y, point.y);
265
+ this.max.y = Math.max(this.max.y, point.y);
265
266
  }
266
267
 
267
268
  /**
@@ -2,8 +2,8 @@
2
2
  import "core-js/proposals/global-this";
3
3
 
4
4
  // es10 string trim functions
5
- import "core-js/es/string/trim-left";
6
- import "core-js/es/string/trim-right";
5
+ import "core-js/es/string/trim-start";
6
+ import "core-js/es/string/trim-end";
7
7
 
8
8
  // "built-in" polyfills
9
9
  import "./console.js";
@@ -47,7 +47,7 @@ class NineSliceSprite extends Sprite {
47
47
  this.width = Math.floor(settings.width);
48
48
  this.height = Math.floor(settings.height);
49
49
 
50
- // nine slice sprite specific local variables
50
+ // nine slice sprite specific internal variables
51
51
  this.nss_width = this.width;
52
52
  this.nss_height = this.height;
53
53
 
@@ -55,6 +55,32 @@ class NineSliceSprite extends Sprite {
55
55
  this.insety = settings.insety;
56
56
  }
57
57
 
58
+ /**
59
+ * width of the NineSliceSprite
60
+ * @public
61
+ * @type {number}
62
+ * @name width
63
+ */
64
+ get width() {
65
+ return super.width;
66
+ }
67
+ set width(value) {
68
+ super.width = this.nss_width = value;
69
+ }
70
+
71
+ /**
72
+ * height of the NineSliceSprite
73
+ * @public
74
+ * @type {number}
75
+ * @name height
76
+ */
77
+ get height() {
78
+ return super.height;
79
+ }
80
+ set height(value) {
81
+ super.height = this.nss_height = value;
82
+ }
83
+
58
84
  /**
59
85
  * @ignore
60
86
  */