'use strict';
-/* global Serpentity */
-
/*
* Node Collections contain nodes, in order to keep the lists of nodes
* that belong to each type.
* instances of that class.
*/
-let NodeCollection = class NodeCollection {
+const NodeCollection = class NodeCollection {
+
+ constructor(config) {
- constructor (config) {
this.nodes = [];
this.type = null;
- Object.assign(this, config || {});
+ Object.assign(this, config);
}
/*
*
* Returns true if added, false otherwise.
*/
- add (entity) {
+ add(entity) {
if (this.type.matches(entity) && !this._entityExists(entity)) {
- let node = new this.type({});
- let types = this.type.types;
+ const node = new this.type({});
+ const types = this.type.types;
+ const typeNames = Object.keys(types);
node.entity = entity;
- for (let typeName in types) {
- if (types.hasOwnProperty(typeName)) {
- node[typeName] = entity.getComponent(types[typeName]);
- }
+ for (const typeName of typeNames) {
+ node[typeName] = entity.getComponent(types[typeName]);
}
this.nodes.push(node);
*
* returns true if it was removed, false otherwise.
*/
- remove (entity) {
- let found = -1;
+ remove(entity) {
+
+ let foundIndex = -1;
+
+ const found = this.nodes.some((node, i) => {
- this.nodes.forEach(function (node, i) {
if (node.entity === entity) {
- found = i;
+ foundIndex = i;
+ return true;
}
});
- if (found >= 0) {
- this.nodes.splice(found, 1);
- return true;
+ if (found) {
+ this.nodes.splice(foundIndex, 1);
}
- return false;
+ return found;
}
/*
* Checks whether we already have nodes for this entity.
*/
- _entityExists (entity) {
+ _entityExists(entity) {
+
let found = false;
- for (let node of this.nodes) {
+ for (const node of this.nodes) {
if (node.entity === entity) {
found = true;
}
}
};
-if (typeof module !== 'undefined' && this.module !== module) {
- module.exports = NodeCollection;
-} else {
- Serpentity.NodeCollection = NodeCollection;
-}
+module.exports = NodeCollection;