Add bounce event to arcade physics Fix #6406

This commit is contained in:
Robert Kowalski 2024-02-20 17:16:32 -05:00
parent 450b0d244f
commit 542bac2d02
5 changed files with 61 additions and 0 deletions

View file

@ -486,6 +486,16 @@ var Body = new Class({
*/
this.onOverlap = false;
/**
* Whether the simulation emits an `bounce` event when this Body bounces.
*
* @name Phaser.Physics.Arcade.StaticBody#onBounce
* @type {boolean}
* @default false
* @since 3.80.0
*/
this.onBounce = false;
/**
* The absolute maximum velocity of this body, in pixels per second.
* The horizontal and vertical components are applied separately.
@ -1222,6 +1232,11 @@ var Body = new Class({
var blocked = this.blocked;
this.world.emit(Events.WORLD_BOUNDS, this, blocked.up, blocked.down, blocked.left, blocked.right);
if (this.onBounce && this.bounce.length())
{
this.emit(Events.BOUNCE, this);
}
}
},

View file

@ -295,6 +295,17 @@ var StaticBody = new Class({
*/
this.onOverlap = false;
/**
* Whether the simulation emits an `bounce` event when this StaticBody bounces.
* Always false for a Static Body. (Static Bodies never bounce and never trigger a `bounce` event.)
*
* @name Phaser.Physics.Arcade.StaticBody#onBounce
* @type {boolean}
* @default false
* @since 3.80.0
*/
this.onBounce = false;
/**
* The StaticBody's inertia, relative to a default unit (1). With `bounce`, this affects the exchange of momentum (velocities) during collisions.
*

View file

@ -1469,6 +1469,16 @@ var World = new Class({
else if (body1.onCollide || body2.onCollide)
{
this.emit(Events.COLLIDE, body1.gameObject, body2.gameObject, body1, body2);
if (body1.onBounce && body1.bounce.length())
{
this.emit(Events.BOUNCE, body1);
}
if (body2.onBounce && body2.bounce.length())
{
this.emit(Events.BOUNCE, body2);
}
}
}

View file

@ -0,0 +1,24 @@
/**
* @author Richard Davey <rich@phaser.io>
* @copyright 2013-2024 Phaser Studio Inc.
* @license {@link https://opensource.org/licenses/MIT|MIT License}
*/
/**
* The Arcade Physics World Bounce Event.
*
* This event is dispatched by an Arcade Physics World instance if a body collides with a body or world bounds
* _and_ it has its [onBounce]{@link Phaser.Physics.Arcade.Body#onBounce} property set to `true` while having
* a bounce magnitude above 0.
*
* It provides an alternative means to handling collide events rather than using the callback approach.
*
* Listen to it from a Scene using: `this.physics.world.on('bounce', listener)`.
*
* @event Phaser.Physics.Arcade.Events#BOUNCE
* @type {string}
* @since 3.80.0
*
* @param {Phaser.Physics.Arcade.Body} body - The Arcade Physics Body that bounced.
*/
module.exports = 'bounce';

View file

@ -10,6 +10,7 @@
module.exports = {
BOUNCE: require('./BOUNCE_EVENT'),
COLLIDE: require('./COLLIDE_EVENT'),
OVERLAP: require('./OVERLAP_EVENT'),
PAUSE: require('./PAUSE_EVENT'),