2011-01-16 00:35:20 +01:00
|
|
|
function EntityCollection(baseAI, entities)
|
|
|
|
{
|
|
|
|
this._ai = baseAI;
|
|
|
|
this._entities = entities;
|
2011-02-05 21:35:34 +01:00
|
|
|
|
2011-03-03 01:16:14 +01:00
|
|
|
// Compute length lazily on demand, since it can be
|
|
|
|
// expensive for large collections
|
|
|
|
var length = undefined;
|
|
|
|
Object.defineProperty(this, "length", {
|
|
|
|
get: function () {
|
|
|
|
if (length === undefined)
|
|
|
|
{
|
|
|
|
length = 0;
|
|
|
|
for (var id in entities)
|
|
|
|
++length;
|
|
|
|
}
|
|
|
|
return length;
|
|
|
|
}
|
|
|
|
});
|
2011-01-16 00:35:20 +01:00
|
|
|
}
|
|
|
|
|
2011-02-05 21:35:34 +01:00
|
|
|
EntityCollection.prototype.toIdArray = function()
|
2011-01-16 00:35:20 +01:00
|
|
|
{
|
|
|
|
var ret = [];
|
|
|
|
for (var id in this._entities)
|
|
|
|
ret.push(+id);
|
|
|
|
return ret;
|
|
|
|
};
|
|
|
|
|
2011-02-05 21:35:34 +01:00
|
|
|
EntityCollection.prototype.toEntityArray = function()
|
|
|
|
{
|
|
|
|
var ret = [];
|
|
|
|
for each (var ent in this._entities)
|
|
|
|
ret.push(new Entity(this._ai, ent));
|
|
|
|
return ret;
|
|
|
|
};
|
|
|
|
|
|
|
|
EntityCollection.prototype.toString = function()
|
|
|
|
{
|
|
|
|
return "[EntityCollection " + this.toEntityArray().join(" ") + "]";
|
|
|
|
};
|
|
|
|
|
2011-02-10 17:06:28 +01:00
|
|
|
/**
|
|
|
|
* Returns the (at most) n entities nearest to targetPos.
|
|
|
|
*/
|
|
|
|
EntityCollection.prototype.filterNearest = function(targetPos, n)
|
|
|
|
{
|
|
|
|
// Compute the distance of each entity
|
|
|
|
var data = []; // [ [id, ent, distance], ... ]
|
|
|
|
for (var id in this._entities)
|
|
|
|
{
|
|
|
|
var ent = this._entities[id];
|
|
|
|
if (ent.position)
|
|
|
|
data.push([id, ent, VectorDistance(targetPos, ent.position)]);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sort by increasing distance
|
|
|
|
data.sort(function (a, b) { return (a[2] - b[2]); });
|
|
|
|
|
|
|
|
// Extract the first n
|
|
|
|
var ret = {};
|
|
|
|
for each (var val in data.slice(0, n))
|
|
|
|
ret[val[0]] = val[1];
|
|
|
|
|
|
|
|
return new EntityCollection(this._ai, ret);
|
|
|
|
};
|
|
|
|
|
2011-01-16 00:35:20 +01:00
|
|
|
EntityCollection.prototype.filter = function(callback, thisp)
|
|
|
|
{
|
|
|
|
var ret = {};
|
|
|
|
for (var id in this._entities)
|
|
|
|
{
|
|
|
|
var ent = this._entities[id];
|
|
|
|
var val = new Entity(this._ai, ent);
|
|
|
|
if (callback.call(thisp, val, id, this))
|
|
|
|
ret[id] = ent;
|
|
|
|
}
|
|
|
|
return new EntityCollection(this._ai, ret);
|
|
|
|
};
|
|
|
|
|
2011-03-03 01:16:14 +01:00
|
|
|
EntityCollection.prototype.filter_raw = function(callback, thisp)
|
|
|
|
{
|
|
|
|
var ret = {};
|
|
|
|
for (var id in this._entities)
|
|
|
|
{
|
|
|
|
var ent = this._entities[id];
|
|
|
|
if (callback.call(thisp, ent, id, this))
|
|
|
|
ret[id] = ent;
|
|
|
|
}
|
|
|
|
return new EntityCollection(this._ai, ret);
|
|
|
|
};
|
|
|
|
|
2011-02-05 21:35:34 +01:00
|
|
|
EntityCollection.prototype.forEach = function(callback, thisp)
|
|
|
|
{
|
|
|
|
for (var id in this._entities)
|
|
|
|
{
|
|
|
|
var ent = this._entities[id];
|
|
|
|
var val = new Entity(this._ai, ent);
|
|
|
|
callback.call(thisp, val, id, this);
|
|
|
|
}
|
|
|
|
return this;
|
|
|
|
};
|
|
|
|
|
2011-01-16 00:35:20 +01:00
|
|
|
EntityCollection.prototype.move = function(x, z)
|
|
|
|
{
|
2011-02-05 21:35:34 +01:00
|
|
|
Engine.PostCommand({"type": "walk", "entities": this.toIdArray(), "x": x, "z": z, "queued": false});
|
2011-01-16 00:35:20 +01:00
|
|
|
return this;
|
|
|
|
};
|
|
|
|
|
|
|
|
EntityCollection.prototype.destroy = function()
|
|
|
|
{
|
2011-02-05 21:35:34 +01:00
|
|
|
Engine.PostCommand({"type": "delete-entities", "entities": this.toIdArray()});
|
2011-01-16 00:35:20 +01:00
|
|
|
return this;
|
|
|
|
};
|