constructor fixes - auto add child components

beta.r3js.org
-=yb4f310 2017-06-19 14:53:52 +02:00
parent 22e0e8828d
commit 67588f1e27
4 changed files with 71 additions and 32 deletions

View File

@ -10,15 +10,27 @@ GameLib.API.EntityManager = function(
id, id,
name, name,
entities, entities,
// systems,
parentEntity 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)) { if (GameLib.Utils.UndefinedOrNull(entities)) {
// systems = []; entities = [];
// } }
// this.systems = systems; this.entities = entities;
if (GameLib.Utils.UndefinedOrNull(parentEntity)) {
parentEntity = null;
}
this.parentEntity = parentEntity;
}; };
GameLib.API.EntityManager.prototype = Object.create(GameLib.Component.prototype); 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( return new GameLib.API.EntityManager(
objectEntityManager.id, objectEntityManager.id,
objectEntityManager.name, objectEntityManager.name,
apiEntities, apiEntities,
// apiSystems,
objectEntityManager.parentEntity objectEntityManager.parentEntity
); );
}; };

View File

@ -139,6 +139,7 @@ GameLib.D3.Input.Editor.prototype.onKeyDown = function(entity, entityManager) {
console.log('entity ' + entity.name + ' emitted keypress ' + event.code); console.log('entity ' + entity.name + ' emitted keypress ' + event.code);
if (event.code === 'Delete') { if (event.code === 'Delete') {
var meshes = entity.getComponents(GameLib.D3.Mesh); var meshes = entity.getComponents(GameLib.D3.Mesh);
var gui = entity.getFirstComponent(GameLib.GUI); var gui = entity.getFirstComponent(GameLib.GUI);

View File

@ -240,22 +240,15 @@ GameLib.EntityManager.prototype.toApiObject = function() {
var apiEntities = this.entities.map( var apiEntities = this.entities.map(
function (entity) { 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( var apiEntityManager = new GameLib.API.EntityManager(
this.id, this.id,
this.name, this.name,
apiEntities, apiEntities,
// apiSystems, GameLib.Utils.IdOrNull(this.parentEntity)
this.parentEntity
); );
return apiEntityManager; return apiEntityManager;
@ -448,7 +441,7 @@ GameLib.EntityManager.prototype.componentCreated = function(data) {
/** /**
* First add this to the 'idToObject' * 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 * Remove this dependency from the dependency list
@ -493,7 +486,7 @@ GameLib.EntityManager.prototype.componentCreated = function(data) {
} else { } 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( this.dependencies[data.component.id] = this.dependencies[data.component.id].reduce(
function (result, component) { function (result, component) {
@ -501,7 +494,7 @@ GameLib.EntityManager.prototype.componentCreated = function(data) {
/** /**
* Link the object to the component * Link the object to the component
*/ */
component.idToObject[data.component.id] = data.component; // component.idToObject[data.component.id] = data.component;
/** /**
* Remove the actual dependency * Remove the actual dependency

View File

@ -59,9 +59,28 @@ GameLib.Entity.prototype.createInstance = function() {
* @param component * @param component
*/ */
GameLib.Entity.prototype.addComponent = function(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; 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; component.parentEntity = null;
var index = this.components.indexOf(component); var index = this.components.indexOf(component);