GameLib.D3.Entity = function( meshId, componentIds ) { this.meshId = meshId; if (typeof componentIds == 'undefined') { componentIds = []; } this.componentIds = componentIds; // constructed at runtime this.parentScene = null; this.mesh = null; // todo: // add position, rotation & scale properties to entity itself! this.permutate = { offset : { position : null, quaternion : null } }; }; /** * Updates the Entity and it's components * @param deltaTime Number */ GameLib.D3.Entity.prototype.update = function( deltaTime ) { for(var c in this.componentIds) { var componentId = this.componentIds[c]; var component = this.parentScene.componentIdToComponent[componentId]; if(component && component.onUpdate) { component.onUpdate(deltaTime, this); } } this.onUpdate(deltaTime); }; /** * Late updates the Entity and it's components * @param deltaTime Number */ GameLib.D3.Entity.prototype.lateUpdate = function( deltaTime ) { for(var c in this.componentIds) { var componentId = this.componentIds[c]; var component = this.parentScene.componentIdToComponent[componentId]; if(component && component.onLateUpdate) { component.onLateUpdate(deltaTime, this); } } 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.meshId != null && parentScene.meshIdToMesh[this.meshId]) { parentScene.threeScene.add(parentScene.meshIdToMesh[this.meshId]); this.mesh = parentScene.meshIdToMesh[this.meshId]; } this.onRegistered(parentScene); }; /** * Add an already registered component to the entity * @param componentId Number */ GameLib.D3.Entity.prototype.addComponentId = function( componentId ) { this.componentIds.push(componentId); }; /** * 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.componentIds.push(component.componentId); if(component.setParentEntity && typeof component.setParentEntity == 'function') { component.setParentEntity(this.parentScene, this); } }; GameLib.D3.Entity.prototype.getComponent = function( componentType ) { for(var c in this.componentIds) { var componentId = this.componentIds[c]; var component = this.parentScene.componentIdToComponent[componentId]; 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 ) { };