phaser/src/core/LinkedList.js

157 lines
3.5 KiB
JavaScript
Raw Normal View History

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}
*/
/**
2013-10-02 10:22:20 +00:00
* A basic linked list data structure.
2013-10-01 12:54:29 +00:00
*
* @class Phaser.LinkedList
* @constructor
*/
Phaser.LinkedList = function () {
2013-10-01 12:54:29 +00:00
/**
* @property {object} next - Next element in the list.
* @default
*/
this.next = null;
2013-10-01 12:54:29 +00:00
/**
* @property {object} prev - Previous element in the list.
* @default
*/
this.prev = null;
2013-10-01 12:54:29 +00:00
/**
* @property {object} first - First element in the list.
* @default
*/
this.first = null;
2013-10-01 12:54:29 +00:00
/**
* @property {object} last - Last element in the list.
* @default
*/
this.last = null;
2013-10-01 12:54:29 +00:00
/**
* @property {object} game - Number of elements in the list.
* @default
*/
this.total = 0;
};
Phaser.LinkedList.prototype = {
/**
2013-10-02 10:22:20 +00:00
* Adds a new element to this linked list.
*
* @method Phaser.LinkedList#add
* @param {object} child - The element to add to this list. Can be a Phaser.Sprite or any other object you need to quickly iterate through.
* @return {object} The child that was added.
2013-10-01 12:54:29 +00:00
*/
add: function (child) {
// If the list is empty
if (this.total === 0 && this.first == null && this.last == null)
{
this.first = child;
this.last = child;
this.next = child;
child.prev = this;
this.total++;
return child;
}
// 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)
this.last.next = child;
child.prev = this.last;
this.last = child;
this.total++;
2013-09-08 21:38:19 +00:00
return child;
2013-09-08 21:38:19 +00:00
},
/**
2013-10-02 10:22:20 +00:00
* Removes the given element from this linked list if it exists.
*
* @method Phaser.LinkedList#remove
* @param {object} child - The child to be removed from the list.
2013-10-01 12:54:29 +00:00
*/
remove: function (child) {
if (child == this.first)
{
// It was 'first', make 'first' point to first.next
this.first = this.first.next;
}
else if (child == this.last)
{
// It was 'last', make 'last' point to last.prev
this.last = this.last.prev;
}
if (child.prev)
{
// make child.prev.next point to childs.next instead of child
child.prev.next = child.next;
}
if (child.next)
{
// make child.next.prev point to child.prev instead of child
child.next.prev = child.prev;
}
child.next = child.prev = null;
if (this.first == null )
{
this.last = null;
}
this.total--;
2013-10-02 10:22:20 +00:00
},
/**
2013-10-02 10:22:20 +00:00
* Calls a function on all members of this list, using the member as the context for the callback.
* The function must exist on the member.
*
* @method Phaser.LinkedList#callAll
* @param {function} callback - The function to call.
2013-10-01 12:54:29 +00:00
*/
callAll: function (callback) {
if (!this.first || !this.last)
{
return;
}
2013-09-13 04:44:04 +00:00
var entity = this.first;
do
{
if (entity && entity[callback])
{
entity[callback].call(entity);
}
entity = entity.next;
}
while(entity != this.last.next)
2013-10-02 10:22:20 +00:00
}
};
Phaser.LinkedList.prototype.constructor = Phaser.LinkedList;