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.
- package/{LICENSE → LICENSE.md} +0 -0
- package/README.md +93 -57
- package/dist/melonjs.js +10334 -11179
- package/dist/melonjs.min.js +4 -10
- package/dist/melonjs.module.d.ts +13206 -0
- package/dist/melonjs.module.js +9913 -10872
- package/package.json +19 -14
- package/src/audio/audio.js +477 -553
- package/src/camera/camera2d.js +67 -65
- package/src/entity/draggable.js +26 -35
- package/src/entity/droptarget.js +17 -14
- package/src/entity/entity.js +59 -79
- package/src/game.js +194 -204
- package/src/index.js +12 -30
- package/src/input/gamepad.js +8 -19
- package/src/input/keyboard.js +4 -4
- package/src/input/pointer.js +14 -12
- package/src/input/pointerevent.js +15 -13
- package/src/lang/deprecated.js +2 -887
- package/src/level/level.js +3 -3
- package/src/level/tiled/TMXGroup.js +7 -11
- package/src/level/tiled/TMXLayer.js +33 -32
- package/src/level/tiled/TMXTileMap.js +15 -19
- package/src/level/tiled/TMXTileset.js +5 -5
- package/src/level/tiled/TMXUtils.js +3 -3
- package/src/level/tiled/renderer/TMXRenderer.js +4 -0
- package/src/loader/loader.js +8 -23
- package/src/loader/loadingscreen.js +51 -60
- package/src/math/matrix3.js +1 -1
- package/src/particles/emitter.js +36 -39
- package/src/particles/particle.js +27 -12
- package/src/particles/particlecontainer.js +17 -16
- package/src/physics/body.js +80 -118
- package/src/physics/collision.js +5 -235
- package/src/physics/detector.js +235 -0
- package/src/physics/quadtree.js +14 -14
- package/src/physics/world.js +84 -18
- package/src/plugin/plugin.js +26 -24
- package/src/polyfill/console.js +9 -14
- package/src/renderable/GUI.js +48 -62
- package/src/renderable/collectable.js +11 -4
- package/src/renderable/colorlayer.js +28 -26
- package/src/renderable/container.js +120 -96
- package/src/renderable/imagelayer.js +94 -93
- package/src/renderable/renderable.js +164 -138
- package/src/renderable/sprite.js +42 -44
- package/src/renderable/trigger.js +24 -17
- package/src/shapes/ellipse.js +27 -27
- package/src/shapes/line.js +12 -8
- package/src/shapes/poly.js +77 -49
- package/src/shapes/rectangle.js +193 -268
- package/src/state/stage.js +23 -25
- package/src/state/state.js +35 -86
- package/src/system/device.js +233 -285
- package/src/system/event.js +485 -432
- package/src/system/pooling.js +61 -54
- package/src/system/save.js +17 -16
- package/src/system/timer.js +34 -38
- package/src/text/bitmaptext.js +44 -46
- package/src/text/text.js +39 -34
- package/src/tweens/easing.js +0 -2
- package/src/tweens/interpolation.js +3 -8
- package/src/tweens/tween.js +332 -351
- package/src/utils/function.js +6 -8
- package/src/utils/utils.js +34 -30
- package/src/video/canvas/canvas_renderer.js +13 -8
- package/src/video/renderer.js +8 -7
- package/src/video/texture.js +8 -8
- package/src/video/texture_cache.js +5 -5
- package/src/video/video.js +373 -403
- package/src/video/webgl/glshader.js +2 -2
- package/src/video/webgl/webgl_compositor.js +14 -8
- package/src/video/webgl/webgl_renderer.js +21 -19
- package/plugins/debug/debugPanel.js +0 -770
- package/plugins/debug/font/PressStart2P.fnt +0 -100
- package/plugins/debug/font/PressStart2P.ltr +0 -1
- package/plugins/debug/font/PressStart2P.png +0 -0
- package/plugins/debug/particleDebugPanel.js +0 -303
package/src/physics/body.js
CHANGED
|
@@ -1,14 +1,13 @@
|
|
|
1
1
|
import Vector2d from "./../math/vector2.js";
|
|
2
|
-
import ObservableVector2d from "./../math/observable_vector2.js";
|
|
3
2
|
import Rect from "./../shapes/rectangle.js";
|
|
4
3
|
import Ellipse from "./../shapes/ellipse.js";
|
|
5
4
|
import Polygon from "./../shapes/poly.js";
|
|
6
5
|
import Bounds from "./bounds.js";
|
|
7
6
|
import collision from "./collision.js";
|
|
8
|
-
import
|
|
7
|
+
import * as arrayUtil from "./../utils/array.js";
|
|
9
8
|
import timer from "./../system/timer.js";
|
|
10
9
|
import { clamp } from "./../math/math.js";
|
|
11
|
-
import
|
|
10
|
+
import { world } from "./../game.js";
|
|
12
11
|
|
|
13
12
|
|
|
14
13
|
/**
|
|
@@ -89,8 +88,7 @@ class Body {
|
|
|
89
88
|
this.collisionType = collision.types.ENEMY_OBJECT;
|
|
90
89
|
|
|
91
90
|
/**
|
|
92
|
-
* body velocity
|
|
93
|
-
*
|
|
91
|
+
* body velocity
|
|
94
92
|
* @public
|
|
95
93
|
* @type me.Vector2d
|
|
96
94
|
* @default <0,0>
|
|
@@ -102,22 +100,6 @@ class Body {
|
|
|
102
100
|
}
|
|
103
101
|
this.vel.set(0, 0);
|
|
104
102
|
|
|
105
|
-
/**
|
|
106
|
-
* body acceleration <br>
|
|
107
|
-
* Not fully implemented yet. At this time accel is used to set the MaximumVelocity allowed.
|
|
108
|
-
* @public
|
|
109
|
-
* @type me.Vector2d
|
|
110
|
-
* @default <0,0>
|
|
111
|
-
* @name accel
|
|
112
|
-
* @deprecated
|
|
113
|
-
* @see me.Body.force
|
|
114
|
-
* @memberOf me.Body
|
|
115
|
-
*/
|
|
116
|
-
if (typeof this.accel === "undefined") {
|
|
117
|
-
this.accel = new Vector2d();
|
|
118
|
-
}
|
|
119
|
-
this.accel.set(0, 0);
|
|
120
|
-
|
|
121
103
|
/**
|
|
122
104
|
* body force or acceleration (automatically) applied to the body.
|
|
123
105
|
* when defining a force, user should also define a max velocity
|
|
@@ -198,32 +180,18 @@ class Body {
|
|
|
198
180
|
// cap by default to half the default gravity force
|
|
199
181
|
this.maxVel.set(490, 490);
|
|
200
182
|
|
|
183
|
+
|
|
201
184
|
/**
|
|
202
|
-
*
|
|
203
|
-
*
|
|
185
|
+
* either this body is a static body or not
|
|
186
|
+
* @readonly
|
|
204
187
|
* @public
|
|
205
|
-
* @
|
|
206
|
-
* @
|
|
207
|
-
* @
|
|
208
|
-
* @deprecated since 8.0.0
|
|
209
|
-
* @name gravity
|
|
188
|
+
* @type Boolean
|
|
189
|
+
* @default false
|
|
190
|
+
* @name isStatic
|
|
210
191
|
* @memberOf me.Body
|
|
211
192
|
*/
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
this.gravity = new ObservableVector2d(0, 0, { onUpdate : function(x, y) {
|
|
215
|
-
// disable gravity or apply a scale if y gravity is different from 0
|
|
216
|
-
if (typeof y === "number") {
|
|
217
|
-
self.gravityScale = y / game.world.gravity.y;
|
|
218
|
-
}
|
|
219
|
-
// deprecation // WARNING:
|
|
220
|
-
console.log(
|
|
221
|
-
"me.Body.gravity is deprecated, " +
|
|
222
|
-
"please see me.Body.gravityScale " +
|
|
223
|
-
"to modify gravity for a specific body"
|
|
224
|
-
);
|
|
225
|
-
}});
|
|
226
|
-
}
|
|
193
|
+
this.isStatic = false;
|
|
194
|
+
|
|
227
195
|
|
|
228
196
|
/**
|
|
229
197
|
* The degree to which this body is affected by the world gravity
|
|
@@ -294,6 +262,19 @@ class Body {
|
|
|
294
262
|
this.ancestor.isKinematic = false;
|
|
295
263
|
}
|
|
296
264
|
|
|
265
|
+
/**
|
|
266
|
+
* set the body as a static body
|
|
267
|
+
* static body do not move automatically and do not check againt collision with others
|
|
268
|
+
* @name setStatic
|
|
269
|
+
* @memberOf me.Body
|
|
270
|
+
* @public
|
|
271
|
+
* @function
|
|
272
|
+
* @param {Boolean} [isStatic=true]
|
|
273
|
+
*/
|
|
274
|
+
setStatic(isStatic = true) {
|
|
275
|
+
this.isStatic = isStatic === true;
|
|
276
|
+
}
|
|
277
|
+
|
|
297
278
|
/**
|
|
298
279
|
* add a collision shape to this body <br>
|
|
299
280
|
* (note: me.Rect objects will be converted to me.Polygon before being added)
|
|
@@ -302,7 +283,6 @@ class Body {
|
|
|
302
283
|
* @public
|
|
303
284
|
* @function
|
|
304
285
|
* @param {me.Rect|me.Polygon|me.Line|me.Ellipse|me.Bounds|Object} shape a shape or JSON object
|
|
305
|
-
* @param {Boolean} batchInsert if true the body bounds won't be updated after adding a shape
|
|
306
286
|
* @return {Number} the shape array length
|
|
307
287
|
* @example
|
|
308
288
|
* // add a rectangle shape
|
|
@@ -472,7 +452,7 @@ class Body {
|
|
|
472
452
|
// clear the current bounds
|
|
473
453
|
this.bounds.clear();
|
|
474
454
|
// remove the shape from shape list
|
|
475
|
-
|
|
455
|
+
arrayUtil.remove(this.shapes, shape);
|
|
476
456
|
// add everything left back
|
|
477
457
|
for (var s = 0; s < this.shapes.length; s++) {
|
|
478
458
|
this.addShape(this.shapes[s]);
|
|
@@ -568,7 +548,7 @@ class Body {
|
|
|
568
548
|
}
|
|
569
549
|
|
|
570
550
|
// cancel the falling an jumping flags if necessary
|
|
571
|
-
var dir = Math.sign(
|
|
551
|
+
var dir = Math.sign(world.gravity.y * this.gravityScale) || 1;
|
|
572
552
|
this.falling = overlap.y >= dir;
|
|
573
553
|
this.jumping = overlap.y <= -dir;
|
|
574
554
|
}
|
|
@@ -685,28 +665,6 @@ class Body {
|
|
|
685
665
|
return this;
|
|
686
666
|
}
|
|
687
667
|
|
|
688
|
-
/**
|
|
689
|
-
* Sets accel to Velocity if x or y is not 0. Net effect is to set the maxVel.x/y to the passed values for x/y<br>
|
|
690
|
-
* note: This does not set the vel member of the body object. This is identical to the setMaxVelocity call except that the
|
|
691
|
-
* accel property is updated to match the passed x and y.
|
|
692
|
-
* setMaxVelocity if needed<br>
|
|
693
|
-
* @name setVelocity
|
|
694
|
-
* @memberOf me.Body
|
|
695
|
-
* @function
|
|
696
|
-
* @param {Number} x velocity on x axis
|
|
697
|
-
* @param {Number} y velocity on y axis
|
|
698
|
-
* @protected
|
|
699
|
-
* @deprecated
|
|
700
|
-
* @see me.Body.force
|
|
701
|
-
*/
|
|
702
|
-
setVelocity(x, y) {
|
|
703
|
-
this.accel.x = x !== 0 ? x : this.accel.x;
|
|
704
|
-
this.accel.y = y !== 0 ? y : this.accel.y;
|
|
705
|
-
|
|
706
|
-
// limit by default to the same max value
|
|
707
|
-
this.setMaxVelocity(x, y);
|
|
708
|
-
}
|
|
709
|
-
|
|
710
668
|
/**
|
|
711
669
|
* cap the body velocity (body.maxVel property) to the specified value<br>
|
|
712
670
|
* @name setMaxVelocity
|
|
@@ -730,69 +688,70 @@ class Body {
|
|
|
730
688
|
* @param {Number} y vertical friction
|
|
731
689
|
* @protected
|
|
732
690
|
*/
|
|
733
|
-
setFriction(x, y) {
|
|
734
|
-
this.friction.x = x
|
|
735
|
-
this.friction.y = y
|
|
736
|
-
}
|
|
737
|
-
|
|
738
|
-
/**
|
|
739
|
-
* apply friction to a vector
|
|
740
|
-
* @ignore
|
|
741
|
-
*/
|
|
742
|
-
applyFriction(vel) {
|
|
743
|
-
var fx = this.friction.x * timer.tick,
|
|
744
|
-
nx = vel.x + fx,
|
|
745
|
-
x = vel.x - fx,
|
|
746
|
-
fy = this.friction.y * timer.tick,
|
|
747
|
-
ny = vel.y + fy,
|
|
748
|
-
y = vel.y - fy;
|
|
749
|
-
|
|
750
|
-
vel.x = (
|
|
751
|
-
(nx < 0) ? nx :
|
|
752
|
-
( x > 0) ? x : 0
|
|
753
|
-
);
|
|
754
|
-
vel.y = (
|
|
755
|
-
(ny < 0) ? ny :
|
|
756
|
-
( y > 0) ? y : 0
|
|
757
|
-
);
|
|
691
|
+
setFriction(x = 0, y = 0) {
|
|
692
|
+
this.friction.x = x;
|
|
693
|
+
this.friction.y = y;
|
|
758
694
|
}
|
|
759
695
|
|
|
760
696
|
/**
|
|
761
697
|
* compute the new velocity value
|
|
762
698
|
* @ignore
|
|
763
699
|
*/
|
|
764
|
-
computeVelocity(
|
|
765
|
-
// apply
|
|
766
|
-
|
|
767
|
-
|
|
768
|
-
}
|
|
769
|
-
if (this.force.y) {
|
|
770
|
-
vel.y += this.force.y * timer.tick;
|
|
771
|
-
}
|
|
772
|
-
|
|
773
|
-
// apply friction
|
|
774
|
-
if (this.friction.x || this.friction.y) {
|
|
775
|
-
this.applyFriction(vel);
|
|
776
|
-
}
|
|
700
|
+
computeVelocity(/* dt */) {
|
|
701
|
+
// apply timer.tick to delta time for linear interpolation (when enabled)
|
|
702
|
+
// #761 add delta time in body update
|
|
703
|
+
var deltaTime = /* dt * */ timer.tick;
|
|
777
704
|
|
|
705
|
+
// apply gravity to the current velocity
|
|
778
706
|
if (!this.ignoreGravity) {
|
|
779
|
-
var worldGravity =
|
|
707
|
+
var worldGravity = world.gravity;
|
|
708
|
+
|
|
780
709
|
// apply gravity if defined
|
|
781
|
-
vel.x += worldGravity.x * this.gravityScale *
|
|
782
|
-
vel.y += worldGravity.y * this.gravityScale *
|
|
710
|
+
this.vel.x += worldGravity.x * this.gravityScale * deltaTime;
|
|
711
|
+
this.vel.y += worldGravity.y * this.gravityScale * deltaTime;
|
|
712
|
+
|
|
783
713
|
// check if falling / jumping
|
|
784
|
-
this.falling = (vel.y * Math.sign(worldGravity.y * this.gravityScale)) > 0;
|
|
714
|
+
this.falling = (this.vel.y * Math.sign(worldGravity.y * this.gravityScale)) > 0;
|
|
785
715
|
this.jumping = (this.falling ? false : this.jumping);
|
|
786
716
|
}
|
|
787
717
|
|
|
788
|
-
//
|
|
789
|
-
if (
|
|
790
|
-
vel.
|
|
718
|
+
// apply force if defined
|
|
719
|
+
if (this.force.x !== 0) {
|
|
720
|
+
this.vel.x += this.force.x * deltaTime;
|
|
721
|
+
}
|
|
722
|
+
if (this.force.y !== 0) {
|
|
723
|
+
this.vel.y += this.force.y * deltaTime;
|
|
724
|
+
}
|
|
725
|
+
|
|
726
|
+
// apply friction if defined
|
|
727
|
+
if (this.friction.x > 0) {
|
|
728
|
+
var fx = this.friction.x * deltaTime,
|
|
729
|
+
nx = this.vel.x + fx,
|
|
730
|
+
x = this.vel.x - fx;
|
|
731
|
+
|
|
732
|
+
this.vel.x = (
|
|
733
|
+
(nx < 0) ? nx :
|
|
734
|
+
( x > 0) ? x : 0
|
|
735
|
+
);
|
|
791
736
|
}
|
|
792
|
-
if (
|
|
793
|
-
|
|
737
|
+
if (this.friction.y > 0) {
|
|
738
|
+
var fy = this.friction.y * deltaTime,
|
|
739
|
+
ny = this.vel.y + fy,
|
|
740
|
+
y = this.vel.y - fy;
|
|
741
|
+
|
|
742
|
+
this.vel.y = (
|
|
743
|
+
(ny < 0) ? ny :
|
|
744
|
+
( y > 0) ? y : 0
|
|
745
|
+
);
|
|
794
746
|
}
|
|
795
747
|
|
|
748
|
+
// cap velocity
|
|
749
|
+
if (this.vel.y !== 0) {
|
|
750
|
+
this.vel.y = clamp(this.vel.y, -this.maxVel.y, this.maxVel.y);
|
|
751
|
+
}
|
|
752
|
+
if (this.vel.x !== 0) {
|
|
753
|
+
this.vel.x = clamp(this.vel.x, -this.maxVel.x, this.maxVel.x);
|
|
754
|
+
}
|
|
796
755
|
}
|
|
797
756
|
|
|
798
757
|
/**
|
|
@@ -807,16 +766,17 @@ class Body {
|
|
|
807
766
|
* In addition, when the gravity calcuation is made, if the Body.vel.y > 0 then the Body.falling
|
|
808
767
|
* property is set to true and Body.jumping is set to !Body.falling.
|
|
809
768
|
*
|
|
810
|
-
* At this time a call to Body.Update does not call the onBodyUpdate callback that is listed in the
|
|
769
|
+
* At this time a call to Body.Update does not call the onBodyUpdate callback that is listed in the constructor arguments.
|
|
811
770
|
* @name update
|
|
771
|
+
* @ignore
|
|
812
772
|
* @memberOf me.Body
|
|
813
773
|
* @function
|
|
814
774
|
* @return {boolean} true if resulting velocity is different than 0
|
|
815
775
|
* @see source code for me.Body.computeVelocity (private member)
|
|
816
776
|
*/
|
|
817
|
-
update(
|
|
777
|
+
update(dt) {
|
|
818
778
|
// update the velocity
|
|
819
|
-
this.computeVelocity(
|
|
779
|
+
this.computeVelocity(dt);
|
|
820
780
|
|
|
821
781
|
// update the body ancestor position
|
|
822
782
|
this.ancestor.pos.add(this.vel);
|
|
@@ -832,6 +792,8 @@ class Body {
|
|
|
832
792
|
destroy() {
|
|
833
793
|
this.onBodyUpdate = undefined;
|
|
834
794
|
this.ancestor = undefined;
|
|
795
|
+
this.bounds = undefined;
|
|
796
|
+
this.setStatic(false);
|
|
835
797
|
this.shapes.length = 0;
|
|
836
798
|
}
|
|
837
799
|
};
|
package/src/physics/collision.js
CHANGED
|
@@ -1,73 +1,9 @@
|
|
|
1
|
-
import
|
|
2
|
-
import Vector2d from "./../math/vector2.js";
|
|
3
|
-
import game from "./../game.js";
|
|
4
|
-
|
|
5
|
-
// a dummy object when using Line for raycasting
|
|
6
|
-
var dummyObj = {
|
|
7
|
-
pos : new Vector2d(0, 0),
|
|
8
|
-
ancestor : {
|
|
9
|
-
_absPos : new Vector2d(0, 0),
|
|
10
|
-
getAbsolutePosition : function () {
|
|
11
|
-
return this._absPos;
|
|
12
|
-
}
|
|
13
|
-
}
|
|
14
|
-
};
|
|
15
|
-
|
|
16
|
-
/**
|
|
17
|
-
* @classdesc
|
|
18
|
-
* An object representing the result of an intersection.
|
|
19
|
-
* @property {me.Renderable} a The first object participating in the intersection
|
|
20
|
-
* @property {me.Renderable} b The second object participating in the intersection
|
|
21
|
-
* @property {Number} overlap Magnitude of the overlap on the shortest colliding axis
|
|
22
|
-
* @property {me.Vector2d} overlapV The overlap vector (i.e. `overlapN.scale(overlap, overlap)`). If this vector is subtracted from the position of a, a and b will no longer be colliding
|
|
23
|
-
* @property {me.Vector2d} overlapN The shortest colliding axis (unit-vector)
|
|
24
|
-
* @property {Boolean} aInB Whether the first object is entirely inside the second
|
|
25
|
-
* @property {Boolean} bInA Whether the second object is entirely inside the first
|
|
26
|
-
* @property {Number} indexShapeA The index of the colliding shape for the object a body
|
|
27
|
-
* @property {Number} indexShapeB The index of the colliding shape for the object b body
|
|
28
|
-
* @name ResponseObject
|
|
29
|
-
* @memberOf me.collision
|
|
30
|
-
* @public
|
|
31
|
-
* @see me.collision.check
|
|
32
|
-
*/
|
|
33
|
-
class ResponseObject {
|
|
34
|
-
constructor() {
|
|
35
|
-
this.a = null;
|
|
36
|
-
this.b = null;
|
|
37
|
-
this.overlapN = new Vector2d();
|
|
38
|
-
this.overlapV = new Vector2d();
|
|
39
|
-
this.aInB = true;
|
|
40
|
-
this.bInA = true;
|
|
41
|
-
this.indexShapeA = -1;
|
|
42
|
-
this.indexShapeB = -1;
|
|
43
|
-
this.overlap = Number.MAX_VALUE;
|
|
44
|
-
return this;
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
/**
|
|
48
|
-
* Set some values of the response back to their defaults. <br>
|
|
49
|
-
* Call this between tests if you are going to reuse a single <br>
|
|
50
|
-
* Response object for multiple intersection tests <br>
|
|
51
|
-
* (recommended as it will avoid allocating extra memory) <br>
|
|
52
|
-
* @name clear
|
|
53
|
-
* @memberOf me.collision.ResponseObject
|
|
54
|
-
* @public
|
|
55
|
-
* @function
|
|
56
|
-
*/
|
|
57
|
-
clear () {
|
|
58
|
-
this.aInB = true;
|
|
59
|
-
this.bInA = true;
|
|
60
|
-
this.overlap = Number.MAX_VALUE;
|
|
61
|
-
this.indexShapeA = -1;
|
|
62
|
-
this.indexShapeB = -1;
|
|
63
|
-
return this;
|
|
64
|
-
}
|
|
65
|
-
}
|
|
1
|
+
import { rayCast, globalResponse } from "./detector.js";
|
|
66
2
|
|
|
67
3
|
/**
|
|
68
|
-
*
|
|
4
|
+
* Collision detection (and projection-based collision response) of 2D shapes.<br>
|
|
69
5
|
* Based on the Separating Axis Theorem and supports detecting collisions between simple Axis-Aligned Boxes, convex polygons and circles based shapes.
|
|
70
|
-
* @namespace
|
|
6
|
+
* @namespace collision
|
|
71
7
|
* @memberOf me
|
|
72
8
|
*/
|
|
73
9
|
|
|
@@ -166,124 +102,7 @@ var collision = {
|
|
|
166
102
|
* @public
|
|
167
103
|
* @type {me.collision.ResponseObject}
|
|
168
104
|
*/
|
|
169
|
-
response :
|
|
170
|
-
|
|
171
|
-
/**
|
|
172
|
-
* a callback used to determine if two objects should collide (based on both respective objects collision mask and type).<br>
|
|
173
|
-
* you can redefine this function if you need any specific rules over what should collide with what.
|
|
174
|
-
* @name shouldCollide
|
|
175
|
-
* @memberOf me.collision
|
|
176
|
-
* @public
|
|
177
|
-
* @function
|
|
178
|
-
* @param {me.Renderable} a a reference to the object A.
|
|
179
|
-
* @param {me.Renderable} b a reference to the object B.
|
|
180
|
-
* @return {Boolean} true if they should collide, false otherwise
|
|
181
|
-
*/
|
|
182
|
-
shouldCollide(a, b) {
|
|
183
|
-
return (
|
|
184
|
-
a.isKinematic !== true && b.isKinematic !== true &&
|
|
185
|
-
a.body && b.body &&
|
|
186
|
-
(a.body.collisionMask & b.body.collisionType) !== 0 &&
|
|
187
|
-
(a.body.collisionType & b.body.collisionMask) !== 0
|
|
188
|
-
);
|
|
189
|
-
},
|
|
190
|
-
|
|
191
|
-
/**
|
|
192
|
-
* Checks if the specified object collides with others
|
|
193
|
-
* @name check
|
|
194
|
-
* @memberOf me.collision
|
|
195
|
-
* @public
|
|
196
|
-
* @function
|
|
197
|
-
* @param {me.Renderable} obj object to be tested for collision
|
|
198
|
-
* @param {me.collision.ResponseObject} [respObj=me.collision.response] a user defined response object that will be populated if they intersect.
|
|
199
|
-
* @return {Boolean} in case of collision, false otherwise
|
|
200
|
-
* @example
|
|
201
|
-
* update : function (dt) {
|
|
202
|
-
* // ...
|
|
203
|
-
*
|
|
204
|
-
* // handle collisions against other shapes
|
|
205
|
-
* me.collision.check(this);
|
|
206
|
-
*
|
|
207
|
-
* // ...
|
|
208
|
-
* },
|
|
209
|
-
*
|
|
210
|
-
* // colision handler
|
|
211
|
-
* onCollision : function (response) {
|
|
212
|
-
* if (response.b.body.collisionType === me.collision.types.ENEMY_OBJECT) {
|
|
213
|
-
* // makes the other object solid, by substracting the overlap vector to the current position
|
|
214
|
-
* this.pos.sub(response.overlapV);
|
|
215
|
-
* this.hurt();
|
|
216
|
-
* // not solid
|
|
217
|
-
* return false;
|
|
218
|
-
* }
|
|
219
|
-
* // Make the object solid
|
|
220
|
-
* return true;
|
|
221
|
-
* },
|
|
222
|
-
*/
|
|
223
|
-
check(objA, responseObject) {
|
|
224
|
-
var collision = 0;
|
|
225
|
-
var response = responseObject || this.response;
|
|
226
|
-
|
|
227
|
-
// retreive a list of potential colliding objects
|
|
228
|
-
var candidates = game.world.broadphase.retrieve(objA);
|
|
229
|
-
|
|
230
|
-
for (var i = candidates.length, objB; i--, (objB = candidates[i]);) {
|
|
231
|
-
|
|
232
|
-
// check if both objects "should" collide
|
|
233
|
-
if ((objB !== objA) && this.shouldCollide(objA, objB) &&
|
|
234
|
-
// fast AABB check if both bounding boxes are overlaping
|
|
235
|
-
objA.body.getBounds().overlaps(objB.body.getBounds())) {
|
|
236
|
-
|
|
237
|
-
// go trough all defined shapes in A
|
|
238
|
-
var aLen = objA.body.shapes.length;
|
|
239
|
-
var bLen = objB.body.shapes.length;
|
|
240
|
-
if (aLen === 0 || bLen === 0) {
|
|
241
|
-
continue;
|
|
242
|
-
}
|
|
243
|
-
|
|
244
|
-
var indexA = 0;
|
|
245
|
-
do {
|
|
246
|
-
var shapeA = objA.body.getShape(indexA);
|
|
247
|
-
// go through all defined shapes in B
|
|
248
|
-
var indexB = 0;
|
|
249
|
-
do {
|
|
250
|
-
var shapeB = objB.body.getShape(indexB);
|
|
251
|
-
|
|
252
|
-
// full SAT collision check
|
|
253
|
-
if (SAT["test" + shapeA.shapeType + shapeB.shapeType]
|
|
254
|
-
.call(
|
|
255
|
-
this,
|
|
256
|
-
objA, // a reference to the object A
|
|
257
|
-
shapeA,
|
|
258
|
-
objB, // a reference to the object B
|
|
259
|
-
shapeB,
|
|
260
|
-
// clear response object before reusing
|
|
261
|
-
response.clear()) === true
|
|
262
|
-
) {
|
|
263
|
-
// we touched something !
|
|
264
|
-
collision++;
|
|
265
|
-
|
|
266
|
-
// set the shape index
|
|
267
|
-
response.indexShapeA = indexA;
|
|
268
|
-
response.indexShapeB = indexB;
|
|
269
|
-
|
|
270
|
-
// execute the onCollision callback
|
|
271
|
-
if (objA.onCollision && objA.onCollision(response, objB) !== false) {
|
|
272
|
-
objA.body.respondToCollision.call(objA.body, response);
|
|
273
|
-
}
|
|
274
|
-
if (objB.onCollision && objB.onCollision(response, objA) !== false) {
|
|
275
|
-
objB.body.respondToCollision.call(objB.body, response);
|
|
276
|
-
}
|
|
277
|
-
}
|
|
278
|
-
indexB++;
|
|
279
|
-
} while (indexB < bLen);
|
|
280
|
-
indexA++;
|
|
281
|
-
} while (indexA < aLen);
|
|
282
|
-
}
|
|
283
|
-
}
|
|
284
|
-
// we could return the amount of objects we collided with ?
|
|
285
|
-
return collision > 0;
|
|
286
|
-
},
|
|
105
|
+
response : globalResponse,
|
|
287
106
|
|
|
288
107
|
/**
|
|
289
108
|
* Checks for object colliding with the given line
|
|
@@ -312,56 +131,7 @@ var collision = {
|
|
|
312
131
|
* // ...
|
|
313
132
|
* }
|
|
314
133
|
*/
|
|
315
|
-
rayCast(line, resultArray) {
|
|
316
|
-
var collision = 0;
|
|
317
|
-
var result = resultArray || [];
|
|
318
|
-
|
|
319
|
-
// retrieve a list of potential colliding objects
|
|
320
|
-
var candidates = game.world.broadphase.retrieve(line);
|
|
321
|
-
|
|
322
|
-
for (var i = candidates.length, objB; i--, (objB = candidates[i]);) {
|
|
323
|
-
|
|
324
|
-
// fast AABB check if both bounding boxes are overlaping
|
|
325
|
-
if (objB.body && line.getBounds().overlaps(objB.getBounds())) {
|
|
326
|
-
|
|
327
|
-
// go trough all defined shapes in B (if any)
|
|
328
|
-
var bLen = objB.body.shapes.length;
|
|
329
|
-
if ( objB.body.shapes.length === 0) {
|
|
330
|
-
continue;
|
|
331
|
-
}
|
|
332
|
-
|
|
333
|
-
var shapeA = line;
|
|
334
|
-
|
|
335
|
-
// go through all defined shapes in B
|
|
336
|
-
var indexB = 0;
|
|
337
|
-
do {
|
|
338
|
-
var shapeB = objB.body.getShape(indexB);
|
|
339
|
-
|
|
340
|
-
// full SAT collision check
|
|
341
|
-
if (SAT["test" + shapeA.shapeType + shapeB.shapeType]
|
|
342
|
-
.call(
|
|
343
|
-
this,
|
|
344
|
-
dummyObj, // a reference to the object A
|
|
345
|
-
shapeA,
|
|
346
|
-
objB, // a reference to the object B
|
|
347
|
-
shapeB
|
|
348
|
-
)) {
|
|
349
|
-
// we touched something !
|
|
350
|
-
result[collision] = objB;
|
|
351
|
-
collision++;
|
|
352
|
-
}
|
|
353
|
-
indexB++;
|
|
354
|
-
} while (indexB < bLen);
|
|
355
|
-
}
|
|
356
|
-
}
|
|
357
|
-
|
|
358
|
-
// cap result in case it was not empty
|
|
359
|
-
result.length = collision;
|
|
360
|
-
|
|
361
|
-
// return the list of colliding objects
|
|
362
|
-
return result;
|
|
363
|
-
}
|
|
364
|
-
|
|
134
|
+
rayCast(line, resultArray) { return rayCast(line, resultArray); }
|
|
365
135
|
};
|
|
366
136
|
|
|
367
137
|
export default collision;
|