phaser/src/physics/arcade/Collider.js

178 lines
5 KiB
JavaScript
Raw Normal View History

2018-02-12 16:01:20 +00:00
/**
* @author Richard Davey <rich@photonstorm.com>
2020-01-15 12:07:09 +00:00
* @copyright 2020 Photon Storm Ltd.
2019-05-10 15:15:04 +00:00
* @license {@link https://opensource.org/licenses/MIT|MIT License}
2018-02-12 16:01:20 +00:00
*/
var Class = require('../../utils/Class');
2018-02-09 03:44:23 +00:00
/**
* @classdesc
2018-10-19 16:45:05 +00:00
* An Arcade Physics Collider will automatically check for collision, or overlaps, between two objects
* every step. If a collision, or overlap, occurs it will invoke the given callbacks.
2018-02-09 03:44:23 +00:00
*
* @class Collider
2018-10-10 09:49:13 +00:00
* @memberof Phaser.Physics.Arcade
2018-02-09 03:44:23 +00:00
* @constructor
* @since 3.0.0
*
2018-10-19 11:32:43 +00:00
* @param {Phaser.Physics.Arcade.World} world - The Arcade physics World that will manage the collisions.
2018-10-19 16:45:05 +00:00
* @param {boolean} overlapOnly - Whether to check for collisions or overlap.
2019-05-09 11:33:37 +00:00
* @param {Phaser.Types.Physics.Arcade.ArcadeColliderType} object1 - The first object to check for collision.
* @param {Phaser.Types.Physics.Arcade.ArcadeColliderType} object2 - The second object to check for collision.
2018-03-19 20:42:07 +00:00
* @param {ArcadePhysicsCallback} collideCallback - The callback to invoke when the two objects collide.
* @param {ArcadePhysicsCallback} processCallback - The callback to invoke when the two objects collide. Must return a boolean.
* @param {any} callbackContext - The scope in which to call the callbacks.
2018-02-09 03:44:23 +00:00
*/
var Collider = new Class({
initialize:
function Collider (world, overlapOnly, object1, object2, collideCallback, processCallback, callbackContext)
{
2018-02-09 03:44:23 +00:00
/**
2018-10-19 11:32:43 +00:00
* The world in which the bodies will collide.
2018-02-09 03:44:23 +00:00
*
* @name Phaser.Physics.Arcade.Collider#world
* @type {Phaser.Physics.Arcade.World}
* @since 3.0.0
*/
this.world = world;
/**
* The name of the collider (unused by Phaser).
*
* @name Phaser.Physics.Arcade.Collider#name
* @type {string}
2018-02-15 14:31:15 +00:00
* @since 3.1.0
*/
this.name = '';
2018-02-09 03:44:23 +00:00
/**
2018-10-19 16:45:05 +00:00
* Whether the collider is active.
2018-02-09 03:44:23 +00:00
*
* @name Phaser.Physics.Arcade.Collider#active
* @type {boolean}
* @default true
* @since 3.0.0
*/
this.active = true;
2018-02-09 03:44:23 +00:00
/**
2018-10-19 16:45:05 +00:00
* Whether to check for collisions or overlaps.
2018-02-09 03:44:23 +00:00
*
* @name Phaser.Physics.Arcade.Collider#overlapOnly
* @type {boolean}
* @since 3.0.0
*/
this.overlapOnly = overlapOnly;
2018-02-09 03:44:23 +00:00
/**
2018-10-19 11:32:43 +00:00
* The first object to check for collision.
2018-02-09 03:44:23 +00:00
*
* @name Phaser.Physics.Arcade.Collider#object1
2019-05-09 11:33:37 +00:00
* @type {Phaser.Types.Physics.Arcade.ArcadeColliderType}
2018-02-09 03:44:23 +00:00
* @since 3.0.0
*/
this.object1 = object1;
2018-02-09 03:44:23 +00:00
/**
2018-10-19 11:32:43 +00:00
* The second object to check for collision.
2018-02-09 03:44:23 +00:00
*
* @name Phaser.Physics.Arcade.Collider#object2
2019-05-09 11:33:37 +00:00
* @type {Phaser.Types.Physics.Arcade.ArcadeColliderType}
2018-02-09 03:44:23 +00:00
* @since 3.0.0
*/
this.object2 = object2;
2018-02-09 03:44:23 +00:00
/**
2018-10-19 11:32:43 +00:00
* The callback to invoke when the two objects collide.
2018-02-09 03:44:23 +00:00
*
* @name Phaser.Physics.Arcade.Collider#collideCallback
2018-03-19 20:42:07 +00:00
* @type {ArcadePhysicsCallback}
2018-02-09 03:44:23 +00:00
* @since 3.0.0
*/
this.collideCallback = collideCallback;
2018-02-09 03:44:23 +00:00
/**
2018-10-19 11:32:43 +00:00
* If a processCallback exists it must return true or collision checking will be skipped.
2018-02-09 03:44:23 +00:00
*
* @name Phaser.Physics.Arcade.Collider#processCallback
2018-03-19 20:42:07 +00:00
* @type {ArcadePhysicsCallback}
2018-02-09 03:44:23 +00:00
* @since 3.0.0
*/
this.processCallback = processCallback;
2018-02-09 03:44:23 +00:00
/**
2018-10-19 11:32:43 +00:00
* The context the collideCallback and processCallback will run in.
2018-02-09 03:44:23 +00:00
*
* @name Phaser.Physics.Arcade.Collider#callbackContext
* @type {object}
* @since 3.0.0
*/
this.callbackContext = callbackContext;
},
/**
2018-10-19 16:45:05 +00:00
* A name for the Collider.
2020-10-15 10:07:27 +00:00
*
2018-10-19 16:45:05 +00:00
* Phaser does not use this value, it's for your own reference.
*
* @method Phaser.Physics.Arcade.Collider#setName
2018-02-15 14:31:15 +00:00
* @since 3.1.0
*
2018-10-19 11:32:43 +00:00
* @param {string} name - The name to assign to the Collider.
*
2018-10-19 16:45:05 +00:00
* @return {Phaser.Physics.Arcade.Collider} This Collider instance.
*/
setName: function (name)
{
this.name = name;
return this;
},
2018-02-09 03:44:23 +00:00
/**
2018-10-19 11:32:43 +00:00
* Called by World as part of its step processing, initial operation of collision checking.
2018-02-09 03:44:23 +00:00
*
* @method Phaser.Physics.Arcade.Collider#update
* @since 3.0.0
*/
update: function ()
{
this.world.collideObjects(
this.object1,
this.object2,
this.collideCallback,
this.processCallback,
this.callbackContext,
this.overlapOnly
);
},
2018-02-09 03:44:23 +00:00
/**
2018-10-19 11:32:43 +00:00
* Removes Collider from World and disposes of its resources.
2018-02-09 03:44:23 +00:00
*
* @method Phaser.Physics.Arcade.Collider#destroy
* @since 3.0.0
*/
destroy: function ()
{
this.world.removeCollider(this);
this.active = false;
this.world = null;
this.object1 = null;
this.object2 = null;
this.collideCallback = null;
this.processCallback = null;
this.callbackContext = null;
}
});
module.exports = Collider;