/** * Runtime Entity * @param graphics GameLib.D3.Graphics * @param apiEntity GameLib.D3.API.Entity * @constructor */ GameLib.Entity = function ( graphics, apiEntity ) { this.graphics = graphics; this.graphics.isNotThreeThrow(); if (GameLib.Utils.UndefinedOrNull(apiEntity)) { apiEntity = {}; } if (apiEntity instanceof GameLib.Entity) { return apiEntity; } GameLib.API.Entity.call( this, apiEntity.id, apiEntity.name, apiEntity.components, apiEntity.parentEntity, apiEntity.parentEntityManager ); this.componentToCreate = 0; // this.components = this.components.map( // // function (apiComponent) { // // if (apiComponent instanceof GameLib.D3.API.PathFollowing) { // return new GameLib.D3.PathFollowing(this.graphics, apiComponent); // } // // if (apiComponent instanceof GameLib.D3.API.Renderer) { // return new GameLib.D3.Renderer(this.graphics, apiComponent); // } // // if (apiComponent instanceof GameLib.D3.API.RenderTarget) { // return new GameLib.D3.RenderTarget(this.graphics, apiComponent); // } // // if (apiComponent instanceof GameLib.D3.API.Pass) { // return new GameLib.D3.Pass(this.graphics, apiComponent); // } // // if (apiComponent instanceof GameLib.D3.API.Composer) { // return new GameLib.D3.Composer(this.graphics, apiComponent); // } // // if (apiComponent instanceof GameLib.D3.API.Camera) { // return new GameLib.D3.Camera(this.graphics, apiComponent); // } // // if (apiComponent instanceof GameLib.D3.API.LookAt) { // return new GameLib.D3.LookAt(this.graphics, apiComponent); // } // // if (apiComponent instanceof GameLib.D3.API.Follow) { // return new GameLib.D3.Follow(this.graphics, apiComponent); // } // // if (apiComponent instanceof GameLib.D3.API.Input.Editor) { // return new GameLib.D3.Input.Editor(this.graphics, apiComponent); // } // // if (apiComponent instanceof GameLib.D3.API.Input.Drive) { // return new GameLib.D3.Input.Drive(this.graphics, apiComponent); // } // // if (apiComponent instanceof GameLib.D3.API.Spline) { // return new GameLib.D3.Spline(this.graphics, apiComponent); // } // // return apiComponent; // // }.bind(this) // ); 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); component.parentEntity = this; }; /** * Returns all components of type 'constructor' * @param constructor */ GameLib.Entity.prototype.getComponents = function(constructor) { var components = this.components.reduce( function(result, component) { if (component instanceof constructor) { result.push(component); } return result; }, [] ); return components; }; /** * Returns the first component or null * @param constructor */ GameLib.Entity.prototype.getFirstComponent = function(constructor) { var components = this.getComponents(constructor); if (components.length > 0) { return components[0]; } return null; }; /** * Returns true when this entity has a certain component, false otherwise * @param constructor */ GameLib.Entity.prototype.hasComponent = function(constructor) { var has = this.components.reduce( function(result, component) { if (component instanceof constructor) { result = true; } return result; }, false ); return has; }; /** * * @param component */ GameLib.Entity.prototype.removeComponent = function(component) { component.parentEntity = null; var index = this.components.indexOf(component); if (index === -1) { console.log('failed to remove component : ', component); return false; } this.components.splice(index, 1); return true; }; /** * 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.toApiObject = function() { var apiComponents = this.components.reduce( function(result, component) { if (typeof component.toApiObject === 'function') { result.push(component.toApiObject()); } else { console.log('ignored runtime component : ' + component.name); } return result; }, [] ); return new GameLib.API.Entity( this.id, this.name, apiComponents, GameLib.Utils.IdOrNull(this.parentEntity), GameLib.Utils.IdOrNull(this.parentEntityManager) ); }; /** * * @param graphics GameLib.D3.Graphics * @param objectEntity Object * @constructor */ GameLib.Entity.FromObjectEntity = function(graphics, objectEntity) { var apiEntity = GameLib.API.Entity.FromObjectEntity(objectEntity); return new GameLib.Entity( graphics, apiEntity ); };