From f321cab77ed6e250bab59ef354f20eb5bc444911 Mon Sep 17 00:00:00 2001 From: photonstorm Date: Tue, 11 Mar 2014 01:23:41 +0000 Subject: [PATCH] The physics configuration object can now be used to start physics systems specifically. --- examples/wip/physics config.js | 108 +++++++++++++++++++++++++++++++++ src/physics/Physics.js | 31 +++++++++- 2 files changed, 138 insertions(+), 1 deletion(-) create mode 100644 examples/wip/physics config.js diff --git a/examples/wip/physics config.js b/examples/wip/physics config.js new file mode 100644 index 000000000..444528029 --- /dev/null +++ b/examples/wip/physics config.js @@ -0,0 +1,108 @@ +// Here we'll use a Physics Configuration object to specify which physics systems should be started with the game. + +var physicsConfig = { + + arcade: true, + ninja: false, + p2: true + +}; + +var game = new Phaser.Game(800, 600, Phaser.AUTO, 'phaser-example', { preload: preload, create: create, update: update, render: render }, false, false, physicsConfig); + +function preload() { + + game.load.image('atari', 'assets/sprites/atari130xe.png'); + game.load.spritesheet('bullets', 'assets/sprites/balls.png', 17, 17); + +} + +var atari; +var balls; + +function create() { + + game.stage.backgroundColor = '#2d2d2d'; + + balls = game.add.group(); + + balls.createMultiple(250, 'bullets', 0, false); + + atari = game.add.sprite(300, 450, 'atari'); + + game.physics.arcade.gravity.y = 400; + + // Enable physics on everything added to the world so far (the true parameter makes it recurse down into children) + game.physics.arcade.enable(game.world, true); + + atari.body.gravityScale.y = 0; + atari.body.immovable = true; + + cursors = game.input.keyboard.createCursorKeys(); + + game.time.events.loop(150, fire, this); + +} + +function fire() { + + var ball = balls.getFirstExists(false); + + if (ball) + { + ball.frame = game.rnd.integerInRange(0,6); + ball.exists = true; + ball.reset(game.world.randomX, 0); + + ball.body.bounce.y = 0.8; + } + +} + +function reflect(a, ball) { + + if (ball.y > (atari.y + 5)) + { + return true; + } + else + { + ball.body.velocity.x = atari.body.velocity.x; + ball.body.velocity.y *= -(ball.body.bounce.y); + + return false; + } + +} + +function update() { + + game.physics.arcade.collide(atari, balls, null, reflect, this); + + atari.body.velocity.x = 0; + + if (cursors.left.isDown) + { + atari.body.velocity.x = -200; + } + else if (cursors.right.isDown) + { + atari.body.velocity.x = 200; + } + + balls.forEachAlive(checkBounds, this); + +} + +function checkBounds(ball) { + + if (ball.y > 600) + { + ball.kill(); + } + +} + +function render() { + +} diff --git a/src/physics/Physics.js b/src/physics/Physics.js index 1f02ea674..bdd477ce5 100644 --- a/src/physics/Physics.js +++ b/src/physics/Physics.js @@ -20,6 +20,8 @@ */ Phaser.Physics = function (game, config) { + config = config || {}; + /** * @property {Phaser.Game} game - Local reference to game. */ @@ -33,7 +35,7 @@ Phaser.Physics = function (game, config) { /** * @property {Phaser.Physics.Arcade} arcade - The Arcade Physics system. */ - this.arcade = new Phaser.Physics.Arcade(game); + this.arcade = null; /** * @property {Phaser.Physics.P2} p2 - The P2.JS Physics system. @@ -55,6 +57,8 @@ Phaser.Physics = function (game, config) { */ this.chipmunk = null; + this.parseConfig(); + }; /** @@ -89,6 +93,31 @@ Phaser.Physics.CHIPMUNK = 5; Phaser.Physics.prototype = { + /** + * Parses the Physics Configuration object passed to the Game constructor and starts any physics systems specified within. + * + * @method Phaser.Physics#parseConfig + */ + parseConfig: function () { + + if ((!this.config.hasOwnProperty('arcade') || this.config['arcade'] === true) && Phaser.Physics.hasOwnProperty('Arcade')) + { + // If Arcade isn't specified, we create it automatically if we can + this.arcade = new Phaser.Physics.Arcade(this.game); + } + + if (this.config.hasOwnProperty('ninja') && this.config['ninja'] === true && Phaser.Physics.hasOwnProperty('Ninja')) + { + this.ninja = new Phaser.Physics.Ninja(this.game); + } + + if (this.config.hasOwnProperty('p2') && this.config['p2'] === true && Phaser.Physics.hasOwnProperty('P2')) + { + this.p2 = new Phaser.Physics.P2(this.game, this.config); + } + + }, + /** * This will create an instance of the requested physics simulation. * Phaser.Physics.Arcade is running by default, but all others need activating directly.