2016-11-28 15:05:02 +01:00
|
|
|
/**
|
|
|
|
* 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];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-01 09:43:36 +01:00
|
|
|
};
|
|
|
|
|
2016-11-01 10:00:15 +01:00
|
|
|
/**
|
|
|
|
* Updates the Entity and it's components
|
|
|
|
* @param deltaTime Number
|
|
|
|
*/
|
|
|
|
GameLib.D3.Entity.prototype.update = function(
|
2016-11-01 09:43:36 +01:00
|
|
|
deltaTime
|
|
|
|
) {
|
2016-11-28 15:05:02 +01:00
|
|
|
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);
|
|
|
|
}
|
2016-11-01 09:43:36 +01:00
|
|
|
}
|
|
|
|
}
|
2016-11-01 10:00:15 +01:00
|
|
|
|
2016-11-28 15:05:02 +01:00
|
|
|
if (this.mesh) {
|
2016-11-04 14:57:48 +01:00
|
|
|
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);
|
2016-11-28 15:05:02 +01:00
|
|
|
this.mesh.updateInstance();
|
2016-11-04 14:57:48 +01:00
|
|
|
}
|
|
|
|
|
2016-11-01 10:00:15 +01:00
|
|
|
this.onUpdate(deltaTime);
|
2016-11-01 09:43:36 +01:00
|
|
|
};
|
|
|
|
|
2016-11-01 10:00:15 +01:00
|
|
|
/**
|
|
|
|
* Late updates the Entity and it's components
|
|
|
|
* @param deltaTime Number
|
|
|
|
*/
|
|
|
|
GameLib.D3.Entity.prototype.lateUpdate = function(
|
2016-11-01 09:43:36 +01:00
|
|
|
deltaTime
|
|
|
|
) {
|
2016-11-28 15:05:02 +01:00
|
|
|
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);
|
|
|
|
}
|
2016-11-01 09:43:36 +01:00
|
|
|
}
|
|
|
|
}
|
2016-11-01 10:00:15 +01:00
|
|
|
|
|
|
|
this.onLateUpdate(deltaTime);
|
2016-11-01 09:43:36 +01:00
|
|
|
};
|
|
|
|
|
2016-11-01 10:00:15 +01:00
|
|
|
/**
|
|
|
|
* Gets called when the entity was registered with it's parent scene
|
|
|
|
* @param parentScene GameLib.D3.Scene
|
|
|
|
*/
|
|
|
|
GameLib.D3.Entity.prototype.register = function(
|
2016-11-01 09:43:36 +01:00
|
|
|
parentScene
|
|
|
|
) {
|
2016-11-28 15:05:02 +01:00
|
|
|
// this.parentScene = parentScene;
|
2016-11-29 11:26:16 +01:00
|
|
|
//
|
|
|
|
// if (this.mesh && this.mesh.id != null && parentScene.meshIdToMesh[this.mesh.id]) {
|
|
|
|
// parentScene.instance.add(parentScene.meshIdToMesh[this.mesh.id]);
|
2016-11-28 15:05:02 +01:00
|
|
|
// this.mesh = parentScene.meshIdToMesh[this.id];
|
|
|
|
// }
|
2016-11-01 10:00:15 +01:00
|
|
|
|
2016-11-28 15:05:02 +01:00
|
|
|
this.onRegistered(this.parentScene);
|
2016-11-01 09:43:36 +01:00
|
|
|
};
|
|
|
|
|
2016-11-01 10:00:15 +01:00
|
|
|
/**
|
|
|
|
* Add an already registered component to the entity
|
2016-11-14 14:48:37 +01:00
|
|
|
* @param id Number
|
2016-11-01 10:00:15 +01:00
|
|
|
*/
|
2016-11-01 09:43:36 +01:00
|
|
|
GameLib.D3.Entity.prototype.addComponentId = function(
|
2016-11-14 14:48:37 +01:00
|
|
|
id
|
2016-11-01 09:43:36 +01:00
|
|
|
) {
|
2016-11-14 14:48:37 +01:00
|
|
|
this.ids.push(id);
|
2016-11-01 09:43:36 +01:00
|
|
|
};
|
|
|
|
|
2016-11-01 10:00:15 +01:00
|
|
|
/**
|
|
|
|
* Adds a components to the entity and registers it with the entity's parent scene
|
|
|
|
* @param component GameLib.D3.ComponentInterface
|
|
|
|
*/
|
2016-11-01 09:43:36 +01:00
|
|
|
GameLib.D3.Entity.prototype.addComponent = function(
|
|
|
|
component
|
|
|
|
) {
|
|
|
|
this.parentScene.registerComponent(component);
|
|
|
|
|
2016-11-14 14:48:37 +01:00
|
|
|
this.ids.push(component.id);
|
2016-11-01 12:42:06 +01:00
|
|
|
|
|
|
|
if(component.setParentEntity && typeof component.setParentEntity == 'function') {
|
|
|
|
component.setParentEntity(this.parentScene, this);
|
|
|
|
}
|
2016-11-01 10:00:15 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2016-11-04 10:58:48 +01:00
|
|
|
GameLib.D3.Entity.prototype.getComponent = function(
|
|
|
|
componentType
|
|
|
|
) {
|
2016-11-28 15:05:02 +01:00
|
|
|
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;
|
|
|
|
}
|
2016-11-04 10:58:48 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
};
|
|
|
|
|
2016-11-01 10:00:15 +01:00
|
|
|
///////////////////////// Methods to override //////////////////////////
|
|
|
|
|
|
|
|
GameLib.D3.Entity.prototype.onUpdate = function(
|
|
|
|
deltaTime
|
|
|
|
) {
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
GameLib.D3.Entity.prototype.onLateUpdate = function(
|
|
|
|
deltaTime
|
|
|
|
) {
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
GameLib.D3.Entity.prototype.onRegistered = function(
|
|
|
|
parentScene
|
|
|
|
) {
|
|
|
|
|
2016-10-28 15:30:15 +02:00
|
|
|
};
|