]> git.r.bdr.sh - rbdr/serpentity/blobdiff - lib/serpentity/node_collection.js
Feature/code update (#1)
[rbdr/serpentity] / lib / serpentity / node_collection.js
index 7c519912364c3a7559b8f0131a27ce90db66f909..8da41da0360e43085596c10ac93633ffe831662f 100644 (file)
@@ -1,7 +1,5 @@
 '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);
   }
 
   /*
@@ -25,19 +24,18 @@ let NodeCollection = class NodeCollection {
    *
    * 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);
@@ -53,30 +51,33 @@ let NodeCollection = class NodeCollection {
    *
    * 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;
       }
@@ -86,8 +87,4 @@ let NodeCollection = class NodeCollection {
   }
 };
 
-if (typeof module !== 'undefined' && this.module !== module) {
-  module.exports = NodeCollection;
-} else {
-  Serpentity.NodeCollection = NodeCollection;
-}
+module.exports = NodeCollection;