diff --git a/src/game-lib-a-1-event.js b/src/game-lib-a-1-event.js index 2968167..659a081 100644 --- a/src/game-lib-a-1-event.js +++ b/src/game-lib-a-1-event.js @@ -47,14 +47,14 @@ GameLib.Event.COMPONENTS_LINKED = 0x1d; GameLib.Event.UNRESOLVED_DEPENDENCIES_UPDATE = 0x1e; GameLib.Event.REGISTER_UPDATE = 0x1f; GameLib.Event.BUILD_GUI = 0x20; -GameLib.Event.MESH_DELETED = 0x21; +GameLib.Event.REMOVE_MESH = 0x21; GameLib.Event.MESH_SELECTED = 0x22; GameLib.Event.MESH_DESELECTED = 0x23; GameLib.Event.COMPONENT_REGISTER = 0x24; GameLib.Event.IMAGE_NOT_FOUND = 0x25; GameLib.Event.BLENDER_DATA_RECEIVED = 0x26; GameLib.Event.IMAGE_UPLOAD_COMPLETE = 0x27; -GameLib.Event.COMPONENT_REMOVE = 0x28; +GameLib.Event.REMOVE_COMPONENT = 0x28; GameLib.Event.KEY_DOWN = 0x29; GameLib.Event.KEY_UP = 0x2a; GameLib.Event.RENDER = 0x2b; @@ -149,14 +149,14 @@ GameLib.Event.GetEventName = function(number) { case 0x1e : return 'unresolved_dependencies_update'; case 0x1f : return 'register_update'; case 0x20 : return 'build_gui'; - case 0x21 : return 'mesh_deleted'; + case 0x21 : return 'remove_mesh'; case 0x22 : return 'mesh_selected'; case 0x23 : return 'mesh_deselected'; case 0x24 : return 'component_register'; case 0x25 : return 'image_not_found'; case 0x26 : return 'blender_data_received'; case 0x27 : return 'image_upload_complete'; - case 0x28 : return 'component_remove'; + case 0x28 : return 'remove_component'; case 0x29 : return 'key_down'; case 0x2a : return 'key_up'; case 0x2b : return 'render'; @@ -442,40 +442,40 @@ GameLib.Event.Emit = function( * We need to execute all the callbacks, but not execute them twice, but also keep in mind they can remove * themselves during execution */ - var length = GameLib.Event.Subscriptions[eventName].length; - - for (var i = 0; i < length; i++) { - - var object = GameLib.Event.Subscriptions[eventName][i]; - - if (object.fn && object.executed === false) { - object.fn(data, clientCallback, clientErrorCallback); - object.executed = true; - } - - if (length !== GameLib.Event.Subscriptions[eventName].length) { - /** - * this callback removed a subscription - reset i and reset the length - */ - i = 0; - length = GameLib.Event.Subscriptions[eventName].length; - } - } - - GameLib.Event.Subscriptions[eventName].map( - function(object){ - object.executed = false; - } - ) + // var length = GameLib.Event.Subscriptions[eventName].length; + // + // for (var i = 0; i < length; i++) { + // + // var object = GameLib.Event.Subscriptions[eventName][i]; + // + // if (object.fn && object.executed === false) { + // object.fn(data, clientCallback, clientErrorCallback); + // object.executed = true; + // } + // + // if (length !== GameLib.Event.Subscriptions[eventName].length) { + // /** + // * this callback removed a subscription - reset i and reset the length + // */ + // i = 0; + // length = GameLib.Event.Subscriptions[eventName].length; + // } + // } // // GameLib.Event.Subscriptions[eventName].map( - // function(callback) { - // if (callback) { - // callback(data, clientCallback, clientErrorCallback); - // count++; - // } + // function(object){ + // object.executed = false; // } // ) + + GameLib.Event.Subscriptions[eventName].map( + function(callback) { + if (callback) { + callback(data, clientCallback, clientErrorCallback); + count++; + } + } + ) } else { if (clientCallback) { /** @@ -500,20 +500,20 @@ GameLib.Event.Subscribe = function( ) { if (GameLib.Event.Subscriptions.hasOwnProperty(eventName)) { - GameLib.Event.Subscriptions[eventName].push( - { - fn : fn, - executed : false - } - ); + GameLib.Event.Subscriptions[eventName].push(fn); + // { + // fn : fn, + // executed : false + // } + // ); } else { GameLib.Event.Subscriptions[eventName] = []; - GameLib.Event.Subscriptions[eventName].push( - { - fn : fn, - executed : false - } - ); + GameLib.Event.Subscriptions[eventName].push(fn); + // { + // fn : fn, + // executed : false + // } + // ); } /** @@ -523,15 +523,16 @@ GameLib.Event.Subscribe = function( fn : fn, remove : function() { - var index = GameLib.Event.Subscriptions[eventName].reduce( - function(result, object, index) { - if (object.fn === fn) { - result = index; - } - return result; - }, - -1 - ); + var index = GameLib.Event.Subscriptions[eventName].indexOf(fn); + // reduce( + // function(result, object, index) { + // if (object.fn === fn) { + // result = index; + // } + // return result; + // }, + // -1 + // ); if (index === -1) { throw new Error('could not remove subscription'); diff --git a/src/game-lib-a-component-a.js b/src/game-lib-a-component-a.js index f3bee44..7586488 100644 --- a/src/game-lib-a-component-a.js +++ b/src/game-lib-a-component-a.js @@ -126,6 +126,14 @@ GameLib.Component.prototype.createInstance = function() { GameLib.Event.EmitInstanceEvents(this); } + if (this instanceof GameLib.Entity) { + GameLib.Event.Emit( + GameLib.Event.ENTITY_LOADED, + { + entity:this + } + ) + } }; /** @@ -437,6 +445,33 @@ GameLib.Component.prototype.generateNewIds = function() { }; +GameLib.Component.prototype.remove = function() { + + this.buildIdToObject(); + + var dependencies = this.getDependencies(); + + dependencies.map( + function(objectId) { + + var childComponent = this.idToObject[objectId]; + + if (childComponent instanceof GameLib.Component) { + childComponent.remove(); + } + + }.bind(this) + ); + + GameLib.Event.Emit( + GameLib.Event.REMOVE_COMPONENT, + { + component : this + } + ) + +}; + GameLib.Component.prototype.clone = function() { var apiObject = this.toApiObject(); diff --git a/src/game-lib-api-entity-manager.js b/src/game-lib-api-entity-manager.js index f352208..8f22978 100644 --- a/src/game-lib-api-entity-manager.js +++ b/src/game-lib-api-entity-manager.js @@ -5,12 +5,14 @@ * @param name * @param entities GameLib.API.Entity[] * @param defaultEntity + * @param defaultRenderer */ GameLib.API.EntityManager = function( id, name, entities, - defaultEntity + defaultEntity, + defaultRenderer ) { if (GameLib.Utils.UndefinedOrNull(id)) { id = GameLib.Utils.RandomId(); @@ -31,6 +33,11 @@ GameLib.API.EntityManager = function( defaultEntity = null; } this.defaultEntity = defaultEntity; + + if (GameLib.Utils.UndefinedOrNull(defaultRenderer)) { + defaultRenderer = null; + } + this.defaultRenderer = defaultRenderer; }; GameLib.API.EntityManager.prototype = Object.create(GameLib.Component.prototype); @@ -53,6 +60,7 @@ GameLib.API.EntityManager.FromObject = function(objectEntityManager) { objectEntityManager.id, objectEntityManager.name, apiEntities, - objectEntityManager.defaultEntity + objectEntityManager.defaultEntity, + objectEntityManager.defaultRenderer ); }; diff --git a/src/game-lib-d3-mesh-0.js b/src/game-lib-d3-mesh-0.js index 5b8ebc0..bbae1e2 100644 --- a/src/game-lib-d3-mesh-0.js +++ b/src/game-lib-d3-mesh-0.js @@ -1412,13 +1412,6 @@ GameLib.D3.Mesh.prototype.removeHelper = function() { this.instance.material.polygonOffset = this.polygonOffset; - GameLib.Event.Emit( - GameLib.Event.COMPONENT_REMOVE, - { - component : this.helper - } - ); - this.helper = null; }; diff --git a/src/game-lib-entity-manager.js b/src/game-lib-entity-manager.js index cacf53f..de496c9 100644 --- a/src/game-lib-entity-manager.js +++ b/src/game-lib-entity-manager.js @@ -17,7 +17,8 @@ GameLib.EntityManager = function(apiEntityManager) { apiEntityManager.id, apiEntityManager.name, apiEntityManager.entities, - apiEntityManager.defaultEntity + apiEntityManager.defaultEntity, + apiEntityManager.defaultRenderer ); /** @@ -33,7 +34,7 @@ GameLib.EntityManager = function(apiEntityManager) { ); GameLib.Event.Subscribe( - GameLib.Event.COMPONENT_REMOVE, + GameLib.Event.REMOVE_COMPONENT, this.removeComponent.bind(this) ); @@ -42,7 +43,8 @@ GameLib.EntityManager = function(apiEntityManager) { GameLib.Component.COMPONENT_ENTITY_MANAGER, { 'entities' : [GameLib.Entity], - 'defaultEntity' : GameLib.Entity + 'defaultEntity' : GameLib.Entity, + 'defaultRenderer' : GameLib.D3.Renderer } ); }; @@ -73,39 +75,6 @@ GameLib.EntityManager.prototype.registerComponent = function(data) { GameLib.EntityManager.prototype.removeComponent = function(data) { - // if (data.component instanceof GameLib.Entity) { - // - // /** - // * if this is an entity - remove all its children components first - // */ - // data.component.components.map( - // function(component) { - // var index = this.register.indexOf(component); - // - // if (index !== -1) { - // - // this.register.splice(index, 1); - // - // GameLib.Event.Emit( - // GameLib.Event.REGISTER_UPDATE, - // { - // register : this.register - // } - // ); - // } - // }.bind(this) - // ) - // } - - if (data.component.parentEntity instanceof GameLib.Entity) { - data.component.parentEntity.removeComponent(data.component); - } - - if (data.component instanceof GameLib.D3.Mesh && - data.component.parentScene instanceof GameLib.D3.Scene) { - data.component.parentScene.removeObject(data.component); - } - var index = this.register.indexOf(data.component); if (index !== -1) { @@ -259,10 +228,10 @@ GameLib.EntityManager.prototype.removeEntity = function(entity) { console.log('failed to remove entity : ', entity); return false; } + this.entities.splice(index, 1); entity.parentEntityManager = null; - this.entities.splice(index, 1); return true; }; @@ -347,7 +316,8 @@ GameLib.EntityManager.prototype.toApiObject = function() { this.id, this.name, apiEntities, - GameLib.Utils.IdOrNull(this.defaultEntity) + GameLib.Utils.IdOrNull(this.defaultEntity), + GameLib.Utils.IdOrNull(this.defaultRenderer) ); return apiEntityManager; diff --git a/src/game-lib-entity.js b/src/game-lib-entity.js index b130658..06e813c 100644 --- a/src/game-lib-entity.js +++ b/src/game-lib-entity.js @@ -160,26 +160,15 @@ 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; - } - } - } - } - } + var childIndex = this.components.indexOf(component); + if (childIndex !== -1) { + this.components.splice(childIndex, 1); + } else { + console.error('component not found'); + } /** - * Now we remove the boss entity - * @type {null} + * Break the dependency to the parent */ component.parentEntity = null; diff --git a/src/game-lib-system-gui.js b/src/game-lib-system-gui.js index 18d7ea3..c8fc7f2 100644 --- a/src/game-lib-system-gui.js +++ b/src/game-lib-system-gui.js @@ -162,7 +162,7 @@ GameLib.System.GUI.prototype.start = function() { ); this.meshDeletedSubscription = this.subscribe( - GameLib.Event.MESH_DELETED, + GameLib.Event.REMOVE_MESH, this.meshDeleted ); @@ -182,7 +182,7 @@ GameLib.System.GUI.prototype.start = function() { ); this.componentRemovedSubscription = this.subscribe( - GameLib.Event.COMPONENT_REMOVE, + GameLib.Event.REMOVE_COMPONENT, this.removeComponent ) }; diff --git a/src/game-lib-system-input.js b/src/game-lib-system-input.js index c6e580b..46cef8f 100644 --- a/src/game-lib-system-input.js +++ b/src/game-lib-system-input.js @@ -359,7 +359,7 @@ GameLib.System.Input.prototype.onKeyDown = function(event) { ); GameLib.Event.Emit( - GameLib.Event.MESH_DELETED, + GameLib.Event.REMOVE_MESH, { meshes : deletedMeshes } diff --git a/src/game-lib-system-linking.js b/src/game-lib-system-linking.js index b5cdf17..7b3def0 100644 --- a/src/game-lib-system-linking.js +++ b/src/game-lib-system-linking.js @@ -28,6 +28,7 @@ GameLib.System.Linking = function( */ this.componentCreatedSubscription = null; this.registerDependenciesSubscription = null; + this.componentRemoveSubscription = null; /** * Parents @@ -54,7 +55,7 @@ GameLib.System.Linking = function( /** * Meshes */ - this.meshDeletedSubscription = null; + this.removeMeshSubscription = null; /** * Images @@ -92,6 +93,11 @@ GameLib.System.Linking.prototype.start = function() { this.registerDependenciesDirect ); + this.componentRemoveSubscription = this.subscribe( + GameLib.Event.REMOVE_COMPONENT, + this.removeComponent + ); + /** * Parents */ @@ -172,9 +178,9 @@ GameLib.System.Linking.prototype.start = function() { /** * Meshes */ - this.meshDeletedSubscription = this.subscribe( - GameLib.Event.MESH_DELETED, - this.meshDeleted + this.removeMeshSubscription = this.subscribe( + GameLib.Event.REMOVE_MESH, + this.removeMesh ); /** @@ -522,6 +528,38 @@ GameLib.System.Linking.prototype.registerDependenciesDirect = function(data) { this.registerDependencies(data.component); }; +GameLib.System.Linking.prototype.removeComponent = function(data) { + + if (!data.component) { + console.error('no component to remove'); + return; + } + + var component = data.component; + + if (component.parentEntity instanceof GameLib.Entity) { + component.parentEntity.removeComponent(component); + } + + if (component instanceof GameLib.D3.Mesh && + component.parentScene instanceof GameLib.D3.Scene) { + + component.removeHelper(); + + component.parentScene.removeObject(component); + } + + if (component instanceof GameLib.D3.Light && + component.parentScene instanceof GameLib.D3.Scene) { + component.parentScene.removeObject(component); + } + + if (component instanceof GameLib.Entity) { + GameLib.EntityManager.Instance.removeEntity(component); + } +}; + + GameLib.System.Linking.prototype.imageChanged = function(data) { @@ -867,6 +905,7 @@ GameLib.System.Linking.prototype.onParentWorldChange = function(data) { data.object instanceof GameLib.D3.RigidBody ) { + if (data.originalWorld instanceof GameLib.D3.PhysicsWorld) { data.originalWorld.removeRigidBody(data.object); } @@ -944,9 +983,7 @@ GameLib.System.Linking.prototype.onParentEntityChange = function(data) { * children objects are in use by another object - if it is - don't delete it, otherwise, do * @param data */ -GameLib.System.Linking.prototype.meshDeleted = function(data) { - - console.log('to delete'); +GameLib.System.Linking.prototype.removeMesh = function(data) { /** * First we get the list of all components we would like to delete @@ -1006,19 +1043,11 @@ GameLib.System.Linking.prototype.meshDeleted = function(data) { /** * componentsToDelete should now be the final list of components to delete */ - componentsToDelete.map(function(component){ - GameLib.Event.Emit( - GameLib.Event.COMPONENT_REMOVE, - { - component : component - } - ) - }); - - GameLib.Event.Emit( - GameLib.Event.BUILD_GUI, - null - ); + componentsToDelete.map( + function(component){ + component.remove(); + } + ); }; GameLib.System.Linking.prototype.stop = function() { @@ -1028,6 +1057,7 @@ GameLib.System.Linking.prototype.stop = function() { */ this.componentCreatedSubscription.remove(); this.registerDependenciesSubscription.remove(); + this.componentRemoveSubscription.remove(); /** * Parents @@ -1054,7 +1084,7 @@ GameLib.System.Linking.prototype.stop = function() { /** * Meshes */ - this.meshDeletedSubscription.remove(); + this.removeMeshSubscription.remove(); /** * Images diff --git a/src/game-lib-system-storage.js b/src/game-lib-system-storage.js index a07144e..6fb5f79 100644 --- a/src/game-lib-system-storage.js +++ b/src/game-lib-system-storage.js @@ -343,12 +343,7 @@ GameLib.System.Storage.prototype.loadComponent = function(apiUrl, toProcess, inc var component = GameLib.EntityManager.Instance.findComponentById(id); if (component) { - GameLib.Event.Emit( - GameLib.Event.COMPONENT_REMOVE, - { - component : component - } - ); + component.remove(); } } );