/** * GameLib.EntityManager * @param parentObject * @param apiEntityManager GameLib.API.EntityManager * @constructor */ GameLib.EntityManager = function( parentObject, apiEntityManager ) { if (GameLib.Utils.UndefinedOrNull(parentObject)) { parentObject = null; } this.parentObject = parentObject; GameLib.API.EntityManager.call( this, apiEntityManager.entities ); this.instance = this.createInstance(); }; GameLib.EntityManager.prototype = Object.create(GameLib.API.EntityManager.prototype); GameLib.EntityManager.prototype.constructor = GameLib.EntityManager; /** * Creates an Entity Manager instance * @returns {*} */ GameLib.EntityManager.prototype.createInstance = function() { /** * Fuck the current ECS bullshit on the internet - both tiny-ecs and ecsjs SUCKS ASS */ return null; }; /** * Creates an GameLib.Entity * @returns {*} */ GameLib.EntityManager.prototype.createEntity = function(name) { var apiEntity = new GameLib.API.Entity(null, name); var entity = new GameLib.Entity(this, this, apiEntity); this.entities.push(entity); return entity; }; /** * Removes an entity * @param entity GameLib.D3.Entity * @returns boolean true if successful */ GameLib.EntityManager.prototype.removeEntity = function(entity) { var index = this.entities.indexOf(entity); if (index == -1) { console.log('failed to remove entity : ', entity); return false; } this.entities.splice(index, 1); return true; }; /** * Returns all the objects with the following components * @param components GameLib.Component[] */ GameLib.EntityManager.prototype.query = function(components) { return this.entities.map( function(__queryComponents) { return function(entity) { __queryComponents.map( function(__entity) { return function(queryComponent) { __entity.components.map( function(__queryComponent) { return function(entityComponent) { if (__queryComponent == entityComponent.constructor.name) { // arrow should point towards a window or sergej ---> return entityComponent; } } }(queryComponent) ); } }(entity) ) } }(components) ); }; /** * Converts a GameLib.Entity to GameLib.API.Entity * @returns {GameLib.API.EntityManager} */ GameLib.EntityManager.prototype.toApiEntityManager = function() { var apiEntities = this.entities.map( function (entity) { return entity.toApiEntity(); } ); var apiEntityManager = new GameLib.API.EntityManager( apiEntities ); return apiEntityManager; }; /** * * @param graphics * @param objectEntities Object * @constructor */ GameLib.EntityManager.FromObjectEntityManager = function(graphics, objectEntities) { var apiEntities = objectEntities.entities.map( function (objectEntity) { objectEntity.components = objectEntity.components.map( function (component) { if (component instanceof Object) { if (component.componentType == GameLib.Component.COMPONENT_TYPE_PATH_FOLLOWING) { return GameLib.D3.PathFollowing.FromObjectComponent(graphics, component); } else { console.warn('no component was associated with this object'); throw new Error('no component was associated with this object'); } } else { return component; } } ); return new GameLib.API.Entity( objectEntity.id, objectEntity.name, objectEntity.components ) } ); var apiEntityManager = new GameLib.API.EntityManager( apiEntities ); var entityManager = new GameLib.EntityManager( this, apiEntityManager ); entityManager.entities = entityManager.entities.map( function(apiEntity) { return new GameLib.Entity( entityManager, entityManager, apiEntity ) } ); return entityManager; }; /** * Links object Ids to actual objects * @param idToObject */ GameLib.EntityManager.prototype.linkObjects = function(idToObject) { this.entities.map( function(entity) { entity.components.map( function (componentId, index, array) { if (componentId instanceof GameLib.Component) { array[index] = componentId; } else { array[index] = idToObject[componentId]; } } ) } ); // TODO: fix // this.components.forEach( // function(currentValue, index, array) { // // if (!idToObject[currentValue]) { // //throw new Error('Unable to locate component with ID: ' + currentValue); // console.log('Unable to locate component with ID: ' + currentValue + ' - it must have been deleted before'); // array.splice(index, 1); // } else { // array[index] = idToObject[currentValue]; // } // // // }.bind(this) // ); };