/** * 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(); GameLib.API.Entity.call( this, apiEntity.id, apiEntity.name, apiEntity.components ); 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.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.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; }; /** * * @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.toApiEntity = function() { var apiComponents = this.components.map( function(component) { return component.toApiComponent(); } ); return new GameLib.API.Entity( this.id, this.name, apiComponents ); }; /** * * @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 ); };