diff options
| author | Ben Beltran <ben@nsovocal.com> | 2014-08-12 15:19:17 -0500 |
|---|---|---|
| committer | Ben Beltran <ben@nsovocal.com> | 2014-08-12 15:19:17 -0500 |
| commit | 946b8327920fb534e53712a332f327380ce2a518 (patch) | |
| tree | d544b2dca1b7b1bc75d50e2c1707ba12982903d8 /lib | |
| parent | 21653e1d2688779376b509e6c2e7086553d55010 (diff) | |
Proper class based lookup of node collections
Diffstat (limited to 'lib')
| -rw-r--r-- | lib/serpentity/serpentity.js | 37 |
1 files changed, 19 insertions, 18 deletions
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); |