]> git.r.bdr.sh - rbdr/serpentity/commitdiff
Proper class based lookup of node collections
authorBen Beltran <redacted>
Tue, 12 Aug 2014 20:19:17 +0000 (15:19 -0500)
committerBen Beltran <redacted>
Tue, 12 Aug 2014 20:19:17 +0000 (15:19 -0500)
bower.json
lib/serpentity/serpentity.js
package.json

index d387ed0f31e0aac3cf7e9e106a88a281c60e11e8..5e5d89c3e3ccb7f554544e6921f89e94df8af00f 100644 (file)
@@ -1,6 +1,6 @@
 {
   "name": "serpentity",
 {
   "name": "serpentity",
-  "version": "0.1.4",
+  "version": "0.1.5",
   "homepage": "https://github.com/benbeltran/serpentity",
   "authors": [
     "Ben Beltran <ben@nsovocal.com>"
   "homepage": "https://github.com/benbeltran/serpentity",
   "authors": [
     "Ben Beltran <ben@nsovocal.com>"
index 6d3920ccd532e441c14f998b3a12c98860ca21f1..18985c56891c27941f2ac6d64d699d9b6d9af5b7 100644 (file)
@@ -100,8 +100,9 @@ Just run `engine.update(dt)` in your game loop :D
 Class("Serpentity")({
     prototype : {
         systems : null,
 Class("Serpentity")({
     prototype : {
         systems : null,
-        nodeCollections : null,
         entities : null,
         entities : null,
+        _nodeCollections : null,
+        _nodeCollectionKeys : null,
 
         init : function init(config) {
             var property;
 
         init : function init(config) {
             var property;
@@ -110,7 +111,8 @@ Class("Serpentity")({
 
             this.systems = [];
             this.entities = [];
 
             this.systems = [];
             this.entities = [];
-            this.nodeCollections = {};
+            this._nodeCollections = [];
+            this._nodeCollectionKeys = [];
 
             for (property in config) {
                 if (config.hasOwnProperty(property)) {
 
             for (property in config) {
                 if (config.hasOwnProperty(property)) {
@@ -179,18 +181,15 @@ Class("Serpentity")({
          * returns true if added, false if already there
          */
         addEntity : function addEntity(entity) {
          * 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);
 
             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;
         },
 
             return true;
         },
 
@@ -204,11 +203,9 @@ Class("Serpentity")({
 
             position = this.entities.indexOf(entity);
             if (position >= 0) {
 
             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;
 
                 this.entities.splice(position, 1);
                 return true;
@@ -222,16 +219,20 @@ Class("Serpentity")({
          * applicable entity.
          */
          getNodes : function getNodes(nodeType) {
          * 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,
             });
             }
 
             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);
 
             this.entities.forEach(function (entity) {
                 nodeCollection.add(entity);
index d31805dc757c0a7f0a5725e02459ab2aa1356218..06ed1b981a2117295ca5449c8fa27dc63d96eb06 100644 (file)
@@ -1,7 +1,7 @@
 {
   "name": "serpentity",
   "description": "A simple entity framework inspired by ash",
 {
   "name": "serpentity",
   "description": "A simple entity framework inspired by ash",
-  "version": "0.1.4",
+  "version": "0.1.5",
   "contributors": [
     {
       "name": "Ben Beltran",
   "contributors": [
     {
       "name": "Ben Beltran",