2014-04-25 14:11:54 +00:00
|
|
|
/**
|
|
|
|
* @author Richard Davey <rich@photonstorm.com>
|
|
|
|
* @copyright 2014 Photon Storm Ltd.
|
|
|
|
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2014-04-25 15:55:09 +00:00
|
|
|
* A set data structure. Allows items to add themselves to and remove themselves from the set. Items can only exist once in the set.
|
2014-04-25 14:11:54 +00:00
|
|
|
*
|
|
|
|
* @class Phaser.ArrayList
|
|
|
|
* @constructor
|
|
|
|
*/
|
|
|
|
Phaser.ArrayList = function () {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @property {number} total - Number of objects in the list.
|
|
|
|
* @default
|
|
|
|
*/
|
|
|
|
this.total = 0;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @property {number} position - Current cursor position.
|
|
|
|
* @default
|
|
|
|
*/
|
|
|
|
this.position = 0;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @property {array} list - The list.
|
|
|
|
*/
|
|
|
|
this.list = [];
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
Phaser.ArrayList.prototype = {
|
|
|
|
|
|
|
|
/**
|
2014-04-25 14:24:55 +00:00
|
|
|
* Adds a new element to this list. The item can only exist in the list once.
|
2014-04-25 14:11:54 +00:00
|
|
|
*
|
|
|
|
* @method Phaser.ArrayList#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.
|
|
|
|
*/
|
|
|
|
add: function (child) {
|
|
|
|
|
|
|
|
if (!this.exists(child))
|
|
|
|
{
|
|
|
|
this.list.push(child);
|
|
|
|
this.total++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return child;
|
|
|
|
|
|
|
|
},
|
|
|
|
|
2014-04-25 14:24:55 +00:00
|
|
|
/**
|
|
|
|
* Gets the index of the child in the list, or -1 if it isn't in the list.
|
|
|
|
*
|
|
|
|
* @method Phaser.ArrayList#getIndex
|
|
|
|
* @param {object} child - The element to get the list index for.
|
|
|
|
* @return {number} The index of the child or -1 if not found.
|
|
|
|
*/
|
2014-04-25 14:11:54 +00:00
|
|
|
getIndex: function (child) {
|
|
|
|
|
2014-04-25 14:24:55 +00:00
|
|
|
return this.list.indexOf(child);
|
2014-04-25 14:11:54 +00:00
|
|
|
|
|
|
|
},
|
|
|
|
|
2014-04-25 14:24:55 +00:00
|
|
|
/**
|
|
|
|
* Checks for the child within this list.
|
|
|
|
*
|
|
|
|
* @method Phaser.ArrayList#exists
|
|
|
|
* @param {object} child - The element to get the list index for.
|
|
|
|
* @return {boolean} True if the child is found in the list, otherwise false.
|
|
|
|
*/
|
2014-04-25 14:11:54 +00:00
|
|
|
exists: function (child) {
|
|
|
|
|
2014-04-25 14:24:55 +00:00
|
|
|
return (this.list.indexOf(child) > -1);
|
2014-04-25 14:11:54 +00:00
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2014-04-25 14:24:55 +00:00
|
|
|
* Resets the list length and drops all items in the list.
|
2014-04-25 14:11:54 +00:00
|
|
|
*
|
|
|
|
* @method Phaser.ArrayList#reset
|
|
|
|
*/
|
|
|
|
reset: function () {
|
|
|
|
|
|
|
|
this.list.length = 0;
|
|
|
|
this.total = 0;
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2014-04-25 14:24:55 +00:00
|
|
|
* Removes the given element from this list if it exists.
|
2014-04-25 14:11:54 +00:00
|
|
|
*
|
|
|
|
* @method Phaser.ArrayList#remove
|
|
|
|
* @param {object} child - The child to be removed from the list.
|
2014-04-25 14:24:55 +00:00
|
|
|
* @return {object} child - The child that was removed.
|
2014-04-25 14:11:54 +00:00
|
|
|
*/
|
|
|
|
remove: function (child) {
|
|
|
|
|
2014-04-25 14:24:55 +00:00
|
|
|
var idx = this.list.indexOf(child);
|
2014-04-25 14:11:54 +00:00
|
|
|
|
|
|
|
if (idx > -1)
|
|
|
|
{
|
|
|
|
this.list.splice(idx, 1);
|
|
|
|
this.total--;
|
|
|
|
return child;
|
|
|
|
}
|
|
|
|
|
|
|
|
},
|
|
|
|
|
2014-07-15 10:20:43 +00:00
|
|
|
/**
|
|
|
|
* Sets the property `key` to the given value on all members of this list.
|
|
|
|
*
|
|
|
|
* @method Phaser.ArrayList#setAll
|
|
|
|
* @param {object} key - The object on the child to set.
|
|
|
|
* @param {*} value - The value to set the property to.
|
|
|
|
*/
|
|
|
|
setAll: function (key, value) {
|
|
|
|
|
|
|
|
var i = this.list.length;
|
|
|
|
|
|
|
|
while (i--)
|
|
|
|
{
|
|
|
|
if (this.list[i] && this.list[i][key])
|
|
|
|
{
|
|
|
|
this.list[i][key] = value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
},
|
|
|
|
|
2014-04-25 14:11:54 +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.ArrayList#callAll
|
|
|
|
* @param {function} callback - The function to call.
|
|
|
|
* @param {...*} parameter - Additional parameters that will be passed to the callback.
|
|
|
|
*/
|
|
|
|
callAll: function (callback) {
|
|
|
|
|
|
|
|
var args = Array.prototype.splice.call(arguments, 1);
|
|
|
|
|
|
|
|
var i = this.list.length;
|
|
|
|
|
|
|
|
while (i--)
|
|
|
|
{
|
2014-04-25 19:01:09 +00:00
|
|
|
if (this.list[i] && this.list[i][callback])
|
2014-04-25 18:38:10 +00:00
|
|
|
{
|
|
|
|
this.list[i][callback].apply(this.list[i], args);
|
|
|
|
}
|
2014-04-25 14:11:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2014-04-25 14:24:55 +00:00
|
|
|
* Resets the cursor to the first item in the list and returns it.
|
2014-04-25 14:11:54 +00:00
|
|
|
*
|
|
|
|
* @name Phaser.ArrayList#first
|
|
|
|
* @property {object} first - The first item in the list.
|
|
|
|
*/
|
|
|
|
Object.defineProperty(Phaser.ArrayList.prototype, "first", {
|
|
|
|
|
|
|
|
get: function () {
|
|
|
|
|
|
|
|
this.position = 0;
|
|
|
|
|
|
|
|
if (this.total > 0)
|
|
|
|
{
|
|
|
|
return this.list[0];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
/**
|
2014-04-25 14:24:55 +00:00
|
|
|
* Gets the next item in the list and returns it, advancing the cursor.
|
2014-04-25 14:11:54 +00:00
|
|
|
*
|
|
|
|
* @name Phaser.ArrayList#next
|
|
|
|
* @property {object} next - Advanced the cursor and return.
|
|
|
|
*/
|
|
|
|
Object.defineProperty(Phaser.ArrayList.prototype, "next", {
|
|
|
|
|
|
|
|
get: function () {
|
|
|
|
|
|
|
|
if (this.position < this.total)
|
|
|
|
{
|
|
|
|
this.position++;
|
|
|
|
|
|
|
|
return this.list[this.position];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
Phaser.ArrayList.prototype.constructor = Phaser.ArrayList;
|