constructor fixes - auto add child components
parent
22e0e8828d
commit
67588f1e27
|
@ -10,15 +10,27 @@ GameLib.API.EntityManager = function(
|
|||
id,
|
||||
name,
|
||||
entities,
|
||||
// systems,
|
||||
parentEntity
|
||||
) {
|
||||
if (GameLib.Utils.UndefinedOrNull(id)) {
|
||||
id = GameLib.Utils.RandomId();
|
||||
}
|
||||
this.id = id;
|
||||
|
||||
if (GameLib.Utils.UndefinedOrNull(name)) {
|
||||
name = 'Entity Manager (' + this.id + ')';
|
||||
}
|
||||
this.name = name;
|
||||
|
||||
// if (GameLib.Utils.UndefinedOrNull(systems)) {
|
||||
// systems = [];
|
||||
// }
|
||||
// this.systems = systems;
|
||||
if (GameLib.Utils.UndefinedOrNull(entities)) {
|
||||
entities = [];
|
||||
}
|
||||
this.entities = entities;
|
||||
|
||||
if (GameLib.Utils.UndefinedOrNull(parentEntity)) {
|
||||
parentEntity = null;
|
||||
}
|
||||
this.parentEntity = parentEntity;
|
||||
};
|
||||
|
||||
GameLib.API.EntityManager.prototype = Object.create(GameLib.Component.prototype);
|
||||
|
@ -37,17 +49,10 @@ GameLib.API.EntityManager.FromObject = function(objectEntityManager) {
|
|||
}
|
||||
);
|
||||
|
||||
// var apiSystems = objectEntityManager.systems.map(
|
||||
// function (objectSystem) {
|
||||
// return GameLib.API.System.FromObject(objectSystem);
|
||||
// }
|
||||
// );
|
||||
|
||||
return new GameLib.API.EntityManager(
|
||||
objectEntityManager.id,
|
||||
objectEntityManager.name,
|
||||
apiEntities,
|
||||
// apiSystems,
|
||||
objectEntityManager.parentEntity
|
||||
);
|
||||
};
|
||||
|
|
|
@ -136,9 +136,10 @@ GameLib.D3.Input.Editor.FromObject = function(graphics, objectComponent) {
|
|||
GameLib.D3.Input.Editor.prototype.onKeyDown = function(entity, entityManager) {
|
||||
|
||||
return function(event) {
|
||||
console.log('entity ' + entity.name + 'emitted keypress ' + event.code);
|
||||
console.log('entity ' + entity.name + ' emitted keypress ' + event.code);
|
||||
|
||||
if (event.code === 'Delete') {
|
||||
|
||||
var meshes = entity.getComponents(GameLib.D3.Mesh);
|
||||
var gui = entity.getFirstComponent(GameLib.GUI);
|
||||
|
||||
|
|
|
@ -240,22 +240,15 @@ GameLib.EntityManager.prototype.toApiObject = function() {
|
|||
|
||||
var apiEntities = this.entities.map(
|
||||
function (entity) {
|
||||
return entity.toApiObject();
|
||||
return GameLib.Utils.IdOrNull(entity);
|
||||
}
|
||||
);
|
||||
|
||||
// var apiSystems = this.systems.map(
|
||||
// function (system) {
|
||||
// return system.toApiObject();
|
||||
// }
|
||||
// );
|
||||
|
||||
var apiEntityManager = new GameLib.API.EntityManager(
|
||||
this.id,
|
||||
this.name,
|
||||
apiEntities,
|
||||
// apiSystems,
|
||||
this.parentEntity
|
||||
GameLib.Utils.IdOrNull(this.parentEntity)
|
||||
);
|
||||
|
||||
return apiEntityManager;
|
||||
|
@ -448,7 +441,7 @@ GameLib.EntityManager.prototype.componentCreated = function(data) {
|
|||
/**
|
||||
* First add this to the 'idToObject'
|
||||
*/
|
||||
data.component.idToObject[id] = this.register[id];
|
||||
// data.component.idToObject[id] = this.register[id];
|
||||
|
||||
/**
|
||||
* Remove this dependency from the dependency list
|
||||
|
@ -493,7 +486,7 @@ GameLib.EntityManager.prototype.componentCreated = function(data) {
|
|||
} else {
|
||||
|
||||
/**
|
||||
* Otherwise, now - for each dependency - update 'idToObject' and check if its loaded
|
||||
* Otherwise, now - for each dependency - check if its loaded
|
||||
*/
|
||||
this.dependencies[data.component.id] = this.dependencies[data.component.id].reduce(
|
||||
function (result, component) {
|
||||
|
@ -501,7 +494,7 @@ GameLib.EntityManager.prototype.componentCreated = function(data) {
|
|||
/**
|
||||
* Link the object to the component
|
||||
*/
|
||||
component.idToObject[data.component.id] = data.component;
|
||||
// component.idToObject[data.component.id] = data.component;
|
||||
|
||||
/**
|
||||
* Remove the actual dependency
|
||||
|
|
|
@ -59,9 +59,28 @@ GameLib.Entity.prototype.createInstance = function() {
|
|||
* @param component
|
||||
*/
|
||||
GameLib.Entity.prototype.addComponent = function(component) {
|
||||
this.components.push(component);
|
||||
component.parentEntity = this;
|
||||
|
||||
this.components.push(component);
|
||||
|
||||
/**
|
||||
* Here we will dig into this component - find all its 'parentEntity' members - and update them accordingly
|
||||
*/
|
||||
component.buildIdToObject();
|
||||
|
||||
for (var property in component.idToObject) {
|
||||
if (component.idToObject.hasOwnProperty(property)) {
|
||||
if (component.idToObject[property] instanceof GameLib.Component) {
|
||||
this.components.push(component.idToObject[property]);
|
||||
component.idToObject[property].parentEntity = this;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Finally, we are the boss component - update my parent entity
|
||||
* @type {GameLib.Entity}
|
||||
*/
|
||||
component.parentEntity = this;
|
||||
|
||||
};
|
||||
|
||||
|
@ -128,6 +147,27 @@ GameLib.Entity.prototype.removeComponent = function(component) {
|
|||
component = this.activeComponent;
|
||||
}
|
||||
|
||||
/**
|
||||
* We also had added all the child components of this component, so we remove them first
|
||||
*/
|
||||
for (var property in component.idToObject) {
|
||||
if (component.idToObject.hasOwnProperty(property)) {
|
||||
if (component.idToObject[property] instanceof GameLib.Component) {
|
||||
if (component.idToObject[property].parentEntity === this) {
|
||||
var childIndex = this.components.indexOf(component.idToObject[property]);
|
||||
if (childIndex !== -1) {
|
||||
this.components.splice(childIndex, 1);
|
||||
component.idToObject[property].parentEntity = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Now we remove the boss entity
|
||||
* @type {null}
|
||||
*/
|
||||
component.parentEntity = null;
|
||||
|
||||
var index = this.components.indexOf(component);
|
||||
|
|
Loading…
Reference in New Issue