/** * Entity Runtime * @param apiEntity * @param parentScene * @param mesh * @constructor */ GameLib.D3.Entity = function Entity( apiEntity, parentScene, mesh ) { for (var property in apiEntity) { if (apiEntity.hasOwnProperty(property)) { this[property] = apiEntity[property]; } } }; /** * Updates the Entity and it's components * @param deltaTime Number */ GameLib.D3.Entity.prototype.update = function( deltaTime ) { for(var c in this.ids) { var id = this.ids[c]; var component = this.parentScene.idToComponent[id]; if(component && component.onUpdate) { component.onUpdate(deltaTime, this); } } // todo: maybe we only need to call this AFTER late update this.updateMesh(); this.onUpdate(deltaTime); }; GameLib.D3.Entity.prototype.updateMesh = function() { if (this.mesh) { /** * Normalize to prevent stretching */ this.quaternion.normalize(); this.mesh.position.x = this.position.x; this.mesh.position.y = this.position.y; this.mesh.position.z = this.position.z; this.mesh.scale.x = this.scale.x; this.mesh.scale.y = this.scale.y; this.mesh.scale.z = this.scale.z; this.mesh.quaternion.x = this.quaternion.x; this.mesh.quaternion.y = this.quaternion.y; this.mesh.quaternion.z = this.quaternion.z; this.mesh.quaternion.w = this.quaternion.w; this.mesh.updateInstance(); } }; /** * Late updates the Entity and it's components * @param deltaTime Number */ GameLib.D3.Entity.prototype.lateUpdate = function( deltaTime ) { for(var c in this.ids) { var id = this.ids[c]; var component = this.parentScene.idToComponent[id]; if(component && component.onLateUpdate) { component.onLateUpdate(deltaTime, this); } } this.updateMesh(); this.onLateUpdate(deltaTime); }; /** * Gets called when the entity was registered with it's parent scene * @param parentScene GameLib.D3.Scene */ GameLib.D3.Entity.prototype.register = function( parentScene ) { // this.parentScene = parentScene; // // if (this.mesh && this.mesh.id != null && parentScene.meshIdToMesh[this.mesh.id]) { // parentScene.instance.add(parentScene.meshIdToMesh[this.mesh.id]); // this.mesh = parentScene.meshIdToMesh[this.id]; // } this.onRegistered(this.parentScene); }; /** * Add an already registered component to the entity * @param id Number */ GameLib.D3.Entity.prototype.addComponentId = function( id ) { this.ids.push(id); }; /** * Adds a components to the entity and registers it with the entity's parent scene * @param component GameLib.D3.ComponentInterface */ GameLib.D3.Entity.prototype.addComponent = function( component ) { this.parentScene.registerComponent(component); this.ids.push(component.id); if(component.setParentEntity && typeof component.setParentEntity == 'function') { component.setParentEntity(this.parentScene, this); } }; GameLib.D3.Entity.prototype.removeComponent = function(component) { if(component && component.id && this.parentScene.idToComponent[component.id]) { this.ids = this.ids.splice(this.ids.indexOf(component), 1); delete this.parentScene.idToComponent[component.id]; } }; GameLib.D3.Entity.prototype.getComponent = function( componentType ) { for (var componentId in this.ids) { if (this.ids.hasOwnProperty(componentId)) { var id = this.ids[componentId]; var component = this.parentScene.idToComponent[id]; if (component instanceof componentType) { return component; } } } return null; }; ///////////////////////// Methods to override ////////////////////////// GameLib.D3.Entity.prototype.onUpdate = function( deltaTime ) { }; GameLib.D3.Entity.prototype.onLateUpdate = function( deltaTime ) { }; GameLib.D3.Entity.prototype.onRegistered = function( parentScene ) { };