2013-09-11 12:21:07 +00:00
|
|
|
/**
|
2013-09-19 11:17:49 +00:00
|
|
|
* @author Richard Davey <rich@photonstorm.com>
|
|
|
|
* @copyright 2013 Photon Storm Ltd.
|
|
|
|
* @license https://github.com/photonstorm/phaser/blob/master/license.txt MIT License
|
|
|
|
* @module Phaser.World
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2013-09-11 12:21:07 +00:00
|
|
|
*
|
|
|
|
* "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.
|
|
|
|
*
|
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
|
|
|
/**
|
2013-09-19 11:17:49 +00:00
|
|
|
* A reference to the currently running Game.
|
|
|
|
* @property game
|
|
|
|
* @public
|
|
|
|
* @type {Phaser.Game}
|
|
|
|
*/
|
2013-08-29 20:57:36 +00:00
|
|
|
this.game = game;
|
|
|
|
|
2013-09-11 12:21:07 +00:00
|
|
|
/**
|
2013-09-19 11:17:49 +00:00
|
|
|
* Bound of this world that objects can not escape from.
|
|
|
|
* @property bounds
|
|
|
|
* @public
|
|
|
|
* @type {Phaser.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
|
|
|
/**
|
2013-09-19 11:17:49 +00:00
|
|
|
* Camera instance.
|
|
|
|
* @property camera
|
|
|
|
* @public
|
|
|
|
* @type {Phaser.Camera}
|
|
|
|
*/
|
2013-09-10 19:40:34 +00:00
|
|
|
this.camera = null;
|
|
|
|
|
2013-09-11 12:21:07 +00:00
|
|
|
/**
|
2013-09-19 11:17:49 +00:00
|
|
|
* Reset each frame, keeps a count of the total number of objects updated.
|
|
|
|
* @property currentRenderOrderID
|
|
|
|
* @public
|
|
|
|
* @type {Number}
|
|
|
|
*/
|
2013-09-10 19:40:34 +00:00
|
|
|
this.currentRenderOrderID = 0;
|
2013-09-11 12:21:07 +00:00
|
|
|
|
|
|
|
/**
|
2013-09-19 11:17:49 +00:00
|
|
|
* Object container stores every object created with `create*` methods.
|
|
|
|
* @property group
|
|
|
|
* @public
|
|
|
|
* @type {Phaser.Group}
|
|
|
|
*/
|
2013-09-11 12:21:07 +00:00
|
|
|
this.group = null;
|
2013-08-29 20:57:36 +00:00
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
Phaser.World.prototype = {
|
|
|
|
|
2013-09-19 11:17:49 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Initialises the game world
|
|
|
|
*
|
|
|
|
* @method boot
|
|
|
|
*/
|
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
|
|
|
/**
|
2013-09-19 11:17:49 +00:00
|
|
|
* This is called automatically every frame, and is where main logic happens.
|
|
|
|
* @method update
|
|
|
|
*/
|
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", {
|
|
|
|
|
2013-09-19 11:17:49 +00:00
|
|
|
/**
|
|
|
|
* @method width
|
|
|
|
* @return {Number} The current width of the game world
|
|
|
|
*/
|
2013-08-29 20:57:36 +00:00
|
|
|
get: function () {
|
|
|
|
return this.bounds.width;
|
|
|
|
},
|
|
|
|
|
2013-09-19 11:17:49 +00:00
|
|
|
/**
|
|
|
|
* @method width
|
|
|
|
* @return {Number} Sets the width of the game world
|
|
|
|
*/
|
2013-08-29 20:57:36 +00:00
|
|
|
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", {
|
|
|
|
|
2013-09-19 11:17:49 +00:00
|
|
|
/**
|
|
|
|
* @method height
|
|
|
|
* @return {Number} The current height of the game world
|
|
|
|
*/
|
2013-08-29 20:57:36 +00:00
|
|
|
get: function () {
|
|
|
|
return this.bounds.height;
|
|
|
|
},
|
|
|
|
|
2013-09-19 11:17:49 +00:00
|
|
|
/**
|
|
|
|
* @method height
|
|
|
|
* @return {Number} Sets the width of the game world
|
|
|
|
*/
|
2013-08-29 20:57:36 +00:00
|
|
|
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", {
|
|
|
|
|
2013-09-19 11:17:49 +00:00
|
|
|
/**
|
|
|
|
* @method centerX
|
|
|
|
* @return {Number} return the X position of the center point of the world
|
|
|
|
*/
|
2013-08-29 20:57:36 +00:00
|
|
|
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", {
|
|
|
|
|
2013-09-19 11:17:49 +00:00
|
|
|
/**
|
|
|
|
* @method centerY
|
|
|
|
* @return {Number} return the Y position of the center point of the world
|
|
|
|
*/
|
2013-08-29 20:57:36 +00:00
|
|
|
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", {
|
|
|
|
|
2013-09-19 11:17:49 +00:00
|
|
|
/**
|
|
|
|
* @method randomX
|
|
|
|
* @return {Number} a random integer which is lesser or equal to the current width of the game world
|
|
|
|
*/
|
2013-08-29 20:57:36 +00:00
|
|
|
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", {
|
|
|
|
|
2013-09-19 11:17:49 +00:00
|
|
|
/**
|
|
|
|
* @method randomY
|
|
|
|
* @return {Number} a random integer which is lesser or equal to the current height of the game world
|
|
|
|
*/
|
2013-08-29 20:57:36 +00:00
|
|
|
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
|
|
|
|
|
|
|
});
|