From d41286bd4faa295aaeb99975fafe1f1c3b7ac1b9 Mon Sep 17 00:00:00 2001 From: Richard Davey Date: Thu, 17 Jan 2019 14:54:38 +0000 Subject: [PATCH] Added Impact Events. --- src/physics/impact/Solver.js | 6 ++++-- src/physics/impact/World.js | 7 +++++-- src/physics/impact/events/COLLIDE_EVENT.js | 20 ++++++++++++++++++++ src/physics/impact/events/PAUSE_EVENT.js | 16 ++++++++++++++++ src/physics/impact/events/RESUME_EVENT.js | 16 ++++++++++++++++ src/physics/impact/events/index.js | 17 +++++++++++++++++ src/physics/impact/index.js | 1 + 7 files changed, 79 insertions(+), 4 deletions(-) create mode 100644 src/physics/impact/events/COLLIDE_EVENT.js create mode 100644 src/physics/impact/events/PAUSE_EVENT.js create mode 100644 src/physics/impact/events/RESUME_EVENT.js create mode 100644 src/physics/impact/events/index.js diff --git a/src/physics/impact/Solver.js b/src/physics/impact/Solver.js index de7302cea..d737723af 100644 --- a/src/physics/impact/Solver.js +++ b/src/physics/impact/Solver.js @@ -5,6 +5,7 @@ */ var COLLIDES = require('./COLLIDES'); +var Events = require('./events'); var SeperateX = require('./SeperateX'); var SeperateY = require('./SeperateY'); @@ -12,6 +13,7 @@ var SeperateY = require('./SeperateY'); * Impact Physics Solver * * @function Phaser.Physics.Impact.Solver + * @fires Phaser.Physics.Impact.Events#COLLIDE * @since 3.0.0 * * @param {Phaser.Physics.Impact.World} world - The Impact simulation to run the solver in. @@ -45,7 +47,7 @@ var Solver = function (world, bodyA, bodyB) bodyA.collideWith(bodyB, 'y'); bodyB.collideWith(bodyA, 'y'); - world.emit('collide', bodyA, bodyB, 'y'); + world.emit(Events.COLLIDE, bodyA, bodyB, 'y'); } else if (bodyA.last.y + bodyA.size.y > bodyB.last.y && bodyA.last.y < bodyB.last.y + bodyB.size.y) { @@ -61,7 +63,7 @@ var Solver = function (world, bodyA, bodyB) bodyA.collideWith(bodyB, 'x'); bodyB.collideWith(bodyA, 'x'); - world.emit('collide', bodyA, bodyB, 'x'); + world.emit(Events.COLLIDE, bodyA, bodyB, 'x'); } }; diff --git a/src/physics/impact/World.js b/src/physics/impact/World.js index 1fc0578ae..b47a0a264 100644 --- a/src/physics/impact/World.js +++ b/src/physics/impact/World.js @@ -9,6 +9,7 @@ var Class = require('../../utils/Class'); var COLLIDES = require('./COLLIDES'); var CollisionMap = require('./CollisionMap'); var EventEmitter = require('eventemitter3'); +var Events = require('./events'); var GetFastValue = require('../../utils/object/GetFastValue'); var HasValue = require('../../utils/object/HasValue'); var Set = require('../../structs/Set'); @@ -556,6 +557,7 @@ var World = new Class({ * [description] * * @method Phaser.Physics.Impact.World#pause + * @fires Phaser.Physics.Impact.Events#PAUSE * @since 3.0.0 * * @return {Phaser.Physics.Impact.World} This World object. @@ -564,7 +566,7 @@ var World = new Class({ { this.enabled = false; - this.emit('pause'); + this.emit(Events.PAUSE); return this; }, @@ -573,6 +575,7 @@ var World = new Class({ * [description] * * @method Phaser.Physics.Impact.World#resume + * @fires Phaser.Physics.Impact.Events#RESUME * @since 3.0.0 * * @return {Phaser.Physics.Impact.World} This World object. @@ -581,7 +584,7 @@ var World = new Class({ { this.enabled = true; - this.emit('resume'); + this.emit(Events.RESUME); return this; }, diff --git a/src/physics/impact/events/COLLIDE_EVENT.js b/src/physics/impact/events/COLLIDE_EVENT.js new file mode 100644 index 000000000..d6e7b5aeb --- /dev/null +++ b/src/physics/impact/events/COLLIDE_EVENT.js @@ -0,0 +1,20 @@ +/** + * @author Richard Davey + * @copyright 2019 Photon Storm Ltd. + * @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License} + */ + +/** + * The Impact Physics World Collide Event. + * + * This event is dispatched by an Impact Physics World instance if two bodies collide. + * + * Listen to it from a Scene using: `this.impact.world.on('collide', listener)`. + * + * @event Phaser.Physics.Impact.Events#COLLIDE + * + * @param {Phaser.Physics.Impact.Body} bodyA - The first body involved in the collision. + * @param {Phaser.Physics.Impact.Body} bodyB - The second body involved in the collision. + * @param {string} axis - The collision axis. Either `x` or `y`. + */ +module.exports = 'collide'; diff --git a/src/physics/impact/events/PAUSE_EVENT.js b/src/physics/impact/events/PAUSE_EVENT.js new file mode 100644 index 000000000..5ce405000 --- /dev/null +++ b/src/physics/impact/events/PAUSE_EVENT.js @@ -0,0 +1,16 @@ +/** + * @author Richard Davey + * @copyright 2019 Photon Storm Ltd. + * @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License} + */ + +/** + * The Impact Physics World Pause Event. + * + * This event is dispatched by an Impact Physics World instance when it is paused. + * + * Listen to it from a Scene using: `this.impact.world.on('pause', listener)`. + * + * @event Phaser.Physics.Impact.Events#PAUSE + */ +module.exports = 'pause'; diff --git a/src/physics/impact/events/RESUME_EVENT.js b/src/physics/impact/events/RESUME_EVENT.js new file mode 100644 index 000000000..41698324a --- /dev/null +++ b/src/physics/impact/events/RESUME_EVENT.js @@ -0,0 +1,16 @@ +/** + * @author Richard Davey + * @copyright 2019 Photon Storm Ltd. + * @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License} + */ + +/** + * The Impact Physics World Resume Event. + * + * This event is dispatched by an Impact Physics World instance when it resumes from a paused state. + * + * Listen to it from a Scene using: `this.impact.world.on('resume', listener)`. + * + * @event Phaser.Physics.Impact.Events#RESUME + */ +module.exports = 'resume'; diff --git a/src/physics/impact/events/index.js b/src/physics/impact/events/index.js new file mode 100644 index 000000000..27f8a88c6 --- /dev/null +++ b/src/physics/impact/events/index.js @@ -0,0 +1,17 @@ +/** + * @author Richard Davey + * @copyright 2019 Photon Storm Ltd. + * @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License} + */ + +/** + * @namespace Phaser.Physics.Impact.Events + */ + +module.exports = { + + COLLIDE: require('./COLLIDE_EVENT'), + PAUSE: require('./PAUSE_EVENT'), + RESUME: require('./RESUME_EVENT') + +}; diff --git a/src/physics/impact/index.js b/src/physics/impact/index.js index 2b22a9e95..85de24de3 100644 --- a/src/physics/impact/index.js +++ b/src/physics/impact/index.js @@ -22,6 +22,7 @@ module.exports = { Body: require('./Body'), + Events: require('./events'), COLLIDES: require('./COLLIDES'), CollisionMap: require('./CollisionMap'), Factory: require('./Factory'),