component remove

beta.r3js.org
-=yb4f310 2017-10-27 15:03:16 +02:00
parent f02f2968c7
commit 7b239ebc74
10 changed files with 171 additions and 150 deletions

View File

@ -47,14 +47,14 @@ GameLib.Event.COMPONENTS_LINKED = 0x1d;
GameLib.Event.UNRESOLVED_DEPENDENCIES_UPDATE = 0x1e; GameLib.Event.UNRESOLVED_DEPENDENCIES_UPDATE = 0x1e;
GameLib.Event.REGISTER_UPDATE = 0x1f; GameLib.Event.REGISTER_UPDATE = 0x1f;
GameLib.Event.BUILD_GUI = 0x20; GameLib.Event.BUILD_GUI = 0x20;
GameLib.Event.MESH_DELETED = 0x21; GameLib.Event.REMOVE_MESH = 0x21;
GameLib.Event.MESH_SELECTED = 0x22; GameLib.Event.MESH_SELECTED = 0x22;
GameLib.Event.MESH_DESELECTED = 0x23; GameLib.Event.MESH_DESELECTED = 0x23;
GameLib.Event.COMPONENT_REGISTER = 0x24; GameLib.Event.COMPONENT_REGISTER = 0x24;
GameLib.Event.IMAGE_NOT_FOUND = 0x25; GameLib.Event.IMAGE_NOT_FOUND = 0x25;
GameLib.Event.BLENDER_DATA_RECEIVED = 0x26; GameLib.Event.BLENDER_DATA_RECEIVED = 0x26;
GameLib.Event.IMAGE_UPLOAD_COMPLETE = 0x27; GameLib.Event.IMAGE_UPLOAD_COMPLETE = 0x27;
GameLib.Event.COMPONENT_REMOVE = 0x28; GameLib.Event.REMOVE_COMPONENT = 0x28;
GameLib.Event.KEY_DOWN = 0x29; GameLib.Event.KEY_DOWN = 0x29;
GameLib.Event.KEY_UP = 0x2a; GameLib.Event.KEY_UP = 0x2a;
GameLib.Event.RENDER = 0x2b; GameLib.Event.RENDER = 0x2b;
@ -149,14 +149,14 @@ GameLib.Event.GetEventName = function(number) {
case 0x1e : return 'unresolved_dependencies_update'; case 0x1e : return 'unresolved_dependencies_update';
case 0x1f : return 'register_update'; case 0x1f : return 'register_update';
case 0x20 : return 'build_gui'; case 0x20 : return 'build_gui';
case 0x21 : return 'mesh_deleted'; case 0x21 : return 'remove_mesh';
case 0x22 : return 'mesh_selected'; case 0x22 : return 'mesh_selected';
case 0x23 : return 'mesh_deselected'; case 0x23 : return 'mesh_deselected';
case 0x24 : return 'component_register'; case 0x24 : return 'component_register';
case 0x25 : return 'image_not_found'; case 0x25 : return 'image_not_found';
case 0x26 : return 'blender_data_received'; case 0x26 : return 'blender_data_received';
case 0x27 : return 'image_upload_complete'; case 0x27 : return 'image_upload_complete';
case 0x28 : return 'component_remove'; case 0x28 : return 'remove_component';
case 0x29 : return 'key_down'; case 0x29 : return 'key_down';
case 0x2a : return 'key_up'; case 0x2a : return 'key_up';
case 0x2b : return 'render'; 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 * We need to execute all the callbacks, but not execute them twice, but also keep in mind they can remove
* themselves during execution * themselves during execution
*/ */
var length = GameLib.Event.Subscriptions[eventName].length; // var length = GameLib.Event.Subscriptions[eventName].length;
//
for (var i = 0; i < length; i++) { // for (var i = 0; i < length; i++) {
//
var object = GameLib.Event.Subscriptions[eventName][i]; // var object = GameLib.Event.Subscriptions[eventName][i];
//
if (object.fn && object.executed === false) { // if (object.fn && object.executed === false) {
object.fn(data, clientCallback, clientErrorCallback); // object.fn(data, clientCallback, clientErrorCallback);
object.executed = true; // object.executed = true;
} // }
//
if (length !== GameLib.Event.Subscriptions[eventName].length) { // if (length !== GameLib.Event.Subscriptions[eventName].length) {
/** // /**
* this callback removed a subscription - reset i and reset the length // * this callback removed a subscription - reset i and reset the length
*/ // */
i = 0; // i = 0;
length = GameLib.Event.Subscriptions[eventName].length; // length = GameLib.Event.Subscriptions[eventName].length;
} // }
} // }
GameLib.Event.Subscriptions[eventName].map(
function(object){
object.executed = false;
}
)
// //
// GameLib.Event.Subscriptions[eventName].map( // GameLib.Event.Subscriptions[eventName].map(
// function(callback) { // function(object){
// if (callback) { // object.executed = false;
// callback(data, clientCallback, clientErrorCallback);
// count++;
// }
// } // }
// ) // )
GameLib.Event.Subscriptions[eventName].map(
function(callback) {
if (callback) {
callback(data, clientCallback, clientErrorCallback);
count++;
}
}
)
} else { } else {
if (clientCallback) { if (clientCallback) {
/** /**
@ -500,20 +500,20 @@ GameLib.Event.Subscribe = function(
) { ) {
if (GameLib.Event.Subscriptions.hasOwnProperty(eventName)) { if (GameLib.Event.Subscriptions.hasOwnProperty(eventName)) {
GameLib.Event.Subscriptions[eventName].push( GameLib.Event.Subscriptions[eventName].push(fn);
{ // {
fn : fn, // fn : fn,
executed : false // executed : false
} // }
); // );
} else { } else {
GameLib.Event.Subscriptions[eventName] = []; GameLib.Event.Subscriptions[eventName] = [];
GameLib.Event.Subscriptions[eventName].push( GameLib.Event.Subscriptions[eventName].push(fn);
{ // {
fn : fn, // fn : fn,
executed : false // executed : false
} // }
); // );
} }
/** /**
@ -523,15 +523,16 @@ GameLib.Event.Subscribe = function(
fn : fn, fn : fn,
remove : function() { remove : function() {
var index = GameLib.Event.Subscriptions[eventName].reduce( var index = GameLib.Event.Subscriptions[eventName].indexOf(fn);
function(result, object, index) { // reduce(
if (object.fn === fn) { // function(result, object, index) {
result = index; // if (object.fn === fn) {
} // result = index;
return result; // }
}, // return result;
-1 // },
); // -1
// );
if (index === -1) { if (index === -1) {
throw new Error('could not remove subscription'); throw new Error('could not remove subscription');

View File

@ -126,6 +126,14 @@ GameLib.Component.prototype.createInstance = function() {
GameLib.Event.EmitInstanceEvents(this); 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() { GameLib.Component.prototype.clone = function() {
var apiObject = this.toApiObject(); var apiObject = this.toApiObject();

View File

@ -5,12 +5,14 @@
* @param name * @param name
* @param entities GameLib.API.Entity[] * @param entities GameLib.API.Entity[]
* @param defaultEntity * @param defaultEntity
* @param defaultRenderer
*/ */
GameLib.API.EntityManager = function( GameLib.API.EntityManager = function(
id, id,
name, name,
entities, entities,
defaultEntity defaultEntity,
defaultRenderer
) { ) {
if (GameLib.Utils.UndefinedOrNull(id)) { if (GameLib.Utils.UndefinedOrNull(id)) {
id = GameLib.Utils.RandomId(); id = GameLib.Utils.RandomId();
@ -31,6 +33,11 @@ GameLib.API.EntityManager = function(
defaultEntity = null; defaultEntity = null;
} }
this.defaultEntity = defaultEntity; this.defaultEntity = defaultEntity;
if (GameLib.Utils.UndefinedOrNull(defaultRenderer)) {
defaultRenderer = null;
}
this.defaultRenderer = defaultRenderer;
}; };
GameLib.API.EntityManager.prototype = Object.create(GameLib.Component.prototype); GameLib.API.EntityManager.prototype = Object.create(GameLib.Component.prototype);
@ -53,6 +60,7 @@ GameLib.API.EntityManager.FromObject = function(objectEntityManager) {
objectEntityManager.id, objectEntityManager.id,
objectEntityManager.name, objectEntityManager.name,
apiEntities, apiEntities,
objectEntityManager.defaultEntity objectEntityManager.defaultEntity,
objectEntityManager.defaultRenderer
); );
}; };

View File

@ -1412,13 +1412,6 @@ GameLib.D3.Mesh.prototype.removeHelper = function() {
this.instance.material.polygonOffset = this.polygonOffset; this.instance.material.polygonOffset = this.polygonOffset;
GameLib.Event.Emit(
GameLib.Event.COMPONENT_REMOVE,
{
component : this.helper
}
);
this.helper = null; this.helper = null;
}; };

