Skip to content

Commit 5494f6a

Browse files
committed
Arcade Physics Events
1 parent 8b9feb8 commit 5494f6a

11 files changed

Lines changed: 201 additions & 83 deletions

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,8 @@ one set of bindings ever created, which makes things a lot cleaner.
299299
* The Keyboard Plugin `keyup` dynamic event string has changed from `keyup_` to `keyup-` to keep it consistent with other keyed events. Note the change from `_` to `-`.
300300
* The `texturesready` event emitted by the Texture Manager has been renamed to `ready`.
301301
* The `loadcomplete` event emitted by the Loader Plugin has been renamed to `postprocess` to be reflect what it's used for.
302+
* Game Objects used to emit a `collide` event if they had an Arcade Physics Body with `onCollide` set, that collided with a Tile. This has changed. The event has been renamed to `tilecollide`. You should now listen for this event from the Arcade Physics World itself: `this.physics.world.on('tilecollide')`. The Game Object will not emit the event any more.
303+
* Game Objects used to emit a `overlap` event if they had an Arcade Physics Body with `onOverlap` set, that overlapped with a Tile. This has changed. The event has been renamed to `tileoverlap`. You should now listen for this event from the Arcade Physics World itself: `this.physics.world.on('tileoverlap')`. The Game Object will not emit the event any more.
302304

303305
### Examples and TypeScript
304306

