diff --git a/src/game-lib-a-1-event.js b/src/game-lib-a-1-event.js index 0cf46ed..5c0e74d 100644 --- a/src/game-lib-a-1-event.js +++ b/src/game-lib-a-1-event.js @@ -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 diff --git a/src/game-lib-a-component-a.js b/src/game-lib-a-component-a.js index e44ce3c..b7a3d6c 100644 --- a/src/game-lib-a-component-a.js +++ b/src/game-lib-a-component-a.js @@ -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); diff --git a/src/game-lib-entity-manager.js b/src/game-lib-entity-manager.js index 11df13f..ed5adc8 100644 --- a/src/game-lib-entity-manager.js +++ b/src/game-lib-entity-manager.js @@ -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; }, diff --git a/src/game-lib-system-linking.js b/src/game-lib-system-linking.js index 9961b24..825e639 100644 --- a/src/game-lib-system-linking.js +++ b/src/game-lib-system-linking.js @@ -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