Tidying up source code for release. Also refactored World to use a Group instance, rather tha duplicate functions.

This commit is contained in:
Richard Davey
2013-09-11 13:21:07 +01:00
parent 87858d6bbf
commit f260108433
25 changed files with 278 additions and 487 deletions
+4 -12
View File
@@ -207,10 +207,8 @@ Object.defineProperty(Phaser.Camera.prototype, "x", {
set: function (value) {
this.view.x = value;
},
}
enumerable: true,
configurable: true
});
Object.defineProperty(Phaser.Camera.prototype, "y", {
@@ -221,10 +219,8 @@ Object.defineProperty(Phaser.Camera.prototype, "y", {
set: function (value) {
this.view.y = value;
},
}
enumerable: true,
configurable: true
});
Object.defineProperty(Phaser.Camera.prototype, "width", {
@@ -235,10 +231,8 @@ Object.defineProperty(Phaser.Camera.prototype, "width", {
set: function (value) {
this.view.width = value;
},
}
enumerable: true,
configurable: true
});
Object.defineProperty(Phaser.Camera.prototype, "height", {
@@ -249,8 +243,6 @@ Object.defineProperty(Phaser.Camera.prototype, "height", {
set: function (value) {
this.view.height = value;
},
}
enumerable: true,
configurable: true
});
+1 -3
View File
@@ -447,9 +447,7 @@ Object.defineProperty(Phaser.Game.prototype, "paused", {
}
}
},
}
enumerable: true,
configurable: true
});
+34 -32
View File
@@ -1,27 +1,39 @@
Phaser.Group = function (game, parent, name) {
Phaser.Group = function (game, parent, name, useStage) {
parent = parent || null;
if (typeof useStage == 'undefined')
{
useStage = false;
}
this.game = game;
this.name = name || 'group';
this._container = new PIXI.DisplayObjectContainer();
this._container.name = this.name;
if (parent)
if (useStage)
{
if (parent instanceof Phaser.Group)
{
parent._container.addChild(this._container);
}
else
{
parent.addChild(this._container);
}
this._container = this.game.stage._stage;
}
else
{
this.game.world.add(this._container);
this._container = new PIXI.DisplayObjectContainer();
this._container.name = this.name;
if (parent)
{
if (parent instanceof Phaser.Group)
{
parent._container.addChild(this._container);
}
else
{
parent.addChild(this._container);
}
}
else
{
this.game.stage._stage.addChild(this._container);
}
}
this.exists = true;
@@ -274,13 +286,13 @@ Phaser.Group.prototype = {
sortHandler: function (obj1, obj2) {
/*
if(!obj1 || !obj2) {
if (!obj1 || !obj2) {
//console.log('null objects in sort', obj1, obj2);
return 0;
}
if(obj1[this._sortIndex] < obj2[this._sortIndex]) {
if (obj1[this._sortIndex] < obj2[this._sortIndex]) {
return this._sortOrder;
} else if(obj1[this._sortIndex] > obj2[this._sortIndex]) {
} else if (obj1[this._sortIndex] > obj2[this._sortIndex]) {
return -this._sortOrder;
}
return 0;
@@ -788,10 +800,8 @@ Object.defineProperty(Phaser.Group.prototype, "x", {
set: function (value) {
this._container.position.x = value;
},
}
enumerable: true,
configurable: true
});
Object.defineProperty(Phaser.Group.prototype, "y", {
@@ -802,10 +812,8 @@ Object.defineProperty(Phaser.Group.prototype, "y", {
set: function (value) {
this._container.position.y = value;
},
}
enumerable: true,
configurable: true
});
Object.defineProperty(Phaser.Group.prototype, "angle", {
@@ -816,10 +824,8 @@ Object.defineProperty(Phaser.Group.prototype, "angle", {
set: function(value) {
this._container.rotation = Phaser.Math.degToRad(value);
},
}
enumerable: true,
configurable: true
});
Object.defineProperty(Phaser.Group.prototype, "rotation", {
@@ -830,10 +836,8 @@ Object.defineProperty(Phaser.Group.prototype, "rotation", {
set: function (value) {
this._container.rotation = value;
},
}
enumerable: true,
configurable: true
});
Object.defineProperty(Phaser.Group.prototype, "visible", {
@@ -844,8 +848,6 @@ Object.defineProperty(Phaser.Group.prototype, "visible", {
set: function (value) {
this._container.visible = value;
},
}
enumerable: true,
configurable: true
});
+7 -48
View File
@@ -91,55 +91,13 @@ Phaser.LinkedList.prototype = {
dump: function () {
console.log("\nNode\t\t|\t\tNext\t\t|\t\tPrev\t\t|\t\tFirst\t\t|\t\tLast");
console.log("\t\t\t|\t\t\t\t\t|\t\t\t\t\t|\t\t\t\t\t|");
var spacing = 20;
var nameNext = '-';
var namePrev = '-';
var nameFirst = '-';
var nameLast = '-';
var output = "\n" + Phaser.Utils.pad('Node', spacing) + "|" + Phaser.Utils.pad('Next', spacing) + "|" + Phaser.Utils.pad('Previous', spacing) + "|" + Phaser.Utils.pad('First', spacing) + "|" + Phaser.Utils.pad('Last', spacing);
console.log(output);
if (this.next)
{
nameNext = this.next.sprite.name;
}
if (this.prev)
{
namePrev = this.prev.sprite.name;
}
if (this.first)
{
nameFirst = this.first.sprite.name;
}
if (this.last)
{
nameLast = this.last.sprite.name;
}
if (typeof nameNext === 'undefined')
{
nameNext = '-';
}
if (typeof namePrev === 'undefined')
{
namePrev = '-';
}
if (typeof nameFirst === 'undefined')
{
nameFirst = '-';
}
if (typeof nameLast === 'undefined')
{
nameLast = '-';
}
console.log('HD' + '\t\t\t|\t\t' + nameNext + '\t\t\t|\t\t' + namePrev + '\t\t\t|\t\t' + nameFirst + '\t\t\t|\t\t' + nameLast);
var output = Phaser.Utils.pad('----------', spacing) + "|" + Phaser.Utils.pad('----------', spacing) + "|" + Phaser.Utils.pad('----------', spacing) + "|" + Phaser.Utils.pad('----------', spacing) + "|" + Phaser.Utils.pad('----------', spacing);
console.log(output);
var entity = this;
@@ -194,7 +152,8 @@ Phaser.LinkedList.prototype = {
nameLast = '-';
}
console.log(name + '\t\t\t|\t\t' + nameNext + '\t\t\t|\t\t' + namePrev + '\t\t\t|\t\t' + nameFirst + '\t\t\t|\t\t' + nameLast);
var output = Phaser.Utils.pad(name, spacing) + "|" + Phaser.Utils.pad(nameNext, spacing) + "|" + Phaser.Utils.pad(namePrev, spacing) + "|" + Phaser.Utils.pad(nameFirst, spacing) + "|" + Phaser.Utils.pad(nameLast, spacing);
console.log(output);
entity = entity.next;
+1 -1
View File
@@ -76,7 +76,7 @@ Phaser.Signal.prototype = {
this._addBinding(binding);
}
if(this.memorize && this._prevParams){
if (this.memorize && this._prevParams){
binding.execute(this._prevParams);
}
+1 -3
View File
@@ -101,8 +101,6 @@ Object.defineProperty(Phaser.Stage.prototype, "backgroundColor", {
this._stage.setBackgroundColor(color);
},
}
enumerable: true,
configurable: true
});
+47 -175
View File
@@ -1,52 +1,65 @@
/**
* 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
*/
Phaser.World = function (game) {
/**
* Local reference to Game.
*/
this.game = game;
/**
* Bound of this world that objects can not escape from.
* @type {Rectangle}
*/
this.bounds = new Phaser.Rectangle(0, 0, game.width, game.height);
/**
* Camera instance.
* @type {Camera}
*/
this.camera = null;
/**
* Reset each frame, keeps a count of the total number of objects updated.
* @type {Number}
*/
this.currentRenderOrderID = 0;
/**
* Object container stores every object created with `create*` methods.
* @type {Group}
*/
this.group = null;
};
Phaser.World.prototype = {
boot: function () {
this.camera = new Phaser.Camera(this.game, 0, 0, 0, this.game.width, this.game.height);
this.game.camera = this.camera;
},
add: function (gameobject) {
this.game.stage._stage.addChild(gameobject);
return gameobject;
},
addAt: function (gameobject, index) {
this.game.stage._stage.addChildAt(gameobject, index);
return gameobject;
},
getAt: function (index) {
return this.game.stage._stage.getChildAt(index);
},
remove: function (gameobject) {
this.game.stage._stage.removeChild(gameobject);
return gameobject;
this.group = new Phaser.Group(this.game, null, '__world', true);
},
/**
* This is called automatically every frame, and is where main logic happens.
*/
update: function () {
this.camera.update();
@@ -87,137 +100,8 @@ Phaser.World.prototype = {
this.bounds.width = width;
this.bounds.height = height;
},
bringToTop: function (child) {
this.remove(child);
this.add(child);
return child;
},
swapChildren: function (child1, child2) {
if (child1 === child2 || !child1.parent || !child2.parent)
{
console.warn('You cannot swap a child with itself or swap un-parented children');
return false;
}
// Cache the values
var child1Prev = child1._iPrev;
var child1Next = child1._iNext;
var child2Prev = child2._iPrev;
var child2Next = child2._iNext;
var endNode = this.game.stage._stage.last._iNext;
var currentNode = this.game.stage._stage.first;
do
{
if (currentNode !== child1 && currentNode !== child2)
{
if (currentNode.first === child1)
{
currentNode.first = child2;
}
else if (currentNode.first === child2)
{
currentNode.first = child1;
}
if (currentNode.last === child1)
{
currentNode.last = child2;
}
else if (currentNode.last === child2)
{
currentNode.last = child1;
}
}
currentNode = currentNode._iNext;
}
while (currentNode != endNode)
if (child1._iNext == child2)
{
// This is a downward (A to B) neighbour swap
child1._iNext = child2Next;
child1._iPrev = child2;
child2._iNext = child1;
child2._iPrev = child1Prev;
if (child1Prev) { child1Prev._iNext = child2; }
if (child2Next) { child2Next._iPrev = child1; }
if (child1.__renderGroup)
{
child1.__renderGroup.updateTexture(child1);
}
if (child2.__renderGroup)
{
child2.__renderGroup.updateTexture(child2);
}
return true;
}
else if (child2._iNext == child1)
{
// This is an upward (B to A) neighbour swap
child1._iNext = child2;
child1._iPrev = child2Prev;
child2._iNext = child1Next;
child2._iPrev = child1;
if (child2Prev) { child2Prev._iNext = child1; }
if (child1Next) { child2Next._iPrev = child2; }
if (child1.__renderGroup)
{
child1.__renderGroup.updateTexture(child1);
}
if (child2.__renderGroup)
{
child2.__renderGroup.updateTexture(child2);
}
return true;
}
else
{
// Children are far apart
child1._iNext = child2Next;
child1._iPrev = child2Prev;
child2._iNext = child1Next;
child2._iPrev = child1Prev;
if (child1Prev) { child1Prev._iNext = child2; }
if (child1Next) { child1Next._iPrev = child2; }
if (child2Prev) { child2Prev._iNext = child1; }
if (child2Next) { child2Next._iPrev = child1; }
if (child1.__renderGroup)
{
child1.__renderGroup.updateTexture(child1);
}
if (child2.__renderGroup)
{
child2.__renderGroup.updateTexture(child2);
}
return true;
}
return false;
}
};
// Getters / Setters
@@ -230,10 +114,8 @@ Object.defineProperty(Phaser.World.prototype, "width", {
set: function (value) {
this.bounds.width = value;
},
}
enumerable: true,
configurable: true
});
Object.defineProperty(Phaser.World.prototype, "height", {
@@ -244,48 +126,38 @@ Object.defineProperty(Phaser.World.prototype, "height", {
set: function (value) {
this.bounds.height = value;
},
}
enumerable: true,
configurable: true
});
Object.defineProperty(Phaser.World.prototype, "centerX", {
get: function () {
return this.bounds.halfWidth;
},
}
enumerable: true,
configurable: true
});
Object.defineProperty(Phaser.World.prototype, "centerY", {
get: function () {
return this.bounds.halfHeight;
},
}
enumerable: true,
configurable: true
});
Object.defineProperty(Phaser.World.prototype, "randomX", {
get: function () {
return Math.round(Math.random() * this.bounds.width);
},
}
enumerable: true,
configurable: true
});
Object.defineProperty(Phaser.World.prototype, "randomY", {
get: function () {
return Math.round(Math.random() * this.bounds.height);
},
}
enumerable: true,
configurable: true
});