2013-09-11 12:21:07 +00:00
|
|
|
/**
|
|
|
|
* World
|
|
|
|
*
|
|
|
|
* "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.
|
|
|
|
*
|
|
|
|
* @package Phaser.World
|
|
|
|
* @author Richard Davey <rich@photonstorm.com>
|
|
|
|
* @copyright 2013 Photon Storm Ltd.
|
|
|
|
* @license https://github.com/photonstorm/phaser/blob/master/license.txt MIT License
|
2013-09-16 14:37:30 +00:00
|
|
|
|
|
|
|
* @class World
|
|
|
|
* @constructor
|
|
|
|
* @param game {Phaser.Game} reference to the current game instance.
|
2013-09-11 12:21:07 +00:00
|
|
|
*/
|
2013-08-29 20:57:36 +00:00
|
|
|
Phaser.World = function (game) {
|
|
|
|
|
2013-09-11 12:21:07 +00:00
|
|
|
/**
|
|
|
|
* Local reference to Game.
|
|
|
|
*/
|
2013-08-29 20:57:36 +00:00
|
|
|
this.game = game;
|
|
|
|
|
2013-09-11 12:21:07 +00:00
|
|
|
/**
|
|
|
|
* Bound of this world that objects can not escape from.
|
|
|
|
* @type {Rectangle}
|
|
|
|
*/
|
2013-08-29 21:53:55 +00:00
|
|
|
this.bounds = new Phaser.Rectangle(0, 0, game.width, game.height);
|
2013-09-10 19:40:34 +00:00
|
|
|
|
2013-09-11 12:21:07 +00:00
|
|
|
/**
|
|
|
|
* Camera instance.
|
|
|
|
* @type {Camera}
|
|
|
|
*/
|
2013-09-10 19:40:34 +00:00
|
|
|
this.camera = null;
|
|
|
|
|
2013-09-11 12:21:07 +00:00
|
|
|
/**
|
|
|
|
* Reset each frame, keeps a count of the total number of objects updated.
|
|
|
|
* @type {Number}
|
|
|
|
*/
|
2013-09-10 19:40:34 +00:00
|
|
|
this.currentRenderOrderID = 0;
|
2013-09-11 12:21:07 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Object container stores every object created with `create*` methods.
|
|
|
|
* @type {Group}
|
|
|
|
*/
|
|
|
|
this.group = null;
|
2013-08-29 20:57:36 +00:00
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
Phaser.World.prototype = {
|
|
|
|
|
2013-08-31 12:54:59 +00:00
|
|
|
boot: function () {
|
|
|
|
|
|
|
|
this.camera = new Phaser.Camera(this.game, 0, 0, 0, this.game.width, this.game.height);
|
2013-09-06 19:20:58 +00:00
|
|
|
|
2013-09-11 12:21:07 +00:00
|
|
|
this.game.camera = this.camera;
|
2013-09-06 19:20:58 +00:00
|
|
|
|
2013-09-11 12:21:07 +00:00
|
|
|
this.group = new Phaser.Group(this.game, null, '__world', true);
|
2013-09-06 19:20:58 +00:00
|
|
|
|
2013-08-29 20:57:36 +00:00
|
|
|
},
|
|
|
|
|
2013-09-11 12:21:07 +00:00
|
|
|
/**
|
|
|
|
* This is called automatically every frame, and is where main logic happens.
|
|
|
|
*/
|
2013-08-30 17:56:10 +00:00
|
|
|
update: function () {
|
|
|
|
|
2013-08-31 12:54:59 +00:00
|
|
|
this.camera.update();
|
|
|
|
|
2013-09-08 12:23:21 +00:00
|
|
|
this.currentRenderOrderID = 0;
|
|
|
|
|
2013-09-09 11:35:09 +00:00
|
|
|
if (this.game.stage._stage.first._iNext)
|
2013-08-30 17:56:10 +00:00
|
|
|
{
|
2013-09-09 11:35:09 +00:00
|
|
|
var currentNode = this.game.stage._stage.first._iNext;
|
2013-09-01 02:57:24 +00:00
|
|
|
|
2013-09-06 19:20:58 +00:00
|
|
|
do
|
2013-09-03 16:28:12 +00:00
|
|
|
{
|
2013-09-10 23:35:21 +00:00
|
|
|
if (currentNode['preUpdate'])
|
|
|
|
{
|
|
|
|
currentNode.preUpdate();
|
|
|
|
}
|
|
|
|
|
2013-09-06 19:20:58 +00:00
|
|
|
if (currentNode['update'])
|
|
|
|
{
|
|
|
|
currentNode.update();
|
|
|
|
}
|
|
|
|
|
|
|
|
currentNode = currentNode._iNext;
|
2013-09-03 16:28:12 +00:00
|
|
|
}
|
2013-09-09 11:35:09 +00:00
|
|
|
while (currentNode != this.game.stage._stage.last._iNext)
|
2013-09-03 16:28:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
},
|
|
|
|
|
2013-08-29 20:57:36 +00:00
|
|
|
/**
|
|
|
|
* Updates the size of this world.
|
2013-09-16 14:37:30 +00:00
|
|
|
* @method setSize
|
2013-08-29 20:57:36 +00:00
|
|
|
* @param width {number} New width of the world.
|
|
|
|
* @param height {number} New height of the world.
|
|
|
|
*/
|
2013-08-31 12:54:59 +00:00
|
|
|
setSize: function (width, height) {
|
2013-08-29 20:57:36 +00:00
|
|
|
|
2013-09-12 01:18:23 +00:00
|
|
|
if (width >= this.game.width)
|
|
|
|
{
|
|
|
|
this.bounds.width = width;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (height >= this.game.height)
|
|
|
|
{
|
|
|
|
this.bounds.height = height;
|
|
|
|
}
|
2013-08-29 20:57:36 +00:00
|
|
|
|
2013-09-13 04:44:04 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Destroyer of worlds.
|
2013-09-16 15:40:56 +00:00
|
|
|
* @method destroy
|
2013-09-13 04:44:04 +00:00
|
|
|
*/
|
|
|
|
destroy: function () {
|
|
|
|
|
|
|
|
this.camera.x = 0;
|
|
|
|
this.camera.y = 0;
|
|
|
|
|
|
|
|
this.game.input.reset(true);
|
|
|
|
|
|
|
|
this.group.removeAll();
|
|
|
|
|
|
|
|
}
|
2013-09-11 12:21:07 +00:00
|
|
|
|
2013-08-29 20:57:36 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// Getters / Setters
|
|
|
|
|
|
|
|
Object.defineProperty(Phaser.World.prototype, "width", {
|
|
|
|
|
|
|
|
get: function () {
|
|
|
|
return this.bounds.width;
|
|
|
|
},
|
|
|
|
|
|
|
|
set: function (value) {
|
|
|
|
this.bounds.width = value;
|
2013-09-11 12:21:07 +00:00
|
|
|
}
|
2013-08-29 20:57:36 +00:00
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
Object.defineProperty(Phaser.World.prototype, "height", {
|
|
|
|
|
|
|
|
get: function () {
|
|
|
|
return this.bounds.height;
|
|
|
|
},
|
|
|
|
|
|
|
|
set: function (value) {
|
|
|
|
this.bounds.height = value;
|
2013-09-11 12:21:07 +00:00
|
|
|
}
|
2013-08-29 20:57:36 +00:00
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
Object.defineProperty(Phaser.World.prototype, "centerX", {
|
|
|
|
|
|
|
|
get: function () {
|
|
|
|
return this.bounds.halfWidth;
|
2013-09-11 12:21:07 +00:00
|
|
|
}
|
2013-08-29 20:57:36 +00:00
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
Object.defineProperty(Phaser.World.prototype, "centerY", {
|
|
|
|
|
|
|
|
get: function () {
|
|
|
|
return this.bounds.halfHeight;
|
2013-09-11 12:21:07 +00:00
|
|
|
}
|
2013-08-29 20:57:36 +00:00
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
Object.defineProperty(Phaser.World.prototype, "randomX", {
|
|
|
|
|
|
|
|
get: function () {
|
|
|
|
return Math.round(Math.random() * this.bounds.width);
|
2013-09-11 12:21:07 +00:00
|
|
|
}
|
2013-08-29 20:57:36 +00:00
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
Object.defineProperty(Phaser.World.prototype, "randomY", {
|
|
|
|
|
|
|
|
get: function () {
|
|
|
|
return Math.round(Math.random() * this.bounds.height);
|
2013-09-11 12:21:07 +00:00
|
|
|
}
|
2013-08-29 20:57:36 +00:00
|
|
|
|
|
|
|
});
|