more ecs stuff

beta.r3js.org
polygonboutique 2016-11-01 10:00:15 +01:00
parent dba92a70ba
commit 92c7b6db6c
5 changed files with 207 additions and 39 deletions

File diff suppressed because one or more lines are too long

View File

@ -177,11 +177,36 @@ GameLib.D3.ComponentInterface = function(
// will be not used
GameLib.D3.ComponentInterface.prototype.setParentEntity = function(
entity
parentScene,
parentEntity
) {
this.parentEntity = entity;
this.parentEntity = parentEntity;
this.onSetParentEntity(parentScene, parentEntity);
};
GameLib.D3.ComponentInterface.prototype.update = function(
deltaTime,
parentEntity
) {
this.onUpdate(deltaTime, parentEntity);
};
GameLib.D3.ComponentInterface.prototype.lateUpdate = function(
deltaTime,
parentEntity
) {
this.onLateUpdate(deltaTime, parentEntity);
};
GameLib.D3.ComponentInterface.prototype.register = function(
parentScene
) {
this.onRegistered(parentScene);
};
///////////////////////// Methods to override //////////////////////////
GameLib.D3.ComponentInterface.prototype.onUpdate = function(
deltaTime,
parentEntity
@ -280,29 +305,47 @@ GameLib.D3.Entity = function(
this.mesh = null;
};
GameLib.D3.Entity.prototype.onUpdate = function(
/**
* 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];
if(this.parentScene.componentIdToComponent[componentId]) {
this.parentScene.componentIdToComponent[componentId].onUpdate(deltaTime, this);
var component = this.parentScene.componentIdToComponent[componentId];
if(component && component.onUpdate) {
component.onUpdate(deltaTime, this);
}
}
this.onUpdate(deltaTime);
};
GameLib.D3.Entity.prototype.onLateUpdate = function(
/**
* 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];
if(this.parentScene.componentIdToComponent[componentId]) {
this.parentScene.componentIdToComponent[componentId].onLateUpdate(deltaTime, this);
var component = this.parentScene.componentIdToComponent[componentId];
if(component && component.onLateUpdate) {
component.onLateUpdate(deltaTime, this);
}
}
this.onLateUpdate(deltaTime);
};
GameLib.D3.Entity.prototype.onRegistered = function(
/**
* 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;
@ -311,22 +354,52 @@ GameLib.D3.Entity.prototype.onRegistered = function(
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);
component.setParentEntity(this);
component.onSetParentEntity(this.parentScene, this);
component.setParentEntity(this.parentScene, this);
};
///////////////////////// Methods to override //////////////////////////
GameLib.D3.Entity.prototype.onUpdate = function(
deltaTime
) {
};
GameLib.D3.Entity.prototype.onLateUpdate = function(
deltaTime
) {
};
GameLib.D3.Entity.prototype.onRegistered = function(
parentScene
) {
};
/**
* Fly Controls
@ -3665,7 +3738,7 @@ GameLib.D3.Scene.prototype.update = function(
deltaTime
) {
for(var e in this.entities) {
this.entities[e].onUpdate(deltaTime);
this.entities[e].update(deltaTime);
}
};
@ -3677,7 +3750,7 @@ GameLib.D3.Scene.prototype.lateUpdate = function(
deltaTime
) {
for(var e in this.entities) {
this.entities[e].onLateUpdate(deltaTime);
this.entities[e].lateUpdate(deltaTime);
}
};
@ -3696,14 +3769,17 @@ GameLib.D3.Scene.prototype.render = function(
};
/**
* Updates the scene
* @param entity
* Registers an entity to the scene.
* This method also links the entity and it's components,
* if they are defined inside the componentIds array.
* The setParentEntity and onSetParentEntity methods are being called here.
* @param entity GameLib.D3.Entity
*/
GameLib.D3.Scene.prototype.registerEntity = function(
entity
) {
this.entities.push(entity);
entity.onRegistered(this);
entity.register(this);
// link components to entity
for(var c in entity.componentIds) {
@ -3711,12 +3787,16 @@ GameLib.D3.Scene.prototype.registerEntity = function(
var component = this.componentIdToComponent[componentId];
if(component) {
component.setParentEntity(entity);
component.onSetParentEntity(this, entity);
component.setParentEntity(this, entity);
}
}
};
/**
* Registers a component to the scene.
* This method also calls the onRegistered-method on the component
* @param component GameLib.D3.ComponentInterface
*/
GameLib.D3.Scene.prototype.registerComponent = function(
component
) {
@ -3726,6 +3806,10 @@ GameLib.D3.Scene.prototype.registerComponent = function(
}
};
/**
* Registers a light to the scene
* @param light THREE.Light
*/
GameLib.D3.Scene.prototype.registerLight = function(
light
) {

View File

@ -9,11 +9,36 @@ GameLib.D3.ComponentInterface = function(
// will be not used
GameLib.D3.ComponentInterface.prototype.setParentEntity = function(
entity
parentScene,
parentEntity
) {
this.parentEntity = entity;
this.parentEntity = parentEntity;
this.onSetParentEntity(parentScene, parentEntity);
};
GameLib.D3.ComponentInterface.prototype.update = function(
deltaTime,
parentEntity
) {
this.onUpdate(deltaTime, parentEntity);
};
GameLib.D3.ComponentInterface.prototype.lateUpdate = function(
deltaTime,
parentEntity
) {
this.onLateUpdate(deltaTime, parentEntity);
};
GameLib.D3.ComponentInterface.prototype.register = function(
parentScene
) {
this.onRegistered(parentScene);
};
///////////////////////// Methods to override //////////////////////////
GameLib.D3.ComponentInterface.prototype.onUpdate = function(
deltaTime,
parentEntity

View File

@ -14,29 +14,47 @@ GameLib.D3.Entity = function(
this.mesh = null;
};
GameLib.D3.Entity.prototype.onUpdate = function(
/**
* 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];
if(this.parentScene.componentIdToComponent[componentId]) {
this.parentScene.componentIdToComponent[componentId].onUpdate(deltaTime, this);
var component = this.parentScene.componentIdToComponent[componentId];
if(component && component.onUpdate) {
component.onUpdate(deltaTime, this);
}
}
this.onUpdate(deltaTime);
};
GameLib.D3.Entity.prototype.onLateUpdate = function(
/**
* 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];
if(this.parentScene.componentIdToComponent[componentId]) {
this.parentScene.componentIdToComponent[componentId].onLateUpdate(deltaTime, this);
var component = this.parentScene.componentIdToComponent[componentId];
if(component && component.onLateUpdate) {
component.onLateUpdate(deltaTime, this);
}
}
this.onLateUpdate(deltaTime);
};
GameLib.D3.Entity.prototype.onRegistered = function(
/**
* 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;
@ -45,20 +63,50 @@ GameLib.D3.Entity.prototype.onRegistered = function(
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);
component.setParentEntity(this);
component.onSetParentEntity(this.parentScene, this);
component.setParentEntity(this.parentScene, this);
};
///////////////////////// Methods to override //////////////////////////
GameLib.D3.Entity.prototype.onUpdate = function(
deltaTime
) {
};
GameLib.D3.Entity.prototype.onLateUpdate = function(
deltaTime
) {
};
GameLib.D3.Entity.prototype.onRegistered = function(
parentScene
) {
};

View File

@ -650,7 +650,7 @@ GameLib.D3.Scene.prototype.update = function(
deltaTime
) {
for(var e in this.entities) {
this.entities[e].onUpdate(deltaTime);
this.entities[e].update(deltaTime);
}
};
@ -662,7 +662,7 @@ GameLib.D3.Scene.prototype.lateUpdate = function(
deltaTime
) {
for(var e in this.entities) {
this.entities[e].onLateUpdate(deltaTime);
this.entities[e].lateUpdate(deltaTime);
}
};
@ -681,14 +681,17 @@ GameLib.D3.Scene.prototype.render = function(
};
/**
* Updates the scene
* @param entity
* Registers an entity to the scene.
* This method also links the entity and it's components,
* if they are defined inside the componentIds array.
* The setParentEntity and onSetParentEntity methods are being called here.
* @param entity GameLib.D3.Entity
*/
GameLib.D3.Scene.prototype.registerEntity = function(
entity
) {
this.entities.push(entity);
entity.onRegistered(this);
entity.register(this);
// link components to entity
for(var c in entity.componentIds) {
@ -696,12 +699,16 @@ GameLib.D3.Scene.prototype.registerEntity = function(
var component = this.componentIdToComponent[componentId];
if(component) {
component.setParentEntity(entity);
component.onSetParentEntity(this, entity);
component.setParentEntity(this, entity);
}
}
};
/**
* Registers a component to the scene.
* This method also calls the onRegistered-method on the component
* @param component GameLib.D3.ComponentInterface
*/
GameLib.D3.Scene.prototype.registerComponent = function(
component
) {
@ -711,6 +718,10 @@ GameLib.D3.Scene.prototype.registerComponent = function(
}
};
/**
* Registers a light to the scene
* @param light THREE.Light
*/
GameLib.D3.Scene.prototype.registerLight = function(
light
) {