]> git.r.bdr.sh - rbdr/serpentity/blobdiff - lib/serpentity/node_collection.js
Yields nodeCollection instead of nodes array
[rbdr/serpentity] / lib / serpentity / node_collection.js
index d1d25fc1fb33d8560dc5913dcde419405de75f3f..7c2c627e13424acf4ca880f6cf90f1be0e964ce5 100644 (file)
@@ -1,3 +1,7 @@
+'use strict';
+
+const Events = require('events');
+
 /*
  * Node Collections contain nodes, in order to keep the lists of nodes
  * that belong to each type.
@@ -5,90 +9,97 @@
  * It has a type which is the class name of the node, and an array of
  * instances of that class.
  */
-Class(Serpentity, "NodeCollection")({
-    prototype : {
-        type : null,
-        nodes : null,
-
-        init : function init(config) {
-            var property;
-
-            config = config || {};
-
-            this.nodes = [];
-
-            for (property in config) {
-                if (config.hasOwnProperty(property)) {
-                    this[property] = config[property];
-                }
-            }
-        },
-
-        /*
-         * Creates a node for an entity if it matches, and adds it to the
-         * node list.
-         *
-         * Returns true if added, false otherwise.
-         */
-        add : function add(entity) {
-            var node, types, property;
-
-            if (this.type.matches(entity) && !this._entityExists(entity)) {
-                node = new this.type({});
-
-                node.entity = entity;
-
-                types = this.type.types;
-
-                for (property in types) {
-                  if (types.hasOwnProperty(property)) {
-                      node[property] = entity.getComponent(types[property]);
-                  }
-                }
-
-                this.nodes.push(node);
-
-                return true;
-            }
-
-            return false;
-        },
-
-        /*
-         * Removes an entity by removing its related node from the list of nodes
-         *
-         * returns true if it was removed, false otherwise.
-         */
-        remove : function (entity) {
-            var found;
-            found = -1;
-            this.nodes.forEach(function (node, i) {
-                if (node.entity === entity) {
-                    found = i;
-                }
-            });
-
-            if (found >= 0) {
-                this.nodes.splice(found, 1);
-                return true;
-            }
-
-            return false;
-        },
-
-        /*
-         * Checks whether we already have nodes for this entity.
-         */
-        _entityExists : function entityExists(entity) {
-            var found;
-            found = false;
-            this.nodes.forEach(function (node) {
-                if (node.entity === entity) {
-                    found = true;
-                }
-            });
-
-            return found;
-        }
+
+const NodeCollection = class NodeCollection extends Events {
+
+  constructor(config) {
+
+    super();
+
+    this.nodes = [];
+    this.type = null;
+
+    Object.assign(this, config);
+  }
+
+  /*
+   * Creates a node for an entity if it matches, and adds it to the
+   * node list.
+   *
+   * Returns true if added, false otherwise.
+   */
+  add(entity) {
+
+    if (this.type.matches(entity) && !this._entityExists(entity)) {
+
+      const node = new this.type({});
+      const types = this.type.types;
+      const typeNames = Object.keys(types);
+
+      node.entity = entity;
+
+      for (const typeName of typeNames) {
+        node[typeName] = entity.getComponent(types[typeName]);
+      }
+
+      this.nodes.push(node);
+      this.emit('nodeAdded', { node });
+
+      return true;
     }
-});
+
+    return false;
+  }
+
+  /*
+   * Removes an entity by removing its related node from the list of nodes
+   *
+   * returns true if it was removed, false otherwise.
+   */
+  remove(entity) {
+
+    let foundIndex = -1;
+
+    const found = this.nodes.some((node, i) => {
+
+      if (node.entity === entity) {
+        foundIndex = i;
+        return true;
+      }
+    });
+
+    if (found) {
+      this.nodes.splice(foundIndex, 1);
+      this.emit('nodeRemoved', { node });
+    }
+
+    return found;
+  }
+
+  /*
+   * Checks whether we already have nodes for this entity.
+   */
+  _entityExists(entity) {
+
+    let found = false;
+
+    for (const node of this.nodes) {
+      if (node.entity === entity) {
+        found = true;
+      }
+    }
+
+    return found;
+  }
+};
+
+
+/*
+ * Make the node collection iterable without returning the array directly
+ */
+NodeCollection.prototype[Symbol.iterator] = function * () {
+
+  yield* this.nodes;
+};
+
+module.exports = NodeCollection;