r3-legacy/src/game-lib-entity.js

126 lines
2.8 KiB
JavaScript
Raw Normal View History

2016-10-28 15:30:15 +02:00
GameLib.D3.Entity = function(
meshId,
componentIds
) {
this.meshId = meshId;
if (typeof componentIds == 'undefined') {
componentIds = [];
}
this.componentIds = componentIds;
2016-11-01 09:43:36 +01:00
// constructed at runtime
2016-11-01 09:43:36 +01:00
this.parentScene = null;
this.mesh = null;
// todo:
// add position, rotation & scale properties to entity itself!
this.permutate = {
offset : {
position : null,
quaternion : null
}
};
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
) {
for(var c in this.componentIds) {
var componentId = this.componentIds[c];
2016-11-01 10:00:15 +01:00
var component = this.parentScene.componentIdToComponent[componentId];
if(component && component.onUpdate) {
component.onUpdate(deltaTime, this);
2016-11-01 09:43:36 +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
) {
for(var c in this.componentIds) {
var componentId = this.componentIds[c];
2016-11-01 10:00:15 +01:00
var component = this.parentScene.componentIdToComponent[componentId];
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
) {
this.parentScene = parentScene;
if(this.meshId != null && parentScene.meshIdToMesh[this.meshId]) {
parentScene.threeScene.add(parentScene.meshIdToMesh[this.meshId]);
this.mesh = parentScene.meshIdToMesh[this.meshId];
}
2016-11-01 10:00:15 +01:00
this.onRegistered(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
* @param componentId Number
*/
2016-11-01 09:43:36 +01:00
GameLib.D3.Entity.prototype.addComponentId = function(
componentId
) {
this.componentIds.push(componentId);
};
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);
this.componentIds.push(component.componentId);
if(component.setParentEntity && typeof component.setParentEntity == 'function') {
component.setParentEntity(this.parentScene, this);
}
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
};