Comparing version 1.2.1 to 2.0.0
@@ -45,2 +45,11 @@ "use strict"; | ||
} | ||
}, { | ||
key: "_setHash", | ||
value: function _setHash(value) { | ||
this._hash = value; | ||
//reset the hash value in each component | ||
for (var componentName in this._components) { | ||
this._components[componentName].entity = value; | ||
} | ||
} | ||
@@ -61,6 +70,14 @@ /** | ||
this._components[component.name] = component.state; | ||
// this should create a copy of the state | ||
this._components[component.name] = { | ||
'state': Object.assign({}, component.state), | ||
'init': component.init, | ||
'remove': component.remove | ||
}; | ||
if (this._manager) { | ||
this._manager._addToComponentList(component.name, this.hash()); | ||
this._manager._invalidateProcessorListsByEntityComponent(this.hash(), component.name); | ||
if (this._components[component.name].init) this._components[component.name].init(this._manager); | ||
} | ||
@@ -131,2 +148,36 @@ } | ||
} | ||
/** | ||
* @description - Puts the entity into an object that can be saved | ||
* @return {Object} - the entity's state | ||
*/ | ||
}, { | ||
key: "_toJSON", | ||
value: function _toJSON() { | ||
var components = {}; | ||
for (var componentName in this._components) { | ||
components[componentName] = this._components[componentName].state; | ||
} | ||
return { | ||
'components': components, | ||
'hash': this.hash() | ||
}; | ||
} | ||
/** | ||
* @description - Takes the object serialized from above and restores the | ||
* state into this entity | ||
* @param {Object} obj - the state of the entity | ||
*/ | ||
}, { | ||
key: "_fromJSON", | ||
value: function _fromJSON(obj) { | ||
for (var componentName in obj.components) { | ||
this.addComponent({ 'name': componentName, | ||
'state': obj.components[componentName] }); | ||
} | ||
this._hash = obj.hash; | ||
} | ||
}]); | ||
@@ -133,0 +184,0 @@ |
@@ -131,2 +131,4 @@ 'use strict'; | ||
this._componentLibrary = {}; | ||
// inital state for reducers allows adding and removing entities | ||
@@ -640,3 +642,3 @@ this._reducers = (_reducers = {}, _defineProperty(_reducers, ADD_ENTITY, [addEntityReducer]), _defineProperty(_reducers, REMOVE_ENTITY, [removeEntityReducer]), _reducers); | ||
shouldInvalidate = false; | ||
break;f; | ||
break; | ||
} | ||
@@ -775,2 +777,152 @@ } | ||
} | ||
/** | ||
* @description - Serializes the entire state of the manager out to a | ||
* javascript object | ||
* @return {Object} - the state of the ECS manager | ||
*/ | ||
}, { | ||
key: 'toJSON', | ||
value: function toJSON() { | ||
var entities = new Array(); | ||
for (var entityHash in this._entitiesByHash) { | ||
var entity = this._entitiesByHash[entityHash]; | ||
entities.push(entity._toJSON()); | ||
} | ||
return { | ||
'entities': entities | ||
}; | ||
} | ||
/** | ||
* @description - Takes the object produced from 'toJSON' and | ||
* restores the entire state of the manager | ||
* @param {Object} obj - the serialized state of the manager | ||
*/ | ||
}, { | ||
key: 'fromJSON', | ||
value: function fromJSON(obj) { | ||
var _iteratorNormalCompletion8 = true; | ||
var _didIteratorError8 = false; | ||
var _iteratorError8 = undefined; | ||
try { | ||
for (var _iterator8 = obj.entities[Symbol.iterator](), _step8; !(_iteratorNormalCompletion8 = (_step8 = _iterator8.next()).done); _iteratorNormalCompletion8 = true) { | ||
var entityObj = _step8.value; | ||
var hashValue = entityObj.hash; | ||
var componentObject = entityObj.components; | ||
var componentList = new Array(); | ||
for (var componentName in componentObject) { | ||
var componentState = componentObject[componentName]; | ||
componentList.push({ | ||
'name': componentName, | ||
'args': componentState | ||
}); | ||
} | ||
this.addEntityFromComponents(componentList, hashValue); | ||
} | ||
} catch (err) { | ||
_didIteratorError8 = true; | ||
_iteratorError8 = err; | ||
} finally { | ||
try { | ||
if (!_iteratorNormalCompletion8 && _iterator8.return) { | ||
_iterator8.return(); | ||
} | ||
} finally { | ||
if (_didIteratorError8) { | ||
throw _iteratorError8; | ||
} | ||
} | ||
} | ||
} | ||
/** | ||
* @description - Adds a component to the manager, allowing entities | ||
* to contain this component | ||
* @param {String} name - the name of the component | ||
* @param {Function} generatorFunction - the return value of this function | ||
* will be given to an entity whenever they request this component | ||
*/ | ||
}, { | ||
key: 'addComponentToLibrary', | ||
value: function addComponentToLibrary(name, generatorFunction) { | ||
this._componentLibrary[name] = generatorFunction; | ||
} | ||
/** | ||
* @description - Removes a component that was previously added to the | ||
* manager. Does NOT remove all instances of the component from current | ||
* entities | ||
* @param {String} name - the name of the component to be removed from | ||
* the library | ||
*/ | ||
}, { | ||
key: 'removeComponentFromLibrary', | ||
value: function removeComponentFromLibrary(name) { | ||
//don't throw an error here, because, why | ||
if (!(name in this._componentLibrary)) return; | ||
delete this._componentLibrary[name]; | ||
} | ||
/** | ||
* @description - Adds an entity and gives it the components listed | ||
* @param {Array} componentList - a list of component objects to give to | ||
* the entity. Each component object should have two fields: 'name': the | ||
* name of the component, 'args': which will be passed to the | ||
* generatorFunction that was given whenever the component was added to | ||
* the manager, along with the manager itself | ||
* @param {String} hashValue - optional paramter for a hashvalue to be | ||
* assigned to the entity rather than one being generated | ||
*/ | ||
}, { | ||
key: 'addEntityFromComponents', | ||
value: function addEntityFromComponents(componentList, hashValue) { | ||
var entity = new Entity(this); | ||
var hash = hashValue || entity.hash(); | ||
//non effect if hashValue is undefined | ||
entity._setHash(hash); | ||
var _iteratorNormalCompletion9 = true; | ||
var _didIteratorError9 = false; | ||
var _iteratorError9 = undefined; | ||
try { | ||
for (var _iterator9 = componentList[Symbol.iterator](), _step9; !(_iteratorNormalCompletion9 = (_step9 = _iterator9.next()).done); _iteratorNormalCompletion9 = true) { | ||
var componentObject = _step9.value; | ||
if (componentObject.name in this._componentLibrary) { | ||
var component = this._componentLibrary[componentObject.name](componentObject.args, this); | ||
//assign the name because without it's not a valid component | ||
component.name = componentObject.name; | ||
//allow the component to know what entity it is attached to | ||
component.entity = entity.hash(); | ||
//init, if present in the component, should be called | ||
entity.addComponent(component); | ||
} | ||
} | ||
} catch (err) { | ||
_didIteratorError9 = true; | ||
_iteratorError9 = err; | ||
} finally { | ||
try { | ||
if (!_iteratorNormalCompletion9 && _iterator9.return) { | ||
_iterator9.return(); | ||
} | ||
} finally { | ||
if (_didIteratorError9) { | ||
throw _iteratorError9; | ||
} | ||
} | ||
} | ||
this.addEntity(entity); | ||
} | ||
}]); | ||
@@ -777,0 +929,0 @@ |
{ | ||
"name": "sam-ecs", | ||
"version": "1.2.1", | ||
"version": "2.0.0", | ||
"description": "A specialized entity component system", | ||
@@ -5,0 +5,0 @@ "main": "./dist/index.js", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
42264
1055