diff --git a/examples/p2 physics/kill and revive.js b/examples/p2 physics/kill and revive.js new file mode 100644 index 000000000..f2e34b5d7 --- /dev/null +++ b/examples/p2 physics/kill and revive.js @@ -0,0 +1,77 @@ + +var game = new Phaser.Game(800, 600, Phaser.CANVAS, 'phaser-example', { preload: preload, create: create, update: update }); + +function preload() { + + game.load.image('atari', 'assets/sprites/atari130xe.png'); + game.load.image('sky', 'assets/skies/sunset.png'); + +} + +var sprite; +var cursors; + +function create() { + + game.add.image(0, 0, 'sky'); + + // Enable p2 physics + game.physics.startSystem(Phaser.Physics.P2JS); + + // Make things a bit more bouncey + game.physics.p2.defaultRestitution = 0.8; + + // Add a sprite + sprite = game.add.sprite(200, 200, 'atari'); + + // Enable if for physics. This creates a default rectangular body. + game.physics.p2.enable(sprite); + + // Modify a few body properties + sprite.body.setZeroDamping(); + sprite.body.fixedRotation = true; + + text = game.add.text(20, 20, 'move with arrow keys, click to kill and reset', { fill: '#ffffff' }); + + game.input.onDown.add(deathToggle, this); + + cursors = game.input.keyboard.createCursorKeys(); + +} + +function deathToggle(pointer) { + + if (sprite.alive) + { + sprite.kill(); + } + else + { + sprite.reset(pointer.x, pointer.y); + } + +} + +function update() { + + sprite.body.setZeroVelocity(); + + if (cursors.left.isDown) + { + sprite.body.moveLeft(400); + } + else if (cursors.right.isDown) + { + sprite.body.moveRight(400); + } + + if (cursors.up.isDown) + { + sprite.body.moveUp(400); + } + else if (cursors.down.isDown) + { + sprite.body.moveDown(400); + } + +} diff --git a/src/core/Game.js b/src/core/Game.js index c5b8a0267..cd24a2b34 100644 --- a/src/core/Game.js +++ b/src/core/Game.js @@ -605,6 +605,7 @@ Phaser.Game.prototype = { } this.debug.preUpdate(); + this.physics.preUpdate(); this.state.preUpdate(); this.plugins.preUpdate(); this.stage.preUpdate(); diff --git a/src/gameobjects/Sprite.js b/src/gameobjects/Sprite.js index 029dffeb5..e082f5278 100644 --- a/src/gameobjects/Sprite.js +++ b/src/gameobjects/Sprite.js @@ -272,7 +272,7 @@ Phaser.Sprite.prototype.preUpdate = function() { this.animations.update(); - if (this.exists && this.body) + if (this.body) { this.body.preUpdate(); } @@ -906,7 +906,7 @@ Object.defineProperty(Phaser.Sprite.prototype, "exists", { if (this.body && this.body.type === Phaser.Physics.P2JS) { - this.body.safeRemove = true; + this.body.removeFromWorld(); } this.visible = false; diff --git a/src/physics/Physics.js b/src/physics/Physics.js index 1ad9a6615..2a851f71f 100644 --- a/src/physics/Physics.js +++ b/src/physics/Physics.js @@ -192,6 +192,23 @@ Phaser.Physics.prototype = { }, + /** + * preUpdate checks. + * + * @method Phaser.Physics#preUpdate + * @protected + */ + preUpdate: function () { + + // ArcadePhysics / Ninja don't have a core to preUpdate + + if (this.p2) + { + this.p2.preUpdate(); + } + + }, + /** * Updates all running physics systems. * diff --git a/src/physics/arcade/Body.js b/src/physics/arcade/Body.js index a9bf74459..f8f7c8ecb 100644 --- a/src/physics/arcade/Body.js +++ b/src/physics/arcade/Body.js @@ -294,31 +294,12 @@ Phaser.Physics.Arcade.Body = function (sprite) { * For example if blocked.up is true then the Body cannot move up. * @property {object} blocked - An object containing on which faces this Body is blocked from moving, if any. */ - this.blocked = { x: 0, y: 0, up: false, down: false, left: false, right: false }; - - /** - * This object is populated with boolean values when the Body collides with the World bounds or a Tile. - * For example if blocked.up is true then the Body cannot move up. - * @property {object} blocked - An object containing on which faces this Body is blocked from moving, if any. - */ - this.result = { x: 0, y: 0, px: 0, py: 0, tx: 0, ty: 0, slope: false }; + this.blocked = { up: false, down: false, left: false, right: false }; }; Phaser.Physics.Arcade.Body.prototype = { - resetResult: function () { - - this.result.x = false; - this.result.y = false; - this.result.slope = false; - this.result.px = this.position.x; - this.result.py = this.position.y; - this.result.tx = 0; - this.result.ty = 0; - - }, - /** * Internal method. * @@ -348,8 +329,6 @@ Phaser.Physics.Arcade.Body.prototype = { */ preUpdate: function () { - // this.resetResult(); - // Store and reset collision flags this.wasTouching.none = this.touching.none; this.wasTouching.up = this.touching.up; @@ -365,33 +344,10 @@ Phaser.Physics.Arcade.Body.prototype = { this.embedded = false; - // this.screenX = (this.sprite.worldTransform[2] - (this.sprite.anchor.x * this.width)) + this.offset.x; - // this.screenY = (this.sprite.worldTransform[5] - (this.sprite.anchor.y * this.height)) + this.offset.y; - - // this is where the Sprite currently is, in world coordinates - // 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; - // this.preRotation = this.sprite.rotation; - - // this.x = this.preX; - // this.y = this.preY; - // this.rotation = this.preRotation; - - // this.overlapX = 0; - // this.overlapY = 0; - - // this.prev.set(this.position.x, this.position.y); - - // this.position.x = (this.sprite.world.x - (this.sprite.anchor.x * this.width)) + this.offset.x; - // this.position.y = (this.sprite.world.y - (this.sprite.anchor.y * this.height)) + this.offset.y; - - - // this.blocked.up = false; - // this.blocked.down = false; - // this.blocked.left = false; - // this.blocked.right = false; + this.blocked.up = false; + this.blocked.down = false; + this.blocked.left = false; + this.blocked.right = false; if (this.moves) { @@ -447,11 +403,6 @@ Phaser.Physics.Arcade.Body.prototype = { if (this.deltaX() !== 0 || this.deltaY() !== 0) { - // this is where the Sprite currently is, in world coordinates - // this.sprite.x = (this.sprite.world.x - (this.sprite.anchor.x * this.width)) + this.offset.x; - // this.sprite.y = (this.sprite.world.y - (this.sprite.anchor.y * this.height)) + this.offset.y; - // this.sprite.x = (this.position.x - this.offset.x); - // this.sprite.y = (this.position.y - this.offset.y); // this.sprite.position.x = this.position.x; // this.sprite.position.y = this.position.y; this.sprite.x += this.deltaX(); diff --git a/src/physics/arcade/World.js b/src/physics/arcade/World.js index 68c1e641d..313068e9a 100644 --- a/src/physics/arcade/World.js +++ b/src/physics/arcade/World.js @@ -606,36 +606,17 @@ Phaser.Physics.Arcade.prototype = { */ collideSpriteVsTilemapLayer: function (sprite, tilemapLayer, collideCallback, processCallback, callbackContext) { - // this._mapData = tilemapLayer.getTiles(sprite.body.left, sprite.body.top, sprite.body.width, sprite.body.height, true); - // this._mapData = tilemapLayer.getTiles(sprite.body.x, sprite.body.y, sprite.body.width, sprite.body.height, true); - - this._mapData = tilemapLayer.getIntersectingTiles(sprite.body.position.x, sprite.body.position.y, sprite.body.width, sprite.body.height, true); - - // var vx = sprite.body.x; - // var vy = sprite.body.y; - - // vx += sprite.body.newVelocity.x; - // vy += sprite.body.newVelocity.y; - - // this._mapData = tilemapLayer.getIntersectingTiles(vx, vy, sprite.body.width, sprite.body.height, true); + this._mapData = tilemapLayer.getIntersectingTiles(sprite.body.position.x, sprite.body.position.y, sprite.body.width, sprite.body.height, sprite.body.right, sprite.body.bottom); if (this._mapData.length === 0) { return; } - // if (this._mapData.length > 1) - // { - // Needs process callback added - this.separateTiles(sprite.body, this._mapData); - // } - - /* - else + for (var i = 0; i < this._mapData.length; i++) { - var i = 0; - - if (this.separateTile(sprite.body, this._mapData[i])) + // console.log('-------------------------------------- tile', this._mapData[i].faceLeft, this._mapData[i].faceRight, this._mapData[i].faceTop, this._mapData[i].faceBottom); + if (this.separateTile(i, sprite.body, this._mapData[i])) { // They collided, is there a custom process callback? if (processCallback) @@ -661,7 +642,6 @@ Phaser.Physics.Arcade.prototype = { } } } - */ }, @@ -954,25 +934,7 @@ Phaser.Physics.Arcade.prototype = { }, - separateTiles: function (body, tiles) { - - body.resetResult(); - - var result = false; - - for (var i = 0; i < tiles.length; i++) - { - if (this.separateTile(i, body, tiles[i])) - { - result = true; - } - } - - return result; - - }, - - separateTile: function (i, body, tile) { + BUSTEDseparateTile: function (i, body, tile) { // we re-check for collision in case body was separated in a previous step if (i > 0 && !tile.intersects(body.position.x, body.position.y, body.right, body.bottom)) @@ -982,9 +944,9 @@ Phaser.Physics.Arcade.prototype = { return false; } - // console.log('body intersecting tile'); - // console.log('x', body.position.x, 'y', body.position.y, 'r', body.right, 'b', body.bottom, 'wh', body.width, body.height); - // console.log('x', tile.x, 'y', tile.y, 'r', tile.right, 'b', tile.bottom); + console.log('body intersecting tile'); + console.log('x', body.position.x, 'y', body.position.y, 'r', body.right, 'b', body.bottom, 'wh', body.width, body.height); + console.log('x', tile.x, 'y', tile.y, 'r', tile.right, 'b', tile.bottom); if (body.newVelocity.x && (tile.faceLeft || tile.faceRight)) { @@ -1059,315 +1021,156 @@ Phaser.Physics.Arcade.prototype = { * @param {Phaser.Tile} tile - The tile to collide against. * @returns {boolean} Returns true if the body was separated, otherwise false. */ - XXXseparateTile: function (body, tile) { + separateTile: function (i, body, tile) { - /* - this._intersection = this.tileIntersects(body, tile); - - // If the intersection area is either entirely null, or has a width/height of zero, we bail out now - if (this._intersection[4] === 0 || this._intersection[2] === 0 || this._intersection[3] === 0) + // we re-check for collision in case body was separated in a previous step + if (i > 0 && !tile.intersects(body.position.x, body.position.y, body.right, body.bottom)) { + // no collision so bail out (separted in a previous step) return false; } - Phaser.Rectangle.intersection(body, tile, this._intersection); - - if (this._intersection.width === 0 && this._intersection.height === 0) - { - return false; - } - */ - - // console.log(this._intersection); - // console.log(tile, body.x, body.y); - // They overlap. Any custom callbacks? - if (tile.tile.callback || tile.layer.callbacks[tile.tile.index]) + if (tile.callback || tile.layer.callbacks[tile.index]) { // A local callback takes priority over a global callback. - if (tile.tile.callback && tile.tile.callback.call(tile.tile.callbackContext, body.sprite, tile) === false) + if (tile.callback && tile.callback.call(tile.callbackContext, body.sprite, tile) === false) { // Is there a tile specific collision callback? If it returns true then we can carry on, otherwise we should abort. return false; } - else if (tile.layer.callbacks[tile.tile.index] && tile.layer.callbacks[tile.tile.index].callback.call(tile.layer.callbacks[tile.tile.index].callbackContext, body.sprite, tile) === false) + else if (tile.layer.callbacks[tile.index] && tile.layer.callbacks[tile.index].callback.call(tile.layer.callbacks[tile.index].callbackContext, body.sprite, tile) === false) { // Is there a tile index collision callback? If it returns true then we can carry on, otherwise we should abort. return false; } } - body.overlapX = 0; + var ox = 0; + var oy = 0; - // if (body.deltaX() < 0 && body.checkCollision.left) - // { - // Body is moving LEFT - // if (tile.tile.faceRight && body.x < tile.right) - // { - // } - // } - - - var process = false; - - if (this._intersection.width > 0) + if (tile.faceLeft || tile.faceRight) { - // Tile is blocking to the right and body is moving left - if (body.deltaX() < 0 && body.checkCollision.left && tile.tile.faceRight) + if (body.deltaX() < 0 && !body.blocked.left && tile.collideRight && body.checkCollision.left) { - body.x = tile.right; - // process = true; - // body.overlapX = -this._intersection.width; + // Body is moving LEFT + if (tile.faceRight && body.x < tile.right) + { + ox = body.x - tile.right; + } + } + else if (body.deltaX() > 0 && !body.blocked.right && tile.collideLeft && body.checkCollision.right) + { + // Body is moving RIGHT + if (tile.faceLeft && body.right > tile.left) + { + ox = body.right - tile.left; + } } - // Tile is blocking to the left and body is moving right - if (body.deltaX() > 0 && body.checkCollision.right && tile.tile.faceLeft) + if (ox !== 0) { - body.right = tile.x; - // process = true; - // body.overlapX = this._intersection.width; + this.processTileSeparationX(body, ox); + } + + // That's horizontal done, check if we still intersects? If not then we can return now + if (!tile.intersects(body.position.x, body.position.y, body.right, body.bottom)) + { + return (ox !== 0); } } -/* - if (body.overlapX !== 0) + if (tile.faceTop || tile.faceBottom) { - if (body.overlapX < 0) + if (body.deltaY() < 0 && !body.blocked.up && tile.collideDown && body.checkCollision.up) { - body.blocked.left = true; + // Body is moving UP + if (tile.faceBottom && body.y < tile.bottom) + { + oy = body.y - tile.bottom; + } } - else if (body.overlapX > 0) + else if (body.deltaY() > 0 && !body.blocked.down && tile.collideUp && body.checkCollision.down) { - body.blocked.right = true; + // Body is moving DOWN + if (tile.faceTop && body.bottom > tile.top) + { + oy = body.bottom - tile.top; + } } - body.x -= body.overlapX; - // body.preX -= body.overlapX; - body.blocked.x = Math.floor(body.x); - body.blocked.y = Math.floor(body.y); - - if (body.bounce.x === 0) + if (oy !== 0) { - body.velocity.x = 0; - } - else - { - body.velocity.x = -body.velocity.x * body.bounce.x; - } - } -*/ - - Phaser.Rectangle.intersection(body, tile, this._intersection); - - if (this._intersection.width === 0 && this._intersection.height === 0) - { - return false; - } - - body.overlapY = 0; - - var process = false; - - if (this._intersection.height > 0) - { - // Tile is blocking to the bottom and body is moving up - if (body.deltaY() < 0 && body.checkCollision.up && tile.tile.faceBottom) - { - - // process = true; - // body.overlapY = -this._intersection.height; - } - - // Tile is blocking to the top and body is moving down - if (body.deltaY() > 0 && body.checkCollision.down && tile.tile.faceTop) - { - process = true; - body.overlapY = this._intersection.height; + this.processTileSeparationY(body, oy); } } - if (body.overlapY !== 0) - { - if (body.overlapY < 0) - { - body.blocked.up = true; - } - else if (body.overlapY > 0) - { - body.blocked.down = true; - } + return (ox !== 0 || oy !== 0); - body.y -= body.overlapY; - // body.preY -= body.overlapY; - body.blocked.x = Math.floor(body.x); - body.blocked.y = Math.floor(body.y); - - if (body.bounce.y === 0) - { - body.velocity.y = 0; - } - else - { - body.velocity.y = -body.velocity.y * body.bounce.y; - } - } - - return; - - - /* - if (body.deltaX() < 0 && body.checkCollision.left && tile.tile.faceRight && !body.blocked.left) - // if (body.deltaX() < 0 && body.checkCollision.left && tile.tile.faceRight) - { - // LEFT - body.overlapX = body.x - tile.right; - - if (body.overlapX < 0) - { - process = true; - } - else - { - body.overlapX = 0; - } - } - else if (body.deltaX() > 0 && body.checkCollision.right && tile.tile.faceLeft && !body.blocked.right) - // else if (body.deltaX() > 0 && body.checkCollision.right && tile.tile.faceLeft) - { - // RIGHT - body.overlapX = body.right - tile.x; - - if (body.overlapX > 0) - { - process = true; - } - else - { - body.overlapX = 0; - } - } - - if (body.deltaY() < 0 && body.checkCollision.up && tile.tile.faceBottom && !body.blocked.up) - // if (body.deltaY() < 0 && body.checkCollision.up && tile.tile.faceBottom) - { - // UP - body.overlapY = body.y - tile.bottom; - - if (body.overlapY < 0) - { - process = true; - } - else - { - body.overlapY = 0; - } - } - else if (body.deltaY() > 0 && body.checkCollision.down && tile.tile.faceTop && !body.blocked.down) - // else if (body.deltaY() > 0 && body.checkCollision.down && tile.tile.faceTop) - { - // DOWN - body.overlapY = body.bottom - tile.y; - - if (body.overlapY > 0) - { - process = true; - } - else - { - body.overlapY = 0; - } - } - */ - - // Only separate on the smallest of the two values if it's a single tile - /* - if (body.overlapX !== 0 && body.overlapY !== 0) - { - if (Math.abs(body.overlapX) > Math.abs(body.overlapY)) - { - body.overlapX = 0; - } - else - { - body.overlapY = 0; - } - } - */ - - // Separate in a single sweep -/* if (process) - { - return this.processTileSeparation(body); - } - else - { - return false; - } -*/ }, /** * Internal function to process the separation of a physics body from a tile. - * @method Phaser.Physics.Arcade#processTileSeparation + * @method Phaser.Physics.Arcade#processTileSeparationX * @protected * @param {Phaser.Physics.Arcade.Body} body - The Body object to separate. - * @returns {boolean} Returns true if separated, false if not. + * @param {number} x - The x separation amount. + * @returns {boolean} Returns true as a pass-thru to the separateTile method. */ - processTileSeparation: function (body) { + processTileSeparationX: function (body, x) { - if (body.overlapX !== 0) + if (x < 0) { - if (body.overlapX < 0) - { - body.blocked.left = true; - } - else if (body.overlapX > 0) - { - body.blocked.right = true; - } - - body.x -= body.overlapX; - body.preX -= body.overlapX; - body.blocked.x = Math.floor(body.x); - body.blocked.y = Math.floor(body.y); - - if (body.bounce.x === 0) - { - body.velocity.x = 0; - } - else - { - body.velocity.x = -body.velocity.x * body.bounce.x; - } + body.blocked.left = true; + } + else if (x > 0) + { + body.blocked.right = true; } - if (body.overlapY !== 0) + body.position.x -= x; + body.preX -= x; + + if (body.bounce.x === 0) { - if (body.overlapY < 0) - { - body.blocked.up = true; - } - else if (body.overlapY > 0) - { - body.blocked.down = true; - } - - body.y -= body.overlapY; - body.preY -= body.overlapY; - body.blocked.x = Math.floor(body.x); - body.blocked.y = Math.floor(body.y); - - if (body.bounce.y === 0) - { - body.velocity.y = 0; - } - else - { - body.velocity.y = -body.velocity.y * body.bounce.y; - } + body.velocity.x = 0; + } + else + { + body.velocity.x = -body.velocity.x * body.bounce.x; } - return true; + }, + + /** + * Internal function to process the separation of a physics body from a tile. + * @method Phaser.Physics.Arcade#processTileSeparationY + * @protected + * @param {Phaser.Physics.Arcade.Body} body - The Body object to separate. + * @param {number} y - The y separation amount. + */ + processTileSeparationY: function (body, y, tile) { + + if (y < 0) + { + body.blocked.up = true; + } + else if (y > 0) + { + body.blocked.down = true; + } + + body.position.y -= y; + body.preY -= y; + + if (body.bounce.y === 0) + { + body.velocity.y = 0; + } + else + { + body.velocity.y = -body.velocity.y * body.bounce.y; + } }, diff --git a/src/physics/p2/Body.js b/src/physics/p2/Body.js index 787ef95b9..5e2ec209d 100644 --- a/src/physics/p2/Body.js +++ b/src/physics/p2/Body.js @@ -101,9 +101,9 @@ Phaser.Physics.P2.Body = function (game, sprite, x, y, mass) { this.collidesWith = []; /** - * @property {boolean} safeRemove - To avoid deleting this body during a physics step, and causing all kinds of problems, set safeRemove to true to have it removed in the next preUpdate. + * @property {boolean} removeNextStep - To avoid deleting this body during a physics step, and causing all kinds of problems, set removeNextStep to true to have it removed in the next preUpdate. */ - this.safeRemove = false; + this.removeNextStep = false; this._collideWorldBounds = true; @@ -652,10 +652,10 @@ Phaser.Physics.P2.Body.prototype = { */ preUpdate: function () { - if (this.safeRemove) + if (this.removeNextStep) { this.removeFromWorld(); - this.safeRemove = false; + this.removeNextStep = false; } }, @@ -734,7 +734,7 @@ Phaser.Physics.P2.Body.prototype = { if (this.data.world === this.game.physics.p2.world) { - this.game.physics.p2.removeBody(this); + this.game.physics.p2.removeBodyNextStep(this); } }, @@ -750,12 +750,19 @@ Phaser.Physics.P2.Body.prototype = { this.clearShapes(); - this.sprite = null; + this._bodyCallbacks = {}; + this._bodyCallbackContext = {}; + this._groupCallbacks = {}; + this._groupCallbackContext = {}; - /* - this.collideCallback = null; - this.collideCallbackContext = null; - */ + if (this.debugBody) + { + this.debugBody.destroy(); + } + + this.debugBody = null + + this.sprite = null; }, diff --git a/src/physics/p2/BodyDebug.js b/src/physics/p2/BodyDebug.js index 6854a2856..d74be111f 100644 --- a/src/physics/p2/BodyDebug.js +++ b/src/physics/p2/BodyDebug.js @@ -71,7 +71,7 @@ Phaser.Utils.extend(Phaser.Physics.P2.BodyDebug.prototype, { */ update: function() { - this.updateSpriteTransform() + this.updateSpriteTransform(); }, diff --git a/src/physics/p2/World.js b/src/physics/p2/World.js index 223a5ceb6..a554f69f4 100644 --- a/src/physics/p2/World.js +++ b/src/physics/p2/World.js @@ -143,6 +143,11 @@ Phaser.Physics.P2 = function (game, config) { this.world.on("beginContact", this.beginContactHandler, this); this.world.on("endContact", this.endContactHandler, this); + /** + * @property {array} _toRemove - Internal var used to hold references to bodies to remove from the world on the next step. + */ + this._toRemove = []; + /** * @property {array} collisionGroups - Internal var. */ @@ -173,6 +178,36 @@ Phaser.Physics.P2.LIME_CORONA_JSON = 0; Phaser.Physics.P2.prototype = { + /** + * This will add a P2 Physics body into the removal list for the next step. + * + * @method Phaser.Physics.P2#removeBodyNextStep + * @param {Phaser.Physics.P2.Body} body - The body to remove at the start of the next step. + */ + removeBodyNextStep: function (body) { + + this._toRemove.push(body); + + }, + + /** + * Called at the start of the core update loop. Purges flagged bodies from the world. + * + * @method Phaser.Physics.P2#preUpdate + */ + preUpdate: function () { + + var i = this._toRemove.length; + + while (i--) + { + this.removeBody(this._toRemove[i]); + } + + this._toRemove.length = 0; + + }, + /** * This will create a P2 Physics body on the given game object or array of game objects. * A game object can only have 1 physics body active at any one time, and it can't be changed until the object is destroyed. diff --git a/src/tilemap/Tile.js b/src/tilemap/Tile.js index f68c2eea1..b21549ae6 100644 --- a/src/tilemap/Tile.js +++ b/src/tilemap/Tile.js @@ -104,17 +104,6 @@ Phaser.Tile = function (layer, index, x, y, width, height) { */ this.faceRight = false; - /** - * @property {boolean} collides - Does this tile collide at all? - */ - this.collides = false; - - /** - * @property {boolean} collideNone - Indicating this Tile doesn't collide at all. - * @default - */ - this.collideNone = true; - /** * @property {boolean} collideLeft - Indicating collide with any object on the left. * @default @@ -155,6 +144,15 @@ Phaser.Tile = function (layer, index, x, y, width, height) { Phaser.Tile.prototype = { + /** + * Check for intersection with this tile. + * + * @method Phaser.Tile#intersects + * @param {number} x - The x axis in pixels. + * @param {number} y - The y axis in pixels. + * @param {number} right - The right point. + * @param {number} bottom - The bottom point. + */ intersects: function (x, y, right, bottom) { if (right <= this.worldX) @@ -198,6 +196,7 @@ Phaser.Tile.prototype = { /** * Clean up memory. + * * @method Phaser.Tile#destroy */ destroy: function () { @@ -210,6 +209,7 @@ Phaser.Tile.prototype = { /** * Set collision settings on this tile. + * * @method Phaser.Tile#setCollision * @param {boolean} left - Indicating collide with any object on the left. * @param {boolean} right - Indicating collide with any object on the right. @@ -223,31 +223,20 @@ Phaser.Tile.prototype = { this.collideUp = up; this.collideDown = down; - if (left || right || up || down) - { - this.collides = true; - this.collideNone = false; - } - else - { - this.collides = false; - this.collideNone = true; - } - }, /** * Reset collision status flags. + * * @method Phaser.Tile#resetCollision */ resetCollision: function () { - this.collideNone = true; this.collideLeft = false; this.collideRight = false; this.collideUp = false; this.collideDown = false; - this.collides = false; + this.faceTop = false; this.faceBottom = false; this.faceLeft = false; @@ -257,6 +246,7 @@ Phaser.Tile.prototype = { /** * Copies the tile data and properties from the given tile to this tile. + * * @method Phaser.Tile#copy * @param {Phaser.Tile} tile - The tile to copy from. */ @@ -265,12 +255,12 @@ Phaser.Tile.prototype = { this.index = tile.index; this.alpha = tile.alpha; this.properties = tile.properties; - this.collides = tile.collides; - this.collideNone = tile.collideNone; + this.collideUp = tile.collideUp; this.collideDown = tile.collideDown; this.collideLeft = tile.collideLeft; this.collideRight = tile.collideRight; + this.collisionCallback = tile.collisionCallback; this.collisionCallbackContext = tile.collisionCallbackContext; @@ -280,6 +270,19 @@ Phaser.Tile.prototype = { Phaser.Tile.prototype.constructor = Phaser.Tile; +/** +* @name Phaser.Tile#canCollide +* @property {boolean} canCollide - True if this tile can collide or has a collision callback. +* @readonly +*/ +Object.defineProperty(Phaser.Tile.prototype, "collides", { + + get: function () { + return (this.collideLeft || this.collideRight || this.collideUp || this.collideDown); + } + +}); + /** * @name Phaser.Tile#canCollide * @property {boolean} canCollide - True if this tile can collide or has a collision callback. @@ -288,20 +291,20 @@ Phaser.Tile.prototype.constructor = Phaser.Tile; Object.defineProperty(Phaser.Tile.prototype, "canCollide", { get: function () { - return (this.collides || this.collisionCallback || this.layer.callbacks[this.index]); + return (this.collideLeft || this.collideRight || this.collideUp || this.collideDown || this.collisionCallback || this.layer.callbacks[this.index]); } }); /** * @name Phaser.Tile#left -* @property {number} left - The x value. +* @property {number} left - The x value in pixels. * @readonly */ Object.defineProperty(Phaser.Tile.prototype, "left", { get: function () { - return this.x; + return this.worldX; } }); @@ -314,7 +317,7 @@ Object.defineProperty(Phaser.Tile.prototype, "left", { Object.defineProperty(Phaser.Tile.prototype, "right", { get: function () { - return this.x + this.width; + return this.worldX + this.width; } }); @@ -327,7 +330,7 @@ Object.defineProperty(Phaser.Tile.prototype, "right", { Object.defineProperty(Phaser.Tile.prototype, "top", { get: function () { - return this.y; + return this.worldY; } }); @@ -340,7 +343,7 @@ Object.defineProperty(Phaser.Tile.prototype, "top", { Object.defineProperty(Phaser.Tile.prototype, "bottom", { get: function () { - return this.y + this.height; + return this.worldY + this.height; } }); diff --git a/src/tilemap/Tilemap.js b/src/tilemap/Tilemap.js index 7ea17e83a..61899f571 100644 --- a/src/tilemap/Tilemap.js +++ b/src/tilemap/Tilemap.js @@ -661,7 +661,15 @@ Phaser.Tilemap.prototype = { if (tile && tile.index === index) { - tile.collides = collides; + if (collides) + { + tile.setCollision(true, true, true, true); + } + else + { + tile.resetCollision(); + } + tile.faceTop = collides; tile.faceBottom = collides; tile.faceLeft = collides; diff --git a/src/tilemap/TilemapLayer.js b/src/tilemap/TilemapLayer.js index ad4e4cbad..fbe5937c6 100644 --- a/src/tilemap/TilemapLayer.js +++ b/src/tilemap/TilemapLayer.js @@ -454,31 +454,20 @@ Phaser.TilemapLayer.prototype.getTileXY = function (x, y, point) { } -Phaser.TilemapLayer.prototype.getIntersectingTiles = function (x, y, width, height) { +Phaser.TilemapLayer.prototype.getIntersectingTiles = function (x, y, width, height, right, bottom) { - // var tiles = this.getTiles(x, y, width,height, true); - var tiles = this.getTiles(x, y, width,height, false); - - /* - var right = x + width; - var bottom = y + height; + var tiles = this.getTiles(x, y, width, height, false); // We only want the ones that we actually intersect with var i = tiles.length; while (i--) { - if (tiles[i].intersects(x, y, right, bottom)) + if (!tiles[i].intersects(x, y, right, bottom)) { - // tiles[i].debug = true; - } - else - { - // console.log('sliced off tile', i); - // tiles.pop(); + tiles.pop(); } } - */ return tiles; @@ -580,8 +569,7 @@ Phaser.TilemapLayer.prototype.getTiles = function (x, y, width, height, collides this._th = (this.game.math.snapToCeil(height, this._ch) + this._ch) / this._ch; // This should apply the layer x/y here - // this._results.length = 0; - this._results = []; + this._results.length = 0; for (var wy = this._ty; wy < this._ty + this._th; wy++) { @@ -591,23 +579,7 @@ Phaser.TilemapLayer.prototype.getTiles = function (x, y, width, height, collides { if (collides === false || (collides && this.layer.data[wy][wx].canCollide)) { - /* - // Convert tile coordinates back to camera space for return - var _wx = this._unfixX(wx * this._cw) / this._cw; - var _wy = this._unfixY(wy * this._ch) / this._ch; - - this._results.push({ - x: _wx * this._cw, - y: _wy * this._ch, - right: (_wx * this._cw) + this._cw, - bottom: (_wy * this._ch) + this._ch, - tile: this.layer.data[wy][wx], - layer: this.layer.data[wy][wx].layer - }); - */ - this._results.push(this.layer.data[wy][wx]); - } } } @@ -708,11 +680,11 @@ Phaser.TilemapLayer.prototype.render = function () { set.draw(this.context, Math.floor(this._tx), Math.floor(this._ty), tile.index); - if (tile.debug) - { - this.context.fillStyle = 'rgba(0, 255, 0, 0.4)'; - this.context.fillRect(Math.floor(this._tx), Math.floor(this._ty), this.map.tileWidth, this.map.tileHeight); - } + // if (tile.debug) + // { + // this.context.fillStyle = 'rgba(0, 255, 0, 0.4)'; + // this.context.fillRect(Math.floor(this._tx), Math.floor(this._ty), this.map.tileWidth, this.map.tileHeight); + // } } this._tx += this.map.tileWidth; @@ -934,14 +906,6 @@ Phaser.TilemapLayer.prototype.renderDebug = function () { this.context.stroke(); } - // Collision callback - // if (tile && (tile.collisionCallback || tile.layer.callbacks[tile.index])) - // { - // this.context.fillStyle = this.debugCallbackColor; - // this.context.fillRect(this._tx, this._ty, this._cw, this._ch); - // this.context.fillStyle = this.debugFillColor; - // } - this._tx += this.map.tileWidth; }