View File

@ -17,7 +17,8 @@ GameLib.EntityManager = function(apiEntityManager) {
apiEntityManager.id, apiEntityManager.id,
apiEntityManager.name, apiEntityManager.name,
apiEntityManager.entities, apiEntityManager.entities,
apiEntityManager.defaultEntity apiEntityManager.defaultEntity,
apiEntityManager.defaultRenderer
); );
/** /**
@ -33,7 +34,7 @@ GameLib.EntityManager = function(apiEntityManager) {
); );
GameLib.Event.Subscribe( GameLib.Event.Subscribe(
GameLib.Event.COMPONENT_REMOVE, GameLib.Event.REMOVE_COMPONENT,
this.removeComponent.bind(this) this.removeComponent.bind(this)
); );
@ -42,7 +43,8 @@ GameLib.EntityManager = function(apiEntityManager) {
GameLib.Component.COMPONENT_ENTITY_MANAGER, GameLib.Component.COMPONENT_ENTITY_MANAGER,
{ {
'entities' : [GameLib.Entity], '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) { 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); var index = this.register.indexOf(data.component);
if (index !== -1) { if (index !== -1) {
@ -259,10 +228,10 @@ GameLib.EntityManager.prototype.removeEntity = function(entity) {
console.log('failed to remove entity : ', entity); console.log('failed to remove entity : ', entity);
return false; return false;
} }
this.entities.splice(index, 1);
entity.parentEntityManager = null; entity.parentEntityManager = null;
this.entities.splice(index, 1);
return true; return true;
}; };
@ -347,7 +316,8 @@ GameLib.EntityManager.prototype.toApiObject = function() {
this.id, this.id,
this.name, this.name,
apiEntities, apiEntities,
GameLib.Utils.IdOrNull(this.defaultEntity) GameLib.Utils.IdOrNull(this.defaultEntity),
GameLib.Utils.IdOrNull(this.defaultRenderer)
); );
return apiEntityManager; return apiEntityManager;

View File

@ -160,26 +160,15 @@ GameLib.Entity.prototype.removeComponent = function(component) {
component = this.activeComponent; component = this.activeComponent;
} }
/** var childIndex = this.components.indexOf(component);
* 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) { if (childIndex !== -1) {
this.components.splice(childIndex, 1); this.components.splice(childIndex, 1);
component.idToObject[property].parentEntity = null; } else {
} console.error('component not found');
}
}
}
} }
/** /**
* Now we remove the boss entity * Break the dependency to the parent
* @type {null}
*/ */
component.parentEntity = null; component.parentEntity = null;

