Skip to content

Commit

Permalink
The main Game class has been modified so that the update methods no l…
Browse files Browse the repository at this point in the history
…onger have any if/else checks in them. Now split into coreUpdate, etc.

Put QuadTree back into Debug class.
Debug class now clears down on WebGL.
Updated RetroFont.type.
Fixed QuadTree example.
  • Loading branch information
photonstorm committed Mar 11, 2014
1 parent 4aba75a commit 6bddf1a
Show file tree
Hide file tree
Showing 7 changed files with 171 additions and 112 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ Significant API changes:
* Animation.looped has been renamed to Animation.loop. It's a boolean you can toggle at run-time to turn on/off animation looping.
* Sprite.damage will now kill the Sprite if health is less than or equal to 0 (before it would only kill if less than zero)
* By default Sprites no longer check if they are within the world bounds. It's quite an expensive process (calling getBounds every frame), so you have to enable directly.
* The main Game class has been modified so that the update methods no longer have any if/else checks in them. Now split into coreUpdate, etc.


New features:
Expand Down
31 changes: 12 additions & 19 deletions examples/arcade physics/quadtree - collision infos.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@
var game = new Phaser.Game(800, 600, Phaser.CANVAS, 'phaser-example', { preload: preload, create: create, update: update, render: render });

function preload() {

game.load.image('ship', 'assets/sprites/xenon2_ship.png');
game.load.image('baddie', 'assets/sprites/space-baddie.png');

}

var ship;
Expand All @@ -12,58 +14,49 @@ var aliens;
function create() {

aliens = game.add.group();
aliens.enableBody = true;

for (var i = 0; i < 50; i++)
{
var s = aliens.create(game.world.randomX, game.world.randomY, 'baddie');
game.physics.enable(s, Phaser.Physics.ARCADE);
s.name = 'alien' + s;
s.body.collideWorldBounds = true;
s.body.bounce.setTo(1, 1);
s.body.bounce.set(1);
s.body.velocity.setTo(10 + Math.random() * 40, 10 + Math.random() * 40);
}

ship = game.add.sprite(400, 400, 'ship');
game.physics.enable(ship, Phaser.Physics.ARCADE);
ship.body.collideWorldBounds = true;
ship.body.bounce.setTo(1, 1);
ship.body.bounce.set(1);

}

function update() {

game.physics.arcade.collide(ship, aliens);

if (game.input.keyboard.isDown(Phaser.Keyboard.LEFT))
{
ship.body.velocity.x -= 2;
ship.body.velocity.x -= 4;
}
else if (game.input.keyboard.isDown(Phaser.Keyboard.RIGHT))
{
ship.body.velocity.x += 2;
ship.body.velocity.x += 4;
}

if (game.input.keyboard.isDown(Phaser.Keyboard.UP))
{
ship.body.velocity.y -= 2;
ship.body.velocity.y -= 4;
}
else if (game.input.keyboard.isDown(Phaser.Keyboard.DOWN))
{
ship.body.velocity.y += 2;
ship.body.velocity.y += 4;
}

game.physics.arcade.collide(ship, aliens);

}

