Lots of work on Group and also resolved the issue of the core game loop structure not being quite right.

This commit is contained in:
Richard Davey
2013-09-06 15:00:05 +01:00
parent 0c97e8e151
commit 822a2df289
9 changed files with 1003 additions and 344 deletions
+17 -13
View File
@@ -375,23 +375,27 @@ Phaser.Game.prototype = {
this.time.update(time);
this.plugins.preUpdate();
this.physics.preUpdate();
if (!this.paused)
{
this.plugins.preUpdate();
// this.world.preUpdate();
this.physics.preUpdate();
this.input.update();
this.tweens.update();
this.sound.update();
this.world.update();
this.state.update();
this.plugins.update();
this.input.update();
this.tweens.update();
this.sound.update();
this.world.update();
this.state.update();
this.plugins.update();
this.renderer.render(this.world._stage);
this.state.render();
this.renderer.render(this.world._stage);
this.state.render();
this.world.postUpdate();
this.physics.postUpdate();
// this.world.postUpdate();
// this.physics.postUpdate();
this.plugins.postRender();
this.plugins.postRender();
}
},
+313 -34
View File
@@ -53,6 +53,7 @@ Phaser.Group.prototype = {
createSprite: function (x, y, key, frame) {
var child = new Phaser.Sprite(this.game, x, y, key, frame);
child.group = this;
this._container.addChild(child);
return child;
@@ -60,13 +61,102 @@ Phaser.Group.prototype = {
swap: function (child1, child2) {
return this.game.world.swapChildren(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._container.last._iNext;
var currentNode = this._container.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; }
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; }
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; }
return true;
}
return false;
},
bringToTop: function (child) {
// ?!
if (!child === this._container.last)
{
this.swap(child, this._container.last);
}
return child;
},
@@ -118,7 +208,104 @@ Phaser.Group.prototype = {
*/
},
setAll: function () {
// key is an ARRAY of values.
setProperty: function (child, key, value, operation) {
operation = operation || 0;
// As ugly as this approach looks, and although it's limited to a depth of only 4, it's extremely fast.
// Much faster than a for loop or object iteration. There are no checks, so if the key isn't valid then it'll fail
// but as you are likely to call this from inner loops that have to perform well, I'll take that trade off.
// 0 = Equals
// 1 = Add
// 2 = Subtract
// 3 = Multiply
// 4 = Divide
if (key.length == 1)
{
if (operation == 0) { child[key[0]] = value; }
else if (operation == 1) { child[key[0]] += value; }
else if (operation == 2) { child[key[0]] -= value; }
else if (operation == 3) { child[key[0]] *= value; }
else if (operation == 4) { child[key[0]] /= value; }
}
else if (key.length == 2)
{
if (operation == 0) { child[key[0]][key[1]] = value; }
else if (operation == 1) { child[key[0]][key[1]] += value; }
else if (operation == 2) { child[key[0]][key[1]] -= value; }
else if (operation == 3) { child[key[0]][key[1]] *= value; }
else if (operation == 4) { child[key[0]][key[1]] /= value; }
}
else if (key.length == 3)
{
if (operation == 0) { child[key[0]][key[1]][key[2]] = value; }
else if (operation == 1) { child[key[0]][key[1]][key[2]] += value; }
else if (operation == 2) { child[key[0]][key[1]][key[2]] -= value; }
else if (operation == 3) { child[key[0]][key[1]][key[2]] *= value; }
else if (operation == 4) { child[key[0]][key[1]][key[2]] /= value; }
}
else if (key.length == 4)
{
if (operation == 0) { child[key[0]][key[1]][key[2]][key[3]] = value; }
else if (operation == 1) { child[key[0]][key[1]][key[2]][key[3]] += value; }
else if (operation == 2) { child[key[0]][key[1]][key[2]][key[3]] -= value; }
else if (operation == 3) { child[key[0]][key[1]][key[2]][key[3]] *= value; }
else if (operation == 4) { child[key[0]][key[1]][key[2]][key[3]] /= value; }
}
else
{
// TODO - Deep property scane
}
},
setAll: function (key, value, checkAlive, checkVisible, operation) {
key = key.split('.');
checkAlive = checkAlive || false;
checkVisible = checkVisible || false;
operation = operation || 0;
var currentNode = this._container.first._iNext;
do
{
if ((checkAlive == false || (checkAlive && currentNode.alive)) && (checkVisible == false || (checkVisible && currentNode.visible)))
{
this.setProperty(currentNode, key, value, operation);
}
currentNode = currentNode._iNext;
}
while (currentNode != this._container.last._iNext)
},
addAll: function (key, value, checkAlive, checkVisible) {
this.setAll(key, value, checkAlive, checkVisible, 1);
},
subAll: function (key, value, checkAlive, checkVisible) {
this.setAll(key, value, checkAlive, checkVisible, 2);
},
multiplyAll: function (key, value, checkAlive, checkVisible) {
this.setAll(key, value, checkAlive, checkVisible, 3);
},
divideAll: function (key, value, checkAlive, checkVisible) {
this.setAll(key, value, checkAlive, checkVisible, 4);
},
callAll: function () {
@@ -133,10 +320,15 @@ Phaser.Group.prototype = {
forEachDead: function () {
},
/**
* Call this function to retrieve the first object with alive == true in the group.
* This is handy for checking if everything's wiped out, or choosing a squad leader, etc.
*
* @return {Any} The first alive child, or null if none found.
*/
getFirstAlive: function () {
var endNode = this._container.last._iNext;
var currentNode = this._container.first;
var currentNode = this._container.first._iNext;
do
{
@@ -147,51 +339,46 @@ Phaser.Group.prototype = {
currentNode = currentNode._iNext;
}
while (currentNode != endNode)
while (currentNode != this._container.last._iNext)
return null;
},
/**
* Call this function to retrieve the first object with alive == false in the group.
* This is handy for checking if everything's wiped out, or choosing a squad leader, etc.
*
* @return {Any} The first dead child, or null if none found.
*/
getFirstDead: function () {
var endNode = this._container.last._iNext;
var currentNode = this._container.first;
var currentNode = this._container.first._iNext;
do
{
if (currentNode.alive == false)
if (!currentNode.alive)
{
return currentNode;
}
currentNode = currentNode._iNext;
}
while (currentNode != endNode)
while (currentNode != this._container.last._iNext)
return null;
},
count: function (alive, visible) {
var endNode = this._container.last._iNext;
var currentNode = this._container.first;
do
{
if (currentNode.alive)
{
return currentNode;
}
currentNode = currentNode._iNext;
}
while (currentNode != endNode)
}
/**
* Call this function to find out how many members of the group are alive.
*
* @return {number} The number of children flagged as alive. Returns -1 if Group is empty.
*/
countLiving: function () {
var total = 0;
var endNode = this._container.last._iNext;
var currentNode = this._container.first;
var total = -1;
var currentNode = this._container.first._iNext;
do
{
@@ -202,16 +389,52 @@ Phaser.Group.prototype = {
currentNode = currentNode._iNext;
}
while (currentNode != endNode);
while (currentNode != this._container.last._iNext);
return total;
},
/**
* Call this function to find out how many members of the group are dead.
*
* @return {number} The number of children flagged as dead. Returns -1 if Group is empty.
*/
countDead: function () {
var total = -1;
var currentNode = this._container.first._iNext;
do
{
if (!currentNode.alive)
{
total++;
}
currentNode = currentNode._iNext;
}
while (currentNode != this._container.last._iNext);
return total;
},
getRandom: function () {
/**
* Returns a member at random from the group.
*
* @param {number} startIndex Optional offset off the front of the array. Default value is 0, or the beginning of the array.
* @param {number} length Optional restriction on the number of values you want to randomly select from.
*
* @return {Any} A random child of this Group.
*/
getRandom: function (startIndex, length) {
startIndex = startIndex || 0;
length = length || this._container.children.length;
return this.game.math.getRandom(this._container.children, startIndex, length);
},
remove: function (child) {
@@ -222,7 +445,63 @@ Phaser.Group.prototype = {
};
Object.defineProperty(Phaser.World.prototype, "visible", {
Object.defineProperty(Phaser.Group.prototype, "x", {
get: function () {
return this._container.position.x;
},
set: function (value) {
this._container.position.x = value;
},
enumerable: true,
configurable: true
});
Object.defineProperty(Phaser.Group.prototype, "y", {
get: function () {
return this._container.position.y;
},
set: function (value) {
this._container.position.y = value;
},
enumerable: true,
configurable: true
});
Object.defineProperty(Phaser.Group.prototype, "angle", {
get: function() {
return Phaser.Math.radToDeg(this._container.rotation);
},
set: function(value) {
this._container.rotation = Phaser.Math.degToRad(value);
}
enumerable: true,
configurable: true
});
Object.defineProperty(Phaser.Group.prototype, "rotation", {
get: function () {
return this._container.rotation;
},
set: function (value) {
this._container.rotation = value;
},
enumerable: true,
configurable: true
});
Object.defineProperty(Phaser.Group.prototype, "visible", {
get: function () {
return this._container.visible;
+3 -1
View File
@@ -3,9 +3,10 @@ Phaser.World = function (game) {
this.game = game;
this._stage = new PIXI.Stage(0x000000);
this._stage.name = '_stage_root';
this._container = new PIXI.DisplayObjectContainer();
this._container.name = '_stage_root';
// this._container.name = '_stage_root';
this._stage.addChild(this._container);
@@ -52,6 +53,7 @@ Phaser.World.prototype = {
this.camera.update();
// TODO - sort this out, the nodes are wrong
var displayObject = this._stage;
var testObject = displayObject.last._iNext;
+1 -1
View File
@@ -140,7 +140,7 @@ Phaser.Sprite.prototype = Object.create(PIXI.Sprite.prototype);
Phaser.Sprite.prototype.constructor = Phaser.Sprite;
/**
* Automatically called by the game loop.
* Automatically called by World.update
*/
Phaser.Sprite.prototype.update = function() {
+4 -1
View File
@@ -22,7 +22,7 @@ Phaser.Physics.Arcade = function (game) {
this.OVERLAP_BIAS = 4;
this.TILE_OVERLAP = false;
this.quadTree = null;
this.quadTree = new Phaser.QuadTree(this, this.game.world.bounds.x, this.game.world.bounds.y, this.game.world.bounds.width, this.game.world.bounds.height, this.maxObjects, this.maxLevels);
this.quadTreeID = 0;
// avoid gc spikes by caching these values for re-use
@@ -124,6 +124,9 @@ Phaser.Physics.Arcade.prototype = {
preUpdate: function () {
// Clear the tree
this.quadTree.clear();
// Create our tree which all of the Physics bodies will add themselves to
this.quadTreeID = 0;
this.quadTree = new Phaser.QuadTree(this, this.game.world.bounds.x, this.game.world.bounds.y, this.game.world.bounds.width, this.game.world.bounds.height, this.maxObjects, this.maxLevels);
+34 -3
View File
@@ -49,7 +49,7 @@ Phaser.Physics.Arcade.Body = function (sprite) {
this.immovable = false;
this.moves = true;
this.rotation = 0;
this.allowRotation = false;
this.allowRotation = true;
this.allowGravity = true;
this.collideWorldBounds = false;
@@ -95,6 +95,10 @@ Phaser.Physics.Arcade.Body.prototype = {
this.lastX = this.x;
this.lastY = this.y;
this.x = this.sprite.x;
this.y = this.sprite.y;
this.rotation = this.sprite.angle;
if (this.moves)
{
this.game.physics.updateMotion(this);
@@ -103,6 +107,11 @@ Phaser.Physics.Arcade.Body.prototype = {
this.bounds.x = this.x;
this.bounds.y = this.y;
if (this.collideWorldBounds)
{
this.checkWorldBounds();
}
if (this.allowCollision.none == false && this.sprite.visible && this.sprite.alive)
{
this.quadTreeIDs = [];
@@ -110,6 +119,15 @@ Phaser.Physics.Arcade.Body.prototype = {
this.game.physics.quadTree.insert(this);
}
// Adjust the sprite based on all of the above, so the x/y coords will be correct going into the State update
this.sprite.x = this.x - this.offset.x + (this.sprite.anchor.x * this.width);
this.sprite.y = this.y - this.offset.y + (this.sprite.anchor.y * this.height);
if (this.allowRotation)
{
this.sprite.angle = this.rotation;
}
},
checkWorldBounds: function () {
@@ -144,18 +162,31 @@ Phaser.Physics.Arcade.Body.prototype = {
postUpdate: function () {
/*
// The State update may (almost certainly?) will had
if (this.collideWorldBounds)
{
// Adjust sprite directly?
this.checkWorldBounds();
}
this.sprite.x = this.x - this.offset.x + (this.sprite.anchor.x * this.width);
this.sprite.y = this.y - this.offset.y + (this.sprite.anchor.y * this.height);
console.log('pre pu', this.x, this.y);
// if (this.moves)
// {
this.sprite.x = this.x - this.offset.x + (this.sprite.anchor.x * this.width);
this.sprite.y = this.y - this.offset.y + (this.sprite.anchor.y * this.height);
// }
console.log('post pu', this.sprite.x, this.sprite.y);
if (this.allowRotation)
{
this.sprite.angle = this.rotation;
}
*/
},