2013-10-01 12:54:29 +00:00
|
|
|
/**
|
|
|
|
* @author Richard Davey <rich@photonstorm.com>
|
|
|
|
* @copyright 2013 Photon Storm Ltd.
|
|
|
|
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
|
|
|
|
* @module Phaser.LinkedList
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A linked list data structure.
|
|
|
|
*
|
|
|
|
* @class Phaser.LinkedList
|
|
|
|
* @constructor
|
|
|
|
*/
|
2013-09-08 10:24:41 +00:00
|
|
|
Phaser.LinkedList = function () {
|
2013-09-10 19:40:34 +00:00
|
|
|
|
2013-10-01 12:54:29 +00:00
|
|
|
/**
|
|
|
|
* @property {object} next - Next element in the list.
|
|
|
|
* @default
|
|
|
|
*/
|
2013-09-10 19:40:34 +00:00
|
|
|
this.next = null;
|
2013-10-01 12:54:29 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @property {object} prev - Previous element in the list.
|
|
|
|
* @default
|
|
|
|
*/
|
2013-09-10 19:40:34 +00:00
|
|
|
this.prev = null;
|
2013-10-01 12:54:29 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @property {object} first - First element in the list.
|
|
|
|
* @default
|
|
|
|
*/
|
2013-09-10 19:40:34 +00:00
|
|
|
this.first = null;
|
2013-10-01 12:54:29 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @property {object} last - Last element in the list.
|
|
|
|
* @default
|
|
|
|
*/
|
2013-09-10 19:40:34 +00:00
|
|
|
this.last = null;
|
2013-10-01 12:54:29 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @property {object} game - Number of elements in the list.
|
|
|
|
* @default
|
|
|
|
*/
|
2013-09-10 19:40:34 +00:00
|
|
|
this.total = 0;
|
|
|
|
|
2013-09-08 10:24:41 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
Phaser.LinkedList.prototype = {
|
|
|
|
|
2013-10-01 12:54:29 +00:00
|
|
|
/**
|
|
|
|
* Add element to a linked list.
|
|
|
|
*
|
|
|
|
* @method add
|
2013-10-02 00:16:40 +00:00
|
|
|
* @memberof Phaser.LinkedList
|
2013-10-01 12:54:29 +00:00
|
|
|
* @param {object} child - Description.
|
|
|
|
* @return {object} Description.
|
|
|
|
*/
|
2013-09-08 10:24:41 +00:00
|
|
|
add: function (child) {
|
|
|
|
|
|
|
|
// If the list is empty
|
2013-09-08 21:38:19 +00:00
|
|
|
if (this.total == 0 && this.first == null && this.last == null)
|
2013-09-08 10:24:41 +00:00
|
|
|
{
|
|
|
|
this.first = child;
|
|
|
|
this.last = child;
|
2013-09-08 12:23:21 +00:00
|
|
|
this.next = child;
|
|
|
|
child.prev = this;
|
2013-09-08 21:38:19 +00:00
|
|
|
this.total++;
|
2013-09-08 10:24:41 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get gets appended to the end of the list, regardless of anything, and it won't have any children of its own (non-nested list)
|
2013-09-08 12:23:21 +00:00
|
|
|
this.last.next = child;
|
2013-09-08 10:24:41 +00:00
|
|
|
|
2013-09-08 12:23:21 +00:00
|
|
|
child.prev = this.last;
|
2013-09-08 10:24:41 +00:00
|
|
|
|
|
|
|
this.last = child;
|
|
|
|
|
2013-09-08 21:38:19 +00:00
|
|
|
this.total++;
|
|
|
|
|
|
|
|
return child;
|
|
|
|
|
2013-09-08 10:24:41 +00:00
|
|
|
},
|
|
|
|
|
2013-10-01 12:54:29 +00:00
|
|
|
/**
|
|
|
|
* Remove element from a linked list.
|
|
|
|
*
|
|
|
|
* @method remove
|
2013-10-02 00:16:40 +00:00
|
|
|
* @memberof Phaser.LinkedList
|
2013-10-01 12:54:29 +00:00
|
|
|
* @param {object} child - Description.
|
|
|
|
*/
|
2013-09-08 10:24:41 +00:00
|
|
|
remove: function (child) {
|
2013-10-01 14:08:09 +00:00
|
|
|
if( child == this.first ) this.first = this.first.next; // It was 'first', make 'first' point to first.next
|
|
|
|
else if ( child == this.last ) this.last = this.last.prev; // It was 'last', make 'last' point to last.prev
|
|
|
|
|
|
|
|
if( child.prev ) child.prev.next = child.next; // make child.prev.next point to childs.next instead of child
|
|
|
|
if( child.next ) child.next.prev = child.prev; // make child.next.prev point to child.prev instead of child
|
|
|
|
child.next = child.prev = null;
|
|
|
|
|
|
|
|
if( this.first == null ) this.last = null;
|
2013-09-08 10:24:41 +00:00
|
|
|
|
2013-09-08 21:38:19 +00:00
|
|
|
this.total--;
|
2013-09-09 11:35:09 +00:00
|
|
|
},
|
|
|
|
|
2013-10-01 12:54:29 +00:00
|
|
|
/**
|
|
|
|
* Description.
|
|
|
|
*
|
|
|
|
* @method callAll
|
2013-10-02 00:16:40 +00:00
|
|
|
* @memberof Phaser.LinkedList
|
2013-10-01 12:54:29 +00:00
|
|
|
* @param {object} callback - Description.
|
|
|
|
*/
|
2013-09-09 11:35:09 +00:00
|
|
|
callAll: function (callback) {
|
|
|
|
|
2013-09-13 04:44:04 +00:00
|
|
|
if (!this.first || !this.last)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-09-09 11:35:09 +00:00
|
|
|
var entity = this.first;
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
2013-09-13 04:44:04 +00:00
|
|
|
if (entity && entity[callback])
|
2013-09-09 11:35:09 +00:00
|
|
|
{
|
|
|
|
entity[callback].call(entity);
|
|
|
|
}
|
|
|
|
|
|
|
|
entity = entity.next;
|
|
|
|
|
|
|
|
}
|
|
|
|
while(entity != this.last.next)
|
|
|
|
|
2013-09-08 10:24:41 +00:00
|
|
|
},
|
|
|
|
|
2013-10-01 12:54:29 +00:00
|
|
|
/**
|
|
|
|
* Description.
|
|
|
|
*
|
|
|
|
* @method dump
|
2013-10-02 00:16:40 +00:00
|
|
|
* @memberof Phaser.LinkedList
|
2013-10-01 12:54:29 +00:00
|
|
|
*/
|
2013-09-08 10:24:41 +00:00
|
|
|
dump: function () {
|
|
|
|
|
2013-09-11 12:21:07 +00:00
|
|
|
var spacing = 20;
|
2013-09-08 10:24:41 +00:00
|
|
|
|
2013-09-11 12:21:07 +00:00
|
|
|
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);
|
2013-09-08 10:24:41 +00:00
|
|
|
|
2013-09-11 12:21:07 +00:00
|
|
|
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);
|
2013-09-08 10:24:41 +00:00
|
|
|
|
|
|
|
var entity = this;
|
|
|
|
|
2013-09-08 12:23:21 +00:00
|
|
|
var testObject = entity.last.next;
|
2013-09-08 10:24:41 +00:00
|
|
|
entity = entity.first;
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
|
|
|
var name = entity.sprite.name || '*';
|
|
|
|
var nameNext = '-';
|
|
|
|
var namePrev = '-';
|
|
|
|
var nameFirst = '-';
|
|
|
|
var nameLast = '-';
|
|
|
|
|
2013-09-08 12:23:21 +00:00
|
|
|
if (entity.next)
|
2013-09-08 10:24:41 +00:00
|
|
|
{
|
2013-09-08 12:23:21 +00:00
|
|
|
nameNext = entity.next.sprite.name;
|
2013-09-08 10:24:41 +00:00
|
|
|
}
|
|
|
|
|
2013-09-08 12:23:21 +00:00
|
|
|
if (entity.prev)
|
2013-09-08 10:24:41 +00:00
|
|
|
{
|
2013-09-08 12:23:21 +00:00
|
|
|
namePrev = entity.prev.sprite.name;
|
2013-09-08 10:24:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (entity.first)
|
|
|
|
{
|
|
|
|
nameFirst = entity.first.sprite.name;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (entity.last)
|
|
|
|
{
|
|
|
|
nameLast = entity.last.sprite.name;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (typeof nameNext === 'undefined')
|
|
|
|
{
|
|
|
|
nameNext = '-';
|
|
|
|
}
|
|
|
|
|
|
|
|
if (typeof namePrev === 'undefined')
|
|
|
|
{
|
|
|
|
namePrev = '-';
|
|
|
|
}
|
|
|
|
|
|
|
|
if (typeof nameFirst === 'undefined')
|
|
|
|
{
|
|
|
|
nameFirst = '-';
|
|
|
|
}
|
|
|
|
|
|
|
|
if (typeof nameLast === 'undefined')
|
|
|
|
{
|
|
|
|
nameLast = '-';
|
|
|
|
}
|
|
|
|
|
2013-09-11 12:21:07 +00:00
|
|
|
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);
|
2013-09-08 10:24:41 +00:00
|
|
|
|
2013-09-08 12:23:21 +00:00
|
|
|
entity = entity.next;
|
2013-09-08 10:24:41 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
while(entity != testObject)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|