change to entitymanager for register

beta.r3js.org
Theunis J. Botha 2017-06-29 16:31:09 +02:00
parent 6901c08c5f
commit 1a0878a094
4 changed files with 36 additions and 47 deletions

View File

@ -50,6 +50,7 @@ GameLib.Event.BUILD_GUI = 0x22;
GameLib.Event.MESH_DELETED = 0x23;
GameLib.Event.MESH_SELECTED = 0x24;
GameLib.Event.MESH_DESELECTED = 0x25;
GameLib.Event.COMPONENT_REGISTER = 0x26;
/**
* Subscribe to some events

View File

@ -30,6 +30,13 @@ GameLib.Component = function(
this.dependencies = this.getDependencies();
GameLib.Event.Emit(
GameLib.Event.COMPONENT_REGISTER,
{
component : this
}
);
if (this.dependencies.length === 0) {
delete this.dependencies;
@ -42,12 +49,7 @@ GameLib.Component = function(
}
}
GameLib.Event.Emit(
GameLib.Event.COMPONENT_CREATED,
{
component : this
}
);
};
GameLib.Component.prototype = Object.create(GameLib.API.Component.prototype);

View File

@ -10,6 +10,13 @@ GameLib.EntityManager = function() {
this.entities = [];
this.register = [];
GameLib.Event.Subscribe(
GameLib.Event.COMPONENT_REGISTER,
this.registerComponent.bind(this)
);
GameLib.Component.call(
this,
GameLib.Component.COMPONENT_ENTITY_MANAGER,
@ -26,6 +33,10 @@ GameLib.EntityManager.prototype.createInstance = function() {
return GameLib.EntityManager.Instance;
};
GameLib.EntityManager.prototype.registerComponent = function(data) {
this.register.push(data.component);
};
/**
* Creates an GameLib.Entity and adds it to entities array
* @returns {*}
@ -187,30 +198,26 @@ GameLib.EntityManager.prototype.query = function(components) {
*/
GameLib.EntityManager.prototype.queryComponents = function(constructors) {
return this.entities.reduce(
function(result, entity) {
return this.register.reduce(
function(result, component) {
entity.components.map(
function(component) {
if (constructors instanceof Array) {
constructors.map(
function(constructor) {
if (component instanceof constructor) {
if (result.indexOf(component) === -1) {
result.push(component);
}
}
}
);
} else {
if (component instanceof constructors) {
if (constructors instanceof Array) {
constructors.map(
function(constructor) {
if (component instanceof constructor) {
if (result.indexOf(component) === -1) {
result.push(component);
}
}
}
);
} else {
if (component instanceof constructors) {
if (result.indexOf(component) === -1) {
result.push(component);
}
}
);
}
return result;
},

View File

@ -330,23 +330,11 @@ GameLib.System.Linking.prototype.componentCreated = function(data) {
this.resolveDependencies(component);
};
GameLib.System.Linking.prototype.queryRegister = function(constructor) {
return this.register.reduce(
function(result, component) {
if (component instanceof constructor) {
result.push(component);
}
return result;
},
[]
);
};
GameLib.System.Linking.prototype.meshInstanceCreated = function(data) {
this.resolveDependencies(data.mesh);
var scenes = this.queryRegister(GameLib.D3.Scene);
var scenes = GameLib.EntityManager.Instance.queryComponents(GameLib.D3.Scene);
scenes.map(function(scene){
if (data.mesh.parentScene === scene) {
@ -360,7 +348,7 @@ GameLib.System.Linking.prototype.lightInstanceCreated = function(data) {
this.resolveDependencies(data.light);
var scenes = this.queryRegister(GameLib.D3.Scene);
var scenes = GameLib.EntityManager.Instance.queryComponents(GameLib.D3.Scene);
scenes.map(function(scene){
if (data.light.parentScene === scene) {
@ -377,16 +365,7 @@ GameLib.System.Linking.prototype.sceneInstanceCreated = function(data) {
/**
* Add all meshes and lights
*/
var object = this.queryRegister(GameLib.D3.Mesh);
object.map(function(object){
if (
object.parentScene === scene
) {
scene.addObject(object);
}
});
object = this.queryRegister(GameLib.D3.Light);
var object = GameLib.EntityManager.Instance.queryComponents([GameLib.D3.Mesh,GameLib.D3.Light]);
object.map(function(object){
if (
object.parentScene === scene