diff --git a/src/game-lib-api-entity-manager.js b/src/game-lib-api-entity-manager.js index 440d21c..66a7c60 100644 --- a/src/game-lib-api-entity-manager.js +++ b/src/game-lib-api-entity-manager.js @@ -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 ); }; diff --git a/src/game-lib-d3-input-editor.js b/src/game-lib-d3-input-editor.js index 81c8a39..a91c373 100644 --- a/src/game-lib-d3-input-editor.js +++ b/src/game-lib-d3-input-editor.js @@ -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); diff --git a/src/game-lib-entity-manager.js b/src/game-lib-entity-manager.js index 84ece06..a72bc85 100644 --- a/src/game-lib-entity-manager.js +++ b/src/game-lib-entity-manager.js @@ -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 diff --git a/src/game-lib-entity.js b/src/game-lib-entity.js index 0d0dbf2..073710b 100644 --- a/src/game-lib-entity.js +++ b/src/game-lib-entity.js @@ -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; }; @@ -124,11 +143,32 @@ GameLib.Entity.prototype.hasComponent = function(constructor) { */ GameLib.Entity.prototype.removeComponent = function(component) { - if (GameLib.Utils.UndefinedOrNull(component)) { - component = this.activeComponent; - } + if (GameLib.Utils.UndefinedOrNull(component)) { + component = this.activeComponent; + } - component.parentEntity = null; + /** + * 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); @@ -137,7 +177,7 @@ GameLib.Entity.prototype.removeComponent = function(component) { return false; } - this.components.splice(index, 1); + this.components.splice(index, 1); return true; };