/** * Entity Runtime * @param graphics GameLib.D3.Graphics * @param apiEntity GameLib.D3.API.Entity * @constructor */ GameLib.D3.Entity = function Entity( graphics, apiEntity ) { for (var property in apiEntity) { if (apiEntity.hasOwnProperty(property)) { this[property] = apiEntity[property]; } } this.graphics = graphics; this.graphics.isNotThreeThrow(); this.position = new GameLib.D3.Vector3( this.graphics, this, this.position ); this.quaternion = new GameLib.D3.Quaternion( this.graphics, this, this.quaternion ); this.scale = new GameLib.D3.Vector3( this.graphics, this, this.scale ); }; /** * Updates the Entity and it's components * @param deltaTime Number * @param late boolean to indicate whether or not this is a late update */ GameLib.D3.Entity.prototype.update = function( deltaTime, late ) { this.components.forEach( function (component) { if (!late && component.onUpdate) { component.onUpdate(deltaTime, this); } if (late && component.onLateUpdate) { component.onLateUpdate(deltaTime, this); } } ); }; /** * Converts a GameLib.D3.Entity to GameLib.D3.API.Entity * @returns {GameLib.D3.API.Entity} */ GameLib.D3.Entity.prototype.toApiEntity = function() { var apiEntity = new GameLib.D3.API.Entity( this.id, this.name, GameLib.D3.Utils.IdArrayOrEmptyArray(this.components), this.position.toApiVector(), this.quaternion.toApiQuaternion(), this.scale.toApiVector(), GameLib.D3.Utils.IdOrNull(this.parentScene), GameLib.D3.Utils.IdOrNull(this.mesh) ); return apiEntity; }; /** * * @param graphics GameLib.D3.Graphics * @param objectEntity Object * @constructor */ GameLib.D3.Entity.FromObjectEntity = function(graphics, objectEntity) { var apiEntity = new GameLib.D3.API.Entity( objectEntity.id, objectEntity.name, objectEntity.components, new GameLib.D3.API.Vector3( objectEntity.position.x, objectEntity.position.y, objectEntity.position.z ), new GameLib.D3.API.Quaternion( objectEntity.quaternion.x, objectEntity.quaternion.y, objectEntity.quaternion.z, objectEntity.quaternion.w, new GameLib.D3.API.Vector3( objectEntity.quaternion.axis.x, objectEntity.quaternion.axis.y, objectEntity.quaternion.axis.z ) ), new GameLib.D3.API.Vector3( objectEntity.scale.x, objectEntity.scale.y, objectEntity.scale.z ), objectEntity.parentScene, objectEntity.mesh ); return new GameLib.D3.Entity( graphics, apiEntity ); }; /** * Gets called when the entity was registered with it's parent scene * @param parentScene GameLib.D3.Scene */ GameLib.D3.Entity.prototype.register = function( parentScene ) { }; /** * Adds a components to the entity and registers it with the entity's parent scene * @param component GameLib.D3.Component */ GameLib.D3.Entity.prototype.addComponent = function( component ) { this.components.push(component); if (_.isFunction(component.onAdd)) { component.onAdd(this); } }; GameLib.D3.Entity.prototype.removeComponent = function(component) { var index = this.components.indexOf(component); this.components.splice(index, 1); if (_.isFunction(component.onRemove)) { component.onRemove(this); } }; /** * Links object Ids to actual objects * @param idToObject */ GameLib.D3.Entity.prototype.linkObjects = function(idToObject) { this.components.forEach( function(currentValue, index, array) { if (!idToObject[currentValue]) { throw new Error('Unable to locate component with ID: ' + currentValue); } array[index] = idToObject[currentValue]; }.bind(this) ); if (this.parentScene) { if (!idToObject[this.parentScene]) { throw new Error('Unable to locate scene with ID: ' + this.parentScene); } this.parentScene = idToObject[this.parentScene]; } if (this.mesh) { if (!idToObject[this.mesh]) { throw new Error('Unable to locate mesh with ID: ' + this.mesh); } this.mesh = idToObject[this.mesh]; } };