2013-10-01 12:54:29 +00:00
|
|
|
/**
|
|
|
|
* @author Richard Davey <rich@photonstorm.com>
|
2016-04-04 21:15:01 +00:00
|
|
|
* @copyright 2016 Photon Storm Ltd.
|
2013-10-01 12:54:29 +00:00
|
|
|
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2014-11-13 09:10:05 +00:00
|
|
|
* A basic Linked List data structure.
|
|
|
|
*
|
|
|
|
* This implementation _modifies_ the `prev` and `next` properties of each item added:
|
|
|
|
* - The `prev` and `next` properties must be writable and should not be used for any other purpose.
|
|
|
|
* - Items _cannot_ be added to multiple LinkedLists at the same time.
|
|
|
|
* - Only objects can be added.
|
2013-10-01 12:54:29 +00:00
|
|
|
*
|
|
|
|
* @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
|
|
|
/**
|
2014-11-13 09:10:05 +00:00
|
|
|
* Next element in the list.
|
|
|
|
* @property {object} next
|
2013-11-25 03:13:04 +00:00
|
|
|
* @default
|
|
|
|
*/
|
2013-09-10 19:40:34 +00:00
|
|
|
this.next = null;
|
2013-10-01 12:54:29 +00:00
|
|
|
|
|
|
|
/**
|
2014-11-13 09:10:05 +00:00
|
|
|
* Previous element in the list.
|
|
|
|
* @property {object} prev
|
2013-11-25 03:13:04 +00:00
|
|
|
* @default
|
|
|
|
*/
|
2013-09-10 19:40:34 +00:00
|
|
|
this.prev = null;
|
2013-10-01 12:54:29 +00:00
|
|
|
|
|
|
|
/**
|
2014-11-13 09:10:05 +00:00
|
|
|
* First element in the list.
|
|
|
|
* @property {object} first
|
2013-11-25 03:13:04 +00:00
|
|
|
* @default
|
|
|
|
*/
|
2013-09-10 19:40:34 +00:00
|
|
|
this.first = null;
|
2013-10-01 12:54:29 +00:00
|
|
|
|
|
|
|
/**
|
2014-11-13 09:10:05 +00:00
|
|
|
* Last element in the list.
|
|
|
|
* @property {object} last
|
2013-11-25 03:13:04 +00:00
|
|
|
* @default
|
|
|
|
*/
|
2013-09-10 19:40:34 +00:00
|
|
|
this.last = null;
|
2014-03-23 07:59:28 +00:00
|
|
|
|
2013-10-01 12:54:29 +00:00
|
|
|
/**
|
2014-11-13 09:10:05 +00:00
|
|
|
* Number of elements in the list.
|
|
|
|
* @property {integer} total
|
2013-11-25 03:13:04 +00:00
|
|
|
* @default
|
|
|
|
*/
|
2013-09-10 19:40:34 +00:00
|
|
|
this.total = 0;
|
|
|
|
|
2013-09-08 10:24:41 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
Phaser.LinkedList.prototype = {
|
|
|
|
|
2013-11-25 03:13:04 +00:00
|
|
|
/**
|
2013-10-02 10:22:20 +00:00
|
|
|
* Adds a new element to this linked list.
|
2014-03-23 07:59:28 +00:00
|
|
|
*
|
2013-11-25 03:13:04 +00:00
|
|
|
* @method Phaser.LinkedList#add
|
2014-11-13 09:10:05 +00:00
|
|
|
* @param {object} item - 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 item that was added.
|
2013-10-01 12:54:29 +00:00
|
|
|
*/
|
2014-11-13 09:10:05 +00:00
|
|
|
add: function (item) {
|
2013-09-08 10:24:41 +00:00
|
|
|
|
2013-11-25 03:13:04 +00:00
|
|
|
// If the list is empty
|
2014-04-22 21:33:25 +00:00
|
|
|
if (this.total === 0 && this.first === null && this.last === null)
|
2013-11-25 03:13:04 +00:00
|
|
|
{
|
2014-11-13 09:10:05 +00:00
|
|
|
this.first = item;
|
|
|
|
this.last = item;
|
|
|
|
this.next = item;
|
|
|
|
item.prev = this;
|
2013-11-25 03:13:04 +00:00
|
|
|
this.total++;
|
2014-11-13 09:10:05 +00:00
|
|
|
return item;
|
2013-11-25 03:13:04 +00:00
|
|
|
}
|
2013-09-08 10:24:41 +00:00
|
|
|
|
2014-04-22 21:33:25 +00:00
|
|
|
// Gets appended to the end of the list, regardless of anything, and it won't have any children of its own (non-nested list)
|
2014-11-13 09:10:05 +00:00
|
|
|
this.last.next = item;
|
2013-09-08 10:24:41 +00:00
|
|
|
|
2014-11-13 09:10:05 +00:00
|
|
|
item.prev = this.last;
|
2013-09-08 10:24:41 +00:00
|
|
|
|
2014-11-13 09:10:05 +00:00
|
|
|
this.last = item;
|
2013-09-08 10:24:41 +00:00
|
|
|
|
2013-11-25 03:13:04 +00:00
|
|
|
this.total++;
|
2013-09-08 21:38:19 +00:00
|
|
|
|
2014-11-13 09:10:05 +00:00
|
|
|
return item;
|
2013-09-08 21:38:19 +00:00
|
|
|
|
2013-09-08 10:24:41 +00:00
|
|
|
},
|
|
|
|
|
2014-04-22 21:33:25 +00:00
|
|
|
/**
|
|
|
|
* Resets the first, last, next and previous node pointers in this list.
|
|
|
|
*
|
|
|
|
* @method Phaser.LinkedList#reset
|
|
|
|
*/
|
|
|
|
reset: function () {
|
|
|
|
|
|
|
|
this.first = null;
|
|
|
|
this.last = null;
|
|
|
|
this.next = null;
|
|
|
|
this.prev = null;
|
|
|
|
this.total = 0;
|
|
|
|
|
|
|
|
},
|
|
|
|
|
2013-11-25 03:13:04 +00:00
|
|
|
/**
|
2013-10-02 10:22:20 +00:00
|
|
|
* Removes the given element from this linked list if it exists.
|
2014-03-23 07:59:28 +00:00
|
|
|
*
|
2013-11-25 03:13:04 +00:00
|
|
|
* @method Phaser.LinkedList#remove
|
2014-11-13 09:10:05 +00:00
|
|
|
* @param {object} item - The item to be removed from the list.
|
2013-10-01 12:54:29 +00:00
|
|
|
*/
|
2014-11-13 09:10:05 +00:00
|
|
|
remove: function (item) {
|
2013-10-01 14:08:09 +00:00
|
|
|
|
2014-04-22 21:33:25 +00:00
|
|
|
if (this.total === 1)
|
|
|
|
{
|
|
|
|
this.reset();
|
2014-11-13 09:10:05 +00:00
|
|
|
item.next = item.prev = null;
|
2014-04-22 21:33:25 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-11-13 09:10:05 +00:00
|
|
|
if (item === this.first)
|
2013-11-25 03:13:04 +00:00
|
|
|
{
|
|
|
|
// It was 'first', make 'first' point to first.next
|
|
|
|
this.first = this.first.next;
|
|
|
|
}
|
2014-11-13 09:10:05 +00:00
|
|
|
else if (item === this.last)
|
2013-11-25 03:13:04 +00:00
|
|
|
{
|
|
|
|
// It was 'last', make 'last' point to last.prev
|
|
|
|
this.last = this.last.prev;
|
|
|
|
}
|
|
|
|
|
2014-11-13 09:10:05 +00:00
|
|
|
if (item.prev)
|
2013-11-25 03:13:04 +00:00
|
|
|
{
|
2014-11-13 09:10:05 +00:00
|
|
|
// make item.prev.next point to childs.next instead of item
|
|
|
|
item.prev.next = item.next;
|
2013-11-25 03:13:04 +00:00
|
|
|
}
|
|
|
|
|
2014-11-13 09:10:05 +00:00
|
|
|
if (item.next)
|
2013-11-25 03:13:04 +00:00
|
|
|
{
|
2014-11-13 09:10:05 +00:00
|
|
|
// make item.next.prev point to item.prev instead of item
|
|
|
|
item.next.prev = item.prev;
|
2013-11-25 03:13:04 +00:00
|
|
|
}
|
|
|
|
|
2014-11-13 09:10:05 +00:00
|
|
|
item.next = item.prev = null;
|
2013-11-25 03:13:04 +00:00
|
|
|
|
2014-04-22 21:33:25 +00:00
|
|
|
if (this.first === null )
|
2013-11-25 03:13:04 +00:00
|
|
|
{
|
|
|
|
this.last = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.total--;
|
2013-10-02 10:22:20 +00:00
|
|
|
|
2013-09-09 11:35:09 +00:00
|
|
|
},
|
|
|
|
|
2013-11-25 03:13:04 +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.
|
2014-03-23 07:59:28 +00:00
|
|
|
*
|
2013-11-25 03:13:04 +00:00
|
|
|
* @method Phaser.LinkedList#callAll
|
|
|
|
* @param {function} callback - The function to call.
|
2013-10-01 12:54:29 +00:00
|
|
|
*/
|
2013-09-09 11:35:09 +00:00
|
|
|
callAll: function (callback) {
|
|
|
|
|
2013-11-25 03:13:04 +00:00
|
|
|
if (!this.first || !this.last)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2013-09-13 04:44:04 +00:00
|
|
|
|
2013-11-25 03:13:04 +00:00
|
|
|
var entity = this.first;
|
2014-03-23 07:59:28 +00:00
|
|
|
|
2013-11-25 03:13:04 +00:00
|
|
|
do
|
|
|
|
{
|
|
|
|
if (entity && entity[callback])
|
|
|
|
{
|
|
|
|
entity[callback].call(entity);
|
|
|
|
}
|
2013-09-09 11:35:09 +00:00
|
|
|
|
2013-11-25 03:13:04 +00:00
|
|
|
entity = entity.next;
|
2013-09-09 11:35:09 +00:00
|
|
|
|
2013-11-25 03:13:04 +00:00
|
|
|
}
|
2016-08-25 12:03:41 +00:00
|
|
|
while (entity !== this.last.next);
|
2013-09-09 11:35:09 +00:00
|
|
|
|
2013-10-02 10:22:20 +00:00
|
|
|
}
|
2013-09-08 10:24:41 +00:00
|
|
|
|
2013-12-30 16:54:00 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
Phaser.LinkedList.prototype.constructor = Phaser.LinkedList;
|