Making some major changes to Camera and World.

This commit is contained in:
Richard Davey
2013-10-04 14:41:15 +01:00
parent 54f073e5cb
commit e8bed83ac3
12 changed files with 235 additions and 536 deletions
+91 -56
View File
@@ -17,7 +17,6 @@
* @param {number} width - The width of the view rectangle
* @param {number} height - The height of the view rectangle
*/
Phaser.Camera = function (game, id, x, y, width, height) {
/**
@@ -50,6 +49,14 @@ Phaser.Camera = function (game, id, x, y, width, height) {
*/
this.screenView = new Phaser.Rectangle(x, y, width, height);
/**
* The Camera is bound to this Rectangle and cannot move outside of it. By default it is enabled and set to the size of the World.
* The Rectangle can be located anywhere in the world and updated as often as you like. If you don't wish the Camera to be bound
* at all then set this to null. The values can be anything and are in World coordinates, with 0,0 being the center of the world.
* @property {Phaser.Rectangle} bounds - The Rectangle in which the Camera is bounded. Set to null to allow for movement anywhere.
*/
this.bounds = new Phaser.Rectangle(x, y, width, height);
/**
* @property {Phaser.Rectangle} deadzone - Moving inside this Rectangle will not cause camera moving.
*/
@@ -168,49 +175,62 @@ Phaser.Camera.prototype = {
*/
update: function () {
return;
// Add dirty flag
if (this.target !== null)
if (this.target)
{
if (this.deadzone)
{
this._edge = this.target.x - this.deadzone.x;
if (this.view.x > this._edge)
{
this.view.x = this._edge;
}
this._edge = this.target.x + this.target.width - this.deadzone.x - this.deadzone.width;
if (this.view.x < this._edge)
{
this.view.x = this._edge;
}
this._edge = this.target.y - this.deadzone.y;
if (this.view.y > this._edge)
{
this.view.y = this._edge;
}
this._edge = this.target.y + this.target.height - this.deadzone.y - this.deadzone.height;
if (this.view.y < this._edge)
{
this.view.y = this._edge;
}
}
else
{
this.focusOnXY(this.target.x, this.target.y);
}
this.updateTarget();
}
this.checkWorldBounds();
this.updateTarget();
if (this.bounds)
{
this.checkBounds();
}
},
updateTarget: function () {
if (this.deadzone)
{
this._edge = this.target.x - this.deadzone.x;
if (this.view.x > this._edge)
{
this.view.x = this._edge;
}
this._edge = this.target.x + this.target.width - this.deadzone.x - this.deadzone.width;
if (this.view.x < this._edge)
{
this.view.x = this._edge;
}
this._edge = this.target.y - this.deadzone.y;
if (this.view.y > this._edge)
{
this.view.y = this._edge;
}
this._edge = this.target.y + this.target.height - this.deadzone.y - this.deadzone.height;
if (this.view.y < this._edge)
{
this.view.y = this._edge;
}
}
else
{
this.focusOnXY(this.target.x, this.target.y);
}
},
setBoundsToWorld: function () {
this.bounds.setTo(this.game.world.x, this.game.world.y, this.game.world.width, this.game.world.height);
},
@@ -218,36 +238,34 @@ Phaser.Camera.prototype = {
* Method called to ensure the camera doesn't venture outside of the game world.
* @method Phaser.Camera#checkWorldBounds
*/
checkWorldBounds: function () {
return;
checkBounds: function () {
this.atLimit.x = false;
this.atLimit.y = false;
// Make sure we didn't go outside the cameras worldBounds
if (this.view.x < this.world.bounds.left)
// Make sure we didn't go outside the cameras bounds
if (this.view.x < this.bounds.left)
{
this.atLimit.x = true;
this.view.x = this.world.bounds.left;
this.view.x = this.bounds.left;
}
if (this.view.x > this.world.bounds.right - this.width)
if (this.view.x > this.bounds.right - this.width)
{
this.atLimit.x = true;
this.view.x = (this.world.bounds.right - this.width) + 1;
this.view.x = (this.bounds.right - this.width) + 1;
}
if (this.view.y < this.world.bounds.top)
if (this.view.y < this.bounds.top)
{
this.atLimit.y = true;
this.view.y = this.world.bounds.top;
this.view.y = this.bounds.top;
}
if (this.view.y > this.world.bounds.bottom - this.height)
if (this.view.y > this.bounds.bottom - this.height)
{
this.atLimit.y = true;
this.view.y = (this.world.bounds.bottom - this.height) + 1;
this.view.y = (this.bounds.bottom - this.height) + 1;
}
this.view.floor();
@@ -266,7 +284,14 @@ Phaser.Camera.prototype = {
this.view.x = x;
this.view.y = y;
this.checkWorldBounds();
this.displayObject.x = -x;
this.displayObject.y = -y;
if (this.bounds)
{
this.checkBounds();
}
},
@@ -298,9 +323,14 @@ Object.defineProperty(Phaser.Camera.prototype, "x", {
},
set: function (value) {
this.view.x = value;
this.displayObject.x = -value;
this.checkWorldBounds();
if (this.bounds)
{
this.checkBounds();
}
}
});
@@ -317,9 +347,14 @@ Object.defineProperty(Phaser.Camera.prototype, "y", {
},
set: function (value) {
this.view.y = value;
this.displayObject.y = -value;
this.checkWorldBounds();
if (this.bounds)
{
this.checkBounds();
}
}
});
+1 -1
View File
@@ -12,7 +12,7 @@
* @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 {string} [name=group] - A name for this Group. Not used internally but useful for debugging.
* @param {boolean} [useStage=false] - Only the root World Group should use this value.
* @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) {
+19 -21
View File
@@ -5,16 +5,16 @@
*/
/**
* "This world is but a canvas to our imagination." - Henry David Thoreau
* <p>
* A game has only one world. The world is an abstract place in which all game objects live. It is not bound
* by stage limits and can be any size. You look into the world via cameras. All game objects live within
* the world at world-based coordinates. By default a world is created the same size as your Stage.
*
* @class Phaser.World
* @constructor
* @param {Phaser.Game} game - Reference to the current game instance.
*/
* "This world is but a canvas to our imagination." - Henry David Thoreau
*
* A game has only one world. The world is an abstract place in which all game objects live. It is not bound
* by stage limits and can be any size. You look into the world via cameras. All game objects live within
* the world at world-based coordinates. By default a world is created the same size as your Stage.
*
* @class Phaser.World
* @constructor
* @param {Phaser.Game} game - Reference to the current game instance.
*/
Phaser.World = function (game) {
/**
@@ -23,6 +23,10 @@ Phaser.World = function (game) {
this.game = game;
/**
* The World has no fixed size, but it does have a bounds outside of which objects are no longer considered as being "in world" and you should use this to clean-up the display list and purge dead objects.
* By default we set the Bounds to be from 0,0 to Game.width,Game.height. I.e. it will match the size given to the game constructor with 0,0 representing the top-left of the display.
* However 0,0 is actually the center of the world, and if you rotate or scale the world all of that will happen from 0,0.
* So if you want to make a game in which the world itself will rotate you should adjust the bounds so that 0,0 is the center point, i.e. set them to -1000,-1000,2000,2000 for a 2000x2000 sized world centered around 0,0.
* @property {Phaser.Rectangle} bounds - Bound of this world that objects can not escape from.
*/
this.bounds = new Phaser.Rectangle(0, 0, game.width, game.height);
@@ -54,7 +58,7 @@ Phaser.World.prototype = {
*/
boot: function () {
this.group = new Phaser.Group(this.game, null, '__world', false);
this.group = new Phaser.Group(this.game, null, '__world');
this.camera = new Phaser.Camera(this.game, 0, 0, 0, this.game.width, this.game.height);
this.camera.displayObject = this.group;
@@ -122,22 +126,16 @@ Phaser.World.prototype = {
},
/**
* Updates the size of this world.
* Updates the size of this world. Note that this doesn't modify the world x/y coordinates, just the width and height.
* If you need to adjust the bounds of the world
* @method Phaser.World#setSize
* @param {number} width - New width of the world.
* @param {number} height - New height of the world.
*/
setSize: function (width, height) {
if (width >= this.game.width)
{
this.bounds.width = width;
}
if (height >= this.game.height)
{
this.bounds.height = height;
}
this.bounds.width = width;
this.bounds.height = height;
},