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
|
|
|
// the top-left of the Body
|
|
|
|
this.x = sprite.x;
|
|
|
|
this.y = sprite.y;
|
|
|
|
|
|
|
|
// 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);
|
|
|
|
|
|
|
|
this.bounds = new Phaser.Rectangle(sprite.x, sprite.y, this.width, this.height);
|
|
|
|
|
|
|
|
// 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-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-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-04 02:48:15 +00:00
|
|
|
this.collideWorldBounds = false;
|
|
|
|
|
2013-09-03 18:34:38 +00:00
|
|
|
this.lastX = sprite.x;
|
|
|
|
this.lastY = sprite.y;
|
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);
|
|
|
|
this.bounds.width = this.width;
|
|
|
|
this.bounds.height = this.height;
|
2013-09-03 16:07:05 +00:00
|
|
|
this._sx = scaleX;
|
|
|
|
this._sy = scaleY;
|
|
|
|
}
|
|
|
|
|
|
|
|
},
|
|
|
|
|
2013-09-03 18:34:38 +00:00
|
|
|
update: 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-03 18:34:38 +00:00
|
|
|
this.lastX = this.x;
|
|
|
|
this.lastY = this.y;
|
2013-09-03 16:07:05 +00:00
|
|
|
|
2013-09-06 14:00:05 +00:00
|
|
|
this.x = this.sprite.x;
|
|
|
|
this.y = this.sprite.y;
|
|
|
|
this.rotation = this.sprite.angle;
|
|
|
|
|
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-09-04 00:10:01 +00:00
|
|
|
this.bounds.x = this.x;
|
|
|
|
this.bounds.y = this.y;
|
2013-09-03 16:07:05 +00:00
|
|
|
|
2013-09-06 14:00:05 +00:00
|
|
|
if (this.collideWorldBounds)
|
|
|
|
{
|
|
|
|
this.checkWorldBounds();
|
|
|
|
}
|
|
|
|
|
2013-09-04 15:12:58 +00:00
|
|
|
if (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-06 14:00:05 +00:00
|
|
|
// Adjust the sprite based on all of the above, so the x/y coords will be correct going into the State update
|
|
|
|
this.sprite.x = this.x - this.offset.x + (this.sprite.anchor.x * this.width);
|
|
|
|
this.sprite.y = this.y - this.offset.y + (this.sprite.anchor.y * this.height);
|
|
|
|
|
|
|
|
if (this.allowRotation)
|
|
|
|
{
|
|
|
|
this.sprite.angle = this.rotation;
|
|
|
|
}
|
|
|
|
|
2013-09-04 02:48:15 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
checkWorldBounds: function () {
|
|
|
|
|
|
|
|
if (this.bounds.x < this.game.world.bounds.x)
|
|
|
|
{
|
|
|
|
this.x = this.game.world.bounds.x;
|
|
|
|
this.velocity.x *= -1;
|
|
|
|
this.velocity.x *= this.bounce.x;
|
|
|
|
}
|
|
|
|
else if (this.bounds.right > this.game.world.bounds.right)
|
|
|
|
{
|
|
|
|
this.x = this.game.world.bounds.right - this.width;
|
|
|
|
this.velocity.x *= -1;
|
|
|
|
this.velocity.x *= this.bounce.x;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.bounds.y < this.game.world.bounds.y)
|
|
|
|
{
|
|
|
|
this.y = this.game.world.bounds.y;
|
|
|
|
this.velocity.y *= -1;
|
|
|
|
this.velocity.y *= this.bounce.y;
|
|
|
|
}
|
|
|
|
else if (this.bounds.bottom > this.game.world.bounds.bottom)
|
|
|
|
{
|
|
|
|
this.y = this.game.world.bounds.bottom - this.height;
|
|
|
|
this.velocity.y *= -1;
|
|
|
|
this.velocity.y *= this.bounce.y;
|
|
|
|
}
|
|
|
|
|
2013-09-03 16:07:05 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
postUpdate: function () {
|
|
|
|
|
2013-09-06 14:00:05 +00:00
|
|
|
/*
|
|
|
|
// The State update may (almost certainly?) will had
|
|
|
|
|
2013-09-04 02:48:15 +00:00
|
|
|
if (this.collideWorldBounds)
|
|
|
|
{
|
2013-09-06 14:00:05 +00:00
|
|
|
// Adjust sprite directly?
|
2013-09-04 02:48:15 +00:00
|
|
|
this.checkWorldBounds();
|
|
|
|
}
|
|
|
|
|
2013-09-06 14:00:05 +00:00
|
|
|
console.log('pre pu', this.x, this.y);
|
|
|
|
|
|
|
|
// if (this.moves)
|
|
|
|
// {
|
|
|
|
this.sprite.x = this.x - this.offset.x + (this.sprite.anchor.x * this.width);
|
|
|
|
this.sprite.y = this.y - this.offset.y + (this.sprite.anchor.y * this.height);
|
|
|
|
// }
|
|
|
|
|
|
|
|
console.log('post pu', this.sprite.x, this.sprite.y);
|
2013-09-04 00:10:01 +00:00
|
|
|
|
|
|
|
if (this.allowRotation)
|
|
|
|
{
|
|
|
|
this.sprite.angle = this.rotation;
|
|
|
|
}
|
2013-09-06 14:00:05 +00:00
|
|
|
*/
|
|
|
|
|
2013-09-03 14:35:40 +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.bounds.width = this.width;
|
|
|
|
this.bounds.height = this.height;
|
|
|
|
this.offset.setTo(offsetX, offsetY);
|
2013-09-03 14:35:40 +00:00
|
|
|
|
|
|
|
},
|
|
|
|
|
2013-09-04 02:48:15 +00:00
|
|
|
/*
|
2013-09-03 16:07:05 +00:00
|
|
|
hullWidth: function () {
|
|
|
|
|
2013-09-04 00:10:01 +00:00
|
|
|
if (this.deltaX() > 0)
|
2013-09-03 16:07:05 +00:00
|
|
|
{
|
2013-09-04 00:10:01 +00:00
|
|
|
return this.width + this.deltaX();
|
2013-09-03 16:07:05 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-09-04 00:10:01 +00:00
|
|
|
return this.width - this.deltaX();
|
2013-09-03 16:07:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
hullHeight: function () {
|
|
|
|
|
2013-09-04 00:10:01 +00:00
|
|
|
if (this.deltaY() > 0)
|
2013-09-03 16:07:05 +00:00
|
|
|
{
|
2013-09-04 00:10:01 +00:00
|
|
|
return this.height + this.deltaY();
|
2013-09-03 16:07:05 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-09-04 00:10:01 +00:00
|
|
|
return this.height - this.deltaY();
|
2013-09-03 16:07:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
hullX: function () {
|
|
|
|
|
2013-09-03 18:34:38 +00:00
|
|
|
if (this.x < this.lastX)
|
2013-09-03 16:07:05 +00:00
|
|
|
{
|
2013-09-03 18:34:38 +00:00
|
|
|
return this.x;
|
2013-09-03 16:07:05 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-09-03 18:34:38 +00:00
|
|
|
return this.lastX;
|
2013-09-03 16:07:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
hullY: function () {
|
|
|
|
|
2013-09-03 18:34:38 +00:00
|
|
|
if (this.y < this.lastY)
|
2013-09-03 16:07:05 +00:00
|
|
|
{
|
2013-09-03 18:34:38 +00:00
|
|
|
return this.y;
|
2013-09-03 16:07:05 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-09-03 18:34:38 +00:00
|
|
|
return this.lastY;
|
2013-09-03 16:07:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
},
|
2013-09-04 02:48:15 +00:00
|
|
|
*/
|
2013-09-03 16:07:05 +00:00
|
|
|
|
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-03 18:34:38 +00:00
|
|
|
return this.x - this.lastX;
|
2013-09-03 16:07:05 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
deltaY: function () {
|
2013-09-03 18:34:38 +00:00
|
|
|
return this.y - this.lastY;
|
2013-09-03 16:07:05 +00:00
|
|
|
}
|
|
|
|
|
2013-09-03 14:35:40 +00:00
|
|
|
};
|