Skip to content

Commit 8b35743

Browse files
committed
Matter Events added
1 parent 99cd025 commit 8b35743

12 files changed

Lines changed: 262 additions & 9 deletions

src/physics/matter-js/World.js

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ var Common = require('./lib/core/Common');
1010
var Composite = require('./lib/body/Composite');
1111
var Engine = require('./lib/core/Engine');
1212
var EventEmitter = require('eventemitter3');
13+
var Events = require('./events');
1314
var GetFastValue = require('../../utils/object/GetFastValue');
1415
var GetValue = require('../../utils/object/GetValue');
1516
var MatterBody = require('./lib/body/Body');
@@ -238,12 +239,12 @@ var World = new Class({
238239

239240
MatterEvents.on(engine, 'beforeUpdate', function (event)
240241
{
241-
_this.emit('beforeupdate', event);
242+
_this.emit(Events.BEFORE_UPDATE, event);
242243
});
243244

244245
MatterEvents.on(engine, 'afterUpdate', function (event)
245246
{
246-
_this.emit('afterupdate', event);
247+
_this.emit(Events.AFTER_UPDATE, event);
247248
});
248249

249250
MatterEvents.on(engine, 'collisionStart', function (event)
@@ -258,7 +259,7 @@ var World = new Class({
258259
bodyB = pairs[0].bodyB;
259260
}
260261

261-
_this.emit('collisionstart', event, bodyA, bodyB);
262+
_this.emit(Events.COLLISION_START, event, bodyA, bodyB);
262263
});
263264

264265
MatterEvents.on(engine, 'collisionActive', function (event)
@@ -273,7 +274,7 @@ var World = new Class({
273274
bodyB = pairs[0].bodyB;
274275
}
275276

276-
_this.emit('collisionactive', event, bodyA, bodyB);
277+
_this.emit(Events.COLLISION_ACTIVE, event, bodyA, bodyB);
277278
});
278279

279280
MatterEvents.on(engine, 'collisionEnd', function (event)
@@ -288,7 +289,7 @@ var World = new Class({
288289
bodyB = pairs[0].bodyB;
289290
}
290291

291-
_this.emit('collisionend', event, bodyA, bodyB);
292+
_this.emit(Events.COLLISION_END, event, bodyA, bodyB);
292293
});
293294
},
294295

@@ -603,6 +604,7 @@ var World = new Class({
603604
* [description]
604605
*
605606
* @method Phaser.Physics.Matter.World#pause
607+
* @fires Phaser.Physics.Matter.Events#PAUSE
606608
* @since 3.0.0
607609
*
608610
* @return {Phaser.Physics.Matter.World} This Matter World object.
@@ -611,7 +613,7 @@ var World = new Class({
611613
{
612614
this.enabled = false;
613615

614-
this.emit('pause');
616+
this.emit(Events.PAUSE);
615617

616618
return this;
617619
},
@@ -620,6 +622,7 @@ var World = new Class({
620622
* [description]
621623
*
622624
* @method Phaser.Physics.Matter.World#resume
625+
* @fires Phaser.Physics.Matter.Events#RESUME
623626
* @since 3.0.0
624627
*
625628
* @return {Phaser.Physics.Matter.World} This Matter World object.
@@ -628,7 +631,7 @@ var World = new Class({
628631
{
629632
this.enabled = true;
630633

631-
this.emit('resume');
634+
this.emit(Events.RESUME);
632635

633636
return this;
634637
},

src/physics/matter-js/components/Sleep.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
55
*/
66

7+
var Events = require('../events');
78
var MatterEvents = require('../lib/core/Events');
89

910
/**
@@ -70,7 +71,7 @@ var Sleep = {
7071

7172
MatterEvents.on(this.body, 'sleepStart', function (event)
7273
{
73-
world.emit('sleepstart', event, this);
74+
world.emit(Events.SLEEP_START, event, this);
7475
});
7576
}
7677
else
@@ -99,7 +100,7 @@ var Sleep = {
99100

100101
MatterEvents.on(this.body, 'sleepEnd', function (event)
101102
{
102-
world.emit('sleepend', event, this);
103+
world.emit(Events.SLEEP_END, event, this);
103104
});
104105
}
105106
else
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+
* @typedef {object} Phaser.Physics.Matter.Events.AfterUpdateEvent
9+
*
10+
* @property {number} timestamp - The Matter Engine `timing.timestamp` value for the event.
11+
* @property {any} source - The source object of the event.
12+
* @property {string} name - The name of the event.
13+
*/
14+
15+
/**
16+
* The Matter Physics After Update Event.
17+
*
18+
* This event is dispatched by a Matter Physics World instance after the engine has updated and all collision events have resolved.
19+
*
20+
* Listen to it from a Scene using: `this.matter.world.on('afterupdate', listener)`.
21+
*
22+
* @event Phaser.Physics.Matter.Events#AFTER_UPDATE
23+
*
24+
* @param {Phaser.Physics.Matter.Events.AfterUpdateEvent} event - The Update Event object.
25+
*/
26+
module.exports = 'afterupdate';
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+
* @typedef {object} Phaser.Physics.Matter.Events.BeforeUpdateEvent
9+
*
10+
* @property {number} timestamp - The Matter Engine `timing.timestamp` value for the event.
11+
* @property {any} source - The source object of the event.
12+
* @property {string} name - The name of the event.
13+
*/
14+
15+
/**
16+
* The Matter Physics Before Update Event.
17+
*
18+
* This event is dispatched by a Matter Physics World instance right before all the collision processing takes place.
19+
*
20+
* Listen to it from a Scene using: `this.matter.world.on('beforeupdate', listener)`.
21+
*
22+
* @event Phaser.Physics.Matter.Events#BEFORE_UPDATE
23+
*
24+
* @param {Phaser.Physics.Matter.Events.BeforeUpdateEvent} event - The Update Event object.
25+
*/
26+
module.exports = 'beforeupdate';
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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+
* @typedef {object} Phaser.Physics.Matter.Events.CollisionActiveEvent
9+
*
10+
* @property {array} pairs - A list of all affected pairs in the collision.
11+
* @property {number} timestamp - The Matter Engine `timing.timestamp` value for the event.
12+
* @property {any} source - The source object of the event.
13+
* @property {string} name - The name of the event.
14+
*/
15+
16+
/**
17+
* The Matter Physics Collision Active Event.
18+
*
19+
* This event is dispatched by a Matter Physics World instance after the engine has updated.
20+
* It provides a list of all pairs that are colliding in the current tick (if any).
21+
*
22+
* Listen to it from a Scene using: `this.matter.world.on('collisionactive', listener)`.
23+
*
24+
* @event Phaser.Physics.Matter.Events#COLLISION_ACTIVE
25+
*
26+
* @param {Phaser.Physics.Matter.Events.CollisionActiveEvent} event - The Collision Event object.
27+
* @param {MatterJS.Body} bodyA - The first body of the first colliding pair. The `event.pairs` array may contain more colliding bodies.
28+
* @param {MatterJS.Body} bodyB - The second body of the first colliding pair. The `event.pairs` array may contain more colliding bodies.
29+
*/
30+
module.exports = 'collisionactive';
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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+
* @typedef {object} Phaser.Physics.Matter.Events.CollisionEndEvent
9+
*
10+
* @property {array} pairs - A list of all affected pairs in the collision.
11+
* @property {number} timestamp - The Matter Engine `timing.timestamp` value for the event.
12+
* @property {any} source - The source object of the event.
13+
* @property {string} name - The name of the event.
14+
*/
15+
16+
/**
17+
* The Matter Physics Collision End Event.
18+
*
19+
* This event is dispatched by a Matter Physics World instance after the engine has updated.
20+
* It provides a list of all pairs that have finished colliding in the current tick (if any).
21+
*
22+
* Listen to it from a Scene using: `this.matter.world.on('collisionend', listener)`.
23+
*
24+
* @event Phaser.Physics.Matter.Events#COLLISION_END
25+
*
26+
* @param {Phaser.Physics.Matter.Events.CollisionEndEvent} event - The Collision Event object.
27+
* @param {MatterJS.Body} bodyA - The first body of the first colliding pair. The `event.pairs` array may contain more colliding bodies.
28+
* @param {MatterJS.Body} bodyB - The second body of the first colliding pair. The `event.pairs` array may contain more colliding bodies.
29+
*/
30+
module.exports = 'collisionend';
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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+
* @typedef {object} Phaser.Physics.Matter.Events.CollisionStartEvent
9+
*
10+
* @property {array} pairs - A list of all affected pairs in the collision.
11+
* @property {number} timestamp - The Matter Engine `timing.timestamp` value for the event.
12+
* @property {any} source - The source object of the event.
13+
* @property {string} name - The name of the event.
14+
*/
15+
16+
/**
17+
* The Matter Physics Collision Start Event.
18+
*
19+
* This event is dispatched by a Matter Physics World instance after the engine has updated.
20+
* It provides a list of all pairs that have started to collide in the current tick (if any).
21+
*
22+
* Listen to it from a Scene using: `this.matter.world.on('collisionstart', listener)`.
23+
*
24+
* @event Phaser.Physics.Matter.Events#COLLISION_START
25+
*
26+
* @param {Phaser.Physics.Matter.Events.CollisionStartEvent} event - The Collision Event object.
27+
* @param {MatterJS.Body} bodyA - The first body of the first colliding pair. The `event.pairs` array may contain more colliding bodies.
28+
* @param {MatterJS.Body} bodyB - The second body of the first colliding pair. The `event.pairs` array may contain more colliding bodies.
29+
*/
30+
module.exports = 'collisionstart';
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 Matter Physics World Pause Event.
9+
*
10+
* This event is dispatched by an Matter Physics World instance when it is paused.
11+
*
12+
* Listen to it from a Scene using: `this.matter.world.on('pause', listener)`.
13+
*
14+
* @event Phaser.Physics.Matter.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 Matter Physics World Resume Event.
9+
*
10+
* This event is dispatched by an Matter Physics World instance when it resumes from a paused state.
11+
*
12+
* Listen to it from a Scene using: `this.matter.world.on('resume', listener)`.
13+
*
14+
* @event Phaser.Physics.Matter.Events#RESUME
15+
*/
16+
module.exports = 'resume';
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+
* @typedef {object} Phaser.Physics.Matter.Events.SleepEndEvent
9+
*
10+
* @property {any} source - The source object of the event.
11+
* @property {string} name - The name of the event.
12+
*/
13+
14+
/**
15+
* The Matter Physics Sleep End Event.
16+
*
17+
* This event is dispatched by a Matter Physics World instance when a Body stop sleeping.
18+
*
19+
* Listen to it from a Scene using: `this.matter.world.on('sleepend', listener)`.
20+
*
21+
* @event Phaser.Physics.Matter.Events#SLEEP_END
22+
*
23+
* @param {Phaser.Physics.Matter.Events.SleepEndEvent} event - The Sleep Event object.
24+
* @param {MatterJS.Body} body - The body that has stopped sleeping.
25+
*/
26+
module.exports = 'sleepend';

0 commit comments

Comments
 (0)