Comparing version 1.1.1 to 1.2.0
@@ -28,2 +28,12 @@ "use strict"; | ||
_createClass(Entity, [{ | ||
key: "clearManager", | ||
value: function clearManager() { | ||
this._manager = null; | ||
} | ||
}, { | ||
key: "setManager", | ||
value: function setManager(manager) { | ||
this._manager = manager; | ||
} | ||
}, { | ||
key: "hash", | ||
@@ -52,3 +62,6 @@ value: function hash() { | ||
this._components[component.name] = component.state; | ||
this._manager._addToComponentList(component.name, this.hash()); | ||
if (this._manager) { | ||
this._manager._addToComponentList(component.name, this.hash()); | ||
this._manager._invalidateProcessorListsByEntityComponent(this.hash(), component.name); | ||
} | ||
} | ||
@@ -96,6 +109,24 @@ | ||
if (this._components[name].remove) this._components[name].remove(this._manager); | ||
delete this._components[name]; | ||
this._manager._removeHashFromComponentList(name, this.hash()); | ||
if (this._manager) { | ||
if (this._manager.hasComponent(name) && this._manager.getEntitiesByComponent(name).has(this.hash())) this._manager._removeHashFromComponentList(name, this.hash()); | ||
this._manager._invalidateProcessorListsByEntityComponent(this.hash(), name); | ||
} | ||
} | ||
/** | ||
* @description - Calls remove component on every component | ||
*/ | ||
}, { | ||
key: "removeComponents", | ||
value: function removeComponents() { | ||
for (var key in this._components) { | ||
this.removeComponent(key); | ||
} | ||
} | ||
}]); | ||
@@ -102,0 +133,0 @@ |
@@ -31,2 +31,5 @@ 'use strict'; | ||
//node inmports | ||
var EventEmitter = require('events'); | ||
/** | ||
@@ -127,2 +130,3 @@ * @description - Set intersection taken from Mozilla | ||
this._entitiesByHash = {}; | ||
this._emitter = new EventEmitter(); | ||
@@ -261,2 +265,3 @@ // inital state for reducers allows adding and removing entities | ||
this._entitiesByHash[entity.hash()] = entity; | ||
entity.setManager(this); | ||
@@ -285,2 +290,5 @@ /* invalidate any cached list that would get to a processor | ||
//grab this before it gets deleted | ||
var entity = this._entitiesByHash[hash]; | ||
// remove from the main '_entities' object | ||
@@ -300,2 +308,5 @@ delete this._entities[hash]; | ||
this._invalidateProcessorLists(entity); | ||
entity.clearManager(); | ||
entity.removeComponents(); | ||
} | ||
@@ -633,3 +644,3 @@ | ||
shouldInvalidate = false; | ||
break; | ||
break;f; | ||
} | ||
@@ -657,2 +668,20 @@ } | ||
/** | ||
* @description - typically called whenever an entity has deleted a | ||
* component, it needs to invalidate any processor list that depended | ||
* on that component | ||
* @param {String} entityHash - the hash denoting the entity | ||
* @param {String} component - denotes the name of the component | ||
*/ | ||
}, { | ||
key: '_invalidateProcessorListsByEntityComponent', | ||
value: function _invalidateProcessorListsByEntityComponent(entityHash, component) { | ||
for (var processorName in this._processors) { | ||
if (this._processors[processorName].getComponentNames().has(component) && this._processorsCachedEntityLists[processorName].set.has(entityHash)) { | ||
this._processorsCachedEntityLists[processorName].invalid = true; | ||
} | ||
} | ||
} | ||
/** | ||
* @description - Updates the list of entities that will be passed down to | ||
@@ -708,2 +737,45 @@ * the given processor | ||
} | ||
/** | ||
* @description - Adds a listener to be called when an event | ||
* is thrown | ||
* @param {String} eventType - denotes the type of event | ||
* @param {Function} fun - the function that will be called | ||
* after the event will be removed | ||
*/ | ||
}, { | ||
key: 'addListener', | ||
value: function addListener(eventType, fun) { | ||
this._emitter.addListener(eventType, fun); | ||
} | ||
/** | ||
* @description - Removes a listener that was previously added | ||
* @param {String} eventType - denotes the type of the event | ||
* @param {Function} fun - the listener to be removed | ||
*/ | ||
}, { | ||
key: 'removeListener', | ||
value: function removeListener(eventType, fun) { | ||
this._emitter.removeListener(eventType, fun); | ||
} | ||
/** | ||
* @description - Emits an event with the given argument | ||
* @param {String} eventType - denotes the type of event to be | ||
* emitted | ||
* @param {Object} arg - the argument that will be passed to the | ||
* event handler | ||
* @return {Bool} - Returns whether there were any listeners | ||
* for the supplied event type or not | ||
*/ | ||
}, { | ||
key: 'emit', | ||
value: function emit(eventType, arg) { | ||
//the event listener will get access to the manager itself | ||
return this._emitter.emit(eventType, arg, this); | ||
} | ||
}]); | ||
@@ -710,0 +782,0 @@ |
{ | ||
"name": "sam-ecs", | ||
"version": "1.1.1", | ||
"version": "1.2.0", | ||
"description": "A specialized entity component system", | ||
@@ -5,0 +5,0 @@ "main": "./dist/index.js", |
35723
876