Fixed an issue where passing null as the Group parent wouldn't set it to game.world as it should have (thanks tito100).

Continued work on the tilemap collision - again, please don't use this version if you need working tilemaps.
This commit is contained in:
photonstorm
2013-12-06 01:07:25 +00:00
parent ea3802a556
commit 1f513a333e
7 changed files with 123 additions and 94 deletions
+12 -12
View File
@@ -10,32 +10,32 @@
* @classdesc A Group is a container for display objects that allows for fast pooling, recycling and collision checks.
* @constructor
* @param {Phaser.Game} game - A reference to the currently running game.
* @param {*} parent - The parent Group or DisplayObjectContainer that will hold this group, if any.
* @param {*} parent - The parent Group or DisplayObjectContainer that will hold this group, if any. If not set, or set to null, it will use game.world.
* @param {string} [name=group] - A name for this Group. Not used internally but useful for debugging.
* @param {boolean} [useStage=false] - Should the DisplayObjectContainer this Group creates be added to the World (default, false) or direct to the Stage (true).
*/
Phaser.Group = function (game, parent, name, useStage) {
if (typeof parent === 'undefined' || typeof parent === null)
/**
* @property {Phaser.Game} game - A reference to the currently running Game.
*/
this.game = game;
if (typeof parent === 'undefined' || parent === null)
{
parent = game.world;
}
/**
* @property {string} name - A name for this Group. Not used internally but useful for debugging.
*/
this.name = name || 'group';
if (typeof useStage === 'undefined')
{
useStage = false;
}
/**
* @property {Phaser.Game} game - A reference to the currently running Game.
*/
this.game = game;
/**
* @property {string} name - A name for this Group. Not used internally but useful for debugging.
*/
this.name = name || 'group';
if (useStage)
{
this._container = this.game.stage._stage;
+1 -1
View File
@@ -90,7 +90,7 @@ Phaser.GameObjectFactory.prototype = {
*
* @method Phaser.GameObjectFactory#group
* @param {*} parent - The parent Group or DisplayObjectContainer that will hold this group, if any.
* @param {string} [name=group] - A name for this Group. Not used internally but useful for debugging.
* @param {string} [name='group'] - A name for this Group. Not used internally but useful for debugging.
* @return {Phaser.Group} The newly created group.
*/
group: function (parent, name) {
+8 -1
View File
@@ -644,7 +644,14 @@ Phaser.Rectangle.intersection = function (a, b, out) {
*/
Phaser.Rectangle.intersects = function (a, b) {
return (a.x < b.right && b.x < a.right && a.y < b.bottom && b.y < a.bottom);
if (a.width <= 0 || a.height <= 0 || b.width <= 0 || b.height <= 0)
{
return false;
}
return !(a.right < b.x || a.bottom < b.y || a.x > b.right || a.y > b.bottom);
// return (a.x < b.right && b.x < a.right && a.y < b.bottom && b.y < a.bottom);
// return (a.x <= b.right && b.x <= a.right && a.y <= b.bottom && b.y <= a.bottom);
+45 -30
View File
@@ -566,6 +566,8 @@ Phaser.Physics.Arcade.prototype = {
return;
}
console.log('collideSpriteVsTilemapLayer', this._mapData.length);
for (var i = 0; i < this._mapData.length; i++)
{
if (this.separateTile(sprite.body, this._mapData[i]))
@@ -1006,20 +1008,27 @@ Phaser.Physics.Arcade.prototype = {
return false;
}
var overlapX = 0;
var overlapY = 0;
// use body var instead
body.overlapX = 0;
body.overlapY = 0;
console.log('x', tile.x, 'y', tile.y, 'r', tile.right, 'b', tile.bottom);
console.log('x', body.x, 'y', body.y, 'r', body.right, 'b', body.bottom);
console.log(Phaser.Rectangle.intersects(body, tile));
// Remember - this happens AFTER the body has been moved by the motion update, so it needs moving back again
// console.log('---------------------------------------------------------------------------------------------');
// console.log('tx', tile.x, 'ty', tile.y, 'tw', tile.width, 'th', tile.height, 'tr', tile.right, 'tb', tile.bottom);
// console.log('bx', body.x, 'by', body.y, 'bw', body.width, 'bh', body.height, 'br', body.right, 'bb', body.bottom);
// console.log(Phaser.Rectangle.intersects(body, tile));
// console.log('dx', body.deltaX(), 'dy', body.deltaY(), 'dax', body.deltaAbsX(), 'day', body.deltaAbsY(), 'cax', Math.ceil(body.deltaAbsX()), 'cay', Math.ceil(body.deltaAbsY()));
if (body.deltaX() < 0 && body.allowCollision.left && tile.tile.faceRight)
{
// LEFT
if (tile.right - body.x < Math.ceil(body.deltaAbsX()))
{
overlapX = tile.right - body.x;
body.overlapX = body.x - tile.right;
if (body.overlapX > body.deltaX())
{
console.log('left overlapX', body.overlapX);
// use touching instead of blocked?
body.blocked.left = true;
body.touching.left = true;
body.touching.none = false;
}
@@ -1027,10 +1036,13 @@ Phaser.Physics.Arcade.prototype = {
else if (body.deltaX() > 0 && body.allowCollision.right && tile.tile.faceLeft)
{
// RIGHT
if (body.right - tile.x < Math.ceil(body.deltaAbsX()))
{
overlapX = tile.x - body.right;
body.overlapX = body.right - tile.x;
// Distance check
if (body.overlapX < body.deltaX())
{
console.log('right overlapX', body.overlapX);
body.blocked.right = true;
body.touching.right = true;
body.touching.none = false;
}
@@ -1039,10 +1051,13 @@ Phaser.Physics.Arcade.prototype = {
if (body.deltaY() < 0 && body.allowCollision.up && tile.tile.faceBottom)
{
// UP
if (tile.bottom - body.y < Math.ceil(body.deltaAbsY()))
{
overlapY = tile.bottom - body.y;
body.overlapY = body.y - tile.bottom;
// Distance check
if (body.overlapY > body.deltaY())
{
console.log('up overlapY', body.overlapY);
body.blocked.up = true;
body.touching.up = true;
body.touching.none = false;
}
@@ -1050,10 +1065,12 @@ Phaser.Physics.Arcade.prototype = {
else if (body.deltaY() > 0 && body.allowCollision.down && tile.tile.faceTop)
{
// DOWN
if (body.bottom - tile.y < Math.ceil(body.deltaAbsY()))
{
overlapY = tile.y - body.bottom;
body.overlapY = body.bottom - tile.y;
if (body.overlapY < body.deltaY())
{
console.log('down overlapY', body.overlapY);
body.blocked.down = true;
body.touching.down = true;
body.touching.none = false;
}
@@ -1061,40 +1078,38 @@ Phaser.Physics.Arcade.prototype = {
// Separate in a single sweep
if (overlapX === 0 && overlapY === 0)
if (body.touching.none)
{
return false;
}
if (overlapX !== 0)
if (body.overlapX !== 0)
{
// body.overlapX = overlapX;
body.x += overlapX;
body.preX = body.x;
body.x -= body.overlapX;
body.preX -= body.overlapX;
if (body.bounce.x === 0)
{
body.velocity.x = 0;
// body.velocity.x = 0;
}
else
{
body.velocity.x = -body.velocity.x * body.bounce.x;
// body.velocity.x = -body.velocity.x * body.bounce.x;
}
}
if (overlapY !== 0)
if (body.overlapY !== 0)
{
// body.overlapY = overlapY;
body.y += overlapY;
body.preY = body.y;
body.y -= body.overlapY;
body.preY -= body.overlapY;
if (body.bounce.y === 0)
{
body.velocity.y = 0;
// body.velocity.y = 0;
}
else
{
body.velocity.y = -body.velocity.y * body.bounce.y;
// body.velocity.y = -body.velocity.y * body.bounce.y;
}
}
+51 -45
View File
@@ -207,6 +207,7 @@ Phaser.Physics.Arcade.Body = function (sprite) {
* For example allowCollision.up = false means it won't collide when the collision happened while moving up.
* @property {object} allowCollision - An object containing allowed collision.
*/
// This would be faster as an array
this.allowCollision = { none: false, any: true, up: true, down: true, left: true, right: true };
/**
@@ -214,12 +215,14 @@ Phaser.Physics.Arcade.Body = function (sprite) {
* touching.up = true means the collision happened to the top of this Body for example.
* @property {object} touching - An object containing touching results.
*/
// This would be faster as an array
this.touching = { none: true, up: false, down: false, left: false, right: false };
/**
* This object is populated with previous touching values from the bodies previous collision.
* @property {object} wasTouching - An object containing previous touching results.
*/
// This would be faster as an array
this.wasTouching = { none: true, up: false, down: false, left: false, right: false };
/**
@@ -310,6 +313,8 @@ Phaser.Physics.Arcade.Body = function (sprite) {
*/
this.collideWorldBounds = false;
this.blocked = { up: false, down: false, left: false, right: false };
};
Phaser.Physics.Arcade.Body.prototype = {
@@ -370,8 +375,13 @@ Phaser.Physics.Arcade.Body.prototype = {
this.y = this.preY;
this.rotation = this.preRotation;
this.overlapX = 0;
this.overlapY = 0;
// this.overlapX = 0;
// this.overlapY = 0;
this.blocked.up = false;
this.blocked.down = false;
this.blocked.left = false;
this.blocked.right = false;
if (this.moves)
{
@@ -406,60 +416,56 @@ Phaser.Physics.Arcade.Body.prototype = {
*/
postUpdate: function () {
if (this.deltaX() < 0)
// if (this.overlapX !== 0)
// {
// this.x -= this.overlapX;
// }
// if (this.overlapY !== 0)
// {
// this.y -= this.overlapY;
// }
if (this.deltaX() < 0 && this.blocked.left === false)
{
this.facing = Phaser.LEFT;
this.sprite.x += this.deltaX();
}
else if (this.deltaX() > 0)
else if (this.deltaX() > 0 && this.blocked.right === false)
{
this.facing = Phaser.RIGHT;
this.sprite.x += this.deltaX();
}
if (this.deltaY() < 0)
if (this.deltaY() < 0 && this.blocked.up === false)
{
this.facing = Phaser.UP;
this.sprite.y += this.deltaY();
}
else if (this.deltaY() > 0)
else if (this.deltaY() > 0 && this.blocked.down === false)
{
this.facing = Phaser.DOWN;
}
/*
if (this.overlapX !== 0)
{
this.sprite.x += this.overlapX;
}
else
{
if (this.deltaX() !== 0)
{
this.sprite.x += this.deltaX();
}
}
if (this.overlapY !== 0)
{
this.sprite.y += this.overlapY;
}
else
{
if (this.deltaY() !== 0)
{
this.sprite.y += this.deltaY();
}
}
*/
if (this.deltaX() !== 0 || this.deltaY() !== 0)
{
// console.log('dx', this.deltaX());
// console.log('dy', this.deltaY());
this.sprite.x += this.deltaX();
this.sprite.y += this.deltaY();
this.center.setTo(this.x + this.halfWidth, this.y + this.halfHeight);
}
// if (this.deltaY() !== 0 && this.blocked.up === false && this.blocked.down === false)
// {
// 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();
@@ -599,7 +605,7 @@ Phaser.Physics.Arcade.Body.prototype = {
* Returns the delta x value. The difference between Body.x now and in the previous step.
*
* @method Phaser.Physics.Arcade.Body#deltaX
* @return {number} The delta value.
* @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;
@@ -609,7 +615,7 @@ Phaser.Physics.Arcade.Body.prototype = {
* Returns the delta y value. The difference between Body.y now and in the previous step.
*
* @method Phaser.Physics.Arcade.Body#deltaY
* @return {number} The delta value.
* @return {number} The delta value. Positive if the motion was downwards, negative if upwards.
*/
deltaY: function () {
return this.y - this.preY;
@@ -633,7 +639,7 @@ Object.defineProperty(Phaser.Physics.Arcade.Body.prototype, "bottom", {
* @return {number}
*/
get: function () {
return this.y + this.height;
return Math.floor(this.y + this.height);
},
/**
@@ -669,7 +675,7 @@ Object.defineProperty(Phaser.Physics.Arcade.Body.prototype, "right", {
* @return {number}
*/
get: function () {
return this.x + this.width;
return Math.floor(this.x + this.width);
},
/**