create instances immediately when dependencies are met

beta.r3js.org
-=yb4f310 2017-06-21 16:36:19 +02:00
parent e09cfed52a
commit 9408afba28
2 changed files with 157 additions and 163 deletions

View File

@ -65,7 +65,7 @@ GameLib.D3.API.Renderer = function (
this.domElement = domElement; this.domElement = domElement;
if (GameLib.Utils.UndefinedOrNull(clearColor)) { 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; this.clearColor = clearColor;

View File

@ -256,7 +256,6 @@ GameLib.EntityManager.prototype.toApiObject = function() {
/** /**
* Returns an EntityManager from an Object entity manager * Returns an EntityManager from an Object entity manager
* @param graphics
* @param objectEntityManager Object * @param objectEntityManager Object
* @constructor * @constructor
*/ */
@ -405,9 +404,11 @@ 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 = [];
return function(data) {
/** /**
* Register this component immediately * Register this component immediately
@ -424,7 +425,7 @@ GameLib.EntityManager.prototype.componentCreated = function(data) {
/** /**
* Store this component into our 'loaded' list * Store this component into our 'loaded' list
*/ */
this.loading.push(data.component); loading.push(data.component);
/** /**
* Store the dependencies too * Store the dependencies too
@ -436,13 +437,6 @@ GameLib.EntityManager.prototype.componentCreated = function(data) {
*/ */
if (this.register.hasOwnProperty(id)) { if (this.register.hasOwnProperty(id)) {
console.log('found a component here');
/**
* First add this to the 'idToObject'
*/
// data.component.idToObject[id] = this.register[id];
/** /**
* Remove this dependency from the dependency list * Remove this dependency from the dependency list
*/ */
@ -483,18 +477,15 @@ GameLib.EntityManager.prototype.componentCreated = function(data) {
* has to load which has dependencies to this object * has to load which has dependencies to this object
*/ */
this.register[data.component.id] = data.component; this.register[data.component.id] = data.component;
} else { } else {
/** /**
* Otherwise, now - for each dependency - check if its loaded * Otherwise, now - for each dependency - check if its loaded
*/ */
this.dependencies[data.component.id] = this.dependencies[data.component.id].reduce( this.dependencies[data.component.id] = this.dependencies[data.component.id].reduce(
function (result, component) {
/** function (result, component) {
* Link the object to the component
*/
// component.idToObject[data.component.id] = data.component;
/** /**
* Remove the actual dependency * Remove the actual dependency
@ -516,6 +507,9 @@ GameLib.EntityManager.prototype.componentCreated = function(data) {
*/ */
if (component.dependencies.length === 0) { if (component.dependencies.length === 0) {
component.loaded = true; component.loaded = true;
component.instance = component.createInstance();
this.emitInstanceEvents(component);
delete component.dependencies;
} }
/** /**
@ -534,6 +528,8 @@ GameLib.EntityManager.prototype.componentCreated = function(data) {
*/ */
if (data.component.dependencies.length === 0) { if (data.component.dependencies.length === 0) {
data.component.loaded = true; data.component.loaded = true;
data.component.instance = data.component.createInstance();
this.emitInstanceEvents(data.component);
delete data.component.dependencies; delete data.component.dependencies;
} }
@ -541,8 +537,8 @@ GameLib.EntityManager.prototype.componentCreated = function(data) {
* Now check if all components are loaded, i.e., no more dependencies - if so - create their instance objects * Now check if all components are loaded, i.e., no more dependencies - if so - create their instance objects
*/ */
var loaded = true; var loaded = true;
for (var i = 0; i < this.loading.length; i++) { for (var i = 0; i < loading.length; i++) {
if (!this.loading[i].loaded) { if (!loading[i].loaded) {
loaded = false; loaded = false;
break break
} }
@ -552,9 +548,12 @@ GameLib.EntityManager.prototype.componentCreated = function(data) {
* All components loaded * All components loaded
*/ */
if (loaded) { if (loaded) {
this.loading.map(function(component) { loading = [];
}
};
};
component.instance = component.createInstance(); GameLib.EntityManager.prototype.emitInstanceEvents = function (component) {
if ( if (
component instanceof GameLib.D3.Mesh || component instanceof GameLib.D3.Mesh ||
@ -589,11 +588,6 @@ GameLib.EntityManager.prototype.componentCreated = function(data) {
} }
); );
} }
});
this.loading = [];
}
}; };
/** /**
@ -618,7 +612,7 @@ GameLib.EntityManager.prototype.registerCallbacks = function() {
this.subscriptions.push( this.subscriptions.push(
this.subscribe( this.subscribe(
GameLib.Event.COMPONENT_CREATED, GameLib.Event.COMPONENT_CREATED,
this.componentCreated this.componentCreated().bind(this)
) )
); );
}; };