function render() {

game.debug.QuadTree(game.physics.arcade.quadTree);
game.debug.geom(ship.body);

// game.debug.text('total: ' + total.length, 32, 50);

game.debug.text('up: ' + ship.body.touching.up, 32, 70);
game.debug.text('down: ' + ship.body.touching.down, 32, 90);
game.debug.text('left: ' + ship.body.touching.left, 32, 110);
game.debug.text('right: ' + ship.body.touching.right, 32, 130);
game.debug.quadTree(game.physics.arcade.quadTree);

}
18 changes: 6 additions & 12 deletions src/Phaser.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,19 +36,13 @@ var Phaser = Phaser || {
WEBGL_FILTER: 15,
ELLIPSE: 16,
SPRITEBATCH: 17,
BITMAPFONT: 18,
RETROFONT: 18,

NONE: 0,
LEFT: 1,
RIGHT: 2,
UP: 3,
DOWN: 4,
// DYNAMIC: 1,
// STATIC: 2,
// KINEMATIC: 4,

DYNAMIC: 1,
STATIC: 2,
KINEMATIC: 4,

// the various blend modes supported by pixi / phaser
// The various blend modes supported by pixi / phaser
blendModes: {
NORMAL:0,
ADD:1,
Expand All @@ -69,7 +63,7 @@ var Phaser = Phaser || {
LUMINOSITY:16
},

// the scale modes
// The scale modes
scaleModes: {
DEFAULT:0,
LINEAR:0,
Expand Down
154 changes: 106 additions & 48 deletions src/core/Game.js
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,16 @@ Phaser.Game = function (width, height, renderer, parent, state, transparent, ant
*/
this.particles = null;

/**
* @property {function} update - The current update method being called.
*/
this.update = this.coreUpdate;

/**
* @property {function} render - The current render method being called.
*/
this.render = this.coreRender;

/**
* @property {boolean} stepping - Enable core loop stepping with Game.enableStep().
* @default
Expand Down Expand Up @@ -532,79 +542,117 @@ Phaser.Game.prototype = {
this.context = null;
}

this.stage.smoothed = this.antialias;
if (this.renderType === Phaser.HEADLESS)
{
this.render = this.headlessRender;
}
else
{
this.stage.smoothed = this.antialias;

Phaser.Canvas.addToDOM(this.canvas, this.parent, true);
Phaser.Canvas.setTouchAction(this.canvas);
Phaser.Canvas.addToDOM(this.canvas, this.parent, true);
Phaser.Canvas.setTouchAction(this.canvas);
}

},

/**
* The core game loop.
*
* @method Phaser.Game#update
* @method Phaser.Game#coreUpdate
* @protected
* @param {number} time - The current time as provided by RequestAnimationFrame.
*/
update: function (time) {
coreUpdate: function (time) {

this.time.update(time);

if (this._paused)
{
this.input.update();
this.debug.preUpdate();
this.state.preUpdate();
this.plugins.preUpdate();
this.stage.preUpdate();

if (this.renderType !== Phaser.HEADLESS)
{
this.renderer.render(this.stage);
this.plugins.render();
this.state.render();
this.stage.update();
this.tweens.update();
this.sound.update();
this.input.update();
this.state.update();
this.physics.update();
this.particles.update();
this.plugins.update();

this.plugins.postRender();
}
}
else
{
if (!this.pendingStep)
{
if (this.stepping)
{
this.pendingStep = true;
}
this.stage.postUpdate();
this.plugins.postUpdate();

this.debug.preUpdate();
this.state.preUpdate();
this.plugins.preUpdate();
this.stage.preUpdate();

this.stage.update();
this.tweens.update();
this.sound.update();
this.input.update();
this.state.update();
this.physics.update();
this.particles.update();
this.plugins.update();

this.stage.postUpdate();
this.plugins.postUpdate();
}
this.render();

if (this.renderType !== Phaser.HEADLESS)
{
this.renderer.render(this.stage);
this.plugins.render();
this.state.render();
},

/**
* The core game loop when in a paused state.
*
* @method Phaser.Game#pausedUpdate
* @protected
* @param {number} time - The current time as provided by RequestAnimationFrame.
*/
pausedUpdate: function (time) {

this.time.update(time);

this.render();

this.plugins.postRender();
},

/**
* The core game loop when in a Stepped state.
*
* @method Phaser.Game#steppedUpdate
* @protected
* @param {number} time - The current time as provided by RequestAnimationFrame.
*/
steppedUpdate: function (time) {

this.time.update(time);

if (!this.pendingStep)
{
if (this.stepping)
{
this.pendingStep = true;
}

this.coreUpdate();
}

},

/**
* The core render loop.
*
* @method Phaser.Game#coreRender
* @protected
*/
coreRender: function () {

this.renderer.render(this.stage);
this.plugins.render();
this.state.render();
this.plugins.postRender();

},

/**
* The empty headless render loop.
*
* @method Phaser.Game#headlessRender
* @protected
*/
headlessRender: function () {
},

/**
* Enable core game loop stepping. When enabled you must call game.step() directly (perhaps via a DOM button?)
* Calling step will advance the game loop by one frame. This is extremely useful to hard to track down errors!
* Calling step will advance the game loop by one frame. This is extremely useful for hard to track down errors!
*
* @method Phaser.Game#enableStep
*/
Expand All @@ -614,6 +662,8 @@ Phaser.Game.prototype = {
this.pendingStep = false;
this.stepCount = 0;

this.update = this.steppedUpdate;

},

/**
Expand All @@ -626,6 +676,8 @@ Phaser.Game.prototype = {
this.stepping = false;
this.pendingStep = false;

this.update = this.coreUpdate;

},

/**
Expand Down Expand Up @@ -670,6 +722,7 @@ Phaser.Game.prototype = {
* Called by the Stage visibility handler.
*
* @method Phaser.Game#gamePaused
* @protected
*/
gamePaused: function (time) {

Expand All @@ -680,6 +733,7 @@ Phaser.Game.prototype = {
this.time.gamePaused(time);
this.sound.setMute();
this.onPause.dispatch(this);
this.update = this.pausedUpdate;
}

},
Expand All @@ -688,6 +742,7 @@ Phaser.Game.prototype = {
* Called by the Stage visibility handler.
*
* @method Phaser.Game#gameResumed
* @protected
*/
gameResumed: function (time) {

Expand All @@ -699,6 +754,7 @@ Phaser.Game.prototype = {
this.input.reset();
this.sound.unsetMute();
this.onResume.dispatch(this);
this.update = this.coreUpdate;
}

}
Expand Down Expand Up @@ -730,6 +786,7 @@ Object.defineProperty(Phaser.Game.prototype, "paused", {
this.sound.mute = true;
this.time.gamePaused();
this.onPause.dispatch(this);
this.update = this.pausedUpdate;
}
}
else
Expand All @@ -742,6 +799,7 @@ Object.defineProperty(Phaser.Game.prototype, "paused", {
this.sound.mute = false;
this.time.gameResumed();
this.onResume.dispatch(this);
this.update = this.coreUpdate;
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/gameobjects/RetroFont.js
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ Phaser.RetroFont = function (game, key, characterWidth, characterHeight, chars,
/**
* @property {number} type - Base Phaser object type.
*/
this.type = Phaser.BITMAPFONT;
this.type = Phaser.RETROFONT;

};

Expand Down
Loading

0 comments on commit 6bddf1a

Please sign in to comment.