View File

@ -162,7 +162,7 @@ GameLib.System.GUI.prototype.start = function() {
); );
this.meshDeletedSubscription = this.subscribe( this.meshDeletedSubscription = this.subscribe(
GameLib.Event.MESH_DELETED, GameLib.Event.REMOVE_MESH,
this.meshDeleted this.meshDeleted
); );
@ -182,7 +182,7 @@ GameLib.System.GUI.prototype.start = function() {
); );
this.componentRemovedSubscription = this.subscribe( this.componentRemovedSubscription = this.subscribe(
GameLib.Event.COMPONENT_REMOVE, GameLib.Event.REMOVE_COMPONENT,
this.removeComponent this.removeComponent
) )
}; };

View File

@ -359,7 +359,7 @@ GameLib.System.Input.prototype.onKeyDown = function(event) {
); );
GameLib.Event.Emit( GameLib.Event.Emit(
GameLib.Event.MESH_DELETED, GameLib.Event.REMOVE_MESH,
{ {
meshes : deletedMeshes meshes : deletedMeshes
} }

View File

@ -28,6 +28,7 @@ GameLib.System.Linking = function(
*/ */
this.componentCreatedSubscription = null; this.componentCreatedSubscription = null;
this.registerDependenciesSubscription = null; this.registerDependenciesSubscription = null;
this.componentRemoveSubscription = null;
/** /**
* Parents * Parents
@ -54,7 +55,7 @@ GameLib.System.Linking = function(
/** /**
* Meshes * Meshes
*/ */
this.meshDeletedSubscription = null; this.removeMeshSubscription = null;
/** /**
* Images * Images
@ -92,6 +93,11 @@ GameLib.System.Linking.prototype.start = function() {
this.registerDependenciesDirect this.registerDependenciesDirect
); );
this.componentRemoveSubscription = this.subscribe(
GameLib.Event.REMOVE_COMPONENT,
this.removeComponent
);
/** /**
* Parents * Parents
*/ */
@ -172,9 +178,9 @@ GameLib.System.Linking.prototype.start = function() {
/** /**
* Meshes * Meshes
*/ */
this.meshDeletedSubscription = this.subscribe( this.removeMeshSubscription = this.subscribe(
GameLib.Event.MESH_DELETED, GameLib.Event.REMOVE_MESH,
this.meshDeleted this.removeMesh
); );
/** /**
@ -522,6 +528,38 @@ GameLib.System.Linking.prototype.registerDependenciesDirect = function(data) {
this.registerDependencies(data.component); 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) { GameLib.System.Linking.prototype.imageChanged = function(data) {
@ -867,6 +905,7 @@ GameLib.System.Linking.prototype.onParentWorldChange = function(data) {
data.object instanceof GameLib.D3.RigidBody data.object instanceof GameLib.D3.RigidBody
) { ) {
if (data.originalWorld instanceof GameLib.D3.PhysicsWorld) { if (data.originalWorld instanceof GameLib.D3.PhysicsWorld) {
data.originalWorld.removeRigidBody(data.object); 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 * children objects are in use by another object - if it is - don't delete it, otherwise, do
* @param data * @param data
*/ */
GameLib.System.Linking.prototype.meshDeleted = function(data) { GameLib.System.Linking.prototype.removeMesh = function(data) {
console.log('to delete');
/** /**
* First we get the list of all components we would like to delete * First we get the list of all components we would like to delete
@ -1006,18 +1043,10 @@ GameLib.System.Linking.prototype.meshDeleted = function(data) {
/** /**
* componentsToDelete should now be the final list of components to delete * componentsToDelete should now be the final list of components to delete
*/ */
componentsToDelete.map(function(component){ componentsToDelete.map(
GameLib.Event.Emit( function(component){
GameLib.Event.COMPONENT_REMOVE, component.remove();
{
component : component
} }
)
});
GameLib.Event.Emit(
GameLib.Event.BUILD_GUI,
null
); );
}; };
@ -1028,6 +1057,7 @@ GameLib.System.Linking.prototype.stop = function() {
*/ */
this.componentCreatedSubscription.remove(); this.componentCreatedSubscription.remove();
this.registerDependenciesSubscription.remove(); this.registerDependenciesSubscription.remove();
this.componentRemoveSubscription.remove();
/** /**
* Parents * Parents
@ -1054,7 +1084,7 @@ GameLib.System.Linking.prototype.stop = function() {
/** /**
* Meshes * Meshes
*/ */
this.meshDeletedSubscription.remove(); this.removeMeshSubscription.remove();
/** /**
* Images * Images

View File

@ -343,12 +343,7 @@ GameLib.System.Storage.prototype.loadComponent = function(apiUrl, toProcess, inc
var component = GameLib.EntityManager.Instance.findComponentById(id); var component = GameLib.EntityManager.Instance.findComponentById(id);
if (component) { if (component) {
GameLib.Event.Emit( component.remove();
GameLib.Event.COMPONENT_REMOVE,
{
component : component
}
);
} }
} }
); );