2014-03-05 02:36:08 +00:00
|
|
|
/**
|
|
|
|
* @author Richard Davey <rich@photonstorm.com>
|
|
|
|
* @copyright 2014 Photon Storm Ltd.
|
|
|
|
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2014-03-06 06:27:16 +00:00
|
|
|
* The Physics Manager is responsible for looking after all of the running physics systems.
|
|
|
|
* Phaser supports 3 physics systems: Arcade Physics, P2 and Ninja Physics (with Box2D and Chipmunk in development)
|
|
|
|
* Game Objects can belong to only 1 physics system, but you can have multiple systems active in a single game.
|
2014-03-05 02:36:08 +00:00
|
|
|
*
|
2014-03-06 06:27:16 +00:00
|
|
|
* For example you could have P2 managing a polygon-built terrain landscape that an vehicle drives over, while it could be firing bullets that use the
|
|
|
|
* faster (due to being much simpler) Arcade Physics system.
|
2014-03-05 02:36:08 +00:00
|
|
|
*
|
2014-03-06 06:27:16 +00:00
|
|
|
* @class Phaser.Physics
|
2014-03-05 02:36:08 +00:00
|
|
|
*
|
|
|
|
* @constructor
|
|
|
|
* @param {Phaser.Game} game - A reference to the currently running game.
|
|
|
|
* @param {object} [physicsConfig=null] - A physics configuration object to pass to the Physics world on creation.
|
|
|
|
*/
|
|
|
|
Phaser.Physics = function (game, config) {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @property {Phaser.Game} game - Local reference to game.
|
|
|
|
*/
|
|
|
|
this.game = game;
|
|
|
|
|
2014-03-06 06:27:16 +00:00
|
|
|
/**
|
|
|
|
* @property {object} config - The physics configuration object as passed to the game on creation.
|
|
|
|
*/
|
2014-03-05 02:36:08 +00:00
|
|
|
this.config = config;
|
|
|
|
|
2014-03-06 06:27:16 +00:00
|
|
|
/**
|
|
|
|
* @property {Phaser.Physics.Arcade} arcade - The Arcade Physics system.
|
|
|
|
*/
|
2014-03-05 02:36:08 +00:00
|
|
|
this.arcade = new Phaser.Physics.Arcade(game);
|
|
|
|
|
2014-03-06 06:27:16 +00:00
|
|
|
/**
|
|
|
|
* @property {Phaser.Physics.P2} p2 - The P2.JS Physics system.
|
|
|
|
*/
|
2014-03-05 02:36:08 +00:00
|
|
|
this.p2 = null;
|
|
|
|
|
2014-03-06 06:27:16 +00:00
|
|
|
/**
|
|
|
|
* @property {Phaser.Physics.Ninja} ninja - The N+ Ninja Physics System.
|
|
|
|
*/
|
|
|
|
this.ninja = null;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @property {Phaser.Physics.Box2D} box2d - The Box2D Physics system (to be done).
|
|
|
|
*/
|
2014-03-05 02:36:08 +00:00
|
|
|
this.box2d = null;
|
|
|
|
|
2014-03-06 06:27:16 +00:00
|
|
|
/**
|
|
|
|
* @property {Phaser.Physics.Chipmunk} chipmunk - The Chipmunk Physics system (to be done).
|
|
|
|
*/
|
2014-03-05 02:36:08 +00:00
|
|
|
this.chipmunk = null;
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @const
|
|
|
|
* @type {number}
|
|
|
|
*/
|
|
|
|
Phaser.Physics.ARCADE = 0;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @const
|
|
|
|
* @type {number}
|
|
|
|
*/
|
2014-03-10 22:14:52 +00:00
|
|
|
Phaser.Physics.P2JS = 1;
|
2014-03-05 02:36:08 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @const
|
|
|
|
* @type {number}
|
|
|
|
*/
|
2014-03-06 06:27:16 +00:00
|
|
|
Phaser.Physics.NINJA = 2;
|
2014-03-05 02:36:08 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @const
|
|
|
|
* @type {number}
|
|
|
|
*/
|
2014-03-06 06:27:16 +00:00
|
|
|
Phaser.Physics.BOX2D = 3;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @const
|
|
|
|
* @type {number}
|
|
|
|
*/
|
|
|
|
Phaser.Physics.CHIPMUNK = 5;
|
2014-03-05 02:36:08 +00:00
|
|
|
|
|
|
|
Phaser.Physics.prototype = {
|
|
|
|
|
2014-03-06 06:27:16 +00:00
|
|
|
/**
|
|
|
|
* This will create an instance of the requested physics simulation.
|
|
|
|
* Phaser.Physics.Arcade is running by default, but all others need activating directly.
|
|
|
|
* You can start the following physics systems:
|
2014-03-10 22:14:52 +00:00
|
|
|
* Phaser.Physics.P2JS - A full-body advanced physics system by Stefan Hedman.
|
2014-03-06 09:48:42 +00:00
|
|
|
* Phaser.Physics.NINJA - A port of Metanet Softwares N+ physics system.
|
2014-03-06 06:27:16 +00:00
|
|
|
* Phaser.Physics.BOX2D and Phaser.Physics.CHIPMUNK are still in development.
|
|
|
|
*
|
|
|
|
* @method Phaser.Physics#startSystem
|
|
|
|
* @param {number} The physics system to start.
|
|
|
|
*/
|
2014-03-05 02:36:08 +00:00
|
|
|
startSystem: function (system) {
|
|
|
|
|
2014-03-06 06:27:16 +00:00
|
|
|
if (system === Phaser.Physics.ARCADE && this.arcade === null)
|
2014-03-05 02:36:08 +00:00
|
|
|
{
|
|
|
|
this.arcade = new Phaser.Physics.Arcade(this.game);
|
|
|
|
}
|
2014-03-10 22:14:52 +00:00
|
|
|
else if (system === Phaser.Physics.P2JS && this.p2 === null)
|
2014-03-05 02:36:08 +00:00
|
|
|
{
|
|
|
|
this.p2 = new Phaser.Physics.P2(this.game, this.config);
|
|
|
|
}
|
2014-03-06 06:27:16 +00:00
|
|
|
if (system === Phaser.Physics.NINJA && this.ninja === null)
|
|
|
|
{
|
|
|
|
this.ninja = new Phaser.Physics.Ninja(this.game);
|
|
|
|
}
|
|
|
|
else if (system === Phaser.Physics.BOX2D && this.box2d === null)
|
2014-03-05 02:36:08 +00:00
|
|
|
{
|
|
|
|
// Coming soon
|
|
|
|
}
|
2014-03-06 06:27:16 +00:00
|
|
|
else if (system === Phaser.Physics.CHIPMUNK && this.chipmunk === null)
|
2014-03-05 02:36:08 +00:00
|
|
|
{
|
|
|
|
// Coming soon
|
|
|
|
}
|
|
|
|
|
|
|
|
},
|
|
|
|
|
2014-03-06 06:27:16 +00:00
|
|
|
/**
|
|
|
|
* This will create a physics body on the given game object.
|
|
|
|
* A game object can only have 1 physics body active at any one time, and it can't be changed until the object is destroyed.
|
|
|
|
* It can be for any of the physics systems that have been started:
|
|
|
|
*
|
|
|
|
* Phaser.Physics.Arcade - A light weight AABB based collision system with basic separation.
|
2014-03-10 22:14:52 +00:00
|
|
|
* Phaser.Physics.P2JS - A full-body advanced physics system supporting multiple object shapes, polygon loading, contact materials, springs and constraints.
|
2014-03-06 09:48:42 +00:00
|
|
|
* Phaser.Physics.NINJA - A port of Metanet Softwares N+ physics system. Advanced AABB and Circle vs. Tile collision.
|
2014-03-06 06:27:16 +00:00
|
|
|
* Phaser.Physics.BOX2D and Phaser.Physics.CHIPMUNK are still in development.
|
|
|
|
*
|
|
|
|
* @method Phaser.Physics#enable
|
|
|
|
* @param {object|array} object - The game object to create the physics body on. Can also be an array of objects, a body will be created on every object in the array.
|
2014-03-07 15:14:53 +00:00
|
|
|
* @param {number} [system=Phaser.Physics.ARCADE] - The physics system that will be used to create the body. Defaults to Arcade Physics.
|
2014-03-10 13:28:44 +00:00
|
|
|
* @param {boolean} [debug=false] - Enable the debug drawing for this body. Defaults to false.
|
2014-03-06 06:27:16 +00:00
|
|
|
*/
|
2014-03-10 13:28:44 +00:00
|
|
|
enable: function (object, system, debug) {
|
2014-03-05 02:36:08 +00:00
|
|
|
|
|
|
|
if (typeof system === 'undefined') { system = Phaser.Physics.ARCADE; }
|
2014-03-10 13:28:44 +00:00
|
|
|
if (typeof debug === 'undefined') { debug = false; }
|
2014-03-05 02:36:08 +00:00
|
|
|
|
|
|
|
var i = 1;
|
|
|
|
|
2014-03-07 15:14:53 +00:00
|
|
|
if (object instanceof Phaser.Group)
|
2014-03-05 02:36:08 +00:00
|
|
|
{
|
2014-03-07 15:14:53 +00:00
|
|
|
|
2014-03-05 02:36:08 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-03-07 15:14:53 +00:00
|
|
|
if (Array.isArray(object))
|
|
|
|
{
|
|
|
|
// Add to Group
|
|
|
|
i = object.length;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
object = [object];
|
|
|
|
}
|
2014-03-05 02:36:08 +00:00
|
|
|
|
2014-03-07 15:14:53 +00:00
|
|
|
while (i--)
|
2014-03-05 02:36:08 +00:00
|
|
|
{
|
2014-03-07 15:14:53 +00:00
|
|
|
if (object[i].body === null)
|
2014-03-06 06:27:16 +00:00
|
|
|
{
|
2014-03-07 15:14:53 +00:00
|
|
|
if (system === Phaser.Physics.ARCADE)
|
|
|
|
{
|
|
|
|
object[i].body = new Phaser.Physics.Arcade.Body(object[i]);
|
|
|
|
}
|
2014-03-10 22:14:52 +00:00
|
|
|
else if (system === Phaser.Physics.P2JS)
|
2014-03-07 15:14:53 +00:00
|
|
|
{
|
|
|
|
object[i].body = new Phaser.Physics.P2.Body(this.game, object[i], object[i].x, object[i].y, 1);
|
2014-03-10 13:28:44 +00:00
|
|
|
object[i].body.debug = debug
|
2014-03-07 15:14:53 +00:00
|
|
|
object[i].anchor.set(0.5);
|
|
|
|
}
|
|
|
|
else if (system === Phaser.Physics.NINJA)
|
|
|
|
{
|
|
|
|
object[i].body = new Phaser.Physics.Ninja.Body(this.ninja, object[i]);
|
|
|
|
object[i].anchor.set(0.5);
|
|
|
|
}
|
2014-03-06 06:27:16 +00:00
|
|
|
}
|
2014-03-05 02:36:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
},
|
|
|
|
|
2014-03-06 06:27:16 +00:00
|
|
|
/**
|
|
|
|
* Updates all running physics systems.
|
|
|
|
*
|
|
|
|
* @method Phaser.Physics#update
|
|
|
|
* @protected
|
|
|
|
*/
|
2014-03-05 02:36:08 +00:00
|
|
|
update: function () {
|
|
|
|
|
2014-03-06 06:27:16 +00:00
|
|
|
// ArcadePhysics / Ninja don't have a core to update
|
2014-03-05 02:36:08 +00:00
|
|
|
|
|
|
|
if (this.p2)
|
|
|
|
{
|
|
|
|
this.p2.update();
|
|
|
|
}
|
|
|
|
|
|
|
|
},
|
|
|
|
|
2014-03-06 06:27:16 +00:00
|
|
|
/**
|
|
|
|
* Updates the physics bounds to match the world dimensions.
|
|
|
|
*
|
|
|
|
* @method Phaser.Physics#setBoundsToWorld
|
|
|
|
* @protected
|
|
|
|
*/
|
2014-03-05 02:36:08 +00:00
|
|
|
setBoundsToWorld: function () {
|
|
|
|
|
2014-03-06 09:48:42 +00:00
|
|
|
if (this.ninja)
|
|
|
|
{
|
|
|
|
this.ninja.setBoundsToWorld();
|
|
|
|
}
|
|
|
|
|
2014-03-05 02:36:08 +00:00
|
|
|
},
|
|
|
|
|
2014-03-06 06:27:16 +00:00
|
|
|
/**
|
|
|
|
* Clears down all active physics systems. This doesn't destroy them, it just clears them of objects and is called when the State changes.
|
|
|
|
*
|
|
|
|
* @method Phaser.Physics#clear
|
|
|
|
* @protected
|
|
|
|
*/
|
2014-03-05 02:36:08 +00:00
|
|
|
clear: function () {
|
|
|
|
|
|
|
|
if (this.p2)
|
|
|
|
{
|
|
|
|
this.p2.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
Phaser.Physics.prototype.constructor = Phaser.Physics;
|