/** * Entity Runtime * @param apiEntity * @param parentScene * @param mesh * @constructor */ GameLib.D3.Entity = function( apiEntity, parentScene, mesh ) { for (var property in apiEntity) { if (apiEntity.hasOwnProperty(property)) { this[property] = apiEntity[property]; } } if (GameLib.D3.Utils.UndefinedOrNull(parentScene)) { parentScene = null; } this.parentScene = parentScene; if (GameLib.D3.Utils.UndefinedOrNull(mesh)) { mesh = null; } this.mesh = mesh; }; /** * Entity API * @param id * @param name * @param ids * @param position * @param quaternion * @param scale * @constructor */ GameLib.D3.Entity.API = function Entity( id, name, ids, position, quaternion, scale ) { if (GameLib.D3.Utils.UndefinedOrNull(id)) { id = GameLib.D3.Tools.RandomId(); } this.id = id; if (GameLib.D3.Utils.UndefinedOrNull(name)) { name = this.constructor.name; } this.name = name; if (GameLib.D3.Utils.UndefinedOrNull(ids)) { ids = []; } this.ids = ids; if(GameLib.D3.Utils.UndefinedOrNull(position)) { position = new GameLib.D3.Vector3(); } this.position = position; if(GameLib.D3.Utils.UndefinedOrNull(quaternion)) { quaternion = new GameLib.D3.Vector4(); } this.quaternion = quaternion; if(GameLib.D3.Utils.UndefinedOrNull(scale)) { scale = new GameLib.D3.Vector3(1, 1, 1); } this.scale = scale; }; /** * Updates the Entity and it's components * @param deltaTime Number */ GameLib.D3.Entity.prototype.update = function( deltaTime ) { for (var componentId in this.ids) { if (this.ids.hasOwnProperty(componentId)) { var id = this.ids[componentId]; var component = this.parentScene.idToComponent[id]; if (component && component.onUpdate) { component.onUpdate(deltaTime, this); } } } if (this.mesh) { this.mesh.position.set(this.position.x, this.position.y, this.position.z); this.mesh.quaternion.set(this.quaternion.x, this.quaternion.y, this.quaternion.z, this.quaternion.w); this.mesh.scale.set(this.scale.x, this.scale.y, this.scale.z); this.mesh.updateInstance(); } this.onUpdate(deltaTime); }; /** * Late updates the Entity and it's components * @param deltaTime Number */ GameLib.D3.Entity.prototype.lateUpdate = function( deltaTime ) { for (var componentId in this.ids) { if (this.ids.hasOwnProperty(componentId)) { var id = this.ids[componentId]; var component = this.parentScene.idToComponent[id]; 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.id != null && parentScene.meshIdToMesh[this.id]) { // parentScene.instance.add(parentScene.meshIdToMesh[this.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.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 ) { };