change to entitymanager for register
parent
6901c08c5f
commit
1a0878a094
|
@ -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
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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,11 +198,9 @@ 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) {
|
||||
|
@ -209,8 +218,6 @@ GameLib.EntityManager.prototype.queryComponents = function(constructors) {
|
|||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
return result;
|
||||
},
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue