2013-09-03 14:35:40 +00:00
|
|
|
Phaser.Physics.Arcade.Body = function (sprite) {
|
|
|
|
|
|
|
|
this.sprite = sprite;
|
|
|
|
this.game = sprite.game;
|
|
|
|
|
|
|
|
this.offset = new Phaser.Point;
|
|
|
|
|
2013-09-04 00:10:01 +00:00
|
|
|
this.x = sprite.x;
|
|
|
|
this.y = sprite.y;
|
2013-09-23 00:06:09 +00:00
|
|
|
this.preX = sprite.x;
|
|
|
|
this.preY = sprite.y;
|
2013-10-07 23:58:20 +00:00
|
|
|
this.preRotation = sprite.angle;
|
2013-09-04 00:10:01 +00:00
|
|
|
|
|
|
|
// un-scaled original size
|
|
|
|
this.sourceWidth = sprite.currentFrame.sourceSizeW;
|
|
|
|
this.sourceHeight = sprite.currentFrame.sourceSizeH;
|
|
|
|
|
|
|
|
// calculated (scaled) size
|
|
|
|
this.width = sprite.currentFrame.sourceSizeW;
|
|
|
|
this.height = sprite.currentFrame.sourceSizeH;
|
|
|
|
this.halfWidth = Math.floor(sprite.currentFrame.sourceSizeW / 2);
|
|
|
|
this.halfHeight = Math.floor(sprite.currentFrame.sourceSizeH / 2);
|
|
|
|
|
|
|
|
// Scale value cache
|
2013-09-03 16:07:05 +00:00
|
|
|
this._sx = sprite.scale.x;
|
|
|
|
this._sy = sprite.scale.y;
|
|
|
|
|
|
|
|
this.velocity = new Phaser.Point;
|
|
|
|
this.acceleration = new Phaser.Point;
|
|
|
|
this.drag = new Phaser.Point;
|
|
|
|
this.gravity = new Phaser.Point;
|
|
|
|
this.bounce = new Phaser.Point;
|
|
|
|
this.maxVelocity = new Phaser.Point(10000, 10000);
|
|
|
|
|
|
|
|
this.angularVelocity = 0;
|
|
|
|
this.angularAcceleration = 0;
|
|
|
|
this.angularDrag = 0;
|
|
|
|
this.maxAngular = 1000;
|
|
|
|
this.mass = 1;
|
|
|
|
|
2013-10-07 23:58:20 +00:00
|
|
|
this.skipQuadTree = false;
|
2013-09-04 15:12:58 +00:00
|
|
|
this.quadTreeIDs = [];
|
|
|
|
this.quadTreeIndex = -1;
|
2013-09-04 12:54:55 +00:00
|
|
|
|
|
|
|
// Allow collision
|
2013-09-04 15:12:58 +00:00
|
|
|
this.allowCollision = { none: false, any: true, up: true, down: true, left: true, right: true };
|
2013-09-04 12:54:55 +00:00
|
|
|
this.touching = { none: true, up: false, down: false, left: false, right: false };
|
|
|
|
this.wasTouching = { none: true, up: false, down: false, left: false, right: false };
|
2013-09-23 21:23:17 +00:00
|
|
|
this.facing = Phaser.NONE;
|
2013-09-04 00:10:01 +00:00
|
|
|
|
2013-09-03 16:28:12 +00:00
|
|
|
this.immovable = false;
|
2013-09-04 00:10:01 +00:00
|
|
|
this.moves = true;
|
|
|
|
this.rotation = 0;
|
2013-09-06 14:00:05 +00:00
|
|
|
this.allowRotation = true;
|
2013-09-04 00:10:01 +00:00
|
|
|
this.allowGravity = true;
|
2013-09-03 16:07:05 +00:00
|
|
|
|
2013-09-13 02:07:39 +00:00
|
|
|
// These two flags allow you to disable the custom separation that takes place
|
|
|
|
// Used in combination with your own collision processHandler you can create whatever
|
|
|
|
// type of collision response you need.
|
|
|
|
this.customSeparateX = false;
|
|
|
|
this.customSeparateY = false;
|
|
|
|
|
|
|
|
// When this body collides with another the amount of overlap is stored in here
|
|
|
|
// These values are useful if you want to provide your own custom separation logic.
|
|
|
|
this.overlapX = 0;
|
|
|
|
this.overlapY = 0;
|
|
|
|
|
2013-09-23 02:26:08 +00:00
|
|
|
// If a body is overlapping with another body, but neither of them are moving (maybe they spawned on-top of each other?) this is set to true
|
|
|
|
this.embedded = false;
|
|
|
|
|
2013-09-04 02:48:15 +00:00
|
|
|
this.collideWorldBounds = false;
|
|
|
|
|
2013-09-03 14:35:40 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
Phaser.Physics.Arcade.Body.prototype = {
|
|
|
|
|
2013-09-04 00:10:01 +00:00
|
|
|
updateBounds: function (centerX, centerY, scaleX, scaleY) {
|
2013-09-03 16:07:05 +00:00
|
|
|
|
|
|
|
if (scaleX != this._sx || scaleY != this._sy)
|
|
|
|
{
|
2013-09-04 00:10:01 +00:00
|
|
|
this.width = this.sourceWidth * scaleX;
|
|
|
|
this.height = this.sourceHeight * scaleY;
|
|
|
|
this.halfWidth = Math.floor(this.width / 2);
|
|
|
|
this.halfHeight = Math.floor(this.height / 2);
|
2013-09-03 16:07:05 +00:00
|
|
|
this._sx = scaleX;
|
|
|
|
this._sy = scaleY;
|
|
|
|
}
|
|
|
|
|
|
|
|
},
|
|
|
|
|
2013-09-23 00:06:09 +00:00
|
|
|
preUpdate: function () {
|
2013-09-03 16:07:05 +00:00
|
|
|
|
2013-09-04 12:54:55 +00:00
|
|
|
// Store and reset collision flags
|
|
|
|
this.wasTouching.none = this.touching.none;
|
|
|
|
this.wasTouching.up = this.touching.up;
|
|
|
|
this.wasTouching.down = this.touching.down;
|
|
|
|
this.wasTouching.left = this.touching.left;
|
|
|
|
this.wasTouching.right = this.touching.right;
|
|
|
|
|
|
|
|
this.touching.none = true;
|
|
|
|
this.touching.up = false;
|
|
|
|
this.touching.down = false;
|
|
|
|
this.touching.left = false;
|
|
|
|
this.touching.right = false;
|
|
|
|
|
2013-09-23 02:26:08 +00:00
|
|
|
this.embedded = false;
|
|
|
|
|
2013-10-10 08:03:38 +00:00
|
|
|
// this.preX = (this.sprite.localTransform[2] - (this.sprite.anchor.x * this.width)) + this.offset.x;
|
|
|
|
// this.preY = (this.sprite.localTransform[5] - (this.sprite.anchor.y * this.height)) + this.offset.y;
|
|
|
|
this.preX = (this.sprite.worldTransform[2] - (this.sprite.anchor.x * this.width)) + this.offset.x;
|
|
|
|
this.preY = (this.sprite.worldTransform[5] - (this.sprite.anchor.y * this.height)) + this.offset.y;
|
2013-10-07 23:58:20 +00:00
|
|
|
this.preRotation = this.sprite.angle;
|
2013-09-06 14:00:05 +00:00
|
|
|
|
2013-09-23 00:06:09 +00:00
|
|
|
this.x = this.preX;
|
|
|
|
this.y = this.preY;
|
2013-10-07 23:58:20 +00:00
|
|
|
this.rotation = this.preRotation;
|
2013-09-23 00:06:09 +00:00
|
|
|
|
2013-09-05 20:07:44 +00:00
|
|
|
if (this.moves)
|
|
|
|
{
|
|
|
|
this.game.physics.updateMotion(this);
|
2013-09-03 16:07:05 +00:00
|
|
|
|
2013-10-07 23:58:20 +00:00
|
|
|
if (this.collideWorldBounds)
|
|
|
|
{
|
|
|
|
this.checkWorldBounds();
|
|
|
|
}
|
2013-09-06 14:00:05 +00:00
|
|
|
}
|
|
|
|
|
2013-10-07 23:58:20 +00:00
|
|
|
if (this.skipQuadTree == false && this.allowCollision.none == false && this.sprite.visible && this.sprite.alive)
|
2013-09-04 02:48:15 +00:00
|
|
|
{
|
2013-09-04 12:54:55 +00:00
|
|
|
this.quadTreeIDs = [];
|
|
|
|
this.quadTreeIndex = -1;
|
2013-09-04 02:48:15 +00:00
|
|
|
this.game.physics.quadTree.insert(this);
|
|
|
|
}
|
|
|
|
|
2013-09-23 00:06:09 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
postUpdate: function () {
|
|
|
|
|
2013-09-23 21:23:17 +00:00
|
|
|
// Calculate forward-facing edge
|
|
|
|
if (this.deltaX() == 0 && this.deltaY() == 0)
|
|
|
|
{
|
|
|
|
// Can't work it out from the Body, how about from x position?
|
|
|
|
if (this.sprite.deltaX() == 0 && this.sprite.deltaY() == 0)
|
|
|
|
{
|
|
|
|
// still as a statue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.deltaX() < 0)
|
|
|
|
{
|
|
|
|
this.facing = Phaser.LEFT;
|
|
|
|
}
|
|
|
|
else if (this.deltaX() > 0)
|
|
|
|
{
|
|
|
|
this.facing = Phaser.RIGHT;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.deltaY() < 0)
|
|
|
|
{
|
|
|
|
this.facing = Phaser.UP;
|
|
|
|
}
|
|
|
|
else if (this.deltaY() > 0)
|
|
|
|
{
|
|
|
|
this.facing = Phaser.DOWN;
|
|
|
|
}
|
|
|
|
|
2013-10-07 23:58:20 +00:00
|
|
|
this.sprite.x += this.deltaX();
|
|
|
|
this.sprite.y += this.deltaY();
|
2013-09-20 12:55:33 +00:00
|
|
|
|
2013-09-23 02:26:08 +00:00
|
|
|
if (this.allowRotation)
|
|
|
|
{
|
2013-10-07 23:58:20 +00:00
|
|
|
this.sprite.angle += this.deltaZ();
|
2013-09-23 02:26:08 +00:00
|
|
|
}
|
2013-09-12 14:39:52 +00:00
|
|
|
|
|
|
|
},
|
|
|
|
|
2013-09-04 02:48:15 +00:00
|
|
|
checkWorldBounds: function () {
|
|
|
|
|
2013-09-08 00:24:59 +00:00
|
|
|
if (this.x < this.game.world.bounds.x)
|
2013-09-04 02:48:15 +00:00
|
|
|
{
|
|
|
|
this.x = this.game.world.bounds.x;
|
2013-09-08 00:24:59 +00:00
|
|
|
this.velocity.x *= -this.bounce.x;
|
2013-09-04 02:48:15 +00:00
|
|
|
}
|
2013-09-08 00:24:59 +00:00
|
|
|
else if (this.right > this.game.world.bounds.right)
|
2013-09-04 02:48:15 +00:00
|
|
|
{
|
|
|
|
this.x = this.game.world.bounds.right - this.width;
|
2013-09-08 00:24:59 +00:00
|
|
|
this.velocity.x *= -this.bounce.x;
|
2013-09-04 02:48:15 +00:00
|
|
|
}
|
|
|
|
|
2013-09-08 00:24:59 +00:00
|
|
|
if (this.y < this.game.world.bounds.y)
|
2013-09-04 02:48:15 +00:00
|
|
|
{
|
|
|
|
this.y = this.game.world.bounds.y;
|
2013-09-08 00:24:59 +00:00
|
|
|
this.velocity.y *= -this.bounce.y;
|
2013-09-04 02:48:15 +00:00
|
|
|
}
|
2013-09-08 00:24:59 +00:00
|
|
|
else if (this.bottom > this.game.world.bounds.bottom)
|
2013-09-04 02:48:15 +00:00
|
|
|
{
|
|
|
|
this.y = this.game.world.bounds.bottom - this.height;
|
2013-09-08 00:24:59 +00:00
|
|
|
this.velocity.y *= -this.bounce.y;
|
2013-09-04 02:48:15 +00:00
|
|
|
}
|
|
|
|
|
2013-09-03 16:07:05 +00:00
|
|
|
},
|
|
|
|
|
2013-09-04 00:10:01 +00:00
|
|
|
setSize: function (width, height, offsetX, offsetY) {
|
|
|
|
|
|
|
|
offsetX = offsetX || this.offset.x;
|
|
|
|
offsetY = offsetY || this.offset.y;
|
2013-09-03 14:35:40 +00:00
|
|
|
|
2013-09-04 00:10:01 +00:00
|
|
|
this.sourceWidth = width;
|
|
|
|
this.sourceHeight = height;
|
|
|
|
this.width = this.sourceWidth * this._sx;
|
|
|
|
this.height = this.sourceHeight * this._sy;
|
|
|
|
this.halfWidth = Math.floor(this.width / 2);
|
|
|
|
this.halfHeight = Math.floor(this.height / 2);
|
|
|
|
this.offset.setTo(offsetX, offsetY);
|
2013-09-03 14:35:40 +00:00
|
|
|
|
|
|
|
},
|
|
|
|
|
2013-09-10 00:26:50 +00:00
|
|
|
reset: function () {
|
|
|
|
|
2013-09-13 02:52:05 +00:00
|
|
|
this.velocity.setTo(0, 0);
|
|
|
|
this.acceleration.setTo(0, 0);
|
2013-09-10 00:26:50 +00:00
|
|
|
|
|
|
|
this.angularVelocity = 0;
|
|
|
|
this.angularAcceleration = 0;
|
2013-09-13 02:52:05 +00:00
|
|
|
|
2013-10-10 08:03:38 +00:00
|
|
|
// this.preX = (this.sprite.localTransform[2] - (this.sprite.anchor.x * this.width)) + this.offset.x;
|
|
|
|
// this.preY = (this.sprite.localTransform[5] - (this.sprite.anchor.y * this.height)) + this.offset.y;
|
|
|
|
this.preX = (this.sprite.worldTransform[2] - (this.sprite.anchor.x * this.width)) + this.offset.x;
|
|
|
|
this.preY = (this.sprite.worldTransform[5] - (this.sprite.anchor.y * this.height)) + this.offset.y;
|
2013-10-08 20:09:46 +00:00
|
|
|
this.preRotation = this.sprite.angle;
|
|
|
|
|
2013-10-10 08:03:38 +00:00
|
|
|
// this.x = (this.sprite.localTransform[2] - (this.sprite.anchor.x * this.width)) + this.offset.x;
|
|
|
|
// this.y = (this.sprite.localTransform[5] - (this.sprite.anchor.y * this.height)) + this.offset.y;
|
|
|
|
this.x = (this.sprite.worldTransform[2] - (this.sprite.anchor.x * this.width)) + this.offset.x;
|
|
|
|
this.y = (this.sprite.worldTransform[5] - (this.sprite.anchor.y * this.height)) + this.offset.y;
|
2013-09-23 02:26:08 +00:00
|
|
|
this.rotation = this.sprite.angle;
|
2013-09-10 00:26:50 +00:00
|
|
|
|
|
|
|
},
|
|
|
|
|
2013-09-24 14:28:29 +00:00
|
|
|
// Basically Math.abs
|
2013-09-03 18:34:38 +00:00
|
|
|
deltaAbsX: function () {
|
|
|
|
return (this.deltaX() > 0 ? this.deltaX() : -this.deltaX());
|
2013-09-03 16:07:05 +00:00
|
|
|
},
|
|
|
|
|
2013-09-03 18:34:38 +00:00
|
|
|
deltaAbsY: function () {
|
|
|
|
return (this.deltaY() > 0 ? this.deltaY() : -this.deltaY());
|
2013-09-03 16:07:05 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
deltaX: function () {
|
2013-09-23 00:06:09 +00:00
|
|
|
return this.x - this.preX;
|
2013-09-03 16:07:05 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
deltaY: function () {
|
2013-09-23 00:06:09 +00:00
|
|
|
return this.y - this.preY;
|
2013-10-07 23:58:20 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
deltaZ: function () {
|
|
|
|
return this.rotation - this.preRotation;
|
2013-09-03 16:07:05 +00:00
|
|
|
}
|
|
|
|
|
2013-09-08 00:24:59 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
Object.defineProperty(Phaser.Physics.Arcade.Body.prototype, "bottom", {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The sum of the y and height properties. Changing the bottom property of a Rectangle object has no effect on the x, y and width properties, but does change the height property.
|
|
|
|
* @method bottom
|
2013-10-01 15:39:39 +00:00
|
|
|
* @return {number}
|
2013-09-08 00:24:59 +00:00
|
|
|
**/
|
|
|
|
get: function () {
|
|
|
|
return this.y + this.height;
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The sum of the y and height properties. Changing the bottom property of a Rectangle object has no effect on the x, y and width properties, but does change the height property.
|
|
|
|
* @method bottom
|
2013-10-01 15:39:39 +00:00
|
|
|
* @param {number} value
|
2013-09-08 00:24:59 +00:00
|
|
|
**/
|
|
|
|
set: function (value) {
|
2013-09-11 12:21:07 +00:00
|
|
|
|
|
|
|
if (value <= this.y)
|
|
|
|
{
|
2013-09-08 00:24:59 +00:00
|
|
|
this.height = 0;
|
2013-09-11 12:21:07 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-09-08 00:24:59 +00:00
|
|
|
this.height = (this.y - value);
|
|
|
|
}
|
2013-09-11 12:21:07 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-09-08 00:24:59 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
Object.defineProperty(Phaser.Physics.Arcade.Body.prototype, "right", {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The sum of the x and width properties. Changing the right property of a Rectangle object has no effect on the x, y and height properties.
|
|
|
|
* However it does affect the width property.
|
|
|
|
* @method right
|
2013-10-01 15:39:39 +00:00
|
|
|
* @return {number}
|
2013-09-08 00:24:59 +00:00
|
|
|
**/
|
|
|
|
get: function () {
|
|
|
|
return this.x + this.width;
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The sum of the x and width properties. Changing the right property of a Rectangle object has no effect on the x, y and height properties.
|
|
|
|
* However it does affect the width property.
|
|
|
|
* @method right
|
2013-10-01 15:39:39 +00:00
|
|
|
* @param {number} value
|
2013-09-08 00:24:59 +00:00
|
|
|
**/
|
|
|
|
set: function (value) {
|
2013-09-11 12:21:07 +00:00
|
|
|
|
|
|
|
if (value <= this.x)
|
|
|
|
{
|
2013-09-08 00:24:59 +00:00
|
|
|
this.width = 0;
|
2013-09-11 12:21:07 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-09-08 00:24:59 +00:00
|
|
|
this.width = this.x + value;
|
|
|
|
}
|
2013-09-11 12:21:07 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-09-08 00:24:59 +00:00
|
|
|
});
|