Added Game core loop stepping support. Super-useful for debugging, and helped me track down the issue with jittery physics collision. Double-win!

This commit is contained in:
photonstorm
2014-01-29 17:10:13 +00:00
parent d51a37211c
commit 651858372c
25 changed files with 1268 additions and 438 deletions
+8 -4
View File
@@ -207,28 +207,32 @@ Phaser.Camera.prototype = {
if (this.deadzone)
{
this._edge = this.target.bounds.x - this.deadzone.x;
// this._edge = this.target.bounds.x - this.deadzone.x;
this._edge = this.target.x - this.deadzone.x;
if (this.view.x > this._edge)
{
this.view.x = this._edge;
}
this._edge = this.target.bounds.right - this.deadzone.x - this.deadzone.width;
// this._edge = this.target.bounds.right - this.deadzone.x - this.deadzone.width;
this._edge = this.target.x + this.target.width - this.deadzone.x - this.deadzone.width;
if (this.view.x < this._edge)
{
this.view.x = this._edge;
}
this._edge = this.target.bounds.y - this.deadzone.y;
// this._edge = this.target.bounds.y - this.deadzone.y;
this._edge = this.target.y - this.deadzone.y;
if (this.view.y > this._edge)
{
this.view.y = this._edge;
}
this._edge = this.target.bounds.bottom - this.deadzone.y - this.deadzone.height;
// this._edge = this.target.bounds.bottom - this.deadzone.y - this.deadzone.height;
this._edge = this.target.y + this.target.height - this.deadzone.y - this.deadzone.height;
if (this.view.y < this._edge)
{
+44 -13
View File
@@ -283,6 +283,10 @@ Phaser.Game = function (width, height, renderer, parent, state, transparent, ant
window.addEventListener('load', this._onBoot, false);
}
this.pendingStep = false;
this.stepping = true;
this.stepCount = 0;
return this;
};
@@ -580,20 +584,32 @@ Phaser.Game.prototype = {
}
else
{
this.plugins.preUpdate();
this.world.preUpdate();
if (!this.pendingStep)
{
if (this.stepping)
{
this.pendingStep = true;
}
this.stage.update();
this.input.update();
this.tweens.update();
this.sound.update();
this.state.update();
this.world.update();
this.particles.update();
this.plugins.update();
this.plugins.preUpdate();
console.log('world preUpdate');
this.world.preUpdate();
this.world.postUpdate();
this.plugins.postUpdate();
this.stage.update();
this.input.update();
this.tweens.update();
this.sound.update();
console.log('state update');
this.state.update();
console.log('world update');
this.world.update();
this.particles.update();
this.plugins.update();
console.log('world postUpdate');
this.world.postUpdate();
this.plugins.postUpdate();
}
if (this.renderType !== Phaser.HEADLESS)
{
@@ -603,11 +619,26 @@ Phaser.Game.prototype = {
this.plugins.postRender();
}
}
},
enableStep: function () {
this.stepping = true;
this.pendingStep = false;
this.stepCount = 0;
},
step: function () {
this.pendingStep = false;
this.stepCount++;
console.log('--------- Game step', this.stepCount);
},
/**
* Nuke the entire game from orbit
*
+8 -9
View File
@@ -156,22 +156,21 @@ Phaser.World.prototype.postUpdate = function () {
*/
Phaser.World.prototype.setBounds = function (x, y, width, height) {
if (width < this.game.width)
{
width = this.game.width;
}
if (width < this.game.width)
{
width = this.game.width;
}
if (height < this.game.height)
{
height = this.game.height;
}
if (height < this.game.height)
{
height = this.game.height;
}
this.bounds.setTo(x, y, width, height);
if (this.camera.bounds)
{
// The Camera can never be smaller than the game size
this.camera.bounds.setTo(x, y, width, height);
}
+42 -5
View File
@@ -373,6 +373,8 @@ Phaser.Sprite = function (game, x, y, key, frame) {
this.updateCache();
this.updateBounds();
this.debug = false;
/**
* @property {PIXI.Point} pivot - The pivot point of the displayObject that it rotates around.
*/
@@ -392,6 +394,11 @@ Phaser.Sprite.prototype.constructor = Phaser.Sprite;
*/
Phaser.Sprite.prototype.preUpdate = function() {
if (this.debug)
{
console.log('Sprite preUpdate xy: ', this.x, this.y, 'wxy:', this.world.x, this.world.y);
}
if (!this.exists || (this.group && !this.group.exists))
{
this.renderOrderID = -1;
@@ -617,9 +624,7 @@ Phaser.Sprite.prototype.updateBounds = function() {
* @memberof Phaser.Sprite
* @param {Phaser.Point} p - The Point object to store the results in.
* @param {number} x - x coordinate within the Sprite to translate.
* @param {number} y - x coordinate within the Sprite to translate.
* @param {number} sx - Scale factor to be applied.
* @param {number} sy - Scale factor to be applied.
* @param {number} y - y coordinate within the Sprite to translate.
* @return {Phaser.Point} The translated point.
*/
Phaser.Sprite.prototype.getLocalPosition = function(p, x, y) {
@@ -638,8 +643,8 @@ Phaser.Sprite.prototype.getLocalPosition = function(p, x, y) {
* @method Phaser.Sprite#getLocalUnmodifiedPosition
* @memberof Phaser.Sprite
* @param {Phaser.Point} p - The Point object to store the results in.
* @param {number} x - x coordinate within the Sprite to translate.
* @param {number} y - x coordinate within the Sprite to translate.
* @param {number} gx - x coordinate within the Sprite to translate.
* @param {number} gy - y coordinate within the Sprite to translate.
* @return {Phaser.Point} The translated point.
*/
Phaser.Sprite.prototype.getLocalUnmodifiedPosition = function(p, gx, gy) {
@@ -701,6 +706,12 @@ Phaser.Sprite.prototype.postUpdate = function() {
this.position.x = this._cache.x;
this.position.y = this._cache.y;
if (this.debug)
{
console.log('Sprite postUpdate xy: ', this.x, this.y, 'right:', this.right);
}
}
};
@@ -1084,6 +1095,32 @@ Object.defineProperty(Phaser.Sprite.prototype, "inCamera", {
});
/**
* @name Phaser.Sprite#worldCenterX
* @property {number} worldCenterX - The center of the Sprite in world coordinates.
* @readonly
*/
Object.defineProperty(Phaser.Sprite.prototype, "worldCenterX", {
get: function () {
return this.game.camera.x + this.center.x;
}
});
/**
* @name Phaser.Sprite#worldCenterY
* @property {number} worldCenterY - The center of the Sprite in world coordinates.
* @readonly
*/
Object.defineProperty(Phaser.Sprite.prototype, "worldCenterY", {
get: function () {
return this.game.camera.y + this.center.y;
}
});
/**
* The width of the sprite in pixels, setting this will actually modify the scale to acheive the value desired.
* If you wish to crop the Sprite instead see the Sprite.crop value.
+67 -61
View File
@@ -70,11 +70,9 @@ Phaser.Line.prototype = {
},
/**
* Checks for intersection between two lines.
* If asSegment is true it will check for segment intersection.
* If asSegment is false it will check for line intersection.
* Checks for intersection between this line and another Line.
* If asSegment is true it will check for segment intersection. If asSegment is false it will check for line intersection.
* Returns the intersection segment of AB and EF as a Point, or null if there is no intersection.
* Adapted from code by Keith Hair
*
* @method Phaser.Line#intersects
* @param {Phaser.Line} line - The line to check against this one.
@@ -84,7 +82,7 @@ Phaser.Line.prototype = {
*/
intersects: function (line, asSegment, result) {
return Phaser.Line.intersects(this, line, asSegment, result);
return Phaser.Line.intersectsPoints(this.start, this.end, line.start, line.end, asSegment, result);
},
@@ -173,6 +171,69 @@ Object.defineProperty(Phaser.Line.prototype, "perpSlope", {
});
/**
* Checks for intersection between two lines as defined by the given start and end points.
* If asSegment is true it will check for line segment intersection. If asSegment is false it will check for line intersection.
* Returns the intersection segment of AB and EF as a Point, or null if there is no intersection.
* Adapted from code by Keith Hair
*
* @method Phaser.Line.intersects
* @param {Phaser.Point} a - The start of the first Line to be checked.
* @param {Phaser.Point} b - The end of the first line to be checked.
* @param {Phaser.Point} e - The start of the second Line to be checked.
* @param {Phaser.Point} f - The end of the second line to be checked.
* @param {boolean} [asSegment=true] - If true it will check for segment intersection, otherwise full line intersection.
* @param {Phaser.Point} [result] - A Point object to store the result in, if not given a new one will be created.
* @return {Phaser.Point} The intersection segment of the two lines as a Point, or null if there is no intersection.
*/
Phaser.Line.intersectsPoints = function (a, b, e, f, asSegment, result) {
if (typeof asSegment === 'undefined') { asSegment = true; }
if (typeof result === 'undefined') { result = new Phaser.Point(); }
var a1 = b.y - a.y;
var a2 = f.y - e.y;
var b1 = a.x - b.x;
var b2 = e.x - f.x;
var c1 = (b.x * a.y) - (a.x * b.y);
var c2 = (f.x * e.y) - (e.x * f.y);
var denom = (a1 * b2) - (a2 * b1);
if (denom === 0)
{
return null;
}
result.x = ((b1 * c2) - (b2 * c1)) / denom;
result.y = ((a2 * c1) - (a1 * c2)) / denom;
if (asSegment)
{
if (Math.pow((result.x - b.x) + (result.y - b.y), 2) > Math.pow((a.x - b.x) + (a.y - b.y), 2))
{
return null;
}
if (Math.pow((result.x - a.x) + (result.y - a.y), 2) > Math.pow((a.x - b.x) + (a.y - b.y), 2))
{
return null;
}
if (Math.pow((result.x - f.x) + (result.y - f.y), 2) > Math.pow((e.x - f.x) + (e.y - f.y), 2))
{
return null;
}
if (Math.pow((result.x - e.x) + (result.y - e.y), 2) > Math.pow((e.x - f.x) + (e.y - f.y), 2))
{
return null;
}
}
return result;
};
/**
* Checks for intersection between two lines.
* If asSegment is true it will check for segment intersection.
@@ -189,61 +250,6 @@ Object.defineProperty(Phaser.Line.prototype, "perpSlope", {
*/
Phaser.Line.intersects = function (a, b, asSegment, result) {
if (typeof asSegment === 'undefined') { asSegment = true; }
if (typeof result === 'undefined') { result = new Phaser.Point(); }
// var a1 = B.y - A.y;
// var a2 = F.y - E.y;
// var b1 = A.x - B.x;
// var b2 = E.x - F.x;
// var c1 = (B.x * A.y) - (A.x * B.y);
// var c2 = (F.x * E.y) - (E.x * F.y);
// var denom = (a1 * b2) - (a2 * b1);
// A = a.start
// B = a.end
// E = b.start
// F = b.end
var a1 = a.end.y - a.start.y;
var a2 = b.end.y - b.start.y;
var b1 = a.start.x - a.end.x;
var b2 = b.start.x - b.end.x;
var c1 = (a.end.x * a.start.y) - (a.start.x * a.end.y);
var c2 = (b.end.x * b.start.y) - (b.start.x * b.end.y);
var denom = (a1 * b2) - (a2 * b1);
if (denom === 0)
{
return null;
}
result.x = ((b1 * c2) - (b2 * c1)) / denom;
result.y = ((a2 * c1) - (a1 * c2)) / denom;
if (asSegment)
{
if (Math.pow((result.x - a.end.x) + (result.y - a.end.y), 2) > Math.pow((a.start.x - a.end.x) + (a.start.y - a.end.y), 2))
{
return null;
}
if (Math.pow((result.x - a.start.x) + (result.y - a.start.y), 2) > Math.pow((a.start.x - a.end.x) + (a.start.y - a.end.y), 2))
{
return null;
}
if (Math.pow((result.x - b.end.x) + (result.y - b.end.y), 2) > Math.pow((b.start.x - b.end.x) + (b.start.y - b.end.y), 2))
{
return null;
}
if (Math.pow((result.x - b.start.x) + (result.y - b.start.y), 2) > Math.pow((b.start.x - b.end.x) + (b.start.y - b.end.y), 2))
{
return null;
}
}
return result;
return Phaser.Line.intersectsPoints(a.start, a.end, b.start, b.end, asSegment, result);
};
+102 -4
View File
@@ -660,7 +660,7 @@ Phaser.Math = {
},
/**
* Significantly faster version of Math.min
* Updated version of Math.min that can be passed either an array of numbers or the numbers as parameters.
* See http://jsperf.com/math-s-min-max-vs-homemade/5
*
* @method Phaser.Math#min
@@ -668,15 +668,113 @@ Phaser.Math = {
*/
min: function () {
for (var i =1 , min = 0, len = arguments.length; i < len; i++)
if (arguments.length === 1 && typeof arguments[0] === 'object')
{
if (arguments[i] < arguments[min])
var data = arguments[0];
}
else
{
var data = arguments;
}
for (var i = 1, min = 0, len = data.length; i < len; i++)
{
if (data[i] < data[min])
{
min = i;
}
}
return arguments[min];
return data[min];
},
/**
* Updated version of Math.max that can be passed either an array of numbers or the numbers as parameters.
*
* @method Phaser.Math#max
* @return {number} The largest value from those given.
*/
max: function () {
if (arguments.length === 1 && typeof arguments[0] === 'object')
{
var data = arguments[0];
}
else
{
var data = arguments;
}
for (var i = 1, max = 0, len = data.length; i < len; i++)
{
if (data[i] > data[max])
{
max = i;
}
}
return data[max];
},
/**
* Updated version of Math.min that can be passed a property and either an array of objects or the objects as parameters.
* It will find the lowest matching property value from the given objects.
*
* @method Phaser.Math#minProperty
* @return {number} The lowest value from those given.
*/
minProperty: function (property) {
if (arguments.length === 2 && typeof arguments[1] === 'object')
{
var data = arguments[1];
}
else
{
var data = arguments.slice(1);
}
for (var i = 1, min = 0, len = data.length; i < len; i++)
{
if (data[i][property] < data[min][property])
{
min = i;
}
}
return data[min][property];
},
/**
* Updated version of Math.max that can be passed a property and either an array of objects or the objects as parameters.
* It will find the largest matching property value from the given objects.
*
* @method Phaser.Math#maxProperty
* @return {number} The largest value from those given.
*/
maxProperty: function (property) {
if (arguments.length === 2 && typeof arguments[1] === 'object')
{
var data = arguments[1];
}
else
{
var data = arguments.slice(1);
}
for (var i = 1, max = 0, len = data.length; i < len; i++)
{
if (data[i][property] > data[max][property])
{
max = i;
}
}
return data[max][property];
},
+116 -19
View File
@@ -706,6 +706,9 @@ Phaser.Physics.Arcade.prototype = {
*/
collideSpriteVsTilemapLayer: function (sprite, tilemapLayer, collideCallback, processCallback, callbackContext) {
console.log('collideSpriteVsTilemapLayer x:', sprite.x, 'y:', sprite.y, 'body left:', sprite.body.left, 'right:', sprite.body.right);
this._mapData = tilemapLayer.getTiles(sprite.body.left, sprite.body.top, sprite.body.width, sprite.body.height, true);
if (this._mapData.length === 0)
@@ -839,6 +842,37 @@ Phaser.Physics.Arcade.prototype = {
},
/**
* Performs a rect intersection test against the two objects.
* Objects must expose properties: width, height, left, right, top, bottom.
* @method Phaser.Physics.Arcade#intersects
* @param {object} body - The Body to test.
* @param {object} tile - The Tile to test.
* @returns {boolean} Returns true if the objects intersect, otherwise false.
*/
tileIntersects: function (body, tile) {
if (body.width <= 0 || body.height <= 0 || tile.width <= 0 || tile.height <= 0)
{
return false;
}
// console.log('body: ', body.left, body.top, body.right, body.bottom);
// console.log('tile: ', tile.x, tile.y, tile.right, tile.bottom);
// console.log('intersect #1', body.right < tile.x, body.right, tile.x);
// console.log('intersect #2', body.bottom < tile.y, body.bottom, tile.y);
// console.log('intersect #3', body.left > tile.right, body.left, tile.right);
// console.log('intersect #4', body.top > tile.bottom, body.top, tile.bottom);
// return !(a.right < b.x || a.bottom < b.y || a.x > b.right || a.y > b.bottom);
result = !(body.right < tile.x || body.bottom < tile.y || body.left > tile.right || body.top > tile.bottom);
return result;
},
/**
* The core separation function to separate a physics body and an array of tiles.
* @method Phaser.Physics.Arcade#separateTiles
@@ -860,12 +894,13 @@ Phaser.Physics.Arcade.prototype = {
var tile;
var localOverlapX = 0;
var localOverlapY = 0;
var process = false;
for (var i = 0; i < tiles.length; i++)
{
tile = tiles[i];
if (this.intersects(body, tile))
if (this.tileIntersects(body, tile))
{
// They overlap. Any custom callbacks?
if (tile.tile.callback || tile.layer.callbacks[tile.tile.index])
@@ -883,55 +918,67 @@ Phaser.Physics.Arcade.prototype = {
}
}
if (body.deltaX() < 0 && body.checkCollision.left && tile.tile.faceRight)
if (body.deltaX() < 0 && body.checkCollision.left && tile.tile.faceRight && !body.blocked.left)
{
// LEFT
localOverlapX = body.left - tile.right;
console.log('STS left', localOverlapX, body.deltaX(), 'bt', body.left, tile.right);
if (localOverlapX >= body.deltaX())
{
body.blocked.left = true;
body.touching.left = true;
body.touching.none = false;
process = true;
}
}
else if (body.deltaX() > 0 && body.checkCollision.right && tile.tile.faceLeft)
else if (body.deltaX() > 0 && body.checkCollision.right && tile.tile.faceLeft && !body.blocked.right)
{
// RIGHT
localOverlapX = body.right - tile.x;
console.log('STS right', localOverlapX, body.deltaX(), 'bt', body.right, tile.x);
// Distance check
if (localOverlapX <= body.deltaX())
{
body.blocked.right = true;
body.touching.right = true;
body.touching.none = false;
process = true;
}
}
if (body.deltaY() < 0 && body.checkCollision.up && tile.tile.faceBottom)
if (body.deltaY() < 0 && body.checkCollision.up && tile.tile.faceBottom && !body.blocked.up)
{
// UP
localOverlapY = body.top - tile.bottom;
console.log('STS up', localOverlapY, body.deltaY(), 'bt', body.top, tile.bottom);
// Distance check
if (localOverlapY >= body.deltaY())
{
body.blocked.up = true;
body.touching.up = true;
body.touching.none = false;
process = true;
}
}
else if (body.deltaY() > 0 && body.checkCollision.down && tile.tile.faceTop)
else if (body.deltaY() > 0 && body.checkCollision.down && tile.tile.faceTop && !body.blocked.down)
{
// DOWN
localOverlapY = body.bottom - tile.y;
console.log('STS down', localOverlapY, body.deltaY(), 'bt', body.bottom, tile.y);
if (localOverlapY <= body.deltaY())
{
body.blocked.down = true;
body.touching.down = true;
body.touching.none = false;
process = true;
}
}
}
@@ -947,7 +994,14 @@ Phaser.Physics.Arcade.prototype = {
body.overlapY = localOverlapY;
}
return this.processTileSeparation(body);
if (process)
{
return this.processTileSeparation(body);
}
else
{
return false;
}
},
@@ -961,11 +1015,16 @@ Phaser.Physics.Arcade.prototype = {
separateTile: function (body, tile) {
// Can't separate two immovable objects (tiles are always immovable)
if (body.immovable || this.intersects(body, tile) === false)
if (body.immovable || this.tileIntersects(body, tile) === false)
{
console.log('fail');
console.log('body: ', body.left, body.top, body.right, body.bottom);
console.log('tile: ', tile.x, tile.y, tile.right, tile.bottom);
return false;
}
console.log('CHECK');
// They overlap. Any custom callbacks?
if (tile.tile.callback || tile.layer.callbacks[tile.tile.index])
{
@@ -986,62 +1045,87 @@ Phaser.Physics.Arcade.prototype = {
body.overlapX = 0;
body.overlapY = 0;
// Remember - this happens AFTER the body has been moved by the motion update, so it needs moving back again
if (body.deltaX() < 0 && body.checkCollision.left && tile.tile.faceRight)
var process = false;
if (body.deltaX() < 0 && body.checkCollision.left && tile.tile.faceRight && !body.blocked.left)
{
// LEFT
body.overlapX = body.left - tile.right;
if (body.overlapX >= body.deltaX())
console.log('ST left', body.overlapX, body.deltaX(), 'bt', body.left, tile.right);
if (body.overlapX <= body.deltaX())
{
// use touching instead of blocked?
console.log('pass');
body.blocked.left = true;
body.touching.left = true;
body.touching.none = false;
process = true;
}
}
else if (body.deltaX() > 0 && body.checkCollision.right && tile.tile.faceLeft)
else if (body.deltaX() > 0 && body.checkCollision.right && tile.tile.faceLeft && !body.blocked.right)
{
// RIGHT
body.overlapX = body.right - tile.x;
console.log(tile);
console.log('ST right', body.overlapX, body.deltaX(), 'bt', body.right, tile.x);
// Distance check
if (body.overlapX <= body.deltaX())
if (body.overlapX >= body.deltaX())
{
console.log('pass');
body.blocked.right = true;
body.touching.right = true;
body.touching.none = false;
process = true;
}
}
if (body.deltaY() < 0 && body.checkCollision.up && tile.tile.faceBottom)
if (body.deltaY() < 0 && body.checkCollision.up && tile.tile.faceBottom && !body.blocked.up)
{
// UP
body.overlapY = body.top - tile.bottom;
console.log('ST up', body.overlapY, body.deltaY(), 'bt', body.top, tile.bottom);
// Distance check
if (body.overlapY >= body.deltaY())
{
console.log('pass');
body.blocked.up = true;
body.touching.up = true;
body.touching.none = false;
process = true;
}
}
else if (body.deltaY() > 0 && body.checkCollision.down && tile.tile.faceTop)
else if (body.deltaY() > 0 && body.checkCollision.down && tile.tile.faceTop && !body.blocked.down)
{
// DOWN
body.overlapY = body.bottom - tile.y;
console.log('ST down', body.overlapY, body.deltaY(), 'bt', body.bottom, tile.y);
if (body.overlapY <= body.deltaY())
{
console.log('pass');
body.blocked.down = true;
body.touching.down = true;
body.touching.none = false;
process = true;
}
}
// Separate in a single sweep
return this.processTileSeparation(body);
if (process)
{
return this.processTileSeparation(body);
}
else
{
return false;
}
},
@@ -1057,13 +1141,26 @@ Phaser.Physics.Arcade.prototype = {
// Swap for a hit tile?
if (body.touching.none)
{
console.log('processTileSeparation QUIT');
return false;
}
body.x += body.overlapX;
body.y += body.overlapY;
console.log('pre processTileSeparation', body.x, body.y);
if (body.touching.left || body.touching.right || body.blocked.left || body.blocked.right)
{
body.x -= body.overlapX;
}
if (body.touching.up || body.touching.down || body.blocked.up || body.blocked.down)
{
body.y -= body.overlapY;
}
console.log('post processTileSeparation', body.x, body.y, body.right);
body.setBlockFlag(body.blocked.left, body.blocked.right, body.blocked.up, body.blocked.down, body.overlapX, body.overlapY);
// body.setBlockFlag(body.blocked.left, body.blocked.right, body.blocked.up, body.blocked.down, body.overlapX, body.overlapY);
// body.reboundCheck(true, true, true);
return true;
+137 -106
View File
@@ -55,13 +55,13 @@ Phaser.Physics.Arcade.Body = function (sprite) {
* @property {number} preX - The previous x position of the physics body.
* @readonly
*/
this.preX = sprite.x;
this.preX = sprite.world.x;
/**
* @property {number} preY - The previous y position of the physics body.
* @readonly
*/
this.preY = sprite.y;
this.preY = sprite.world.y;
/**
* @property {number} preRotation - The previous rotation of the physics body.
@@ -333,7 +333,7 @@ Phaser.Physics.Arcade.Body = function (sprite) {
this.overlapY = 0;
// Set-up the default shape
this.setRectangle(sprite.width, sprite.height, -sprite._cache.halfWidth, -sprite._cache.halfHeight);
this.setRectangle(sprite.width, sprite.height, 0, 0);
};
@@ -350,11 +350,11 @@ Phaser.Physics.Arcade.Body.prototype = {
*/
setCircle: function (radius, offsetX, offsetY) {
if (typeof offsetX === 'undefined') { offsetX = 0; }
if (typeof offsetY === 'undefined') { offsetY = 0; }
if (typeof offsetX === 'undefined') { offsetX = this.sprite._cache.halfWidth; }
if (typeof offsetY === 'undefined') { offsetY = this.sprite._cache.halfHeight; }
this.type = Phaser.Physics.Arcade.CIRCLE;
this.shape = new SAT.Circle(new SAT.Vector(this.sprite.center.x, this.sprite.center.y), radius);
this.shape = new SAT.Circle(new SAT.Vector(this.sprite.x, this.sprite.y), radius);
this.polygon = null;
this.offset.setTo(offsetX, offsetY);
@@ -379,7 +379,7 @@ Phaser.Physics.Arcade.Body.prototype = {
if (typeof translateY === 'undefined') { translateY = -this.sprite._cache.halfHeight; }
this.type = Phaser.Physics.Arcade.RECT;
this.shape = new SAT.Box(new SAT.Vector(this.sprite.center.x, this.sprite.center.y), width, height);
this.shape = new SAT.Box(new SAT.Vector(this.sprite.world.x, this.sprite.world.y), width, height);
this.polygon = this.shape.toPolygon();
this.polygon.translate(translateX, translateY);
@@ -446,8 +446,11 @@ Phaser.Physics.Arcade.Body.prototype = {
this.preX = this.x;
this.preY = this.y;
this.x = this.sprite.center.x + this.offset.x;
this.y = this.sprite.center.y + this.offset.y;
// this.x = this.sprite.center.x + this.offset.x;
// this.y = this.sprite.center.y + this.offset.y;
this.x = this.sprite.world.x + this.offset.x;
this.y = this.sprite.world.y + this.offset.y;
if (this.allowRotation)
{
@@ -462,26 +465,32 @@ Phaser.Physics.Arcade.Body.prototype = {
this.updateBounds();
// if (this.blocked.left && this.blockFlags[0] !== this.left)
// {
// this.blocked.left = false;
// }
if (this.sprite.debug)
{
console.log('Body postUpdate x:', this.x, 'y:', this.y, 'left:', this.left, 'right:', this.right);
}
// if (this.blocked.right && this.blockFlags[1] !== this.right)
// {
// this.blocked.right = false;
// }
// if (this.blocked.up && this.blockFlags[2] !== this.top)
// {
// this.blocked.up = false;
// }
if (this.blocked.left && this.blockFlags[0] !== this.left)
{
this.blocked.left = false;
}
// if (this.blocked.down && this.blockFlags[3] !== this.bottom)
// {
// console.log('reset down block flag', this.blockFlags[3], this.bottom);
// this.blocked.down = false;
// }
if (this.blocked.right && this.blockFlags[1] !== this.right)
{
this.blocked.right = false;
}
if (this.blocked.up && this.blockFlags[2] !== this.top)
{
this.blocked.up = false;
}
if (this.blocked.down && this.blockFlags[3] !== this.bottom)
{
// console.log('reset down block flag', this.blockFlags[3], this.bottom);
this.blocked.down = false;
}
this.blocked.left = false;
this.blocked.right = false;
@@ -508,26 +517,32 @@ Phaser.Physics.Arcade.Body.prototype = {
setBlockFlag: function (left, right, up, down, x, y) {
this.updateBounds();
if (left)
{
this.blockFlags[0] = this.left + x;
this.blockFlags[0] = this.left;
// this.blockFlags[0] = this.left + x;
// console.log('left flag set to', this.blockFlags[0]);
}
else if (right)
{
this.blockFlags[1] = this.right + y;
this.blockFlags[1] = this.right;
// this.blockFlags[1] = this.right + x;
// console.log('right flag set to', this.blockFlags[1]);
}
if (up)
{
this.blockFlags[2] = this.top + y;
this.blockFlags[2] = this.top;
// this.blockFlags[2] = this.top + y;
// this.blockFlags[2] = this.top;
// console.log('up flag set to', this.blockFlags[2]);
}
else if (down)
{
this.blockFlags[3] = this.bottom + y;
this.blockFlags[3] = this.bottom;
// this.blockFlags[3] = this.bottom + y;
// this.blockFlags[3] = this.bottom;
// console.log('down flag set to', this.blockFlags[3]);
}
@@ -551,53 +566,15 @@ Phaser.Physics.Arcade.Body.prototype = {
}
else
{
this.left = this.polygon.pos.x - this.polygon.points[0].x;
this.right = this.polygon.pos.x + this.polygon.points[0].x;
this.top = this.polygon.pos.y - this.polygon.points[0].y;
this.bottom = this.polygon.pos.y + this.polygon.points[0].y;
var temp;
for (var i = 1, len = this.polygon.points.length; i < len; i++)
{
// Left
temp = this.polygon.pos.x - this.polygon.points[i].x;
if (temp < this.left)
{
this.left = temp;
}
// Right
temp = this.polygon.pos.x + this.polygon.points[i].x;
if (temp > this.right)
{
this.right = temp;
}
// Top
temp = this.polygon.pos.y - this.polygon.points[i].y;
if (temp < this.top)
{
this.top = temp;
}
// Bottom
temp = this.polygon.pos.y + this.polygon.points[i].y;
if (temp > this.bottom)
{
this.bottom = temp;
}
}
this.width = this.right - this.left;
this.height = this.bottom - this.top;
this.left = Phaser.Math.minProperty('x', this.polygon.points) + this.polygon.pos.x;
this.right = Phaser.Math.maxProperty('x', this.polygon.points) + this.polygon.pos.x;
this.top = Phaser.Math.minProperty('y', this.polygon.points) + this.polygon.pos.y;
this.bottom = Phaser.Math.maxProperty('y', this.polygon.points) + this.polygon.pos.y;
}
this.width = this.right - this.left;
this.height = this.bottom - this.top;
},
/**
@@ -906,7 +883,7 @@ Phaser.Physics.Arcade.Body.prototype = {
return this.customSeparateCallback.call(this.customSeparateContext, this, response);
}
console.log(this.sprite.name, 'collided with', body.sprite.name, response);
// console.log(this.sprite.name, 'collided with', body.sprite.name, response);
this._distances[0] = body.right - this.x; // Distance of B to face on left side of A
this._distances[1] = this.right - body.x; // Distance of B to face on right side of A
@@ -918,12 +895,12 @@ Phaser.Physics.Arcade.Body.prototype = {
// Which is smaller? Left or Right?
if (this._distances[0] < this._distances[1])
{
console.log(this.sprite.name, 'collided on the LEFT with', body.sprite.name, response);
// console.log(this.sprite.name, 'collided on the LEFT with', body.sprite.name, response);
this.hitLeft(body, response);
}
else if (this._distances[1] < this._distances[0])
{
console.log(this.sprite.name, 'collided on the RIGHT with', body.sprite.name, response);
// console.log(this.sprite.name, 'collided on the RIGHT with', body.sprite.name, response);
this.hitRight(body, response);
}
}
@@ -932,12 +909,12 @@ Phaser.Physics.Arcade.Body.prototype = {
// Which is smaller? Top or Bottom?
if (this._distances[2] < this._distances[3])
{
console.log(this.sprite.name, 'collided on the TOP with', body.sprite.name, response);
// console.log(this.sprite.name, 'collided on the TOP with', body.sprite.name, response);
this.hitTop(body, response);
}
else if (this._distances[3] < this._distances[2])
{
console.log(this.sprite.name, 'collided on the BOTTOM with', body.sprite.name, response);
// console.log(this.sprite.name, 'collided on the BOTTOM with', body.sprite.name, response);
this.hitBottom(body, response);
}
}
@@ -964,17 +941,17 @@ Phaser.Physics.Arcade.Body.prototype = {
hitLeft: function (body, response) {
// We know that Body is overlapping with This on the left hand side (deltaX < 0 = moving left, > 0 = moving right)
if (body.speed > 0 && (body.deltaX() <= 0 || (body.deltaX() > 0 && !this.checkCollision.left)))
{
return;
}
// if (body.speed > 0 && (body.deltaX() <= 0 || (body.deltaX() > 0 && !this.checkCollision.left)))
// {
// return;
// }
if (this.collideCallback && !this.collideCallback.call(this.collideCallbackContext, Phaser.LEFT, this, body, response))
{
return;
}
if (this.immovable || this.blocked.right || this.touching.right)
if (!this.moves || this.immovable || this.blocked.right || this.touching.right)
{
body.give(this, response);
}
@@ -1012,11 +989,11 @@ Phaser.Physics.Arcade.Body.prototype = {
hitRight: function (body, response) {
// We know that Body is overlapping with This on the right hand side (deltaX < 0 = moving left, > 0 = moving right)
if (body.speed > 0 && (body.deltaX() >= 0 || (body.deltaX() < 0 && !this.checkCollision.right)))
{
console.log('bail 1', body.deltaX());
return;
}
// if (body.speed > 0 && (body.deltaX() >= 0 || (body.deltaX() < 0 && !this.checkCollision.right)))
// {
// console.log('bail 1', body.deltaX());
// return;
// }
if (this.collideCallback && !this.collideCallback.call(this.collideCallbackContext, Phaser.RIGHT, this, body))
{
@@ -1024,7 +1001,7 @@ Phaser.Physics.Arcade.Body.prototype = {
return;
}
if (this.immovable || this.blocked.left || this.touching.left)
if (!this.moves || this.immovable || this.blocked.left || this.touching.left)
{
body.give(this, response);
}
@@ -1062,17 +1039,19 @@ Phaser.Physics.Arcade.Body.prototype = {
hitTop: function (body, response) {
// We know that Body is overlapping with This on the bottom side (deltaY < 0 = moving up, > 0 = moving down)
if (body.speed > 0 && (body.deltaY() <= 0 || (body.deltaY() > 0 && !this.checkCollision.up)))
{
return;
}
// if (body.speed > 0 && (body.deltaY() <= 0 || (body.deltaY() > 0 && !this.checkCollision.up)))
// if (body.speed > 0 && (body.deltaY() <= 0 || (body.deltaY() > 0 && !this.checkCollision.up)))
// {
// console.log('bail', body.sprite.name, body.deltaY());
// return;
// }
if (this.collideCallback && !this.collideCallback.call(this.collideCallbackContext, Phaser.UP, this, body))
{
return;
}
if (this.immovable || this.blocked.down || this.touching.down)
if (!this.moves || this.immovable || this.blocked.down || this.touching.down)
{
body.give(this, response);
}
@@ -1110,17 +1089,17 @@ Phaser.Physics.Arcade.Body.prototype = {
hitBottom: function (body, response) {
// We know that Body is overlapping with This on the bottom side (deltaY < 0 = moving up, > 0 = moving down)
if (body.speed > 0 && (body.deltaY() >= 0 || (body.deltaY() < 0 && !this.checkCollision.down)))
{
return;
}
// if (body.speed > 0 && (body.deltaY() >= 0 || (body.deltaY() < 0 && !this.checkCollision.down)))
// {
// return;
// }
if (this.collideCallback && !this.collideCallback.call(this.collideCallbackContext, Phaser.DOWN, this, body))
{
return;
}
if (this.immovable || this.blocked.up || this.touching.up)
if (!this.moves || this.immovable || this.blocked.up || this.touching.up)
{
body.give(this, response);
}
@@ -1162,13 +1141,14 @@ Phaser.Physics.Arcade.Body.prototype = {
{
this.x += this._dx;
this.velocity.x += this._temp.x;
console.log('x added', this._dx);
}
if ((this._dy < 0 && !this.blocked.up && !this.touching.up) || (this._dy > 0 && !this.blocked.down && !this.touching.down))
{
this.y += this._dy;
this.velocity.y += this._temp.y;
// console.log('y added', this._dy);
console.log('y added', this._dy);
}
if (this.velocity.x > this.maxVelocity.x)
@@ -1225,8 +1205,37 @@ Phaser.Physics.Arcade.Body.prototype = {
this.facing = Phaser.DOWN;
}
this.sprite.x = this.x + (this.sprite.x - this.sprite.center.x) - this.offset.x;
this.sprite.y = this.y + (this.sprite.y - this.sprite.center.y) - this.offset.y;
this.updateBounds();
if (this.sprite.debug)
{
console.log('Body postUpdate x:', this.x, 'y:', this.y, 'left:', this.left, 'right:', this.right);
}
// this.sprite.x = this.x + (this.sprite.x - this.sprite.center.x) - this.offset.x;
// this.sprite.y = this.y + (this.sprite.y - this.sprite.center.y) - this.offset.y;
if (this.sprite.name === 'mushroom')
{
// console.log('old x', this.preX, 'new x', this.x, 'delta', this.deltaX());
// console.log('old y', this.preY, 'new y', this.y);
}
this.sprite.x = this.x - this.offset.x;
this.sprite.y = this.y - this.offset.y;
this.sprite.worldTransform[2] = this.x - this.offset.x;
this.sprite.worldTransform[5] = this.y - this.offset.y;
// this.world.setTo(this.game.camera.x + this.worldTransform[2], this.game.camera.y + this.worldTransform[5]);
// this.sprite.x = this.x + (this.sprite.world.x - this.game.camera.x) - this.offset.x;
// this.sprite.y = this.y + (this.sprite.world.y - this.game.camera.y) - this.offset.y;
// this.world.setTo(this.game.camera.x + this.worldTransform[2], this.game.camera.y + this.worldTransform[5]);
if (this.allowRotation)
{
@@ -1269,7 +1278,16 @@ Phaser.Physics.Arcade.Body.prototype = {
* @return {number} The delta value. Positive if the motion was to the right, negative if to the left.
*/
deltaX: function () {
return this.x - this.preX;
if (this.moves)
{
return this.x - this.preX;
}
else
{
return this.sprite.deltaX;
}
},
/**
@@ -1279,7 +1297,16 @@ Phaser.Physics.Arcade.Body.prototype = {
* @return {number} The delta value. Positive if the motion was downwards, negative if upwards.
*/
deltaY: function () {
return this.y - this.preY;
if (this.moves)
{
return this.y - this.preY;
}
else
{
return this.sprite.deltaY;
}
},
/**
@@ -1334,6 +1361,8 @@ Object.defineProperty(Phaser.Physics.Arcade.Body.prototype, "x", {
this.polygon.pos.x = value;
}
// this.updateBounds();
}
});
@@ -1376,6 +1405,8 @@ Object.defineProperty(Phaser.Physics.Arcade.Body.prototype, "y", {
this.polygon.pos.y = value;
}
// this.updateBounds();
}
});
+2 -2
View File
@@ -60,9 +60,9 @@ Phaser.Tile = function (layer, index, x, y, width, height) {
this.properties = {};
/**
* @property {boolean} walked - Has this tile been walked / turned into a poly?
* @property {boolean} scanned - Has this tile been walked / turned into a poly?
*/
this.walked = false;
this.scanned = false;
/**
* @property {boolean} faceTop - Is the top of this tile an interesting edge?
+1
View File
@@ -1227,6 +1227,7 @@ Phaser.Tilemap.prototype = {
destroy: function () {
this.removeAllLayers();
this.data = [];
this.game = null;
}
+3
View File
@@ -492,6 +492,8 @@ Phaser.TilemapLayer.prototype.getTiles = function (x, y, width, height, collides
this._results.length = 0;
// var _tile = null;
// this.context.fillStyle = 'rgba(255,0,0,0.3)';
// this.context.fillRect(this._tx * this._cw, this._ty * this._ch, this._tw * this._cw, this._th * this._ch);
for (var wy = this._ty; wy < this._ty + this._th; wy++)
{
@@ -517,6 +519,7 @@ Phaser.TilemapLayer.prototype.getTiles = function (x, y, width, height, collides
tile: this.layer.data[wy][wx],
layer: this.layer.data[wy][wx].layer
});
}
// }
}
-2
View File
@@ -371,8 +371,6 @@ Phaser.TilemapParser = {
}
console.log(map);
return map;
}
+24 -32
View File
@@ -609,23 +609,16 @@ Phaser.Utils.Debug.prototype = {
color = color || 'rgb(255, 255, 255)';
this.start(x, y, color);
this.start(x, y, color, 100);
if (sprite.name)
{
this.line(sprite.name);
}
this.line('x: ' + sprite.x);
this.line('y: ' + sprite.y);
this.line('pos x: ' + sprite.position.x);
this.line('pos y: ' + sprite.position.y);
this.line('local x: ' + sprite.localTransform[2]);
this.line('local y: ' + sprite.localTransform[5]);
this.line('t x: ' + sprite.worldTransform[2]);
this.line('t y: ' + sprite.worldTransform[5]);
this.line('world x: ' + sprite.world.x);
this.line('world y: ' + sprite.world.y);
this.splitline('x:', sprite.x.toFixed(2), 'y:', sprite.y.toFixed(2));
this.splitline('pos x:', sprite.position.x.toFixed(2), 'pos y:', sprite.position.y.toFixed(2));
this.splitline('world x:', sprite.world.x.toFixed(2), 'world y:', sprite.world.y.toFixed(2));
this.stop();
@@ -965,8 +958,8 @@ Phaser.Utils.Debug.prototype = {
color = color || 'rgb(255,255,255)';
var x = body.x;
var y = body.y;
var x = body.x - this.game.camera.x;
var y = body.y - this.game.camera.y;
if (body.type === Phaser.Physics.Arcade.CIRCLE)
{
@@ -977,8 +970,8 @@ Phaser.Utils.Debug.prototype = {
this.context.stroke();
this.context.closePath();
this.context.strokeStyle = 'rgb(0,0,255)';
this.context.strokeRect(body.left, body.top, body.width, body.height);
// this.context.strokeStyle = 'rgb(0,0,255)';
// this.context.strokeRect(body.left, body.top, body.width, body.height);
this.stop();
}
@@ -988,27 +981,27 @@ Phaser.Utils.Debug.prototype = {
this.start(0, 0, color);
this.context.beginPath();
this.context.moveTo(x + points[0].x, y + points[0].y);
// this.context.beginPath();
// this.context.moveTo(x + points[0].x, y + points[0].y);
for (var i = 1; i < points.length; i++)
{
this.context.lineTo(x + points[i].x, y + points[i].y);
}
// for (var i = 1; i < points.length; i++)
// {
// this.context.lineTo(x + points[i].x, y + points[i].y);
// }
this.context.closePath();
this.context.strokeStyle = color;
this.context.stroke();
// this.context.closePath();
// this.context.strokeStyle = color;
// this.context.stroke();
this.context.fillStyle = 'rgb(255,0,0)';
this.context.fillRect(x + points[0].x - 2, y + points[0].y - 2, 5, 5);
// this.context.fillStyle = 'rgb(255,0,0)';
// this.context.fillRect(x + points[0].x - 2, y + points[0].y - 2, 5, 5);
for (var i = 1; i < points.length; i++)
{
this.context.fillRect(x + points[i].x - 2, y + points[i].y - 2, 5, 5);
}
// for (var i = 1; i < points.length; i++)
// {
// this.context.fillRect(x + points[i].x - 2, y + points[i].y - 2, 5, 5);
// }
this.context.strokeStyle = 'rgb(0,0,255)';
this.context.strokeStyle = 'rgb(0,255,255)';
this.context.strokeRect(body.left, body.top, body.width, body.height);
this.stop();
@@ -1047,7 +1040,6 @@ Phaser.Utils.Debug.prototype = {
}
this.context.closePath();
// this.context.strokeStyle = 'rgba(255, 0, 255, 0.7)';
this.context.strokeStyle = color;
this.context.stroke();