mirror of
https://github.com/wassname/phaser.git
synced 2026-08-15 12:45:11 +08:00
Warning: This version has a new ArcadePhysics handler in it. Don't upgrade if you need this for live game code, wait until we go to master. Otherwise, this commit contains lots of new physics demos and a new updateMotion and Body class to try and fix, once and for all, the physics issues with applied forces.
This commit is contained in:
@@ -144,15 +144,14 @@ Phaser.GameObjectFactory.prototype = {
|
||||
* @param {number} width - the width of the tilesprite.
|
||||
* @param {number} height - the height of the tilesprite.
|
||||
* @param {string|Phaser.RenderTexture|PIXI.Texture} key - This is the image or texture used by the Sprite during rendering. It can be a string which is a reference to the Cache entry, or an instance of a RenderTexture or PIXI.Texture.
|
||||
* @param {string|number} frame - If this Sprite is using part of a sprite sheet or texture atlas you can specify the exact frame to use by giving a string or numeric index.
|
||||
* @param {Phaser.Group} [group] - Optional Group to add the object to. If not specified it will be added to the World group.
|
||||
* @return {Phaser.TileSprite} The newly created tileSprite object.
|
||||
*/
|
||||
tileSprite: function (x, y, width, height, key, frame, group) {
|
||||
tileSprite: function (x, y, width, height, key, group) {
|
||||
|
||||
if (typeof group === 'undefined') { group = this.world; }
|
||||
|
||||
return group.add(new Phaser.TileSprite(this.game, x, y, width, height, key, frame));
|
||||
return group.add(new Phaser.TileSprite(this.game, x, y, width, height, key));
|
||||
|
||||
},
|
||||
|
||||
|
||||
@@ -15,18 +15,16 @@
|
||||
* @param {number} width - the width of the tilesprite.
|
||||
* @param {number} height - the height of the tilesprite.
|
||||
* @param {string|Phaser.RenderTexture|PIXI.Texture} key - This is the image or texture used by the Sprite during rendering. It can be a string which is a reference to the Cache entry, or an instance of a RenderTexture or PIXI.Texture.
|
||||
* @param {string|number} frame - If this Sprite is using part of a sprite sheet or texture atlas you can specify the exact frame to use by giving a string or numeric index.
|
||||
*/
|
||||
Phaser.TileSprite = function (game, x, y, width, height, key, frame) {
|
||||
Phaser.TileSprite = function (game, x, y, width, height, key) {
|
||||
|
||||
x = x || 0;
|
||||
y = y || 0;
|
||||
width = width || 256;
|
||||
height = height || 256;
|
||||
key = key || null;
|
||||
frame = frame || null;
|
||||
|
||||
Phaser.Sprite.call(this, game, x, y, key, frame);
|
||||
Phaser.Sprite.call(this, game, x, y, key);
|
||||
|
||||
/**
|
||||
* @property {PIXI.Texture} texture - The texture that the sprite renders with.
|
||||
|
||||
@@ -167,7 +167,7 @@ Phaser.Physics.Arcade.prototype = {
|
||||
* @method Phaser.Physics.Arcade#updateMotion
|
||||
* @param {Phaser.Physics.Arcade.Body} The Body object to be updated.
|
||||
*/
|
||||
updateMotion: function (body) {
|
||||
OLDupdateMotion: function (body) {
|
||||
|
||||
// If you're wondering why the velocity is halved and applied twice, read this: http://www.niksula.hut.fi/~hkankaan/Homepages/gravity.html
|
||||
|
||||
@@ -177,27 +177,120 @@ Phaser.Physics.Arcade.prototype = {
|
||||
body.rotation += (body.angularVelocity * this.game.time.physicsElapsed);
|
||||
body.angularVelocity += this._velocityDelta;
|
||||
|
||||
if(body.allowGravity)
|
||||
if (body.allowGravity)
|
||||
{
|
||||
// Gravity was previously applied without taking physicsElapsed into account
|
||||
// so it needs to be multiplied by 60 (fps) for compatibility with existing games
|
||||
this._gravityX = (this.gravity.x + body.gravity.x) * 60;
|
||||
this._gravityY = (this.gravity.y + body.gravity.y) * 60;
|
||||
} else {
|
||||
this._gravityX = this._gravityY = 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
this._gravityX = 0;
|
||||
this._gravityY = 0;
|
||||
}
|
||||
|
||||
body.motionVelocity.x = (this.computeVelocity(body, body.velocity.x, body.acceleration.x, body.drag.x, body.maxVelocity.x, this._gravityX) - body.velocity.x) * 0.5;
|
||||
body.motionVelocity.y = (this.computeVelocity(body, body.velocity.y, body.acceleration.y, body.drag.y, body.maxVelocity.y, this._gravityY) - body.velocity.y) * 0.5;
|
||||
|
||||
// Horizontal
|
||||
this._velocityDelta = (this.computeVelocity(body, body.velocity.x, body.acceleration.x, body.drag.x, body.maxVelocity.x, this._gravityX) - body.velocity.x) * 0.5;
|
||||
body.velocity.x += this._velocityDelta;
|
||||
body.x += (body.velocity.x * this.game.time.physicsElapsed);
|
||||
body.velocity.x += this._velocityDelta;
|
||||
// this._velocityDelta = (this.computeVelocity(body, body.velocity.x, body.acceleration.x, body.drag.x, body.maxVelocity.x, this._gravityX) - body.velocity.x) * 0.5;
|
||||
// body.velocity.x += this._velocityDelta;
|
||||
// body.x += (body.velocity.x * this.game.time.physicsElapsed);
|
||||
// body.velocity.x += this._velocityDelta;
|
||||
|
||||
// Vertical
|
||||
this._velocityDelta = (this.computeVelocity(body, body.velocity.y, body.acceleration.y, body.drag.y, body.maxVelocity.y, this._gravityY) - body.velocity.y) * 0.5;
|
||||
body.velocity.y += this._velocityDelta;
|
||||
body.y += (body.velocity.y * this.game.time.physicsElapsed);
|
||||
body.velocity.y += this._velocityDelta;
|
||||
// this._velocityDelta = (this.computeVelocity(body, body.velocity.y, body.acceleration.y, body.drag.y, body.maxVelocity.y, this._gravityY) - body.velocity.y) * 0.5;
|
||||
// body.motionVelocity.y = this._velocityDelta;
|
||||
|
||||
// body.velocity.y += this._velocityDelta;
|
||||
// body.y += (body.velocity.y * this.game.time.physicsElapsed);
|
||||
// body.velocity.y += this._velocityDelta;
|
||||
|
||||
},
|
||||
|
||||
/**
|
||||
* Called automatically by a Physics body, it updates all motion related values on the Body.
|
||||
*
|
||||
* @method Phaser.Physics.Arcade#updateMotion
|
||||
* @param {Phaser.Physics.Arcade.Body} The Body object to be updated.
|
||||
*/
|
||||
updateMotion: function (body) {
|
||||
|
||||
// If you're wondering why the velocity is halved and applied twice, read this: http://www.niksula.hut.fi/~hkankaan/Homepages/gravity.html
|
||||
|
||||
if (body.allowGravity)
|
||||
{
|
||||
this._gravityX = (this.gravity.x + body.gravity.x);
|
||||
this._gravityY = (this.gravity.y + body.gravity.y);
|
||||
}
|
||||
else
|
||||
{
|
||||
this._gravityX = 0;
|
||||
this._gravityY = 0;
|
||||
}
|
||||
|
||||
// Rotation
|
||||
// this._velocityDelta = (this.computeVelocity(body, body.angularVelocity, body.angularAcceleration, body.angularDrag, body.maxAngular, 0) - body.angularVelocity) * 0.5;
|
||||
// body.angularVelocity += this._velocityDelta;
|
||||
// body.rotation += (body.angularVelocity * this.game.time.physicsElapsed);
|
||||
// body.angularVelocity += this._velocityDelta;
|
||||
|
||||
// body.motionVelocity.x = (this.computeVelocity(body, body.velocity.x, body.acceleration.x, body.drag.x, body.maxVelocity.x, this._gravityX) - body.velocity.x) * 0.5;
|
||||
// body.motionVelocity.y = (this.computeVelocity(body, body.velocity.y, body.acceleration.y, body.drag.y, body.maxVelocity.y, this._gravityY) - body.velocity.y) * 0.5;
|
||||
|
||||
// velocity = velocity + gravity*delta_time/2
|
||||
// position = position + velocity*delta_time
|
||||
// velocity = velocity + gravity*delta_time/2
|
||||
|
||||
// temp = acc*dt
|
||||
// pos = pos + dt*(vel + temp/2)
|
||||
// vel = vel + temp
|
||||
|
||||
this._drag = 0;
|
||||
|
||||
if (body.drag.x !== 0 && body.velocity.x !== 0 && body.acceleration.x === 0)
|
||||
{
|
||||
if (body.velocity.x - body.drag.x > 0)
|
||||
{
|
||||
this._drag = -body.drag.x;
|
||||
}
|
||||
else if (body.velocity.x + body.drag.x < 0)
|
||||
{
|
||||
this._drag = body.drag.x;
|
||||
}
|
||||
}
|
||||
|
||||
// body.motionVelocity.x = body.acceleration.x + this._gravityX - body.drag.x * this.game.time.physicsElapsed;
|
||||
// body.motionVelocity.x = body.acceleration.x + this._gravityX * this.game.time.physicsElapsed;
|
||||
body.motionVelocity.x = (body.acceleration.x + this._gravityX + this._drag) * this.game.time.physicsElapsed;
|
||||
|
||||
this._drag = 0;
|
||||
|
||||
if (body.drag.y !== 0 && body.velocity.y !== 0 && body.acceleration.y === 0)
|
||||
{
|
||||
if (body.velocity.y - body.drag.y > 0)
|
||||
{
|
||||
this._drag = -body.drag.y;
|
||||
}
|
||||
else if (body.velocity.y + body.drag.y < 0)
|
||||
{
|
||||
this._drag = body.drag.y;
|
||||
}
|
||||
}
|
||||
|
||||
// body.motionVelocity.y = body.acceleration.y + this._gravityY - body.drag.y * this.game.time.physicsElapsed;
|
||||
// body.motionVelocity.y = body.acceleration.y + this._gravityY * this.game.time.physicsElapsed;
|
||||
body.motionVelocity.y = (body.acceleration.y + this._gravityY + this._drag) * this.game.time.physicsElapsed;
|
||||
|
||||
// if (body.sleeping === false)
|
||||
// {
|
||||
// body.x = body.x + this.game.time.physicsElapsed * (body.velocity.x + body.motionVelocity.x / 2);
|
||||
// body.velocity.x = body.velocity.x + body.motionVelocity.x;
|
||||
|
||||
// body.y = body.y + this.game.time.physicsElapsed * (body.velocity.y + body.motionVelocity.y / 2);
|
||||
// body.velocity.y = body.velocity.y + body.motionVelocity.y;
|
||||
// }
|
||||
|
||||
},
|
||||
|
||||
@@ -213,7 +306,56 @@ Phaser.Physics.Arcade.prototype = {
|
||||
* @param {number} gravity - The acceleration due to gravity. Gravity will not induce drag.
|
||||
* @return {number} The altered Velocity value.
|
||||
*/
|
||||
computeVelocity: function (body, velocity, acceleration, drag, max, gravity) {
|
||||
computeVelocity: function (body, velocity, acceleration, drag, max) {
|
||||
|
||||
max = max || 10000;
|
||||
|
||||
velocity += acceleration;
|
||||
|
||||
if (acceleration === 0 && drag !== 0)
|
||||
{
|
||||
this._drag = drag;
|
||||
|
||||
if (velocity - this._drag > 0)
|
||||
{
|
||||
velocity -= this._drag;
|
||||
}
|
||||
else if (velocity + this._drag < 0)
|
||||
{
|
||||
velocity += this._drag;
|
||||
}
|
||||
else
|
||||
{
|
||||
velocity = 0;
|
||||
}
|
||||
}
|
||||
|
||||
if (velocity > max)
|
||||
{
|
||||
velocity = max;
|
||||
}
|
||||
else if (velocity < -max)
|
||||
{
|
||||
velocity = -max;
|
||||
}
|
||||
|
||||
return velocity;
|
||||
|
||||
},
|
||||
|
||||
/**
|
||||
* A tween-like function that takes a starting velocity and some other factors and returns an altered velocity.
|
||||
*
|
||||
* @method Phaser.Physics.Arcade#computeVelocity
|
||||
* @param {Phaser.Physics.Arcade.Body} body - The Body object to be updated.
|
||||
* @param {number} velocity - Any component of velocity (e.g. 20).
|
||||
* @param {number} acceleration - Rate at which the velocity is changing.
|
||||
* @param {number} drag - Really kind of a deceleration, this is how much the velocity changes if Acceleration is not set.
|
||||
* @param {number} [max=10000] - An absolute value cap for the velocity.
|
||||
* @param {number} gravity - The acceleration due to gravity. Gravity will not induce drag.
|
||||
* @return {number} The altered Velocity value.
|
||||
*/
|
||||
OLDcomputeVelocity: function (body, velocity, acceleration, drag, max, gravity) {
|
||||
|
||||
max = max || 10000;
|
||||
|
||||
|
||||
+113
-92
@@ -121,11 +121,21 @@ Phaser.Physics.Arcade.Body = function (sprite) {
|
||||
*/
|
||||
this._sy = sprite.scale.y;
|
||||
|
||||
/**
|
||||
* @property {Phaser.Point} motionVelocity - The data from the updateMotion function.
|
||||
*/
|
||||
this.motionVelocity = new Phaser.Point();
|
||||
|
||||
/**
|
||||
* @property {Phaser.Point} velocity - The velocity in pixels per second sq. of the Body.
|
||||
*/
|
||||
this.velocity = new Phaser.Point();
|
||||
|
||||
/**
|
||||
* @property {Phaser.Point} prevVelocity - The velocity in pixels per second sq. of the Body.
|
||||
*/
|
||||
this.prevVelocity = new Phaser.Point();
|
||||
|
||||
/**
|
||||
* @property {Phaser.Point} acceleration - The velocity in pixels per second sq. of the Body.
|
||||
*/
|
||||
@@ -137,7 +147,7 @@ Phaser.Physics.Arcade.Body = function (sprite) {
|
||||
this.drag = new Phaser.Point();
|
||||
|
||||
/**
|
||||
* @property {Phaser.Point} gravity - A private Gravity setting for the Body.
|
||||
* @property {Phaser.Point} gravity - The gravity applied to the motion of the Body.
|
||||
*/
|
||||
this.gravity = new Phaser.Point();
|
||||
|
||||
@@ -232,7 +242,7 @@ Phaser.Physics.Arcade.Body = function (sprite) {
|
||||
this.facing = Phaser.NONE;
|
||||
|
||||
/**
|
||||
* @property {boolean} immovable - An immovable Body will not receive any impacts from other bodies.
|
||||
* @property {boolean} immovable - An immovable Body will not receive any impacts or exchanges of velocity from other bodies.
|
||||
* @default
|
||||
*/
|
||||
this.immovable = false;
|
||||
@@ -289,17 +299,16 @@ Phaser.Physics.Arcade.Body = function (sprite) {
|
||||
*/
|
||||
this.overlapY = 0;
|
||||
|
||||
this.hull = new Phaser.Rectangle();
|
||||
|
||||
/**
|
||||
* @property {Phaser.Rectangle} hullX - The dynamically calculated hull used during collision.
|
||||
* @property {boolean} canSleep - A Body that canSleep will have its velocity set to zero if it falls below sleepThreshold for longer than sleepDuration.
|
||||
* @default
|
||||
*/
|
||||
this.hullX = new Phaser.Rectangle();
|
||||
|
||||
/**
|
||||
* @property {Phaser.Rectangle} hullY - The dynamically calculated hull used during collision.
|
||||
*/
|
||||
this.hullY = new Phaser.Rectangle();
|
||||
this.sleeping = true;
|
||||
this.canSleep = true;
|
||||
this.sleepMin = new Phaser.Point(-20, -20);
|
||||
this.sleepMax = new Phaser.Point(20, 20);
|
||||
this.sleepDuration = 2000; // ms
|
||||
this._sleepTimer = 0; // ms
|
||||
|
||||
/**
|
||||
* 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.
|
||||
@@ -368,16 +377,18 @@ Phaser.Physics.Arcade.Body.prototype = {
|
||||
|
||||
this.preX = (this.sprite.world.x - (this.sprite.anchor.x * this.width)) + this.offset.x;
|
||||
this.preY = (this.sprite.world.y - (this.sprite.anchor.y * this.height)) + this.offset.y;
|
||||
|
||||
this.preRotation = this.sprite.angle;
|
||||
|
||||
if (this.canSleep && this.sleeping && this.velocity.equals(this.prevVelocity) === false)
|
||||
{
|
||||
this.sleeping = false;
|
||||
this._sleepTimer = 0;
|
||||
}
|
||||
|
||||
this.x = this.preX;
|
||||
this.y = this.preY;
|
||||
this.rotation = this.preRotation;
|
||||
|
||||
// this.overlapX = 0;
|
||||
// this.overlapY = 0;
|
||||
|
||||
this.blocked.up = false;
|
||||
this.blocked.down = false;
|
||||
this.blocked.left = false;
|
||||
@@ -392,10 +403,46 @@ Phaser.Physics.Arcade.Body.prototype = {
|
||||
this.checkWorldBounds();
|
||||
}
|
||||
|
||||
// this.hull.setTo(this.preX, this.preY, this.width, this.height);
|
||||
// this.hullX.setTo(this.x, this.preY, this.width, this.height);
|
||||
// this.hullY.setTo(this.preX, this.y, this.width, this.height);
|
||||
// this.updateHulls(true);
|
||||
if (this.velocity.x > this.maxVelocity.x)
|
||||
{
|
||||
this.velocity.x = this.maxVelocity.x;
|
||||
}
|
||||
else if (this.velocity.x < -this.maxVelocity.x)
|
||||
{
|
||||
this.velocity.x = -this.maxVelocity.x;
|
||||
}
|
||||
|
||||
if (this.canSleep)
|
||||
{
|
||||
if (!this.sleeping)
|
||||
{
|
||||
if (this.velocity.x >= this.sleepMin.x && this.velocity.x <= this.sleepMax.x && this.velocity.y >= this.sleepMin.y && this.velocity.y <= this.sleepMax.y)
|
||||
{
|
||||
if (this._sleepTimer >= this.sleepDuration)
|
||||
{
|
||||
console.log('sleeping true');
|
||||
this.sleeping = true;
|
||||
// this.velocity.setTo(0, 0);
|
||||
// this.motionVelocity.setTo(0, 0);
|
||||
// this.preX = this.x;
|
||||
// this.preY = this.y;
|
||||
}
|
||||
else
|
||||
{
|
||||
this._sleepTimer += this.game.time.elapsed;
|
||||
this.applyMotion();
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
this.applyMotion();
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
this.applyMotion();
|
||||
}
|
||||
}
|
||||
|
||||
if (this.skipQuadTree === false && this.allowCollision.none === false && this.sprite.visible && this.sprite.alive)
|
||||
@@ -405,99 +452,69 @@ Phaser.Physics.Arcade.Body.prototype = {
|
||||
this.game.physics.quadTree.insert(this);
|
||||
}
|
||||
|
||||
this.prevVelocity.copyFrom(this.velocity);
|
||||
|
||||
},
|
||||
|
||||
applyMotion: function () {
|
||||
|
||||
this.x = this.x + this.game.time.physicsElapsed * (this.velocity.x + this.motionVelocity.x / 2);
|
||||
this.velocity.x = this.velocity.x + this.motionVelocity.x;
|
||||
|
||||
this.y = this.y + this.game.time.physicsElapsed * (this.velocity.y + this.motionVelocity.y / 2);
|
||||
this.velocity.y = this.velocity.y + this.motionVelocity.y;
|
||||
|
||||
},
|
||||
|
||||
/**
|
||||
* Internal method.
|
||||
* Internal method. This is called directly before the sprites are sent to the renderer.
|
||||
*
|
||||
* @method Phaser.Physics.Arcade#postUpdate
|
||||
* @protected
|
||||
*/
|
||||
postUpdate: function () {
|
||||
|
||||
// if (this.overlapX !== 0)
|
||||
// {
|
||||
// this.x -= this.overlapX;
|
||||
// }
|
||||
|
||||
// if (this.overlapY !== 0)
|
||||
// {
|
||||
// this.y -= this.overlapY;
|
||||
// }
|
||||
|
||||
if (this.deltaX() < 0 && this.blocked.left === false)
|
||||
if (this.moves)
|
||||
{
|
||||
this.facing = Phaser.LEFT;
|
||||
this.sprite.x += this.deltaX();
|
||||
}
|
||||
else if (this.deltaX() > 0 && this.blocked.right === false)
|
||||
{
|
||||
this.facing = Phaser.RIGHT;
|
||||
this.sprite.x += this.deltaX();
|
||||
}
|
||||
if (this.collideWorldBounds)
|
||||
{
|
||||
// this.checkWorldBounds();
|
||||
}
|
||||
|
||||
if (this.deltaY() < 0 && this.blocked.up === false)
|
||||
{
|
||||
this.facing = Phaser.UP;
|
||||
this.sprite.y += this.deltaY();
|
||||
}
|
||||
else if (this.deltaY() > 0 && this.blocked.down === false)
|
||||
{
|
||||
this.facing = Phaser.DOWN;
|
||||
this.sprite.y += this.deltaY();
|
||||
}
|
||||
if (this.deltaX() < 0 && this.blocked.left === false)
|
||||
{
|
||||
this.facing = Phaser.LEFT;
|
||||
this.sprite.x += this.deltaX();
|
||||
}
|
||||
else if (this.deltaX() > 0 && this.blocked.right === false)
|
||||
{
|
||||
this.facing = Phaser.RIGHT;
|
||||
this.sprite.x += this.deltaX();
|
||||
}
|
||||
|
||||
// if (this.deltaY() !== 0 && this.blocked.up === false && this.blocked.down === false)
|
||||
// {
|
||||
// this.sprite.y += this.deltaY();
|
||||
// }
|
||||
if (this.deltaY() < 0 && this.blocked.up === false)
|
||||
{
|
||||
this.facing = Phaser.UP;
|
||||
this.sprite.y += this.deltaY();
|
||||
}
|
||||
else if (this.deltaY() > 0 && this.blocked.down === false)
|
||||
{
|
||||
this.facing = Phaser.DOWN;
|
||||
this.sprite.y += this.deltaY();
|
||||
}
|
||||
|
||||
// if (this.deltaX() !== 0 || this.deltaY() !== 0)
|
||||
// {
|
||||
// this.sprite.x += this.deltaX();
|
||||
// this.sprite.y += this.deltaY();
|
||||
|
||||
// this.center.setTo(this.x + this.halfWidth, this.y + this.halfHeight);
|
||||
// }
|
||||
|
||||
// if (this.deltaX() !== 0 || this.deltaY() !== 0)
|
||||
// {
|
||||
this.center.setTo(this.x + this.halfWidth, this.y + this.halfHeight);
|
||||
// }
|
||||
|
||||
if (this.allowRotation)
|
||||
{
|
||||
this.sprite.angle += this.deltaZ();
|
||||
if (this.allowRotation)
|
||||
{
|
||||
this.sprite.angle += this.deltaZ();
|
||||
}
|
||||
}
|
||||
|
||||
},
|
||||
|
||||
/**
|
||||
* Internal method.
|
||||
*
|
||||
* @method Phaser.Physics.Arcade#updateHulls
|
||||
* @protected
|
||||
*/
|
||||
updateHulls: function (separation) {
|
||||
|
||||
// this.hull.setTo(this.x, this.y, this.width, this.height);
|
||||
|
||||
// if (separation)
|
||||
// {
|
||||
// this.hullX.setTo(this.x, this.preY, this.width, this.height);
|
||||
// this.hullY.setTo(this.preX, this.y, this.width, this.height);
|
||||
// }
|
||||
// else
|
||||
// {
|
||||
// if this has separated then the preX/Y values are no longer valid
|
||||
// this.hullX.setTo(this.x, this.y, this.width, this.height);
|
||||
// this.hullY.setTo(this.x, this.y, this.width, this.height);
|
||||
// }
|
||||
|
||||
},
|
||||
|
||||
/**
|
||||
* Internal method.
|
||||
* Internal method used to check the Body against the World Bounds.
|
||||
*
|
||||
* @method Phaser.Physics.Arcade#checkWorldBounds
|
||||
* @protected
|
||||
@@ -506,22 +523,26 @@ Phaser.Physics.Arcade.Body.prototype = {
|
||||
|
||||
if (this.x < this.game.world.bounds.x)
|
||||
{
|
||||
this.blocked.left = true;
|
||||
this.x = this.game.world.bounds.x;
|
||||
this.velocity.x *= -this.bounce.x;
|
||||
}
|
||||
else if (this.right > this.game.world.bounds.right)
|
||||
{
|
||||
this.blocked.right = true;
|
||||
this.x = this.game.world.bounds.right - this.width;
|
||||
this.velocity.x *= -this.bounce.x;
|
||||
}
|
||||
|
||||
if (this.y < this.game.world.bounds.y)
|
||||
{
|
||||
this.blocked.up = true;
|
||||
this.y = this.game.world.bounds.y;
|
||||
this.velocity.y *= -this.bounce.y;
|
||||
}
|
||||
else if (this.bottom > this.game.world.bounds.bottom)
|
||||
{
|
||||
this.blocked.down = true;
|
||||
this.y = this.game.world.bounds.bottom - this.height;
|
||||
this.velocity.y *= -this.bounce.y;
|
||||
}
|
||||
|
||||
+1
-1
@@ -58,7 +58,7 @@ Phaser.Time = function (game) {
|
||||
this.now = 0;
|
||||
|
||||
/**
|
||||
* @property {number} elapsed - Elapsed time since the last frame.
|
||||
* @property {number} elapsed - Elapsed time since the last frame (in ms).
|
||||
*/
|
||||
this.elapsed = 0;
|
||||
|
||||
|
||||
+80
-22
@@ -30,6 +30,11 @@ Phaser.Utils.Debug = function (game) {
|
||||
*/
|
||||
this.font = '14px Courier';
|
||||
|
||||
/**
|
||||
* @property {number} columnWidth - The spacing between columns.
|
||||
*/
|
||||
this.columnWidth = 100;
|
||||
|
||||
/**
|
||||
* @property {number} lineHeight - The line height between the debug text.
|
||||
*/
|
||||
@@ -65,11 +70,12 @@ Phaser.Utils.Debug.prototype = {
|
||||
/**
|
||||
* Internal method that resets and starts the debug output values.
|
||||
* @method Phaser.Utils.Debug#start
|
||||
* @param {number} x - The X value the debug info will start from.
|
||||
* @param {number} y - The Y value the debug info will start from.
|
||||
* @param {string} color - The color the debug info will drawn in.
|
||||
* @param {number} [x=0] - The X value the debug info will start from.
|
||||
* @param {number} [y=0] - The Y value the debug info will start from.
|
||||
* @param {string} [color='rgb(255,255,255)'] - The color the debug text will drawn in.
|
||||
* @param {number} [columnWidth=0] - The spacing between columns.
|
||||
*/
|
||||
start: function (x, y, color) {
|
||||
start: function (x, y, color, columnWidth) {
|
||||
|
||||
if (this.context == null)
|
||||
{
|
||||
@@ -78,13 +84,14 @@ Phaser.Utils.Debug.prototype = {
|
||||
|
||||
if (typeof x !== 'number') { x = 0; }
|
||||
if (typeof y !== 'number') { y = 0; }
|
||||
|
||||
color = color || 'rgb(255,255,255)';
|
||||
if (typeof columnWidth === 'undefined') { columnWidth = 0; }
|
||||
|
||||
this.currentX = x;
|
||||
this.currentY = y;
|
||||
this.currentColor = color;
|
||||
this.currentAlpha = this.context.globalAlpha;
|
||||
this.columnWidth = columnWidth;
|
||||
|
||||
this.context.save();
|
||||
this.context.setTransform(1, 0, 0, 1, 0, 0);
|
||||
@@ -100,7 +107,6 @@ Phaser.Utils.Debug.prototype = {
|
||||
*/
|
||||
stop: function () {
|
||||
|
||||
|
||||
this.context.restore();
|
||||
this.context.globalAlpha = this.currentAlpha;
|
||||
|
||||
@@ -110,8 +116,8 @@ Phaser.Utils.Debug.prototype = {
|
||||
* Internal method that outputs a single line of text.
|
||||
* @method Phaser.Utils.Debug#line
|
||||
* @param {string} text - The line of text to draw.
|
||||
* @param {number} x - The X value the debug info will start from.
|
||||
* @param {number} y - The Y value the debug info will start from.
|
||||
* @param {number} [x] - The X value the debug info will start from.
|
||||
* @param {number} [y] - The Y value the debug info will start from.
|
||||
*/
|
||||
line: function (text, x, y) {
|
||||
|
||||
@@ -120,16 +126,8 @@ Phaser.Utils.Debug.prototype = {
|
||||
return;
|
||||
}
|
||||
|
||||
x = x || null;
|
||||
y = y || null;
|
||||
|
||||
if (x !== null) {
|
||||
this.currentX = x;
|
||||
}
|
||||
|
||||
if (y !== null) {
|
||||
this.currentY = y;
|
||||
}
|
||||
if (typeof x !== 'undefined') { this.currentX = x; }
|
||||
if (typeof y !== 'undefined') { this.currentY = y; }
|
||||
|
||||
if (this.renderShadow)
|
||||
{
|
||||
@@ -143,6 +141,38 @@ Phaser.Utils.Debug.prototype = {
|
||||
|
||||
},
|
||||
|
||||
/**
|
||||
* Internal method that outputs a single line of text split over as many columns as needed, one per parameter.
|
||||
* @method Phaser.Utils.Debug#splitline
|
||||
* @param {string} text - The text to render. You can have as many columns of text as you want, just pass them as additional parameters.
|
||||
*/
|
||||
splitline: function (text) {
|
||||
|
||||
if (this.context == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var x = this.currentX;
|
||||
|
||||
for (var i = 0; i < arguments.length; i++)
|
||||
{
|
||||
if (this.renderShadow)
|
||||
{
|
||||
this.context.fillStyle = 'rgb(0,0,0)';
|
||||
this.context.fillText(arguments[i], x + 1, this.currentY + 1);
|
||||
this.context.fillStyle = this.currentColor;
|
||||
}
|
||||
|
||||
this.context.fillText(arguments[i], x, this.currentY);
|
||||
|
||||
x += this.columnWidth;
|
||||
}
|
||||
|
||||
this.currentY += this.lineHeight;
|
||||
|
||||
},
|
||||
|
||||
/**
|
||||
* Visually renders a QuadTree to the display.
|
||||
* @method Phaser.Utils.Debug#renderQuadTree
|
||||
@@ -399,14 +429,42 @@ Phaser.Utils.Debug.prototype = {
|
||||
|
||||
color = color || 'rgb(255,255,255)';
|
||||
|
||||
this.start(x, y, color);
|
||||
this.line('Sprite Collision: (' + sprite.width + ' x ' + sprite.height + ')');
|
||||
this.start(x, y, color, 100);
|
||||
|
||||
this.line('Body: (width: ' + sprite.width + ' height: ' + sprite.height + ')');
|
||||
this.line('left: ' + sprite.body.touching.left);
|
||||
this.line('right: ' + sprite.body.touching.right);
|
||||
this.line('up: ' + sprite.body.touching.up);
|
||||
this.line('down: ' + sprite.body.touching.down);
|
||||
this.line('velocity.x: ' + sprite.body.velocity.x);
|
||||
this.line('velocity.y: ' + sprite.body.velocity.y);
|
||||
this.stop();
|
||||
|
||||
},
|
||||
|
||||
/**
|
||||
* Render Sprite Body Physics Data as text.
|
||||
* @method Phaser.Utils.Debug#renderBodyInfo
|
||||
* @param {Phaser.Sprite} sprite - The sprite to be rendered.
|
||||
* @param {number} x - X position of the debug info to be rendered.
|
||||
* @param {number} y - Y position of the debug info to be rendered.
|
||||
* @param {string} [color='rgb(255,255,255)'] - color of the debug info to be rendered. (format is css color string).
|
||||
*/
|
||||
renderBodyInfo: function (sprite, x, y, color) {
|
||||
|
||||
color = color || 'rgb(255,255,255)';
|
||||
|
||||
this.start(x, y, color, 220);
|
||||
|
||||
this.splitline('Width: ' + sprite.width, 'Height: ' + sprite.height);
|
||||
this.splitline('x: ' + sprite.body.x.toFixed(2), 'y: ' + sprite.body.y.toFixed(2));
|
||||
this.splitline('old x: ' + sprite.body.preX.toFixed(2), 'y: ' + sprite.body.preY.toFixed(2));
|
||||
this.splitline('drag x: ' + sprite.body.drag.x, 'y: ' + sprite.body.drag.y);
|
||||
this.splitline('gravity x: ' + sprite.body.gravity.x, 'y: ' + sprite.body.gravity.y);
|
||||
this.splitline('acceleration x: ' + sprite.body.acceleration.x.toFixed(2), 'y: ' + sprite.body.acceleration.y.toFixed(2));
|
||||
this.splitline('velocity x: ' + sprite.body.velocity.x.toFixed(2), 'y: ' + sprite.body.velocity.y.toFixed(2));
|
||||
this.splitline('motion x: ' + sprite.body.motionVelocity.x.toFixed(2), 'y: ' + sprite.body.motionVelocity.y.toFixed(2));
|
||||
this.splitline('sleeping: ' + sprite.body.sleeping, 'sleepTimer: ' + sprite.body._sleepTimer.toFixed(2));
|
||||
this.splitline('sleepMin x: ' + sprite.body.sleepMin.x, 'y: ' + sprite.body.sleepMin.y);
|
||||
this.splitline('sleepMax x: ' + sprite.body.sleepMax.x, 'y: ' + sprite.body.sleepMax.y);
|
||||
this.stop();
|
||||
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user