From 9408afba286e1a6434ed22e084167eed539a0836 Mon Sep 17 00:00:00 2001 From: -=yb4f310 Date: Wed, 21 Jun 2017 16:36:19 +0200 Subject: [PATCH] create instances immediately when dependencies are met --- src/game-lib-d3-api-renderer.js | 2 +- src/game-lib-entity-manager.js | 318 ++++++++++++++++---------------- 2 files changed, 157 insertions(+), 163 deletions(-) diff --git a/src/game-lib-d3-api-renderer.js b/src/game-lib-d3-api-renderer.js index 9f6987c..39a57a2 100644 --- a/src/game-lib-d3-api-renderer.js +++ b/src/game-lib-d3-api-renderer.js @@ -65,7 +65,7 @@ GameLib.D3.API.Renderer = function ( this.domElement = domElement; if (GameLib.Utils.UndefinedOrNull(clearColor)) { - clearColor = new GameLib.API.Color(0.58, 0.58, 0.58); + clearColor = new GameLib.API.Color(0.11, 0.11, 0.11); } this.clearColor = clearColor; diff --git a/src/game-lib-entity-manager.js b/src/game-lib-entity-manager.js index bdd5b44..a8f71d3 100644 --- a/src/game-lib-entity-manager.js +++ b/src/game-lib-entity-manager.js @@ -256,7 +256,6 @@ GameLib.EntityManager.prototype.toApiObject = function() { /** * Returns an EntityManager from an Object entity manager - * @param graphics * @param objectEntityManager Object * @constructor */ @@ -405,195 +404,190 @@ GameLib.EntityManager.prototype.link = function(component, data) { } }; -GameLib.EntityManager.prototype.componentCreated = function(data) { +GameLib.EntityManager.prototype.componentCreated = function() { - //console.log('component created : ' + data.component.name); + var loading = []; - /** - * Register this component immediately - */ - this.checkRegister.push(data.component); - - /** - * If we notify ourselves - ignore it - */ - if (data.component === this) { - return; - } - - /** - * Store this component into our 'loaded' list - */ - this.loading.push(data.component); - - /** - * Store the dependencies too - */ - data.component.dependencies.map(function(id) { + return function(data) { /** - * Check if we already processed a component on which this component is dependent + * Register this component immediately */ - if (this.register.hasOwnProperty(id)) { + this.checkRegister.push(data.component); - console.log('found a component here'); + /** + * If we notify ourselves - ignore it + */ + if (data.component === this) { + return; + } - /** - * First add this to the 'idToObject' - */ - // data.component.idToObject[id] = this.register[id]; + /** + * Store this component into our 'loaded' list + */ + loading.push(data.component); + + /** + * Store the dependencies too + */ + data.component.dependencies.map(function (id) { /** - * Remove this dependency from the dependency list + * Check if we already processed a component on which this component is dependent */ - var index = data.component.dependencies.indexOf(id); - if (index === -1) { - console.log('failed to locate dependency which should exist'); + if (this.register.hasOwnProperty(id)) { + + /** + * Remove this dependency from the dependency list + */ + var index = data.component.dependencies.indexOf(id); + if (index === -1) { + console.log('failed to locate dependency which should exist'); + } + data.component.dependencies.splice(index, 1); + + /** + * Now link the component + */ + this.link(data.component, {component: this.register[id]}); + + } else { + + if (GameLib.Utils.UndefinedOrNull(this.dependencies[id])) { + this.dependencies[id] = []; + } + + /** + * Don't store duplicate dependencies + */ + if (this.dependencies[id].indexOf(data.component === -1)) { + this.dependencies[id].push(data.component); + } } - data.component.dependencies.splice(index, 1); + + }.bind(this)); + + /** + * Now find all the components which depend on this component + */ + if (GameLib.Utils.UndefinedOrNull(this.dependencies[data.component.id])) { /** - * Now link the component + * We don't know about any dependencies on this object - but maybe a component still + * has to load which has dependencies to this object */ - this.link(data.component, {component:this.register[id]}); + this.register[data.component.id] = data.component; } else { - if (GameLib.Utils.UndefinedOrNull(this.dependencies[id])) { - this.dependencies[id] = []; - } - /** - * Don't store duplicate dependencies + * Otherwise, now - for each dependency - check if its loaded */ - if (this.dependencies[id].indexOf(data.component === -1)) { - this.dependencies[id].push(data.component); + this.dependencies[data.component.id] = this.dependencies[data.component.id].reduce( + + function (result, component) { + + /** + * Remove the actual dependency + */ + var index = component.dependencies.indexOf(data.component.id); + if (index === -1) { + console.warn('dependency mismatch'); + } else { + component.dependencies.splice(index, 1); + } + + /** + * Find the actual place where this object should be linked - and link them + */ + this.link(component, data); + + /** + * If we now managed to link the objects, and this object has no more dependencies + */ + if (component.dependencies.length === 0) { + component.loaded = true; + component.instance = component.createInstance(); + this.emitInstanceEvents(component); + delete component.dependencies; + } + + /** + * Also remove this from the current dependency list + */ + return result; + }.bind(this), + [] + ); + + delete this.dependencies[data.component.id]; + } + + /** + * Now if this new component has no dependencies, load it + */ + if (data.component.dependencies.length === 0) { + data.component.loaded = true; + data.component.instance = data.component.createInstance(); + this.emitInstanceEvents(data.component); + delete data.component.dependencies; + } + + /** + * Now check if all components are loaded, i.e., no more dependencies - if so - create their instance objects + */ + var loaded = true; + for (var i = 0; i < loading.length; i++) { + if (!loading[i].loaded) { + loaded = false; + break } } - }.bind(this)); - - /** - * Now find all the components which depend on this component - */ - if (GameLib.Utils.UndefinedOrNull(this.dependencies[data.component.id])) { - /** - * We don't know about any dependencies on this object - but maybe a component still - * has to load which has dependencies to this object + * All components loaded */ - this.register[data.component.id] = data.component; - } else { - - /** - * Otherwise, now - for each dependency - check if its loaded - */ - this.dependencies[data.component.id] = this.dependencies[data.component.id].reduce( - function (result, component) { - - /** - * Link the object to the component - */ - // component.idToObject[data.component.id] = data.component; - - /** - * Remove the actual dependency - */ - var index = component.dependencies.indexOf(data.component.id); - if (index === -1) { - console.warn('dependency mismatch'); - } else { - component.dependencies.splice(index, 1); - } - - /** - * Find the actual place where this object should be linked - and link them - */ - this.link(component, data); - - /** - * If we now managed to link the objects, and this object has no more dependencies - */ - if (component.dependencies.length === 0) { - component.loaded = true; - } - - /** - * Also remove this from the current dependency list - */ - return result; - }.bind(this), - [] - ); - - delete this.dependencies[data.component.id]; - } - - /** - * Now if this new component has no dependencies, load it - */ - if (data.component.dependencies.length === 0) { - data.component.loaded = true; - delete data.component.dependencies; - } - - /** - * Now check if all components are loaded, i.e., no more dependencies - if so - create their instance objects - */ - var loaded = true; - for (var i = 0; i < this.loading.length; i++) { - if (!this.loading[i].loaded) { - loaded = false; - break + if (loaded) { + loading = []; } + }; +}; + +GameLib.EntityManager.prototype.emitInstanceEvents = function (component) { + + if ( + component instanceof GameLib.D3.Mesh || + component instanceof GameLib.D3.Light + ) { + GameLib.Event.Emit( + GameLib.Event.SCENE_OBJECT_INSTANCE_CREATED, + { + object: component + } + ) } - /** - * All components loaded - */ - if (loaded) { - this.loading.map(function(component) { - - component.instance = component.createInstance(); - - if ( - component instanceof GameLib.D3.Mesh || - component instanceof GameLib.D3.Light - ) { - GameLib.Event.Emit( - GameLib.Event.SCENE_OBJECT_INSTANCE_CREATED, - { - object: component - } - ) - } - - if ( - component instanceof GameLib.D3.Scene - ) { - GameLib.Event.Emit( - GameLib.Event.SCENE_INSTANCE_CREATED, - { - scene: component - } - ); - } - - if ( - component instanceof GameLib.D3.Material - ) { - GameLib.Event.Emit( - GameLib.Event.MATERIAL_LOADED, - { - material: component - } - ); - } - }); - - this.loading = []; + if ( + component instanceof GameLib.D3.Scene + ) { + GameLib.Event.Emit( + GameLib.Event.SCENE_INSTANCE_CREATED, + { + scene: component + } + ); } + if ( + component instanceof GameLib.D3.Material + ) { + GameLib.Event.Emit( + GameLib.Event.MATERIAL_LOADED, + { + material: component + } + ); + } }; /** @@ -618,7 +612,7 @@ GameLib.EntityManager.prototype.registerCallbacks = function() { this.subscriptions.push( this.subscribe( GameLib.Event.COMPONENT_CREATED, - this.componentCreated + this.componentCreated().bind(this) ) ); };