2016-12-15 14:53:39 +01:00
|
|
|
/**
|
|
|
|
* GameLib.EntityManager
|
|
|
|
* @constructor
|
|
|
|
*/
|
2017-06-16 15:49:53 +02:00
|
|
|
GameLib.EntityManager = function() {
|
2016-12-16 16:03:48 +01:00
|
|
|
|
2017-06-16 15:49:53 +02:00
|
|
|
this.id = GameLib.Utils.RandomId();
|
2016-12-15 14:53:39 +01:00
|
|
|
|
2017-06-16 15:49:53 +02:00
|
|
|
this.name = 'Entity Manager (' + this.id + ')';
|
2017-01-17 13:24:45 +01:00
|
|
|
|
2017-06-16 15:49:53 +02:00
|
|
|
this.entities = [];
|
2017-05-11 04:48:02 +02:00
|
|
|
|
2017-06-16 15:49:53 +02:00
|
|
|
GameLib.Component.call(
|
2016-12-15 14:53:39 +01:00
|
|
|
this,
|
2017-06-16 15:49:53 +02:00
|
|
|
GameLib.Component.COMPONENT_ENTITY_MANAGER,
|
|
|
|
{
|
|
|
|
'entities' : [GameLib.Entity]
|
|
|
|
},
|
|
|
|
null
|
2017-01-05 19:34:28 +01:00
|
|
|
);
|
|
|
|
|
2017-06-16 15:49:53 +02:00
|
|
|
this.loaded = [];
|
2017-05-11 17:52:33 +02:00
|
|
|
|
2017-06-16 15:49:53 +02:00
|
|
|
this.dependencies = {};
|
2017-01-19 17:50:11 +01:00
|
|
|
|
2017-06-16 15:49:53 +02:00
|
|
|
this.subscriptions = [];
|
2017-06-04 14:36:13 +02:00
|
|
|
|
2017-06-17 19:27:01 +02:00
|
|
|
this.register = {};
|
|
|
|
|
2017-06-04 14:36:13 +02:00
|
|
|
this.registerCallbacks();
|
2016-12-15 14:53:39 +01:00
|
|
|
};
|
|
|
|
|
2017-06-16 15:49:53 +02:00
|
|
|
GameLib.EntityManager.prototype = Object.create(GameLib.Component.prototype);
|
2016-12-15 14:53:39 +01:00
|
|
|
GameLib.EntityManager.prototype.constructor = GameLib.EntityManager;
|
|
|
|
|
|
|
|
GameLib.EntityManager.prototype.createInstance = function() {
|
2017-06-16 15:49:53 +02:00
|
|
|
return GameLib.EntityManager.Instance;
|
2016-12-15 14:53:39 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2017-05-11 17:52:33 +02:00
|
|
|
* Creates an GameLib.Entity and adds it to entities array
|
2016-12-15 14:53:39 +01:00
|
|
|
* @returns {*}
|
|
|
|
*/
|
2016-12-16 16:03:48 +01:00
|
|
|
GameLib.EntityManager.prototype.createEntity = function(name) {
|
|
|
|
|
2017-01-19 17:50:11 +01:00
|
|
|
var apiEntity = new GameLib.API.Entity(
|
|
|
|
null,
|
2017-05-22 15:42:05 +02:00
|
|
|
name,
|
|
|
|
null,
|
|
|
|
null,
|
|
|
|
this
|
2017-01-19 17:50:11 +01:00
|
|
|
);
|
2016-12-15 14:53:39 +01:00
|
|
|
|
2017-01-19 17:50:11 +01:00
|
|
|
var entity = new GameLib.Entity(
|
|
|
|
this.graphics,
|
|
|
|
apiEntity
|
|
|
|
);
|
2016-12-15 14:53:39 +01:00
|
|
|
|
|
|
|
this.entities.push(entity);
|
|
|
|
|
2017-06-13 10:45:24 +02:00
|
|
|
GameLib.Event.Emit(
|
|
|
|
GameLib.Event.NEW_ENTITY,
|
|
|
|
{
|
|
|
|
entity : entity
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2016-12-15 14:53:39 +01:00
|
|
|
return entity;
|
|
|
|
};
|
|
|
|
|
2017-06-04 14:36:13 +02:00
|
|
|
/**
|
|
|
|
* Returns an entity by ID or null
|
|
|
|
* @param id
|
|
|
|
* @returns {*}
|
|
|
|
*/
|
|
|
|
GameLib.EntityManager.prototype.findEntityById = function(id) {
|
|
|
|
|
|
|
|
return this.entities.reduce(
|
|
|
|
function(result, entity){
|
|
|
|
|
|
|
|
if (entity.id === id) {
|
|
|
|
result = entity;
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
},
|
|
|
|
null
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
GameLib.EntityManager.prototype.findHelperByObject = function(object) {
|
|
|
|
|
|
|
|
return this.entities.reduce(
|
|
|
|
function(result, entity) {
|
|
|
|
var helpers = entity.getComponents(GameLib.D3.Helper);
|
|
|
|
var helper = helpers.reduce(
|
|
|
|
function(helperResult, tmpHelper) {
|
|
|
|
|
|
|
|
if (tmpHelper.object === object) {
|
|
|
|
helperResult = tmpHelper;
|
|
|
|
}
|
|
|
|
|
|
|
|
return helperResult;
|
|
|
|
},
|
|
|
|
null
|
|
|
|
);
|
|
|
|
|
|
|
|
if (helper) {
|
|
|
|
result = helper;
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
},
|
|
|
|
null
|
|
|
|
);
|
|
|
|
|
|
|
|
};
|
|
|
|
|
2016-12-15 14:53:39 +01:00
|
|
|
/**
|
2017-05-11 13:30:20 +02:00
|
|
|
* Adds an entity to this manager
|
2017-05-11 17:52:33 +02:00
|
|
|
* @param entity GameLib.Entity
|
2017-05-11 13:30:20 +02:00
|
|
|
*/
|
|
|
|
GameLib.EntityManager.prototype.addEntity = function(entity) {
|
2017-05-22 15:42:05 +02:00
|
|
|
entity.parentEntityManager = this;
|
2017-05-11 13:30:20 +02:00
|
|
|
this.entities.push(entity);
|
|
|
|
};
|
|
|
|
|
2017-05-11 17:52:33 +02:00
|
|
|
// /**
|
|
|
|
// * Adds a system to this manager
|
|
|
|
// * @param system GameLib.System
|
|
|
|
// */
|
|
|
|
// GameLib.EntityManager.prototype.addSystem = function(system) {
|
|
|
|
// this.systems.push(system);
|
|
|
|
// };
|
|
|
|
|
2017-05-11 13:30:20 +02:00
|
|
|
/**
|
|
|
|
* Returns entity by name
|
|
|
|
* @param name
|
|
|
|
* @returns {*}
|
|
|
|
*/
|
|
|
|
GameLib.EntityManager.prototype.queryByName = function(name) {
|
|
|
|
return this.entities.reduce(
|
|
|
|
function(result, entity){
|
|
|
|
if (entity.name === name) {
|
|
|
|
result = entity;
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
},
|
|
|
|
null
|
|
|
|
)
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Removes an entity - do we remove all its components as well?
|
2016-12-15 14:53:39 +01:00
|
|
|
* @param entity GameLib.D3.Entity
|
2016-12-16 16:03:48 +01:00
|
|
|
* @returns boolean true if successful
|
2016-12-15 14:53:39 +01:00
|
|
|
*/
|
|
|
|
GameLib.EntityManager.prototype.removeEntity = function(entity) {
|
|
|
|
|
|
|
|
var index = this.entities.indexOf(entity);
|
|
|
|
|
2017-05-11 13:30:20 +02:00
|
|
|
if (index === -1) {
|
2016-12-15 14:53:39 +01:00
|
|
|
console.log('failed to remove entity : ', entity);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-05-22 15:42:05 +02:00
|
|
|
entity.parentEntityManager = null;
|
|
|
|
|
2016-12-15 14:53:39 +01:00
|
|
|
this.entities.splice(index, 1);
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2017-05-11 13:30:20 +02:00
|
|
|
* Returns all the entities with the following components
|
2016-12-15 14:53:39 +01:00
|
|
|
* @param components GameLib.Component[]
|
|
|
|
*/
|
2016-12-16 16:03:48 +01:00
|
|
|
GameLib.EntityManager.prototype.query = function(components) {
|
|
|
|
|
2017-05-11 13:30:20 +02:00
|
|
|
var entities = this.entities.reduce(
|
|
|
|
function(result, entity) {
|
2016-12-19 17:44:15 +01:00
|
|
|
|
2017-05-11 13:30:20 +02:00
|
|
|
var hasAllComponents = components.reduce(
|
|
|
|
function(componentResult, component) {
|
|
|
|
if (!entity.hasComponent(component)) {
|
|
|
|
componentResult = false;
|
2016-12-19 17:44:15 +01:00
|
|
|
}
|
2017-05-11 13:30:20 +02:00
|
|
|
return componentResult;
|
|
|
|
},
|
|
|
|
true
|
|
|
|
);
|
2016-12-19 17:44:15 +01:00
|
|
|
|
2017-05-11 13:30:20 +02:00
|
|
|
if (hasAllComponents) {
|
|
|
|
result.push(entity);
|
2016-12-16 16:03:48 +01:00
|
|
|
}
|
2017-05-11 13:30:20 +02:00
|
|
|
|
|
|
|
return result;
|
|
|
|
},
|
|
|
|
[]
|
2016-12-16 16:03:48 +01:00
|
|
|
);
|
|
|
|
|
2017-05-11 13:30:20 +02:00
|
|
|
return entities;
|
|
|
|
};
|
2016-12-19 17:44:15 +01:00
|
|
|
|
2017-05-11 13:30:20 +02:00
|
|
|
/**
|
|
|
|
* Returns all actual components of all entities that contain this component
|
|
|
|
* @param constructor
|
|
|
|
*/
|
|
|
|
GameLib.EntityManager.prototype.queryComponents = function(constructor) {
|
|
|
|
|
|
|
|
var entities = this.query([constructor]);
|
|
|
|
|
|
|
|
var components = entities.reduce(
|
|
|
|
function(result, entity){
|
2017-05-16 11:50:06 +02:00
|
|
|
var ecs = entity.getComponents(constructor);
|
2017-05-11 13:30:20 +02:00
|
|
|
ecs.map(function(ec){
|
|
|
|
result.push(ec);
|
|
|
|
});
|
|
|
|
return result;
|
|
|
|
},
|
|
|
|
[]
|
|
|
|
);
|
2016-12-19 17:44:15 +01:00
|
|
|
|
2017-05-11 13:30:20 +02:00
|
|
|
return components;
|
2016-12-15 14:53:39 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Converts a GameLib.Entity to GameLib.API.Entity
|
2016-12-16 16:03:48 +01:00
|
|
|
* @returns {GameLib.API.EntityManager}
|
2016-12-15 14:53:39 +01:00
|
|
|
*/
|
2017-05-16 14:51:57 +02:00
|
|
|
GameLib.EntityManager.prototype.toApiObject = function() {
|
2016-12-15 14:53:39 +01:00
|
|
|
|
2016-12-16 16:03:48 +01:00
|
|
|
var apiEntities = this.entities.map(
|
|
|
|
function (entity) {
|
2017-05-16 14:51:57 +02:00
|
|
|
return entity.toApiObject();
|
2016-12-16 16:03:48 +01:00
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2017-05-11 17:52:33 +02:00
|
|
|
// var apiSystems = this.systems.map(
|
|
|
|
// function (system) {
|
2017-05-16 14:51:57 +02:00
|
|
|
// return system.toApiObject();
|
2017-05-11 17:52:33 +02:00
|
|
|
// }
|
|
|
|
// );
|
|
|
|
|
2016-12-16 16:03:48 +01:00
|
|
|
var apiEntityManager = new GameLib.API.EntityManager(
|
2017-01-19 17:50:11 +01:00
|
|
|
this.id,
|
|
|
|
this.name,
|
|
|
|
apiEntities,
|
2017-05-11 17:52:33 +02:00
|
|
|
// apiSystems,
|
2017-01-19 17:50:11 +01:00
|
|
|
this.parentEntity
|
2016-12-16 16:03:48 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
return apiEntityManager;
|
2016-12-15 14:53:39 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2017-01-06 16:53:53 +01:00
|
|
|
* Returns an EntityManager from an Object entity manager
|
2016-12-16 16:03:48 +01:00
|
|
|
* @param graphics
|
2017-01-06 16:53:53 +01:00
|
|
|
* @param objectEntityManager Object
|
2016-12-15 14:53:39 +01:00
|
|
|
* @constructor
|
|
|
|
*/
|
2017-06-16 15:49:53 +02:00
|
|
|
GameLib.EntityManager.FromObject = function(objectEntityManager) {
|
2016-12-16 16:03:48 +01:00
|
|
|
|
2017-06-14 14:21:57 +02:00
|
|
|
var apiEntityManager = GameLib.API.EntityManager.FromObject(objectEntityManager);
|
2016-12-16 16:03:48 +01:00
|
|
|
|
2017-06-16 15:49:53 +02:00
|
|
|
var entityManager = new GameLib.EntityManager(apiEntityManager);
|
2016-12-16 16:03:48 +01:00
|
|
|
|
|
|
|
return entityManager;
|
2016-12-15 14:53:39 +01:00
|
|
|
};
|
|
|
|
|
2017-06-04 18:17:16 +02:00
|
|
|
/**
|
|
|
|
* Defines what should happen when a parent scene changes
|
|
|
|
* @param data
|
|
|
|
*/
|
2017-06-04 14:36:13 +02:00
|
|
|
GameLib.EntityManager.prototype.onParentSceneChange = function(data) {
|
|
|
|
|
2017-06-13 14:09:18 +02:00
|
|
|
if (
|
|
|
|
data.object instanceof GameLib.D3.Mesh ||
|
|
|
|
data.object instanceof GameLib.D3.Light
|
|
|
|
) {
|
2017-06-04 14:36:13 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* We remove the helper (if any) from the old scene and add it to the new scene
|
|
|
|
*/
|
|
|
|
var helper = this.findHelperByObject(data.object);
|
|
|
|
if (helper) {
|
2017-06-14 14:21:57 +02:00
|
|
|
|
|
|
|
if (data.originalScene && data.originalScene.instance) {
|
|
|
|
data.originalScene.instance.remove(helper.instance);
|
|
|
|
}
|
2017-06-04 14:36:13 +02:00
|
|
|
data.newScene.instance.add(helper.instance);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* We remove the mesh from the old scene and add it to the new scene
|
|
|
|
*/
|
2017-06-14 14:21:57 +02:00
|
|
|
if (data.originalScene && data.originalScene.removeObject) {
|
|
|
|
data.originalScene.removeObject(data.object);
|
|
|
|
}
|
2017-06-10 10:04:29 +02:00
|
|
|
data.newScene.addObject(data.object);
|
2017-06-04 14:36:13 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* We inherit the parent entity of this new scene
|
|
|
|
*/
|
|
|
|
var originalEntity = null;
|
|
|
|
var newEntity = null;
|
|
|
|
|
|
|
|
if (data.object.hasOwnProperty('parentEntity')) {
|
|
|
|
originalEntity = data.object.parentEntity
|
|
|
|
}
|
|
|
|
|
|
|
|
if (data.newScene.hasOwnProperty('parentEntity')) {
|
|
|
|
newEntity = data.newScene.parentEntity;
|
|
|
|
}
|
|
|
|
|
2017-06-14 14:21:57 +02:00
|
|
|
var gui = null;
|
2017-06-04 14:36:13 +02:00
|
|
|
|
2017-06-14 14:21:57 +02:00
|
|
|
if (originalEntity) {
|
2017-06-04 14:36:13 +02:00
|
|
|
|
2017-06-14 14:21:57 +02:00
|
|
|
if (originalEntity.removeComponent) {
|
|
|
|
if (helper) {
|
|
|
|
originalEntity.removeComponent(helper);
|
|
|
|
}
|
|
|
|
originalEntity.removeComponent(data.object);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (originalEntity.getFirstComponent) {
|
|
|
|
gui = originalEntity.getFirstComponent(GameLib.GUI);
|
|
|
|
if (gui) {
|
|
|
|
gui.removeObject(data.object);
|
|
|
|
gui.build(this);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (newEntity) {
|
|
|
|
|
|
|
|
if (newEntity.addComponent) {
|
|
|
|
if (helper) {
|
|
|
|
newEntity.addComponent(helper);
|
|
|
|
}
|
|
|
|
newEntity.addComponent(data.object);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (newEntity.getFirstComponent) {
|
|
|
|
gui = newEntity.getFirstComponent(GameLib.GUI);
|
|
|
|
if (gui) {
|
|
|
|
gui.addObject(data.object);
|
|
|
|
gui.build(this);
|
|
|
|
}
|
|
|
|
}
|
2017-06-04 14:36:13 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
|
2017-06-08 18:17:03 +02:00
|
|
|
/**
|
|
|
|
* Change parent entity
|
|
|
|
* TODO: also change parent entity of children objects
|
|
|
|
* @param data
|
|
|
|
*/
|
2017-06-05 12:00:54 +02:00
|
|
|
GameLib.EntityManager.prototype.onParentEntityChange = function(data) {
|
2017-06-08 18:17:03 +02:00
|
|
|
|
|
|
|
if (data.originalEntity) {
|
|
|
|
data.originalEntity.removeComponent(data.object);
|
|
|
|
}
|
|
|
|
|
2017-06-05 12:00:54 +02:00
|
|
|
data.newEntity.addComponent(data.object);
|
2017-06-13 14:09:18 +02:00
|
|
|
|
|
|
|
// - ok not so cool - we may have parent entities of entities -
|
|
|
|
// - so not all children should inherit the parent entity
|
|
|
|
// data.object.buildIdToObject();
|
|
|
|
//
|
|
|
|
// for (var property in data.object.idToObject) {
|
|
|
|
// if (data.object.idToObject.hasOwnProperty(property)) {
|
|
|
|
// if (data.object.idToObject[property].hasOwnProperty('parentEntity')) {
|
|
|
|
// data.object.idToObject[property].parentEntity = data.newEntity;
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
|
2017-06-05 12:00:54 +02:00
|
|
|
};
|
|
|
|
|
2017-06-16 15:49:53 +02:00
|
|
|
|
2017-06-17 19:27:01 +02:00
|
|
|
GameLib.EntityManager.prototype.link = function(component, data) {
|
|
|
|
for (var property in component.linkedObjects) {
|
|
|
|
if (component.linkedObjects.hasOwnProperty(property)) {
|
|
|
|
if (component.linkedObjects[property] instanceof Array) {
|
|
|
|
component[property] = component[property].map(function (entry) {
|
|
|
|
if (entry === data.component.id) {
|
|
|
|
return data.component;
|
|
|
|
} else {
|
|
|
|
return entry;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
} else {
|
|
|
|
if (component[property] &&
|
|
|
|
component[property] === data.component.id) {
|
|
|
|
component[property] = data.component;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2017-06-16 15:49:53 +02:00
|
|
|
|
2017-06-17 19:27:01 +02:00
|
|
|
GameLib.EntityManager.prototype.componentCreated = function(data) {
|
2017-06-16 15:49:53 +02:00
|
|
|
|
2017-06-17 19:27:01 +02:00
|
|
|
console.log('component created : ' + data.component.name);
|
2017-06-16 15:49:53 +02:00
|
|
|
|
2017-06-17 19:27:01 +02:00
|
|
|
/**
|
|
|
|
* If we notify ourselves - ignore it
|
|
|
|
*/
|
|
|
|
if (data.component === this) {
|
|
|
|
return;
|
|
|
|
}
|
2017-06-16 15:49:53 +02:00
|
|
|
|
2017-06-17 19:27:01 +02:00
|
|
|
/**
|
|
|
|
* Store this component into our 'loaded' list
|
|
|
|
*/
|
|
|
|
this.loaded.push(data.component);
|
2017-06-16 15:49:53 +02:00
|
|
|
|
2017-06-17 19:27:01 +02:00
|
|
|
/**
|
|
|
|
* Store the dependencies too
|
|
|
|
*/
|
|
|
|
data.component.dependencies.map(function(id) {
|
2017-06-16 15:49:53 +02:00
|
|
|
|
2017-06-17 19:27:01 +02:00
|
|
|
/**
|
|
|
|
* Check if we already processed a component on which this component is dependent
|
|
|
|
*/
|
|
|
|
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
|
|
|
|
*/
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
2017-06-16 15:49:53 +02:00
|
|
|
|
2017-06-17 19:27:01 +02:00
|
|
|
}.bind(this));
|
2017-06-16 15:49:53 +02:00
|
|
|
|
2017-06-17 19:27:01 +02:00
|
|
|
/**
|
|
|
|
* Now find all the components which depend on this component
|
|
|
|
*/
|
|
|
|
if (GameLib.Utils.UndefinedOrNull(this.dependencies[data.component.id])) {
|
2017-06-16 15:49:53 +02:00
|
|
|
|
2017-06-17 19:27:01 +02:00
|
|
|
/**
|
|
|
|
* 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.register[data.component.id] = data.component;
|
|
|
|
} else {
|
2017-06-16 15:49:53 +02:00
|
|
|
|
2017-06-17 19:27:01 +02:00
|
|
|
/**
|
|
|
|
* Otherwise, now - for each dependency - update 'idToObject' and 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];
|
|
|
|
}
|
2017-06-16 15:49:53 +02:00
|
|
|
|
2017-06-17 19:27:01 +02:00
|
|
|
/**
|
|
|
|
* Now if this new component has no dependencies, load it
|
|
|
|
*/
|
|
|
|
if (data.component.dependencies.length === 0) {
|
|
|
|
data.component.loaded = true;
|
|
|
|
}
|
2017-06-16 15:49:53 +02:00
|
|
|
|
2017-06-17 19:27:01 +02:00
|
|
|
/**
|
|
|
|
* 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.loaded.length; i++) {
|
|
|
|
if (!this.loaded[i].loaded) {
|
|
|
|
loaded = false;
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2017-06-16 15:49:53 +02:00
|
|
|
|
2017-06-17 19:27:01 +02:00
|
|
|
/**
|
|
|
|
* All components loaded
|
|
|
|
*/
|
|
|
|
if (loaded) {
|
|
|
|
this.loaded.map(function(component){
|
|
|
|
component.instance = component.createInstance();
|
|
|
|
});
|
|
|
|
this.loaded = [];
|
|
|
|
}
|
2017-06-16 15:49:53 +02:00
|
|
|
|
|
|
|
};
|
|
|
|
|
2017-06-04 14:36:13 +02:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
GameLib.EntityManager.prototype.registerCallbacks = function() {
|
|
|
|
|
2017-06-16 15:49:53 +02:00
|
|
|
this.subscriptions.push(
|
|
|
|
this.subscribe(
|
|
|
|
GameLib.Event.PARENT_SCENE_CHANGE,
|
|
|
|
this.onParentSceneChange
|
|
|
|
)
|
2017-06-05 12:00:54 +02:00
|
|
|
);
|
|
|
|
|
2017-06-16 15:49:53 +02:00
|
|
|
this.subscriptions.push(
|
|
|
|
this.subscribe(
|
|
|
|
GameLib.Event.PARENT_ENTITY_CHANGE,
|
|
|
|
this.onParentEntityChange
|
|
|
|
)
|
|
|
|
);
|
2017-06-04 14:36:13 +02:00
|
|
|
|
2017-06-16 15:49:53 +02:00
|
|
|
this.subscriptions.push(
|
|
|
|
this.subscribe(
|
|
|
|
GameLib.Event.COMPONENT_CREATED,
|
|
|
|
this.componentCreated
|
|
|
|
)
|
|
|
|
);
|
2017-06-04 14:36:13 +02:00
|
|
|
};
|
|
|
|
|
2016-12-15 14:53:39 +01:00
|
|
|
/**
|
|
|
|
* Links object Ids to actual objects
|
|
|
|
* @param idToObject
|
|
|
|
*/
|
2017-02-22 16:06:27 +01:00
|
|
|
// GameLib.EntityManager.prototype.linkObjects = function(idToObject) {
|
|
|
|
//
|
|
|
|
// this.entities.map(
|
|
|
|
// function(entity) {
|
|
|
|
// entity.components.map(
|
|
|
|
// function (componentId, index, array) {
|
|
|
|
// if (componentId instanceof GameLib.Component) {
|
|
|
|
// array[index] = componentId;
|
|
|
|
// } else {
|
|
|
|
// array[index] = idToObject[componentId];
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// Object.keys(array[index].linkedObjects).map(
|
|
|
|
// function (propertyName) {
|
|
|
|
// array[index][propertyName] = idToObject[array[index][propertyName]];
|
|
|
|
// }
|
|
|
|
// );
|
|
|
|
//
|
|
|
|
// array[index].loaded = true;
|
|
|
|
// }
|
|
|
|
// )
|
|
|
|
// }
|
|
|
|
// );
|
|
|
|
//
|
|
|
|
// };
|