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.
- package/LICENSE.md +1 -1
- package/README.md +29 -23
- package/dist/melonjs.js +2220 -1070
- package/dist/melonjs.min.js +4 -4
- package/dist/melonjs.module.d.ts +1395 -485
- package/dist/melonjs.module.js +2244 -1131
- package/package.json +17 -14
- package/src/camera/camera2d.js +1 -1
- package/src/entity/entity.js +6 -7
- package/src/game.js +2 -2
- package/src/geometries/ellipse.js +20 -21
- package/src/geometries/line.js +7 -7
- package/src/geometries/path2d.js +319 -0
- package/src/geometries/poly.js +27 -27
- package/src/geometries/rectangle.js +19 -19
- package/src/geometries/roundrect.js +164 -0
- package/src/index.js +12 -2
- package/src/input/gamepad.js +2 -2
- package/src/input/pointerevent.js +1 -1
- package/src/lang/deprecated.js +8 -6
- package/src/level/tiled/TMXLayer.js +1 -1
- package/src/level/tiled/TMXObject.js +9 -12
- package/src/level/tiled/TMXTileMap.js +23 -4
- package/src/level/tiled/TMXUtils.js +1 -1
- package/src/level/tiled/renderer/TMXHexagonalRenderer.js +1 -1
- package/src/level/tiled/renderer/TMXIsometricRenderer.js +1 -1
- package/src/level/tiled/renderer/TMXOrthogonalRenderer.js +1 -1
- package/src/level/tiled/renderer/TMXRenderer.js +1 -1
- package/src/level/tiled/renderer/TMXStaggeredRenderer.js +1 -1
- package/src/loader/loader.js +4 -4
- package/src/loader/loadingscreen.js +17 -6
- package/src/math/color.js +6 -5
- package/src/math/matrix2.js +1 -1
- package/src/math/matrix3.js +1 -1
- package/src/math/observable_vector2.js +1 -1
- package/src/math/observable_vector3.js +1 -1
- package/src/math/vector2.js +1 -1
- package/src/math/vector3.js +1 -1
- package/src/particles/emitter.js +34 -26
- package/src/particles/particle.js +3 -2
- package/src/physics/body.js +67 -51
- package/src/physics/bounds.js +8 -9
- package/src/physics/world.js +1 -1
- package/src/polyfill/index.js +1 -0
- package/src/polyfill/roundrect.js +235 -0
- package/src/renderable/GUI.js +5 -5
- package/src/renderable/collectable.js +9 -2
- package/src/renderable/colorlayer.js +1 -1
- package/src/renderable/container.js +27 -27
- package/src/renderable/imagelayer.js +3 -3
- package/src/renderable/light2d.js +115 -0
- package/src/renderable/renderable.js +23 -22
- package/src/renderable/sprite.js +15 -16
- package/src/renderable/trigger.js +10 -4
- package/src/state/stage.js +73 -3
- package/src/state/state.js +1 -1
- package/src/system/device.js +10 -8
- package/src/system/pooling.js +156 -149
- package/src/text/bitmaptext.js +1 -1
- package/src/text/text.js +14 -18
- package/src/utils/utils.js +2 -2
- package/src/video/canvas/canvas_renderer.js +144 -81
- package/src/video/renderer.js +64 -37
- package/src/video/{texture.js → texture/atlas.js} +8 -8
- package/src/video/{texture_cache.js → texture/cache.js} +4 -4
- package/src/video/texture/canvas_texture.js +87 -0
- package/src/video/webgl/glshader.js +29 -193
- package/src/video/webgl/utils/attributes.js +16 -0
- package/src/video/webgl/utils/precision.js +11 -0
- package/src/video/webgl/utils/program.js +58 -0
- package/src/video/webgl/utils/string.js +16 -0
- package/src/video/webgl/utils/uniforms.js +87 -0
- package/src/video/webgl/webgl_compositor.js +1 -14
- package/src/video/webgl/webgl_renderer.js +191 -231
|
@@ -1,9 +1,6 @@
|
|
|
1
|
-
import
|
|
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((
|
|
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 =
|
|
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 =
|
|
290
|
-
p2 =
|
|
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(
|
|
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((
|
|
298
|
+
shapes.push((pool.pull("Polygon",
|
|
302
299
|
0, 0, [
|
|
303
|
-
|
|
304
|
-
|
|
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
|
|
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,
|
|
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,
|
|
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
|
}
|
|
@@ -48,7 +48,7 @@ function setTMXValue(name, type, value) {
|
|
|
48
48
|
match = value.split(/^eval:/i)[1];
|
|
49
49
|
try {
|
|
50
50
|
// eslint-disable-next-line
|
|
51
|
-
value =
|
|
51
|
+
value = Function("'use strict';return (" + match + ")")();
|
|
52
52
|
}
|
|
53
53
|
catch (e) {
|
|
54
54
|
throw new Error("Unable to evaluate: " + match);
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import Vector2d from "./../../../math/vector2.js";
|
|
2
|
-
import
|
|
2
|
+
import pool from "./../../../system/pooling.js";
|
|
3
3
|
import TMXHexagonalRenderer from "./TMXHexagonalRenderer.js";
|
|
4
4
|
import { degToRad } from "./../../../math/math.js";
|
|
5
5
|
|
package/src/loader/loader.js
CHANGED
|
@@ -46,7 +46,7 @@ function checkLoadStatus(onload) {
|
|
|
46
46
|
}
|
|
47
47
|
}
|
|
48
48
|
else {
|
|
49
|
-
timerId = setTimeout(
|
|
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(
|
|
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 =
|
|
281
|
+
script.onload = () => {
|
|
282
282
|
// callback
|
|
283
283
|
onload();
|
|
284
284
|
};
|
|
285
285
|
|
|
286
|
-
script.onerror =
|
|
286
|
+
script.onerror = () => {
|
|
287
287
|
// callback
|
|
288
288
|
onerror(data.name);
|
|
289
289
|
};
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import { world, viewport } from "./../game.js";
|
|
2
|
-
import {
|
|
2
|
+
import { renderer } from "./../video/video.js";
|
|
3
3
|
import * as event from "./../system/event.js";
|
|
4
4
|
import {nextPowerOfTwo} from "./../math/math.js";
|
|
5
|
-
import
|
|
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
|
|
|
@@ -69,13 +69,13 @@ class IconLogo extends Renderable {
|
|
|
69
69
|
constructor(x, y) {
|
|
70
70
|
super(x, y, 100, 85);
|
|
71
71
|
|
|
72
|
-
this.
|
|
72
|
+
this.iconTexture = pool.pull("CanvasTexture",
|
|
73
73
|
renderer.WebGLVersion > 1 ? this.width : nextPowerOfTwo(this.width),
|
|
74
74
|
renderer.WebGLVersion > 1 ? this.height : nextPowerOfTwo(this.height),
|
|
75
|
-
|
|
75
|
+
true
|
|
76
76
|
);
|
|
77
77
|
|
|
78
|
-
var context =
|
|
78
|
+
var context = this.iconTexture.context;
|
|
79
79
|
|
|
80
80
|
context.beginPath();
|
|
81
81
|
context.moveTo(0.7, 48.9);
|
|
@@ -110,7 +110,18 @@ class IconLogo extends Renderable {
|
|
|
110
110
|
* @ignore
|
|
111
111
|
*/
|
|
112
112
|
draw(renderer) {
|
|
113
|
-
renderer.drawImage(this.
|
|
113
|
+
renderer.drawImage(this.iconTexture.canvas, renderer.getWidth() / 2, this.pos.y);
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* Destroy function
|
|
118
|
+
* @ignore
|
|
119
|
+
*/
|
|
120
|
+
destroy() {
|
|
121
|
+
// call the parent destroy method
|
|
122
|
+
super.destroy(arguments);
|
|
123
|
+
pool.push(this.iconTexture);
|
|
124
|
+
this.iconTexture = undefined;
|
|
114
125
|
}
|
|
115
126
|
};
|
|
116
127
|
|
package/src/math/color.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { clamp, random } from "./math.js";
|
|
2
|
-
import
|
|
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) {
|
|
@@ -561,11 +561,11 @@ class Color {
|
|
|
561
561
|
* @function
|
|
562
562
|
* @returns {string}
|
|
563
563
|
*/
|
|
564
|
-
toHex8() {
|
|
564
|
+
toHex8(alpha = this.alpha) {
|
|
565
565
|
// TODO : Memoize this function by caching its result until any of
|
|
566
566
|
// the r,g,b,a values are changed
|
|
567
567
|
|
|
568
|
-
return "#" + toHex(this.r) + toHex(this.g) + toHex(this.b) + toHex(
|
|
568
|
+
return "#" + toHex(this.r) + toHex(this.g) + toHex(this.b) + toHex(alpha * 255);
|
|
569
569
|
}
|
|
570
570
|
|
|
571
571
|
/**
|
|
@@ -591,9 +591,10 @@ class Color {
|
|
|
591
591
|
* @name toRGBA
|
|
592
592
|
* @memberof Color
|
|
593
593
|
* @function
|
|
594
|
+
* @param {number} [alpha=1.0] alpha value [0.0 .. 1.0]
|
|
594
595
|
* @returns {string}
|
|
595
596
|
*/
|
|
596
|
-
toRGBA() {
|
|
597
|
+
toRGBA(alpha = this.alpha) {
|
|
597
598
|
// TODO : Memoize this function by caching its result until any of
|
|
598
599
|
// the r,g,b,a values are changed
|
|
599
600
|
|
|
@@ -601,7 +602,7 @@ class Color {
|
|
|
601
602
|
this.r + "," +
|
|
602
603
|
this.g + "," +
|
|
603
604
|
this.b + "," +
|
|
604
|
-
|
|
605
|
+
alpha +
|
|
605
606
|
")";
|
|
606
607
|
}
|
|
607
608
|
};
|
package/src/math/matrix2.js
CHANGED
package/src/math/matrix3.js
CHANGED
package/src/math/vector2.js
CHANGED
package/src/math/vector3.js
CHANGED
package/src/particles/emitter.js
CHANGED
|
@@ -1,23 +1,18 @@
|
|
|
1
|
-
import
|
|
2
|
-
import * as pool from "./../system/pooling.js";
|
|
1
|
+
import pool from "./../system/pooling.js";
|
|
3
2
|
import ParticleEmitterSettings from "./settings.js";
|
|
4
3
|
import { randomFloat } from "./../math/math.js";
|
|
5
4
|
import Container from "./../renderable/container.js";
|
|
6
5
|
|
|
7
6
|
|
|
8
|
-
// default texture used when no sprite is defined
|
|
9
|
-
let defaultParticleTexture;
|
|
10
|
-
|
|
11
7
|
/**
|
|
12
8
|
* @ignore
|
|
13
9
|
*/
|
|
14
10
|
function createDefaultParticleTexture(w = 8, h = 8) {
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
}
|
|
11
|
+
var defaultParticleTexture = pool.pull("CanvasTexture", w, h, true);
|
|
12
|
+
|
|
13
|
+
defaultParticleTexture.context.fillStyle = "#fff";
|
|
14
|
+
defaultParticleTexture.context.fillRect(0, 0, w, h);
|
|
15
|
+
|
|
21
16
|
return defaultParticleTexture;
|
|
22
17
|
};
|
|
23
18
|
|
|
@@ -32,14 +27,17 @@ class ParticleEmitter extends Container {
|
|
|
32
27
|
* @param {number} y y position of the particle emitter
|
|
33
28
|
* @param {ParticleEmitterSettings} [settings=ParticleEmitterSettings] the settings for the particle emitter.
|
|
34
29
|
* @example
|
|
35
|
-
* // Create a
|
|
36
|
-
* var emitter = new ParticleEmitter(100, 100
|
|
37
|
-
*
|
|
38
|
-
*
|
|
39
|
-
*
|
|
40
|
-
*
|
|
41
|
-
*
|
|
42
|
-
*
|
|
30
|
+
* // Create a particle emitter at position 100, 100
|
|
31
|
+
* var emitter = new ParticleEmitter(100, 100, {
|
|
32
|
+
* width: 16,
|
|
33
|
+
* height : 16,
|
|
34
|
+
* tint: "#f00",
|
|
35
|
+
* totalParticles: 32,
|
|
36
|
+
* angle: 0,
|
|
37
|
+
* angleVariation: 6.283185307179586,
|
|
38
|
+
* maxLife: 5,
|
|
39
|
+
* speed: 3
|
|
40
|
+
* });
|
|
43
41
|
*
|
|
44
42
|
* // Add the emitter to the game world
|
|
45
43
|
* me.game.world.addChild(emitter);
|
|
@@ -62,6 +60,15 @@ class ParticleEmitter extends Container {
|
|
|
62
60
|
settings.height | 1
|
|
63
61
|
);
|
|
64
62
|
|
|
63
|
+
/**
|
|
64
|
+
* the current (active) emitter settings
|
|
65
|
+
* @public
|
|
66
|
+
* @type {ParticleEmitterSettings}
|
|
67
|
+
* @name settings
|
|
68
|
+
* @memberof ParticleEmitter
|
|
69
|
+
*/
|
|
70
|
+
this.settings = {};
|
|
71
|
+
|
|
65
72
|
// center the emitter around the given coordinates
|
|
66
73
|
this.centerOn(x, y);
|
|
67
74
|
|
|
@@ -92,9 +99,6 @@ class ParticleEmitter extends Container {
|
|
|
92
99
|
// count the updates
|
|
93
100
|
this._updateCount = 0;
|
|
94
101
|
|
|
95
|
-
// the emitter settings
|
|
96
|
-
this.settings = {};
|
|
97
|
-
|
|
98
102
|
// internally store how much time was skipped when frames are skipped
|
|
99
103
|
this._dt = 0;
|
|
100
104
|
|
|
@@ -106,14 +110,14 @@ class ParticleEmitter extends Container {
|
|
|
106
110
|
|
|
107
111
|
/**
|
|
108
112
|
* Reset the emitter with particle emitter settings.
|
|
109
|
-
* @param {
|
|
113
|
+
* @param {ParticleEmitterSettings} settings [optional] object with emitter settings. See {@link ParticleEmitterSettings}
|
|
110
114
|
*/
|
|
111
115
|
reset(settings = {}) {
|
|
112
116
|
Object.assign(this.settings, ParticleEmitterSettings, settings);
|
|
113
117
|
|
|
114
|
-
// Cache the image reference
|
|
115
118
|
if (typeof this.settings.image === "undefined") {
|
|
116
|
-
this.
|
|
119
|
+
this._defaultParticle = createDefaultParticleTexture(settings.textureSize, settings.textureSize);
|
|
120
|
+
this.settings.image = this._defaultParticle.canvas;
|
|
117
121
|
}
|
|
118
122
|
|
|
119
123
|
this.floating = this.settings.floating;
|
|
@@ -140,7 +144,7 @@ class ParticleEmitter extends Container {
|
|
|
140
144
|
// Add count particles in the game world
|
|
141
145
|
/** @ignore */
|
|
142
146
|
addParticles(count) {
|
|
143
|
-
for (var i = 0; i <
|
|
147
|
+
for (var i = 0; i < count; i++) {
|
|
144
148
|
// Add particle to the container
|
|
145
149
|
this.addChild(pool.pull("Particle", this), this.pos.z);
|
|
146
150
|
}
|
|
@@ -243,6 +247,10 @@ class ParticleEmitter extends Container {
|
|
|
243
247
|
// call the parent destroy method
|
|
244
248
|
super.destroy(arguments);
|
|
245
249
|
// clean emitter specific Properties
|
|
250
|
+
if (typeof this._defaultParticle !== "undefined") {
|
|
251
|
+
pool.push(this._defaultParticle);
|
|
252
|
+
this._defaultParticle = undefined;
|
|
253
|
+
}
|
|
246
254
|
this.settings.image = undefined;
|
|
247
255
|
this.settings = undefined;
|
|
248
256
|
}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import
|
|
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 =
|
|
42
|
+
this.vel = pool.pull("Vector2d");
|
|
42
43
|
}
|
|
43
44
|
|
|
44
45
|
this.image = emitter.settings.image;
|
package/src/physics/body.js
CHANGED
|
@@ -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 =
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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] =
|
|
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.
|
|
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
|
|