diff --git a/examples/tilemaps/create from objects.js b/examples/tilemaps/create from objects.js index 1428783be7..879a5351c5 100644 --- a/examples/tilemaps/create from objects.js +++ b/examples/tilemaps/create from objects.js @@ -33,12 +33,13 @@ function create() { layer = map.createLayer('Tile Layer 1'); - // layer.debug = true; - layer.resizeWorld(); + game.physics.startSystem(Phaser.Physics.ARCADE); + // Here we create our coins group coins = game.add.group(); + coins.enableBody = true; // And now we convert all of the Tiled objects with an ID of 34 into sprites within the coins group map.createFromObjects('Object Layer 1', 34, 'coin', 0, true, false, coins); @@ -48,10 +49,12 @@ function create() { coins.callAll('animations.play', 'animations', 'spin'); sprite = game.add.sprite(260, 100, 'phaser'); - sprite.anchor.setTo(0.5, 0.5); + sprite.anchor.set(0.5); + + game.physics.arcade.enable(sprite); // This adjusts the collision body size. - sprite.body.setRectangle(16, 16, 25, 15); + sprite.body.setSize(32, 32, 16, 16); // We'll set a lower max angular velocity here to keep it from going totally nuts sprite.body.maxAngular = 500; @@ -67,8 +70,8 @@ function create() { function update() { - game.physics.collide(sprite, layer); - game.physics.overlap(sprite, coins, collectCoin, null, this); + game.physics.arcade.collide(sprite, layer); + game.physics.arcade.overlap(sprite, coins, collectCoin, null, this); sprite.body.velocity.x = 0; sprite.body.velocity.y = 0; @@ -85,10 +88,9 @@ function update() { if (cursors.up.isDown) { - game.physics.velocityFromAngle(sprite.angle, 300, sprite.body.velocity); + game.physics.arcade.velocityFromAngle(sprite.angle, 300, sprite.body.velocity); } - } function collectCoin(player, coin) { @@ -99,6 +101,6 @@ function collectCoin(player, coin) { function render() { - game.debug.physicsBody(sprite.body); + game.debug.body(sprite); } \ No newline at end of file diff --git a/examples/tilemaps/fill tiles.js b/examples/tilemaps/fill tiles.js index 6203a2e9d9..8807782cbf 100644 --- a/examples/tilemaps/fill tiles.js +++ b/examples/tilemaps/fill tiles.js @@ -17,6 +17,8 @@ var sprite; function create() { + game.physics.startSystem(Phaser.Physics.ARCADE); + map = game.add.tilemap('desert'); map.addTilesetImage('Desert', 'tiles'); @@ -28,6 +30,8 @@ function create() { sprite = game.add.sprite(450, 80, 'car'); sprite.anchor.setTo(0.5, 0.5); + game.physics.enable(sprite); + game.camera.follow(sprite); cursors = game.input.keyboard.createCursorKeys(); @@ -44,7 +48,7 @@ function fillTiles() { function update() { - game.physics.collide(sprite, layer); + game.physics.arcade.collide(sprite, layer); sprite.body.velocity.x = 0; sprite.body.velocity.y = 0; @@ -61,7 +65,7 @@ function update() { if (cursors.up.isDown) { - sprite.body.velocity.copyFrom(game.physics.velocityFromAngle(sprite.angle, 300)); + sprite.body.velocity.copyFrom(game.physics.arcade.velocityFromAngle(sprite.angle, 300)); } } diff --git a/examples/tilemaps/mapcollide.js b/examples/tilemaps/map collide.js similarity index 87% rename from examples/tilemaps/mapcollide.js rename to examples/tilemaps/map collide.js index 584b70f730..95e55a5fd6 100644 --- a/examples/tilemaps/mapcollide.js +++ b/examples/tilemaps/map collide.js @@ -17,6 +17,8 @@ var cursors; function create() { + game.physics.startSystem(Phaser.Physics.ARCADE); + game.stage.backgroundColor = '#787878'; map = game.add.tilemap('mario'); @@ -40,7 +42,9 @@ function create() { p = game.add.sprite(32, 32, 'player'); - game.physics.gravity.y = 250; + game.physics.enable(p); + + game.physics.arcade.gravity.y = 250; p.body.bounce.y = 0.2; p.body.linearDamping = 1; @@ -54,7 +58,7 @@ function create() { function update() { - game.physics.collide(p, layer); + game.physics.arcade.collide(p, layer); p.body.velocity.x = 0; @@ -79,7 +83,7 @@ function update() { function render() { - game.debug.cameraInfo(game.camera, 420, 320); - game.debug.physicsBody(p.body); + // game.debug.body(p); + game.debug.bodyInfo(p, 32, 320); } diff --git a/examples/tilemaps/mario.js b/examples/tilemaps/mario.js deleted file mode 100644 index dc20714579..0000000000 --- a/examples/tilemaps/mario.js +++ /dev/null @@ -1,53 +0,0 @@ - -var game = new Phaser.Game(800, 600, Phaser.AUTO, 'phaser-example', { preload: preload, create: create, update: update }); - -function preload() { - - game.load.tilemap('mario', 'assets/tilemaps/maps/super_mario.json', null, Phaser.Tilemap.TILED_JSON); - game.load.image('tiles', 'assets/tilemaps/tiles/super_mario.png'); - game.load.image('player', 'assets/sprites/phaser-dude.png'); - -} - -var map; -var layer; -var p; -var cursors; - -function create() { - - game.stage.backgroundColor = '#787878'; - - map = game.add.tilemap('mario'); - - map.addTilesetImage('SuperMarioBros-World1-1', 'tiles'); - - layer = map.createLayer('World1'); - - layer.resizeWorld(); - - cursors = game.input.keyboard.createCursorKeys(); - -} - -function update() { - - if (cursors.left.isDown) - { - game.camera.x -= 8; - } - else if (cursors.right.isDown) - { - game.camera.x += 8; - } - - if (cursors.up.isDown) - { - game.camera.y -= 8; - } - else if (cursors.down.isDown) - { - game.camera.y += 8; - } - -} \ No newline at end of file diff --git a/examples/tilemaps/randomise tiles.js b/examples/tilemaps/randomise tiles.js index b2eca6e401..36abe4466c 100644 --- a/examples/tilemaps/randomise tiles.js +++ b/examples/tilemaps/randomise tiles.js @@ -18,6 +18,8 @@ var marker; function create() { + game.physics.startSystem(Phaser.Physics.ARCADE); + map = game.add.tilemap('desert'); map.addTilesetImage('Desert', 'tiles'); @@ -33,11 +35,12 @@ function create() { sprite = game.add.sprite(450, 80, 'car'); sprite.anchor.setTo(0.5, 0.5); + game.physics.enable(sprite); + game.camera.follow(sprite); cursors = game.input.keyboard.createCursorKeys(); - game.input.onDown.add(randomiseTiles, this); } @@ -68,7 +71,7 @@ function update() { if (cursors.up.isDown) { - sprite.body.velocity.copyFrom(game.physics.velocityFromAngle(sprite.angle, 300)); + sprite.body.velocity.copyFrom(game.physics.arcade.velocityFromAngle(sprite.angle, 300)); } } diff --git a/examples/tilemaps/replace tiles.js b/examples/tilemaps/replace tiles.js index b03b6788e9..79a0c0618e 100644 --- a/examples/tilemaps/replace tiles.js +++ b/examples/tilemaps/replace tiles.js @@ -17,6 +17,8 @@ var sprite; function create() { + game.physics.startSystem(Phaser.Physics.ARCADE); + map = game.add.tilemap('desert'); map.addTilesetImage('Desert', 'tiles'); @@ -28,6 +30,8 @@ function create() { sprite = game.add.sprite(450, 80, 'car'); sprite.anchor.setTo(0.5, 0.5); + game.physics.enable(sprite); + game.camera.follow(sprite); cursors = game.input.keyboard.createCursorKeys(); @@ -64,7 +68,7 @@ function update() { if (cursors.up.isDown) { - sprite.body.velocity.copyFrom(game.physics.velocityFromAngle(sprite.angle, 300)); + sprite.body.velocity.copyFrom(game.physics.arcade.velocityFromAngle(sprite.angle, 300)); } } diff --git a/examples/tilemaps/shuffle tiles.js b/examples/tilemaps/shuffle tiles.js index 29d734cbd6..fad8371f55 100644 --- a/examples/tilemaps/shuffle tiles.js +++ b/examples/tilemaps/shuffle tiles.js @@ -18,6 +18,8 @@ var marker; function create() { + game.physics.startSystem(Phaser.Physics.ARCADE); + map = game.add.tilemap('desert'); map.addTilesetImage('Desert', 'tiles'); @@ -33,11 +35,12 @@ function create() { sprite = game.add.sprite(450, 80, 'car'); sprite.anchor.setTo(0.5, 0.5); + game.physics.enable(sprite); + game.camera.follow(sprite); cursors = game.input.keyboard.createCursorKeys(); - game.input.onDown.add(randomiseTiles, this); } @@ -68,7 +71,7 @@ function update() { if (cursors.up.isDown) { - sprite.body.velocity.copyFrom(game.physics.velocityFromAngle(sprite.angle, 300)); + sprite.body.velocity.copyFrom(game.physics.arcade.velocityFromAngle(sprite.angle, 300)); } } diff --git a/examples/tilemaps/tile callbacks.js b/examples/tilemaps/tile callbacks.js index 8244dad6ca..6450d7fc5f 100644 --- a/examples/tilemaps/tile callbacks.js +++ b/examples/tilemaps/tile callbacks.js @@ -19,29 +19,29 @@ var cursors; function create() { + game.physics.startSystem(Phaser.Physics.ARCADE); + map = game.add.tilemap('map'); map.addTilesetImage('ground_1x1'); map.addTilesetImage('coin'); map.setCollisionBetween(1, 12); - map.setTileIndexCallback(26, hitCoin, this); - map.setTileLocationCallback(2, 0, 1, 1, hitCoin, this); layer = map.createLayer('Tile Layer 1'); - // layer.debug = true; - layer.resizeWorld(); - game.physics.gravity.y = 100; + game.physics.arcade.gravity.y = 100; sprite = game.add.sprite(260, 100, 'phaser'); - sprite.anchor.setTo(0.5, 0.5); + sprite.anchor.set(0.5); - sprite.body.setRectangle(16, 16, 8, 8); + game.physics.enable(sprite); + + sprite.body.setSize(16, 16, 8, 8); // We'll set a lower max angular velocity here to keep it from going totally nuts sprite.body.maxAngular = 500; @@ -49,11 +49,6 @@ function create() { // Apply a drag otherwise the sprite will just spin and never slow down sprite.body.angularDrag = 50; - // sprite.body.bounce.x = 0.8; - // sprite.body.bounce.y = 0.8; - - debugSprite = sprite; - game.camera.follow(sprite); cursors = game.input.keyboard.createCursorKeys(); @@ -72,7 +67,7 @@ function hitCoin(sprite, tile) { function update() { - game.physics.collide(sprite, layer); + game.physics.arcade.collide(sprite, layer); sprite.body.velocity.x = 0; sprite.body.velocity.y = 0; @@ -89,14 +84,14 @@ function update() { if (cursors.up.isDown) { - game.physics.velocityFromAngle(sprite.angle, 300, sprite.body.velocity); + game.physics.arcade.velocityFromAngle(sprite.angle, 300, sprite.body.velocity); } } function render() { - // game.debug.bodyInfo(sprite, 16, 24); - // game.debug.physicsBody(sprite.body); + game.debug.bodyInfo(sprite, 16, 24); + game.debug.body(sprite); } \ No newline at end of file diff --git a/src/core/Group.js b/src/core/Group.js index b841a20afd..ac83478ae4 100644 --- a/src/core/Group.js +++ b/src/core/Group.js @@ -100,8 +100,8 @@ Phaser.Group = function (game, parent, name, addToStage, enableBody, physicsBody this.cameraOffset = new Phaser.Point(); /** + * @property {boolean} enableBody - If true all Sprites created by, or added to this Group, will have a physics body enabled on them. Change the body type with `Group.physicsBodyType`. * @default - * @property {boolean} enableBody - If true all Sprites created with `Group.create` or `Group.createMulitple` will have a physics body created on them. Change the body type with `Group.physicsBodyType`. */ this.enableBody = false; @@ -188,6 +188,11 @@ Phaser.Group.prototype.add = function (child) { if (child.parent !== this) { + if (this.enableBody) + { + this.game.physics.enable(child, this.physicsBodyType); + } + this.addChild(child); child.z = this.children.length; @@ -220,6 +225,11 @@ Phaser.Group.prototype.addAt = function (child, index) { if (child.parent !== this) { + if (this.enableBody) + { + this.game.physics.enable(child, this.physicsBodyType); + } + this.addChildAt(child, index); this.updateZ(); @@ -279,18 +289,7 @@ Phaser.Group.prototype.create = function (x, y, key, frame, exists) { if (this.enableBody) { - if (this.physicsBodyType === Phaser.Physics.ARCADE) - { - this.game.physics.arcade.enable(child); - } - else if (this.physicsBodyType === Phaser.Physics.NINJA && this.game.physics.ninja) - { - this.game.physics.ninja.enable(child); - } - else if (this.physicsBodyType === Phaser.Physics.P2JS && this.game.physics.p2) - { - this.game.physics.p2.enable(child, this.enableBodyDebug); - } + this.game.physics.enable(child, this.physicsBodyType); } child.exists = exists; diff --git a/src/physics/arcade/Body.js b/src/physics/arcade/Body.js index 02141169a5..b42353768f 100644 --- a/src/physics/arcade/Body.js +++ b/src/physics/arcade/Body.js @@ -35,22 +35,22 @@ Phaser.Physics.Arcade.Body = function (sprite) { */ this.offset = new Phaser.Point(); - /** - * @property {Phaser.Point} position - The position of the physics body. - * @readonly - */ - this.position = new Phaser.Point(sprite.x, sprite.y); - if (sprite.anchor.x !== 0) { - this.position.x -= (sprite.anchor.x * sprite.width); + this.offset.x = (sprite.anchor.x * sprite.width); } if (sprite.anchor.y !== 0) { - this.position.y -= (sprite.anchor.y * sprite.height); + this.offset.y = (sprite.anchor.y * sprite.height); } + /** + * @property {Phaser.Point} position - The position of the physics body. + * @readonly + */ + this.position = new Phaser.Point(sprite.x, sprite.y); + /** * @property {Phaser.Point} prev - The previous position of the physics body. * @readonly @@ -316,7 +316,7 @@ Phaser.Physics.Arcade.Body.prototype = { * @method Phaser.Physics.Arcade#updateBounds * @protected */ - updateBounds: function (centerX, centerY, scaleX, scaleY) { + updateBounds: function (scaleX, scaleY) { if (scaleX != this._sx || scaleY != this._sy) { @@ -411,12 +411,11 @@ Phaser.Physics.Arcade.Body.prototype = { this.facing = Phaser.DOWN; } - 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); - } + this.sprite.x = this.position.x + this.offset.x; + this.sprite.y = this.position.y + this.offset.y; + // this.sprite.x += this.deltaX(); + // this.sprite.y += this.deltaY(); + this.center.setTo(this.x + this.halfWidth, this.y + this.halfHeight); if (this.allowRotation) { @@ -440,22 +439,26 @@ Phaser.Physics.Arcade.Body.prototype = { { this.x = this.game.world.bounds.x; this.velocity.x *= -this.bounce.x; + this.blocked.left = true; } else if (this.right > this.game.world.bounds.right) { this.x = this.game.world.bounds.right - this.width; this.velocity.x *= -this.bounce.x; + this.blocked.right = true; } if (this.y < this.game.world.bounds.y) { this.y = this.game.world.bounds.y; this.velocity.y *= -this.bounce.y; + this.blocked.up = true; } else if (this.bottom > this.game.world.bounds.bottom) { this.y = this.game.world.bounds.bottom - this.height; this.velocity.y *= -this.bounce.y; + this.blocked.down = true; } }, @@ -512,6 +515,26 @@ Phaser.Physics.Arcade.Body.prototype = { }, + /** + * Returns true if the bottom of this Body is in contact with either the world bounds or a tile. + * + * @method Phaser.Physics.Arcade.Body#onFloor + * @return {boolean} True if in contact with either the world bounds or a tile. + */ + onFloor: function () { + return this.blocked.down; + }, + + /** + * Returns true if either side of this Body is in contact with either the world bounds or a tile. + * + * @method Phaser.Physics.Arcade.Body#onWall + * @return {boolean} True if in contact with either the world bounds or a tile. + */ + onWall: function () { + return (this.blocked.left || this.blocked.right); + }, + /** * Returns the absolute delta x value. * @@ -664,12 +687,10 @@ Phaser.Physics.Arcade.Body.renderBodyInfo = function (debug, body) { debug.line('x: ' + body.x.toFixed(2), 'y: ' + body.y.toFixed(2), 'width: ' + body.width, 'height: ' + body.height); // debug.line('velocity x: ' + body.velocity.x.toFixed(2), 'y: ' + body.velocity.y.toFixed(2), 'deltaX: ' + body.deltaX().toFixed(2), 'deltaY: ' + body.deltaY().toFixed(2)); debug.line('velocity x: ' + body.velocity.x.toFixed(2), 'y: ' + body.velocity.y.toFixed(2)); - debug.line('acceleration x: ' + body.acceleration.x.toFixed(2), 'y: ' + body.acceleration.y.toFixed(2)); - // debug.line('gravity x: ' + body.gravity.x, 'y: ' + body.gravity.y, 'world gravity x: ' + this.game.physics.gravity.x, 'y: ' + this.game.physics.gravity.y); - debug.line('gravity x: ' + body.gravity.x, 'y: ' + body.gravity.y); - debug.line('bounce x: ' + body.bounce.x.toFixed(2), 'y: ' + body.bounce.y.toFixed(2)); - debug.line('speed: ' + body.speed.toFixed(2), 'angle: ' + body.angle.toFixed(2)); + debug.line('acceleration x: ' + body.acceleration.x.toFixed(2), 'y: ' + body.acceleration.y.toFixed(2), 'speed: ' + body.speed.toFixed(2), 'angle: ' + body.angle.toFixed(2)); + debug.line('gravity x: ' + body.gravity.x, 'y: ' + body.gravity.y, 'bounce x: ' + body.bounce.x.toFixed(2), 'y: ' + body.bounce.y.toFixed(2)); debug.line('touching left: ' + body.touching.left, 'right: ' + body.touching.right, 'up: ' + body.touching.up, 'down: ' + body.touching.down); + debug.line('blocked left: ' + body.blocked.left, 'right: ' + body.blocked.right, 'up: ' + body.blocked.up, 'down: ' + body.blocked.down); } diff --git a/src/tilemap/Tilemap.js b/src/tilemap/Tilemap.js index 61899f571c..b2ffdb0694 100644 --- a/src/tilemap/Tilemap.js +++ b/src/tilemap/Tilemap.js @@ -290,14 +290,6 @@ Phaser.Tilemap.prototype = { }, - /* - createFromTiles: function (layer, tileIndex, key, frame, group) { - - if (typeof group === 'undefined') { group = this.game.world; } - - }, - */ - /** * Creates a Sprite for every object matching the given gid in the map data. You can optionally specify the group that the Sprite will be created in. If none is * given it will be created in the World. All properties from the map data objectgroup are copied across to the Sprite, so you can use this as an easy way to @@ -347,7 +339,6 @@ Phaser.Tilemap.prototype = { { group.set(sprite, property, this.objects[name][i].properties[property], false, false, 0); } - } } diff --git a/src/tilemap/TilemapLayer.js b/src/tilemap/TilemapLayer.js index 73e22915d7..b26002b088 100644 --- a/src/tilemap/TilemapLayer.js +++ b/src/tilemap/TilemapLayer.js @@ -686,138 +686,6 @@ Phaser.TilemapLayer.prototype.render = function () { } -/** -* Renders the tiles to the layer canvas and pushes to the display. -* @method Phaser.TilemapLayer#render -* @memberof Phaser.TilemapLayer -*/ -Phaser.TilemapLayer.prototype.OLDrender = function () { - - if (this.layer.dirty) - { - this.dirty = true; - } - - if (!this.dirty || !this.visible) - { - return; - } - - this._prevX = this._dx; - this._prevY = this._dy; - - this._dx = -(this._x - (this._startX * this.map.tileWidth)); - this._dy = -(this._y - (this._startY * this.map.tileHeight)); - - this._tx = this._dx; - this._ty = this._dy; - - this.context.clearRect(0, 0, this.canvas.width, this.canvas.height); - - this.context.fillStyle = this.tileColor; - - var tile; - var set; - // var ox = 0; - // var oy = 0; - - if (this.debug) - { - this.context.globalAlpha = this.debugAlpha; - } - - for (var y = this._startY, lenY = this._startY + this._maxY; y < lenY; y++) - { - this._column = this.layer.data[y]; - - for (var x = this._startX, lenX = this._startX + this._maxX; x < lenX; x++) - { - if (this._column[x]) - { - tile = this._column[x]; - - if (this.map.tiles[tile.index]) - { - set = this.map.tilesets[this.map.tiles[tile.index][2]] - - if (set.image) - { - if (this.debug === false && tile.alpha !== this.context.globalAlpha) - { - this.context.globalAlpha = tile.alpha; - } - - if (set.tileWidth !== this.map.tileWidth || set.tileHeight !== this.map.tileHeight) - { - // TODO: Smaller sized tile check - this.context.drawImage( - this.map.tilesets[this.map.tiles[tile.index][2]].image, - this.map.tiles[tile.index][0], - this.map.tiles[tile.index][1], - set.tileWidth, - set.tileHeight, - Math.floor(this._tx), - Math.floor(this._ty) - (set.tileHeight - this.map.tileHeight), - set.tileWidth, - set.tileHeight - ); - } - else - { - this.context.drawImage( - this.map.tilesets[this.map.tiles[tile.index][2]].image, - this.map.tiles[tile.index][0], - this.map.tiles[tile.index][1], - this.map.tileWidth, - this.map.tileHeight, - 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); - } - } - else - { - this.context.fillRect(Math.floor(this._tx), Math.floor(this._ty), this.map.tileWidth, this.map.tileHeight); - } - } - } - - this._tx += this.map.tileWidth; - - } - - this._tx = this._dx; - this._ty += this.map.tileHeight; - - } - - if (this.debug) - { - this.context.globalAlpha = 1; - this.renderDebug(); - } - - if (this.game.renderType === Phaser.WEBGL) - { - // PIXI.updateWebGLTexture(this.baseTexture, renderSession.gl); - PIXI.updateWebGLTexture(this.baseTexture, this.game.renderer.gl); - } - - this.dirty = false; - this.layer.dirty = false; - - return true; - -} - /** * Renders a collision debug overlay on-top of the canvas. Called automatically by render when debug = true. * @method Phaser.TilemapLayer#renderDebug