Class("Serpentity")({
prototype : {
systems : null,
- nodeCollections : null,
entities : null,
+ _nodeCollections : null,
+ _nodeCollectionKeys : null,
init : function init(config) {
var property;
this.systems = [];
this.entities = [];
- this.nodeCollections = {};
+ this._nodeCollections = [];
+ this._nodeCollectionKeys = [];
for (property in config) {
if (config.hasOwnProperty(property)) {
* returns true if added, false if already there
*/
addEntity : function addEntity(entity) {
- var property;
-
if (this.entities.indexOf(entity) >= 0) {
return false;
}
this.entities.push(entity);
- for (property in this.nodeCollections) {
- if (this.nodeCollections.hasOwnProperty(property)) {
- this.nodeCollections[property].add(entity);
- }
- }
+ this._nodeCollections.forEach(function (collection) {
+ collection.add(entity);
+ });
+
return true;
},
position = this.entities.indexOf(entity);
if (position >= 0) {
- for (property in this.nodeCollections) {
- if (this.nodeCollections.hasOwnProperty(property)) {
- this.nodeCollections[property].remove(entity);
- }
- }
+ this._nodeCollections.forEach(function (collection) {
+ collection.remove(entity);
+ });
this.entities.splice(position, 1);
return true;
* applicable entity.
*/
getNodes : function getNodes(nodeType) {
- var nodeCollection;
+ var position, nodeCollection;
+
+ position = this._nodeCollectionKeys.indexOf(nodeType);
- if (this.nodeCollections.hasOwnProperty(nodeType)) {
- return this.nodeCollections[nodeType].nodes;
+ if (position >= 0) {
+ return this._nodeCollections[position].nodes;
}
nodeCollection = new Serpentity.NodeCollection({
type : nodeType,
});
- this.nodeCollections[nodeType] = nodeCollection;
+
+ this._nodeCollectionKeys.push(nodeType);
+ this._nodeCollections.push(nodeCollection);
this.entities.forEach(function (entity) {
nodeCollection.add(entity);