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_DELETED = 0x23;
|
||||||
GameLib.Event.MESH_SELECTED = 0x24;
|
GameLib.Event.MESH_SELECTED = 0x24;
|
||||||
GameLib.Event.MESH_DESELECTED = 0x25;
|
GameLib.Event.MESH_DESELECTED = 0x25;
|
||||||
|
GameLib.Event.COMPONENT_REGISTER = 0x26;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Subscribe to some events
|
* Subscribe to some events
|
||||||
|
|
|
@ -30,6 +30,13 @@ GameLib.Component = function(
|
||||||
|
|
||||||
this.dependencies = this.getDependencies();
|
this.dependencies = this.getDependencies();
|
||||||
|
|
||||||
|
GameLib.Event.Emit(
|
||||||
|
GameLib.Event.COMPONENT_REGISTER,
|
||||||
|
{
|
||||||
|
component : this
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
if (this.dependencies.length === 0) {
|
if (this.dependencies.length === 0) {
|
||||||
delete this.dependencies;
|
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);
|
GameLib.Component.prototype = Object.create(GameLib.API.Component.prototype);
|
||||||
|
|
|
@ -10,6 +10,13 @@ GameLib.EntityManager = function() {
|
||||||
|
|
||||||
this.entities = [];
|
this.entities = [];
|
||||||
|
|
||||||
|
this.register = [];
|
||||||
|
|
||||||
|
GameLib.Event.Subscribe(
|
||||||
|
GameLib.Event.COMPONENT_REGISTER,
|
||||||
|
this.registerComponent.bind(this)
|
||||||
|
);
|
||||||
|
|
||||||
GameLib.Component.call(
|
GameLib.Component.call(
|
||||||
this,
|
this,
|
||||||
GameLib.Component.COMPONENT_ENTITY_MANAGER,
|
GameLib.Component.COMPONENT_ENTITY_MANAGER,
|
||||||
|
@ -26,6 +33,10 @@ GameLib.EntityManager.prototype.createInstance = function() {
|
||||||
return GameLib.EntityManager.Instance;
|
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
|
* Creates an GameLib.Entity and adds it to entities array
|
||||||
* @returns {*}
|
* @returns {*}
|
||||||
|
@ -187,30 +198,26 @@ GameLib.EntityManager.prototype.query = function(components) {
|
||||||
*/
|
*/
|
||||||
GameLib.EntityManager.prototype.queryComponents = function(constructors) {
|
GameLib.EntityManager.prototype.queryComponents = function(constructors) {
|
||||||
|
|
||||||
return this.entities.reduce(
|
return this.register.reduce(
|
||||||
function(result, entity) {
|
function(result, component) {
|
||||||
|
|
||||||
entity.components.map(
|
if (constructors instanceof Array) {
|
||||||
function(component) {
|
constructors.map(
|
||||||
if (constructors instanceof Array) {
|
function(constructor) {
|
||||||
constructors.map(
|
if (component instanceof constructor) {
|
||||||
function(constructor) {
|
|
||||||
if (component instanceof constructor) {
|
|
||||||
if (result.indexOf(component) === -1) {
|
|
||||||
result.push(component);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
);
|
|
||||||
} else {
|
|
||||||
if (component instanceof constructors) {
|
|
||||||
if (result.indexOf(component) === -1) {
|
if (result.indexOf(component) === -1) {
|
||||||
result.push(component);
|
result.push(component);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
if (component instanceof constructors) {
|
||||||
|
if (result.indexOf(component) === -1) {
|
||||||
|
result.push(component);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
);
|
}
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
},
|
},
|
||||||
|
|
|
@ -330,23 +330,11 @@ GameLib.System.Linking.prototype.componentCreated = function(data) {
|
||||||
this.resolveDependencies(component);
|
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) {
|
GameLib.System.Linking.prototype.meshInstanceCreated = function(data) {
|
||||||
|
|
||||||
this.resolveDependencies(data.mesh);
|
this.resolveDependencies(data.mesh);
|
||||||
|
|
||||||
var scenes = this.queryRegister(GameLib.D3.Scene);
|
var scenes = GameLib.EntityManager.Instance.queryComponents(GameLib.D3.Scene);
|
||||||
|
|
||||||
scenes.map(function(scene){
|
scenes.map(function(scene){
|
||||||
if (data.mesh.parentScene === scene) {
|
if (data.mesh.parentScene === scene) {
|
||||||
|
@ -360,7 +348,7 @@ GameLib.System.Linking.prototype.lightInstanceCreated = function(data) {
|
||||||
|
|
||||||
this.resolveDependencies(data.light);
|
this.resolveDependencies(data.light);
|
||||||
|
|
||||||
var scenes = this.queryRegister(GameLib.D3.Scene);
|
var scenes = GameLib.EntityManager.Instance.queryComponents(GameLib.D3.Scene);
|
||||||
|
|
||||||
scenes.map(function(scene){
|
scenes.map(function(scene){
|
||||||
if (data.light.parentScene === scene) {
|
if (data.light.parentScene === scene) {
|
||||||
|
@ -377,16 +365,7 @@ GameLib.System.Linking.prototype.sceneInstanceCreated = function(data) {
|
||||||
/**
|
/**
|
||||||
* Add all meshes and lights
|
* Add all meshes and lights
|
||||||
*/
|
*/
|
||||||
var object = this.queryRegister(GameLib.D3.Mesh);
|
var object = GameLib.EntityManager.Instance.queryComponents([GameLib.D3.Mesh,GameLib.D3.Light]);
|
||||||
object.map(function(object){
|
|
||||||
if (
|
|
||||||
object.parentScene === scene
|
|
||||||
) {
|
|
||||||
scene.addObject(object);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
object = this.queryRegister(GameLib.D3.Light);
|
|
||||||
object.map(function(object){
|
object.map(function(object){
|
||||||
if (
|
if (
|
||||||
object.parentScene === scene
|
object.parentScene === scene
|
||||||
|
|
Loading…
Reference in New Issue