linking and loading done

beta.r3js.org
Theunis J. Botha 2017-06-30 09:50:29 +02:00
parent 1a0878a094
commit 943a81916f
3 changed files with 32 additions and 56 deletions

View File

@ -10,6 +10,11 @@ GameLib.EntityManager = function() {
this.entities = [];
/**
* The 'register' array is a register of each component currently loaded - when the linking
* system starts it also loads all the current components from the entity manager
* @type {Array}
*/
this.register = [];
GameLib.Event.Subscribe(
@ -35,6 +40,12 @@ GameLib.EntityManager.prototype.createInstance = function() {
GameLib.EntityManager.prototype.registerComponent = function(data) {
this.register.push(data.component);
GameLib.Event.Emit(
GameLib.Event.REGISTER_UPDATE,
{
register : this.register
}
);
};
/**
@ -87,6 +98,18 @@ GameLib.EntityManager.prototype.findEntityById = function(id) {
);
};
GameLib.EntityManager.prototype.findComponentById = function(id) {
return this.register.reduce(
function(result, component){
if (component.id === id){
result = component;
}
return result;
},
null
);
};
GameLib.EntityManager.prototype.findHelperByObject = function(object) {
return this.entities.reduce(

View File

@ -21,12 +21,6 @@ GameLib.System.Linking = function(
*/
this.dependencies = {};
/**
* The 'register' array is a register of each component currently loaded - when the linking
* system starts it also loads all the current components from the entity manager
* @type {Array}
*/
this.register = [];
this.resolved = [];
@ -46,15 +40,6 @@ GameLib.System.Linking.prototype.constructor = GameLib.System.Linking;
GameLib.System.Linking.prototype.start = function() {
this.register = GameLib.EntityManager.Instance.queryComponents([GameLib.Component]);
GameLib.Event.Emit(
GameLib.Event.REGISTER_UPDATE,
{
register : this.register
}
);
this.componentCreatedSubscription = this.subscribe(
GameLib.Event.COMPONENT_CREATED,
this.componentCreated.bind(this)
@ -219,18 +204,6 @@ GameLib.System.Linking.prototype.resolveDependencies = function(component) {
GameLib.System.Linking.prototype.registerDependencies = function(component) {
/**
* Register this component immediately
*/
this.register.push(component);
GameLib.Event.Emit(
GameLib.Event.REGISTER_UPDATE,
{
register : this.register
}
);
/**
* We only care about components with unloaded dependencies -
* other components will have already had their instance objects created
@ -244,15 +217,7 @@ GameLib.System.Linking.prototype.registerDependencies = function(component) {
/**
* Check if we already processed a component on which this component is dependent
*/
var processedComponent = this.register.reduce(
function(result, component){
if (component.id === id){
result = component;
}
return result;
},
null
);
var processedComponent = GameLib.EntityManager.Instance.findComponentById(id);
if (processedComponent) {
@ -450,13 +415,6 @@ GameLib.System.Linking.prototype.onParentEntityChange = function(data) {
};
GameLib.System.Linking.prototype.stop = function() {
this.register = [];
GameLib.Event.Emit(
GameLib.Event.REGISTER_UPDATE,
{
register : this.register
}
);
this.componentCreatedSubscription.remove();
this.parentSceneChangeSubscription.remove();
this.parentEntityChangeSubscription.remove();

View File

@ -275,24 +275,19 @@ GameLib.System.Storage.prototype.loadComponent = function(toProcess, includeDepe
/**
* Now - we should systematically check if we have the dependency already
* loaded (in our runtime environment) - if we have - we just ignore loading this dependency (for now)
* TODO: decide what to do with runtime versions of 'stale' dependencies
*
* We don't override runtime versions of the same component in the database because the user
* could be working with it and it should be the latest version.
*/
var components = GameLib.EntityManager.Instance.queryComponents(componentClass);
dependencies = dependencies.reduce(
function (result, dependency) {
var found = components.reduce(
function (result, component) {
if (component.id === dependency) {
found = true;
}
return result;
},
false
);
if (!found) {
if (GameLib.EntityManager.Instance.findComponentById(dependency)) {
/**
* Don't add the dependency
*/
} else {
result.push(dependency);
}