/** * Entity Runtime * @constructor */ GameLib.Entity = function RuntimeEntity( entityManager, parentObject, apiEntity ) { if (GameLib.Utils.UndefinedOrNull(entityManager)) { throw new Error('You cannot create entities without an entity manager') } this.entityManager = entityManager; if (GameLib.Utils.UndefinedOrNull(parentObject)) { parentObject = null; } this.parentObject = parentObject; GameLib.API.Entity.call( this, apiEntity.id, apiEntity.name, apiEntity.components ); this.instance = this.createInstance(); }; GameLib.Entity.prototype = Object.create(GameLib.API.Entity.prototype); GameLib.Entity.prototype.constructor = GameLib.Entity; /** * Creates an entity instance */ GameLib.Entity.prototype.createInstance = function() { /** * FUCK ecsjs and tiny-ecs - no client-side support and shitty code (only takes constructors as args) */ return null; }; /** * Adds a component to this entity through the instance (should notify the entity manager instance) * @param component */ GameLib.Entity.prototype.addComponent = function(component) { this.components.push(component); }; /** * Updates an entity instance */ GameLib.Entity.prototype.updateInstance = function() { this.instance = this.createInstance(true); }; /** * Converts a GameLib.Entity to GameLib.API.Entity * @returns {GameLib.API.Entity} */ GameLib.Entity.prototype.toApiEntity = function() { var apiComponents = this.components.map( function(component) { return component.toApiComponent(); } ); return new GameLib.API.Entity( this.id, this.name, apiComponents ); }; /** * * @param entityManager GameLib.EntityManager * @param objectEntity Object * @constructor */ GameLib.Entity.FromObjectEntity = function(entityManager, objectEntity) { var apiEntity = new GameLib.API.Entity( objectEntity.id, objectEntity.name, objectEntity.components ); return new GameLib.Entity( entityManager, this, apiEntity ); };