phaser/src/core/LinkedList.js

152 lines
3 KiB
JavaScript
Raw Normal View History

Phaser.LinkedList = function () {
this.next = null;
this.prev = null;
this.first = null;
this.last = null;
this.total = 0;
};
Phaser.LinkedList.prototype = {
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)
{
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++;
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 12:23:21 +00:00
child.prev = this.last;
this.last = child;
2013-09-08 21:38:19 +00:00
this.total++;
return child;
},
remove: function (child) {
if( this.first ) {
child.next = this.last.next;
child.prev = this.last;
if( this.last.next ) {
this.last.next.prev = child;
}
this.last.next = child;
this.last = this.last.next;
} else {
this.first = this.last = child;
}
2013-09-08 21:38:19 +00:00
this.total--;
},
callAll: function (callback) {
2013-09-13 04:44:04 +00:00
if (!this.first || !this.last)
{
return;
}
var entity = this.first;
do
{
2013-09-13 04:44:04 +00:00
if (entity && entity[callback])
{
entity[callback].call(entity);
}
entity = entity.next;
}
while(entity != this.last.next)
},
dump: function () {
var spacing = 20;
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);
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;
2013-09-08 12:23:21 +00:00
var testObject = entity.last.next;
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 12:23:21 +00:00
nameNext = entity.next.sprite.name;
}
2013-09-08 12:23:21 +00:00
if (entity.prev)
{
2013-09-08 12:23:21 +00:00
namePrev = entity.prev.sprite.name;
}
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 = '-';
}
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 12:23:21 +00:00
entity = entity.next;
}
while(entity != testObject)
}
};