Physics World events added.

Group has new 'addToWorld' parameter, which fulfills the same function as the old useStage.
Stage now extends PIXI.Stage rather than owns one.
This commit is contained in:
photonstorm
2014-02-12 19:45:09 +00:00
parent 424d1126bc
commit f6113ac6c4
10 changed files with 401 additions and 159 deletions
+4 -4
View File
@@ -37,6 +37,7 @@ Phaser.Physics.Body = function (sprite) {
* @protected
*/
this.data = new p2.Body({ position:[this.px2p(sprite.position.x), this.px2p(sprite.position.y)], mass: 1 });
this.data.parent = this;
/**
* @property {Phaser.PointProxy} velocity - The velocity of the body. Set velocity.x to a negative value to move to the left, position to the right. velocity.y negative values move up, positive move down.
@@ -48,15 +49,14 @@ Phaser.Physics.Body = function (sprite) {
*/
this.force = new Phaser.Physics.PointProxy(this.data.force);
// this.onAdded = new Phaser.Signal();
// this.onRemoved = new Phaser.Signal();
// Set-up the default shape
this.setRectangleFromSprite(sprite);
this.game.physics.addBody(this.data);
// Set-up contact events
// this.sprite.events.onBeginContact = new Phaser.Signal();
// this.sprite.events.onEndContact = new Phaser.Signal();
};
Phaser.Physics.Body.prototype = {
+103 -6
View File
@@ -9,6 +9,9 @@
*/
Phaser.Physics = {};
// Add an extra property to p2.Body
p2.Body.prototype.parent = null;
/**
* @class Phaser.Physics.World
* @classdesc Physics World Constructor
@@ -22,10 +25,18 @@ Phaser.Physics.World = function (game) {
*/
this.game = game;
this.onBodyAdded = new Phaser.Signal();
this.onBodyRemoved = new Phaser.Signal();
this.bounds = null;
p2.World.call(this, { gravity: [0, 0] });
this.on("addBody", this.addBodyHandler);
this.on("removeBody", this.removeBodyHandler);
this.on("postStep", this.postStepHandler);
this.on("postBroadphase", this.postBroadphaseHandler);
this.setBoundsToWorld(true, true, true, true);
};
@@ -33,6 +44,84 @@ Phaser.Physics.World = function (game) {
Phaser.Physics.World.prototype = Object.create(p2.World.prototype);
Phaser.Physics.World.prototype.constructor = Phaser.Physics.World;
/**
* 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);
// }
};
/**
* 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.
@@ -70,17 +159,25 @@ Phaser.Physics.World.prototype.setBounds = function (x, y, width, height, left,
if (typeof top === 'undefined') { top = true; }
if (typeof bottom === 'undefined') { bottom = true; }
if (this.bounds !== null)
{
this.removeBody(this.bounds);
}
var hw = (width / 2);
var hh = (height / 2);
var cx = hw + x;
var cy = hh + y;
this.bounds = new p2.Body({ mass: 0, position:[this.game.math.px2p(cx), this.game.math.px2p(cy)] });
if (this.bounds !== null)
{
this.removeBody(this.bounds);
for (var i = this.bounds.shapes.length - 1; i >= 0; i--)
{
var shape = this.bounds.shapes[i];
this.bounds.removeShape(shape);
}
}
else
{
this.bounds = new p2.Body({ mass: 0, position:[this.game.math.px2p(cx), this.game.math.px2p(cy)] });
}
if (left)
{