From: Ben Beltran Date: Tue, 12 Aug 2014 20:19:17 +0000 (-0500) Subject: Proper class based lookup of node collections X-Git-Tag: v0.1.5~2 X-Git-Url: https://git.r.bdr.sh/rbdr/serpentity/commitdiff_plain/946b8327920fb534e53712a332f327380ce2a518?ds=inline;hp=21653e1d2688779376b509e6c2e7086553d55010 Proper class based lookup of node collections --- diff --git a/bower.json b/bower.json index d387ed0..5e5d89c 100644 --- a/bower.json +++ b/bower.json @@ -1,6 +1,6 @@ { "name": "serpentity", - "version": "0.1.4", + "version": "0.1.5", "homepage": "https://github.com/benbeltran/serpentity", "authors": [ "Ben Beltran " diff --git a/lib/serpentity/serpentity.js b/lib/serpentity/serpentity.js index 6d3920c..18985c5 100644 --- a/lib/serpentity/serpentity.js +++ b/lib/serpentity/serpentity.js @@ -100,8 +100,9 @@ Just run `engine.update(dt)` in your game loop :D Class("Serpentity")({ prototype : { systems : null, - nodeCollections : null, entities : null, + _nodeCollections : null, + _nodeCollectionKeys : null, init : function init(config) { var property; @@ -110,7 +111,8 @@ Class("Serpentity")({ this.systems = []; this.entities = []; - this.nodeCollections = {}; + this._nodeCollections = []; + this._nodeCollectionKeys = []; 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) { - 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; }, @@ -204,11 +203,9 @@ Class("Serpentity")({ 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; @@ -222,16 +219,20 @@ Class("Serpentity")({ * 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); diff --git a/package.json b/package.json index d31805d..06ed1b9 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "serpentity", "description": "A simple entity framework inspired by ash", - "version": "0.1.4", + "version": "0.1.5", "contributors": [ { "name": "Ben Beltran",