2014-02-10 16:01:30 +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}
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @class Phaser.Physics
|
|
|
|
*/
|
|
|
|
Phaser.Physics = {};
|
|
|
|
|
2014-02-14 23:51:49 +00:00
|
|
|
/**
|
|
|
|
* @const
|
|
|
|
*/
|
|
|
|
Phaser.Physics.LIME_CORONA_JSON = 0;
|
|
|
|
|
2014-02-12 19:45:09 +00:00
|
|
|
// Add an extra property to p2.Body
|
|
|
|
p2.Body.prototype.parent = null;
|
|
|
|
|
2014-02-10 16:01:30 +00:00
|
|
|
/**
|
|
|
|
* @class Phaser.Physics.World
|
|
|
|
* @classdesc Physics World Constructor
|
|
|
|
* @constructor
|
|
|
|
* @param {Phaser.Game}
|
|
|
|
*/
|
|
|
|
Phaser.Physics.World = function (game) {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @property {Phaser.Game} game - Local reference to game.
|
|
|
|
*/
|
|
|
|
this.game = game;
|
|
|
|
|
2014-02-12 19:45:09 +00:00
|
|
|
this.onBodyAdded = new Phaser.Signal();
|
|
|
|
this.onBodyRemoved = new Phaser.Signal();
|
|
|
|
|
2014-02-10 23:28:32 +00:00
|
|
|
this.bounds = null;
|
|
|
|
|
2014-02-10 22:54:56 +00:00
|
|
|
p2.World.call(this, { gravity: [0, 0] });
|
2014-02-10 16:01:30 +00:00
|
|
|
|
2014-02-12 19:45:09 +00:00
|
|
|
this.on("addBody", this.addBodyHandler);
|
|
|
|
this.on("removeBody", this.removeBodyHandler);
|
|
|
|
this.on("postStep", this.postStepHandler);
|
|
|
|
this.on("postBroadphase", this.postBroadphaseHandler);
|
|
|
|
|
2014-02-10 23:28:32 +00:00
|
|
|
this.setBoundsToWorld(true, true, true, true);
|
|
|
|
|
2014-02-10 16:01:30 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
Phaser.Physics.World.prototype = Object.create(p2.World.prototype);
|
|
|
|
Phaser.Physics.World.prototype.constructor = Phaser.Physics.World;
|
|
|
|
|
2014-02-12 19:45:09 +00:00
|
|
|
/**
|
|
|
|
* Handles a p2 addBody event.
|
|
|
|
*
|
|
|
|
* @method Phaser.Physics.Arcade#addBodyHandler
|
|
|
|
* @private
|
|
|
|
* @param {object} event - The event data.
|
|
|
|
*/
|
|
|
|
Phaser.Physics.World.prototype.addBodyHandler = function (event) {
|
|
|
|
|
|
|
|
if (event.body.parent)
|
|
|
|
{
|
|
|
|
this.onBodyAdded.dispatch(event.body.parent, event.target);
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handles a p2 removeBody event.
|
|
|
|
*
|
|
|
|
* @method Phaser.Physics.Arcade#removeBodyHandler
|
|
|
|
* @private
|
|
|
|
* @param {object} event - The event data.
|
|
|
|
*/
|
|
|
|
Phaser.Physics.World.prototype.removeBodyHandler = function (event) {
|
|
|
|
|
|
|
|
if (event.body.parent)
|
|
|
|
{
|
|
|
|
this.onBodyRemoved.dispatch(event.body.parent, event.target);
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handles a p2 postStep event.
|
|
|
|
*
|
|
|
|
* @method Phaser.Physics.Arcade#postStepHandler
|
|
|
|
* @private
|
|
|
|
* @param {object} event - The event data.
|
|
|
|
*/
|
|
|
|
Phaser.Physics.World.prototype.postStepHandler = function (event) {
|
|
|
|
|
|
|
|
// console.log(event);
|
|
|
|
|
|
|
|
// if (event.body.parent)
|
|
|
|
// {
|
|
|
|
// this.onBodyRemoved.dispatch(event.body.parent, event.target);
|
|
|
|
// }
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handles a p2 postBroadphase event.
|
|
|
|
*
|
|
|
|
* @method Phaser.Physics.Arcade#postBroadphaseHandler
|
|
|
|
* @private
|
|
|
|
* @param {object} event - The event data.
|
|
|
|
*/
|
|
|
|
Phaser.Physics.World.prototype.postBroadphaseHandler = function (event) {
|
|
|
|
|
|
|
|
// Body.id 1 is always the World bounds object
|
|
|
|
|
|
|
|
for (var i = 0; i < event.pairs.length; i++)
|
|
|
|
{
|
|
|
|
// console.log(i, event.pairs[i]);
|
|
|
|
|
|
|
|
if (event.pairs[i].parent)
|
|
|
|
{
|
|
|
|
// console.log(event.pairs[i].parent.sprite.name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// if (event.body.parent)
|
|
|
|
// {
|
|
|
|
// this.onBodyRemoved.dispatch(event.body.parent, event.target);
|
|
|
|
// }
|
|
|
|
|
|
|
|
};
|
|
|
|
|
2014-02-10 23:28:32 +00:00
|
|
|
/**
|
|
|
|
* Sets the bounds of the Physics world to match the Game.World dimensions.
|
|
|
|
* You can optionally set which 'walls' to create: left, right, top or bottom.
|
|
|
|
*
|
|
|
|
* @method Phaser.Physics#setBoundsToWorld
|
|
|
|
* @param {boolean} [left=true] - If true will create the left bounds wall.
|
|
|
|
* @param {boolean} [right=true] - If true will create the right bounds wall.
|
|
|
|
* @param {boolean} [top=true] - If true will create the top bounds wall.
|
|
|
|
* @param {boolean} [bottom=true] - If true will create the bottom bounds wall.
|
|
|
|
*/
|
|
|
|
Phaser.Physics.World.prototype.setBoundsToWorld = function (left, right, top, bottom) {
|
|
|
|
|
|
|
|
this.setBounds(this.game.world.bounds.x, this.game.world.bounds.y, this.game.world.bounds.width, this.game.world.bounds.height, left, right, top, bottom);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the bounds of the Physics world to match the given world pixel dimensions.
|
|
|
|
* You can optionally set which 'walls' to create: left, right, top or bottom.
|
|
|
|
*
|
|
|
|
* @method Phaser.Physics.Arcade#setBounds
|
|
|
|
* @param {number} x - The x coordinate of the top-left corner of the bounds.
|
|
|
|
* @param {number} y - The y coordinate of the top-left corner of the bounds.
|
|
|
|
* @param {number} width - The width of the bounds.
|
|
|
|
* @param {number} height - The height of the bounds.
|
|
|
|
* @param {boolean} [left=true] - If true will create the left bounds wall.
|
|
|
|
* @param {boolean} [right=true] - If true will create the right bounds wall.
|
|
|
|
* @param {boolean} [top=true] - If true will create the top bounds wall.
|
|
|
|
* @param {boolean} [bottom=true] - If true will create the bottom bounds wall.
|
|
|
|
*/
|
|
|
|
Phaser.Physics.World.prototype.setBounds = function (x, y, width, height, left, right, top, bottom) {
|
|
|
|
|
|
|
|
if (typeof left === 'undefined') { left = true; }
|
|
|
|
if (typeof right === 'undefined') { right = true; }
|
|
|
|
if (typeof top === 'undefined') { top = true; }
|
|
|
|
if (typeof bottom === 'undefined') { bottom = true; }
|
|
|
|
|
|
|
|
var hw = (width / 2);
|
|
|
|
var hh = (height / 2);
|
|
|
|
var cx = hw + x;
|
|
|
|
var cy = hh + y;
|
|
|
|
|
2014-02-12 19:45:09 +00:00
|
|
|
if (this.bounds !== null)
|
|
|
|
{
|
|
|
|
this.removeBody(this.bounds);
|
|
|
|
|
2014-02-14 17:52:59 +00:00
|
|
|
var i = this.bounds.shapes.length;
|
|
|
|
|
|
|
|
while (i--)
|
2014-02-12 19:45:09 +00:00
|
|
|
{
|
|
|
|
var shape = this.bounds.shapes[i];
|
|
|
|
this.bounds.removeShape(shape);
|
|
|
|
}
|
2014-02-14 17:52:59 +00:00
|
|
|
|
|
|
|
this.bounds.position[0] = this.game.math.px2p(cx);
|
|
|
|
this.bounds.position[1] = this.game.math.px2p(cy);
|
2014-02-12 19:45:09 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
this.bounds = new p2.Body({ mass: 0, position:[this.game.math.px2p(cx), this.game.math.px2p(cy)] });
|
|
|
|
}
|
2014-02-10 23:28:32 +00:00
|
|
|
|
|
|
|
if (left)
|
|
|
|
{
|
|
|
|
this.bounds.addShape(new p2.Plane(), [this.game.math.px2p(-hw), 0], 1.5707963267948966 );
|
|
|
|
}
|
|
|
|
|
|
|
|
if (right)
|
|
|
|
{
|
|
|
|
this.bounds.addShape(new p2.Plane(), [this.game.math.px2p(hw), 0], -1.5707963267948966 );
|
|
|
|
}
|
|
|
|
|
|
|
|
if (top)
|
|
|
|
{
|
|
|
|
this.bounds.addShape(new p2.Plane(), [0, this.game.math.px2p(-hh)], -3.141592653589793 );
|
|
|
|
}
|
|
|
|
|
|
|
|
if (bottom)
|
|
|
|
{
|
|
|
|
this.bounds.addShape(new p2.Plane(), [0, this.game.math.px2p(hh)] );
|
|
|
|
}
|
|
|
|
|
|
|
|
this.addBody(this.bounds);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2014-02-10 16:01:30 +00:00
|
|
|
/**
|
|
|
|
* @method Phaser.Physics.World.prototype.update
|
|
|
|
*/
|
|
|
|
Phaser.Physics.World.prototype.update = function () {
|
|
|
|
|
|
|
|
this.step(1 / 60);
|
|
|
|
|
|
|
|
};
|
2014-02-15 02:19:37 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @method Phaser.Physics.World.prototype.destroy
|
|
|
|
*/
|
|
|
|
Phaser.Physics.World.prototype.destroy = function () {
|
|
|
|
|
|
|
|
this.clear();
|
|
|
|
|
|
|
|
this.game = null;
|
|
|
|
|
|
|
|
};
|
2014-02-17 17:54:10 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @method Phaser.Physics.World.prototype.createBody
|
|
|
|
* @param {number} x - The x coordinate of Body.
|
|
|
|
* @param {number} y - The y coordinate of Body.
|
|
|
|
* @param {number} mass - The mass of the Body. A mass of 0 means a 'static' Body is created.
|
|
|
|
* @param {boolean} [addToWorld=false] - Automatically add this Body to the world? (usually false as it won't have any shapes on construction).
|
|
|
|
* @param {object} options - An object containing the build options:
|
|
|
|
* @param {boolean} [options.optimalDecomp=false] - Set to true if you need optimal decomposition. Warning: very slow for polygons with more than 10 vertices.
|
|
|
|
* @param {boolean} [options.skipSimpleCheck=false] - Set to true if you already know that the path is not intersecting itself.
|
|
|
|
* @param {boolean|number} [options.removeCollinearPoints=false] - Set to a number (angle threshold value) to remove collinear points, or false to keep all points.
|
|
|
|
* @param {(number[]|...number)} points - An array of 2d vectors that form the convex or concave polygon.
|
|
|
|
* Either [[0,0], [0,1],...] or a flat array of numbers that will be interpreted as [x,y, x,y, ...],
|
|
|
|
* or the arguments passed can be flat x,y values e.g. `setPolygon(options, x,y, x,y, x,y, ...)` where `x` and `y` are numbers.
|
|
|
|
*/
|
|
|
|
Phaser.Physics.World.prototype.createBody = function (x, y, mass, addToWorld, options, data) {
|
|
|
|
|
|
|
|
if (typeof addToWorld === 'undefined') { addToWorld = false; }
|
|
|
|
|
|
|
|
var body = new Phaser.Physics.Body(this.game, null, x, y, mass);
|
|
|
|
|
|
|
|
if (data)
|
|
|
|
{
|
|
|
|
body.addPolygon(options, data);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (addToWorld)
|
|
|
|
{
|
|
|
|
this.addBody(body.data);
|
|
|
|
}
|
|
|
|
|
|
|
|
return body;
|
|
|
|
|
|
|
|
};
|