src/physics/arcade/Body.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
var CircleContains = require('../../geom/circle/Contains');
88
var Class = require('../../utils/Class');
99
var CONST = require('./const');
10+
var Events = require('./events');
1011
var RadToDeg = require('../../math/RadToDeg');
1112
var Rectangle = require('../../geom/rectangle/Rectangle');
1213
var RectangleContains = require('../../geom/rectangle/Contains');
@@ -946,7 +947,7 @@ var Body = new Class({
946947

947948
if (this.collideWorldBounds && this.checkWorldBounds() && this.onWorldBounds)
948949
{
949-
this.world.emit('worldbounds', this, this.blocked.up, this.blocked.down, this.blocked.left, this.blocked.right);
950+
this.world.emit(Events.WORLD_BOUNDS, this, this.blocked.up, this.blocked.down, this.blocked.left, this.blocked.right);
950951
}
951952
}
952953

src/physics/arcade/World.js

Lines changed: 17 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ var Collider = require('./Collider');
1111
var CONST = require('./const');
1212
var DistanceBetween = require('../../math/distance/DistanceBetween');
1313
var EventEmitter = require('eventemitter3');
14+
var Events = require('./events');
1415
var FuzzyEqual = require('../../math/fuzzy/Equal');
1516
var FuzzyGreaterThan = require('../../math/fuzzy/GreaterThan');
1617
var FuzzyLessThan = require('../../math/fuzzy/LessThan');
@@ -31,50 +32,6 @@ var TransformMatrix = require('../../gameobjects/components/TransformMatrix');
3132
var Vector2 = require('../../math/Vector2');
3233
var Wrap = require('../../math/Wrap');
3334

34-
/**
35-
* The physics simulation paused.
36-
* @event Phaser.Physics.Arcade.World#pauseEvent
37-
*/
38-
39-
/**
40-
* The physics simulation resumed (from a paused state).
41-
* @event Phaser.Physics.Arcade.World#resumeEvent
42-
*/
43-
44-
/**
45-
* Two Game Objects collided.
46-
* This event is emitted only if at least one body has `onCollide` enabled.
47-
* @event Phaser.Physics.Arcade.World#collideEvent
48-
* @param {Phaser.GameObjects.GameObject} gameObject1
49-
* @param {Phaser.GameObjects.GameObject} gameObject2
50-
* @param {Phaser.Physics.Arcade.Body|Phaser.Physics.Arcade.StaticBody} body1
51-
* @param {Phaser.Physics.Arcade.Body|Phaser.Physics.Arcade.StaticBody} body2
52-
* @see Phaser.Physics.Arcade.Body#onCollide
53-
*/
54-
55-
/**
56-
* Two Game Objects overlapped.
57-
* This event is emitted only if at least one body has `onOverlap` enabled.
58-
* @event Phaser.Physics.Arcade.World#overlapEvent
59-
* @param {Phaser.GameObjects.GameObject} gameObject1
60-
* @param {Phaser.GameObjects.GameObject} gameObject2
61-
* @param {Phaser.Physics.Arcade.Body|Phaser.Physics.Arcade.StaticBody} body1
62-
* @param {Phaser.Physics.Arcade.Body|Phaser.Physics.Arcade.StaticBody} body2
63-
* @see Phaser.Physics.Arcade.Body#onOverlap
64-
*/
65-
66-
/**
67-
* A Body contacted the world boundary.
68-
* This event is emitted only if the body has `onWorldBounds` enabled.
69-
* @event Phaser.Physics.Arcade.World#worldboundsEvent
70-
* @param {Phaser.Physics.Arcade.Body} body
71-
* @param {boolean} up
72-
* @param {boolean} down
73-
* @param {boolean} left
74-
* @param {boolean} right
75-
* @see Phaser.Physics.Arcade.Body#onWorldBounds
76-
*/
77-
7835
/**
7936
* @typedef {object} ArcadeWorldConfig
8037
*
@@ -864,7 +821,7 @@ var World = new Class({
864821
* checks.
865822
*
866823
* @method Phaser.Physics.Arcade.World#pause
867-
* @fires Phaser.Physics.Arcade.World#pauseEvent
824+
* @fires Phaser.Physics.Arcade.Events#PAUSE
868825
* @since 3.0.0
869826
*
870827
* @return {Phaser.Physics.Arcade.World} This World object.
@@ -873,7 +830,7 @@ var World = new Class({
873830
{
874831
this.isPaused = true;
875832

876-
this.emit('pause');
833+
this.emit(Events.PAUSE);
877834

878835
return this;
879836
},
@@ -882,7 +839,7 @@ var World = new Class({
882839
* Resumes the simulation, if paused.
883840
*
884841
* @method Phaser.Physics.Arcade.World#resume
885-
* @fires Phaser.Physics.Arcade.World#resumeEvent
842+
* @fires Phaser.Physics.Arcade.Events#RESUME
886843
* @since 3.0.0
887844
*
888845
* @return {Phaser.Physics.Arcade.World} This World object.
@@ -891,7 +848,7 @@ var World = new Class({
891848
{
892849
this.isPaused = false;
893850

894-
this.emit('resume');
851+
this.emit(Events.RESUME);
895852

896853
return this;
897854
},
@@ -1372,8 +1329,8 @@ var World = new Class({
13721329
* Separates two Bodies.
13731330
*
13741331
* @method Phaser.Physics.Arcade.World#separate
1375-
* @fires Phaser.Physics.Arcade.World#collideEvent
1376-
* @fires Phaser.Physics.Arcade.World#overlapEvent
1332+
* @fires Phaser.Physics.Arcade.Events#COLLIDE
1333+
* @fires Phaser.Physics.Arcade.Events#OVERLAP
13771334
* @since 3.0.0
13781335
*
13791336
* @param {Phaser.Physics.Arcade.Body} body1 - The first Body to be separated.
@@ -1466,7 +1423,7 @@ var World = new Class({
14661423
{
14671424
if (overlapOnly && (body1.onOverlap || body2.onOverlap))
14681425
{
1469-
this.emit('overlap', body1.gameObject, body2.gameObject, body1, body2);
1426+
this.emit(Events.OVERLAP, body1.gameObject, body2.gameObject, body1, body2);
14701427
}
14711428
else
14721429
{
@@ -1475,7 +1432,7 @@ var World = new Class({
14751432

14761433
if (body1.onCollide || body2.onCollide)
14771434
{
1478-
this.emit('collide', body1.gameObject, body2.gameObject, body1, body2);
1435+
this.emit(Events.COLLIDE, body1.gameObject, body2.gameObject, body1, body2);
14791436
}
14801437
}
14811438
}
@@ -1487,8 +1444,8 @@ var World = new Class({
14871444
* Separates two Bodies, when both are circular.
14881445
*
14891446
* @method Phaser.Physics.Arcade.World#separateCircle
1490-
* @fires Phaser.Physics.Arcade.World#collideEvent
1491-
* @fires Phaser.Physics.Arcade.World#overlapEvent
1447+
* @fires Phaser.Physics.Arcade.Events#COLLIDE
1448+
* @fires Phaser.Physics.Arcade.Events#OVERLAP
14921449
* @since 3.0.0
14931450
*
14941451
* @param {Phaser.Physics.Arcade.Body} body1 - The first Body to be separated.
@@ -1561,7 +1518,7 @@ var World = new Class({
15611518
{
15621519
if (overlap !== 0 && (body1.onOverlap || body2.onOverlap))
15631520
{
1564-
this.emit('overlap', body1.gameObject, body2.gameObject, body1, body2);
1521+
this.emit(Events.OVERLAP, body1.gameObject, body2.gameObject, body1, body2);
15651522
}
15661523

15671524
// return true if there was some overlap, otherwise false
@@ -1673,7 +1630,7 @@ var World = new Class({
16731630

16741631
if (body1.onCollide || body2.onCollide)
16751632
{
1676-
this.emit('collide', body1.gameObject, body2.gameObject, body1, body2);
1633+
this.emit(Events.COLLIDE, body1.gameObject, body2.gameObject, body1, body2);
16771634
}
16781635

16791636
// sync changes back to the bodies
@@ -2143,10 +2100,8 @@ var World = new Class({
21432100
* Please use Phaser.Physics.Arcade.World#collide instead.
21442101
*
21452102
* @method Phaser.Physics.Arcade.World#collideSpriteVsTilemapLayer
2146-
* @fires Phaser.GameObjects.GameObject#collideEvent
2147-
* @fires Phaser.GameObjects.GameObject#overlapEvent
2148-
* @fires Phaser.Physics.Arcade.World#collideEvent
2149-
* @fires Phaser.Physics.Arcade.World#overlapEvent
2103+
* @fires Phaser.Physics.Arcade.Events#TILE_COLLIDE
2104+
* @fires Phaser.Physics.Arcade.Events#TILE_OVERLAP
21502105
* @since 3.0.0
21512106
*
21522107
* @param {Phaser.GameObjects.GameObject} sprite - The first object to check for collision.
@@ -2232,11 +2187,11 @@ var World = new Class({
22322187

22332188
if (overlapOnly && body.onOverlap)
22342189
{
2235-
sprite.emit('overlap', body.gameObject, tile, body, null);
2190+
this.emit(Events.TILE_OVERLAP, body.gameObject, tile, body);
22362191
}
22372192
else if (body.onCollide)
22382193
{
2239-
sprite.emit('collide', body.gameObject, tile, body, null);
2194+
this.emit(Events.TILE_COLLIDE, body.gameObject, tile, body);
22402195
}
22412196

22422197
// sync changes back to the body
@@ -2376,23 +2331,3 @@ var World = new Class({
23762331
});
23772332

23782333
module.exports = World;
2379-
2380-
/**
2381-
* A physics-enabled Game Object collided with a Tile.
2382-
* This event is emitted only if the Game Object's body has `onCollide` enabled.
2383-
* @event Phaser.GameObjects.GameObject#collideEvent
2384-
* @param {Phaser.GameObjects.GameObject} gameObject
2385-
* @param {Phaser.Tilemaps.Tile} tile
2386-
* @param {Phaser.Physics.Arcade.Body|Phaser.Physics.Arcade.StaticBody} body
2387-
* @see Phaser.Physics.Arcade.Body#onCollide
2388-
*/
2389-
2390-
/**
2391-
* A physics-enabled Game Object overlapped with a Tile.
2392-
* This event is emitted only if the Game Object's body has `onOverlap` enabled.
2393-
* @event Phaser.GameObjects.GameObject#overlapEvent
2394-
* @param {Phaser.GameObjects.GameObject} gameObject
2395-
* @param {Phaser.Tilemaps.Tile} tile
2396-
* @param {Phaser.Physics.Arcade.Body|Phaser.Physics.Arcade.StaticBody} body
2397-
* @see Phaser.Physics.Arcade.Body#onOverlap
2398-
*/
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/**
2+
* @author Richard Davey <rich@photonstorm.com>
3+
* @copyright 2019 Photon Storm Ltd.
4+
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
5+
*/
6+
7+
/**
8+
* The Arcade Physics World Collide Event.
9+
*
10+
* This event is dispatched by an Arcade Physics World instance if two bodies collide _and_ at least
11+
* one of them has their [onCollide]{Phaser.Physics.Arcade.Body#onCollide} property set to `true`.
12+
*
13+
* It provides an alternative means to handling collide events rather than using the callback approach.
14+
*
15+
* Listen to it from a Scene using: `this.physics.world.on('collide', listener)`.
16+
*
17+
* Please note that 'collide' and 'overlap' are two different things in Arcade Physics.
18+
*
19+
* @event Phaser.Physics.Arcade.Events#COLLIDE
20+
*
21+
* @param {Phaser.GameObjects.GameObject} gameObject1 - The first Game Object involved in the collision. This is the parent of `body1`.
22+
* @param {Phaser.GameObjects.GameObject} gameObject2 - The second Game Object involved in the collision. This is the parent of `body2`.
23+
* @param {Phaser.Physics.Arcade.Body|Phaser.Physics.Arcade.StaticBody} body1 - The first Physics Body involved in the collision.
24+
* @param {Phaser.Physics.Arcade.Body|Phaser.Physics.Arcade.StaticBody} body2 - The second Physics Body involved in the collision.
25+
*/
26+
module.exports = 'collide';
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/**
2+
* @author Richard Davey <rich@photonstorm.com>
3+
* @copyright 2019 Photon Storm Ltd.
4+
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
5+
*/
6+
7+
/**
8+
* The Arcade Physics World Overlap Event.
9+
*
10+
* This event is dispatched by an Arcade Physics World instance if two bodies overlap _and_ at least
11+
* one of them has their [onOverlap]{Phaser.Physics.Arcade.Body#onOverlap} property set to `true`.
12+
*
13+
* It provides an alternative means to handling overlap events rather than using the callback approach.
14+
*
15+
* Listen to it from a Scene using: `this.physics.world.on('overlap', listener)`.
16+
*
17+
* Please note that 'collide' and 'overlap' are two different things in Arcade Physics.
18+
*
19+
* @event Phaser.Physics.Arcade.Events#OVERLAP
20+
*
21+
* @param {Phaser.GameObjects.GameObject} gameObject1 - The first Game Object involved in the overlap. This is the parent of `body1`.
22+
* @param {Phaser.GameObjects.GameObject} gameObject2 - The second Game Object involved in the overlap. This is the parent of `body2`.
23+
* @param {Phaser.Physics.Arcade.Body|Phaser.Physics.Arcade.StaticBody} body1 - The first Physics Body involved in the overlap.
24+
* @param {Phaser.Physics.Arcade.Body|Phaser.Physics.Arcade.StaticBody} body2 - The second Physics Body involved in the overlap.
25+
*/
26+
module.exports = 'overlap';
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/**
2+
* @author Richard Davey <rich@photonstorm.com>
3+
* @copyright 2019 Photon Storm Ltd.
4+
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
5+
*/
6+
7+
/**
8+
* The Arcade Physics World Pause Event.
9+
*
10+
* This event is dispatched by an Arcade Physics World instance when it is paused.
11+
*
12+
* Listen to it from a Scene using: `this.physics.world.on('pause', listener)`.
13+
*
14+
* @event Phaser.Physics.Arcade.Events#PAUSE
15+
*/
16+
module.exports = 'pause';
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/**
2+
* @author Richard Davey <rich@photonstorm.com>
3+
* @copyright 2019 Photon Storm Ltd.
4+
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
5+
*/
6+
7+
/**
8+
* The Arcade Physics World Resume Event.
9+
*
10+
* This event is dispatched by an Arcade Physics World instance when it resumes from a paused state.
11+
*
12+
* Listen to it from a Scene using: `this.physics.world.on('resume', listener)`.
13+
*
14+
* @event Phaser.Physics.Arcade.Events#RESUME
15+
*/
16+
module.exports = 'resume';
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/**
2+
* @author Richard Davey <rich@photonstorm.com>
3+
* @copyright 2019 Photon Storm Ltd.
4+
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
5+
*/
6+
7+
/**
8+
* The Arcade Physics Tile Collide Event.
9+
*
10+
* This event is dispatched by an Arcade Physics World instance if a body collides with a Tile _and_
11+
* has its [onCollide]{Phaser.Physics.Arcade.Body#onCollide} property set to `true`.
12+
*
13+
* It provides an alternative means to handling collide events rather than using the callback approach.
14+
*
15+
* Listen to it from a Scene using: `this.physics.world.on('tilecollide', listener)`.
16+
*
17+
* Please note that 'collide' and 'overlap' are two different things in Arcade Physics.
18+
*
19+
* @event Phaser.Physics.Arcade.Events#TILE_COLLIDE
20+
*
21+
* @param {Phaser.GameObjects.GameObject} gameObject - The Game Object involved in the collision. This is the parent of `body`.
22+
* @param {Phaser.Tilemaps.Tile} tile - The tile the body collided with.
23+
* @param {Phaser.Physics.Arcade.Body} body - The Arcade Physics Body of the Game Object involved in the collision.
24+
*/
25+
module.exports = 'tilecollide';
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/**
2+
* @author Richard Davey <rich@photonstorm.com>
3+
* @copyright 2019 Photon Storm Ltd.
4+
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
5+
*/
6+
7+
/**
8+
* The Arcade Physics Tile Overlap Event.
9+
*
10+
* This event is dispatched by an Arcade Physics World instance if a body overlaps with a Tile _and_
11+
* has its [onOverlap]{Phaser.Physics.Arcade.Body#onOverlap} property set to `true`.
12+
*
13+
* It provides an alternative means to handling overlap events rather than using the callback approach.
14+
*
15+
* Listen to it from a Scene using: `this.physics.world.on('tileoverlap', listener)`.
16+
*
17+
* Please note that 'collide' and 'overlap' are two different things in Arcade Physics.
18+
*
19+
* @event Phaser.Physics.Arcade.Events#TILE_OVERLAP
20+
*
21+
* @param {Phaser.GameObjects.GameObject} gameObject - The Game Object involved in the overlap. This is the parent of `body`.
22+
* @param {Phaser.Tilemaps.Tile} tile - The tile the body overlapped.
23+
* @param {Phaser.Physics.Arcade.Body} body - The Arcade Physics Body of the Game Object involved in the overlap.
24+
*/
25+
module.exports = 'tileoverlap';
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/**
2+
* @author Richard Davey <rich@photonstorm.com>
3+
* @copyright 2019 Photon Storm Ltd.
4+
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
5+
*/
6+
7+
/**
8+
* The Arcade Physics World Bounds Event.
9+
*
10+
* This event is dispatched by an Arcade Physics World instance if a body makes contact with the world bounds _and_
11+
* it has its [onWorldBounds]{Phaser.Physics.Arcade.Body#onWorldBounds} property set to `true`.
12+
*
13+
* It provides an alternative means to handling collide events rather than using the callback approach.
14+
*
15+
* Listen to it from a Scene using: `this.physics.world.on('worldbounds', listener)`.
16+
*
17+
* @event Phaser.Physics.Arcade.Events#WORLD_BOUNDS
18+
*
19+
* @param {Phaser.Physics.Arcade.Body} body - The Arcade Physics Body that hit the world bounds.
20+
* @param {boolean} up - Is the Body blocked up? I.e. collided with the top of the world bounds.
21+
* @param {boolean} down - Is the Body blocked down? I.e. collided with the bottom of the world bounds.
22+
* @param {boolean} left - Is the Body blocked left? I.e. collided with the left of the world bounds.
23+
* @param {boolean} right - Is the Body blocked right? I.e. collided with the right of the world bounds.
24+
*/
25+
module.exports = 'worldbounds';

0 commit comments

Comments
 (0)