UNPKG

sam-ecs

Version:

A specialized entity component system

130 lines (105 loc) 4.67 kB
'use strict'; var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } //ComponentManager.js// /** * @description - Manager responsible for component generator functions * and creating entities from those component generator functions * @author - Sam Faulkner */ //node imports var Dict = require('collections/dict.js'); //user imports var Entity = require('./Entity.js'); var ComponentManager = function () { function ComponentManager(stateManager) { _classCallCheck(this, ComponentManager); this._stateManager = stateManager; this._componentLibrary = new Dict(); } _createClass(ComponentManager, [{ key: 'createComponent', value: function createComponent(name, args) { if (!this._componentLibrary.has(name)) { throw new TypeError("Can't create component: '" + name + "'!"); } var generatorFunction = this._componentLibrary.get(name); return generatorFunction(args); } /** * @description - Adds a component and generator function to the * component library * @param {String} componentName - the name of the component to add * @param {Function} generatorFunction - function that defines the * component to be returned given arguments */ }, { key: 'addComponentToLibrary', value: function addComponentToLibrary(componentName, generatorFunction) { this._componentLibrary.set(componentName, generatorFunction); } /** * @description - Removes the component from the library * @param {String} componentName - the component generator function to remove */ }, { key: 'removeComponentFromLibrary', value: function removeComponentFromLibrary(componentName) { if (!this._componentLibrary.has(componentName)) { throw new TypeError(componentName + " isn't in the component library!"); } this._componentLibrary.delete(componentName); } /** * @description - Creates an entity from a list of component * objects * @param {Array} componentList - the list of component objects that * describe the name of the component and the arguments to pass to the * generator function * @param {String} hashValue - optional parameter for making an entity * with a predefined hash value * @returns {Entity} the entity with the components listed */ }, { key: 'createEntityFromComponents', value: function createEntityFromComponents(componentList, hashValue) { var entity; if (this._stateManager) entity = new Entity(this._stateManager);else entity = new Entity(); var hash = hashValue || entity.hash(); entity._setHash(hash); var _iteratorNormalCompletion = true; var _didIteratorError = false; var _iteratorError = undefined; try { for (var _iterator = componentList[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) { var componentObject = _step.value; if (this._componentLibrary.has(componentObject.name)) { var component = this.createComponent(componentObject.name, componentObject.args); //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) { _didIteratorError = true; _iteratorError = err; } finally { try { if (!_iteratorNormalCompletion && _iterator.return) { _iterator.return(); } } finally { if (_didIteratorError) { throw _iteratorError; } } } return entity; } }]); return ComponentManager; }(); module.exports = ComponentManager;