From b6bb9cb9a71a968e504ca7faf7a87e2ac43cf8c6 Mon Sep 17 00:00:00 2001 From: -=yb4f310 Date: Wed, 8 Nov 2017 20:22:36 +0100 Subject: [PATCH] cleanup linking system --- src/game-lib-a-1-event.js | 161 ++---------- src/game-lib-a-component-a.js | 39 ++- src/game-lib-d3-particle.js | 2 +- src/game-lib-d3-scene.js | 28 ++ src/game-lib-system-linking.js | 451 ++++++++++----------------------- src/game-lib-system-storage.js | 8 +- 6 files changed, 225 insertions(+), 464 deletions(-) diff --git a/src/game-lib-a-1-event.js b/src/game-lib-a-1-event.js index 34b824c..7f95764 100644 --- a/src/game-lib-a-1-event.js +++ b/src/game-lib-a-1-event.js @@ -18,7 +18,7 @@ GameLib.Event.OnceSubscriptions = {}; GameLib.Event.WINDOW_RESIZE = 0x1; GameLib.Event.PARENT_SCENE_CHANGE = 0x2; GameLib.Event.PARENT_ENTITY_CHANGE = 0x3; -GameLib.Event.IMAGE_INSTANCE_CREATED = 0x4; +GameLib.Event.INSTANCE_CLONED = 0x4; GameLib.Event.LOAD_IMAGE = 0x5; GameLib.Event.NEW_ENTITY = 0x6; GameLib.Event.MATERIAL_TYPE_CHANGED = 0x7; @@ -29,17 +29,17 @@ GameLib.Event.LOAD_COMPONENT = 0xb; GameLib.Event.LOAD_COMPONENT_ERROR = 0xc; GameLib.Event.LOGGED_IN = 0xd; GameLib.Event.COMPONENT_CREATED = 0xe; -GameLib.Event.SCENE_INSTANCE_CREATED = 0xf; -GameLib.Event.SCENE_OBJECT_INSTANCE_CREATED = 0x10; -GameLib.Event.PHYSICS_WORLD_INSTANCE_CREATED = 0x11; -GameLib.Event.RIGID_BODY_INSTANCE_CREATED = 0x12; -GameLib.Event.TEXTURE_INSTANCE_CREATED = 0x13; +GameLib.Event.COMPONENT_CLONED = 0xf; +//GameLib.Event.SCENE_OBJECT_INSTANCE_CREATED = 0x10; +//GameLib.Event.PHYSICS_WORLD_INSTANCE_CREATED = 0x11; +//GameLib.Event.RIGID_BODY_INSTANCE_CREATED = 0x12; +//GameLib.Event.TEXTURE_INSTANCE_CREATED = 0x13; GameLib.Event.TEXTURE_INSTANCE_UPDATED = 0x14; -GameLib.Event.MATERIAL_INSTANCE_CREATED = 0x15; +//GameLib.Event.MATERIAL_INSTANCE_CREATED = 0x15; GameLib.Event.MATERIAL_INSTANCE_UPDATED = 0x16; -GameLib.Event.MESH_INSTANCE_CREATED = 0x17; +//GameLib.Event.MESH_INSTANCE_CREATED = 0x17; GameLib.Event.MESH_INSTANCE_UPDATED = 0x18; -GameLib.Event.LIGHT_INSTANCE_CREATED = 0x19; +//GameLib.Event.LIGHT_INSTANCE_CREATED = 0x19; GameLib.Event.LIGHT_INSTANCE_UPDATED = 0x1a; GameLib.Event.DELETE_COMPONENT = 0x1b; GameLib.Event.COMPONENT_DOWNLOAD_COMPLETE = 0x1c; @@ -67,9 +67,9 @@ GameLib.Event.MATERIAL_TEXTURES_UPDATED = 0x31; GameLib.Event.DELETE_COMPONENT_ERROR = 0x32; GameLib.Event.COMPONENT_DELETED = 0x33; GameLib.Event.COMPONENT_TYPES_UPDATED = 0x34; -GameLib.Event.SHAPE_INSTANCE_CREATED = 0x35; +//GameLib.Event.SHAPE_INSTANCE_CREATED = 0x35; GameLib.Event.COMPONENT_LINKED = 0x36; -GameLib.Event.SOLVER_INSTANCE_CREATED = 0x37; +//GameLib.Event.SOLVER_INSTANCE_CREATED = 0x37; GameLib.Event.BEFORE_RENDER = 0x38; GameLib.Event.AFTER_RENDER = 0x39; GameLib.Event.ARRAY_ITEM_ADDED = 0x3a; @@ -101,7 +101,7 @@ GameLib.Event.GET_SCENE = 0x53; GameLib.Event.CUSTOM_CODE_WINDOW_RESIZE = 0x54; GameLib.Event.LOAD_FONT = 0x55; GameLib.Event.FONT_NOT_FOUND = 0x56; -GameLib.Event.FONT_INSTANCE_CREATED = 0x57; +//GameLib.Event.FONT_INSTANCE_CREATED = 0x57; GameLib.Event.REGISTER_DEPENDENCIES = 0x58; GameLib.Event.GAME_LOADED = 0x59; GameLib.Event.GAME_RESTART = 0x5a; @@ -125,7 +125,7 @@ GameLib.Event.GetEventName = function(number) { case 0x1 : return 'window_resize'; case 0x2 : return 'parent_scene_change'; case 0x3 : return 'parent_entity_change'; - case 0x4 : return 'image_instance_created'; + case 0x4 : return 'instance_cloned'; case 0x5 : return 'load_image'; case 0x6 : return 'new_entity'; case 0x7 : return 'material_type_changed'; @@ -136,17 +136,17 @@ GameLib.Event.GetEventName = function(number) { case 0xc : return 'load_component_error'; case 0xd : return 'logged_in'; case 0xe : return 'component_created'; - case 0xf : return 'scene_instance_created'; - case 0x10 : return 'scene_object_instance_created'; - case 0x11 : return 'world_instance_created'; - case 0x12 : return 'rigid_body_instance_created'; - case 0x13 : return 'texture_instance_created'; + case 0xf : return 'component_cloned'; + case 0x10 : return 'unused';//'scene_object_instance_created'; + case 0x11 : return 'unused';//'world_instance_created'; + case 0x12 : return 'unused';//'rigid_body_instance_created'; + case 0x13 : return 'unused';//'texture_instance_created'; case 0x14 : return 'texture_instance_updated'; - case 0x15 : return 'material_instance_created'; + case 0x15 : return 'unused';//'material_instance_created'; case 0x16 : return 'material_instance_updated'; - case 0x17 : return 'mesh_instance_created'; + case 0x17 : return 'unused';//'mesh_instance_created'; case 0x18 : return 'mesh_instance_updated'; - case 0x19 : return 'light_instance_created'; + case 0x19 : return 'unused';//'light_instance_created'; case 0x1a : return 'light_instance_updated'; case 0x1b : return 'delete_component'; case 0x1c : return 'component_download_complete'; @@ -174,9 +174,9 @@ GameLib.Event.GetEventName = function(number) { case 0x32 : return 'delete_component_error'; case 0x33 : return 'component_deleted'; case 0x34 : return 'component_types_updated'; - case 0x35 : return 'shape_instance_created'; + case 0x35 : return 'unused';//'shape_instance_created'; case 0x36 : return 'component_linked'; - case 0x37 : return 'solver_instance_created'; + case 0x37 : return 'unused';//'solver_instance_created'; case 0x38 : return 'before_render'; case 0x39 : return 'after_render'; case 0x3a : return 'array_item_added'; @@ -208,7 +208,7 @@ GameLib.Event.GetEventName = function(number) { case 0x54 : return 'custom_code_window_resize'; case 0x55 : return 'load_font'; case 0x56 : return 'font_not_found'; - case 0x57 : return 'font_instance_created'; + case 0x57 : return 'unused';//'font_instance_created'; case 0x58 : return 'register_dependencies'; case 0x59 : return 'game_loaded'; case 0x5a : return 'game_restart'; @@ -237,119 +237,6 @@ GameLib.Event.prototype.subscribe = function( return GameLib.Event.Subscribe(eventName, callback.bind(this)); }; -GameLib.Event.EmitInstanceEvents = function(component) { - - if ( - component instanceof GameLib.D3.Mesh - ) { - GameLib.Event.Emit( - GameLib.Event.MESH_INSTANCE_CREATED, - { - mesh: component - } - ); - - return; - } - - if ( - component instanceof GameLib.D3.Light - ) { - GameLib.Event.Emit( - GameLib.Event.LIGHT_INSTANCE_CREATED, - { - light: component - } - ); - - return; - } - - if ( - component instanceof GameLib.D3.Scene - ) { - GameLib.Event.Emit( - GameLib.Event.SCENE_INSTANCE_CREATED, - { - scene: component - } - ); - - return; - } - - if ( - component instanceof GameLib.D3.Material - ) { - GameLib.Event.Emit( - GameLib.Event.MATERIAL_INSTANCE_CREATED, - { - material: component - } - ); - - return; - } - - if ( - component instanceof GameLib.D3.Texture - ) { - GameLib.Event.Emit( - GameLib.Event.TEXTURE_INSTANCE_CREATED, - { - texture: component - } - ); - - return; - } - - if ( - component instanceof GameLib.D3.Image - ) { - GameLib.Event.Emit( - GameLib.Event.IMAGE_INSTANCE_CREATED, - { - image: component - } - ); - - return; - } - - if ( - component instanceof GameLib.D3.Shape - ) { - GameLib.Event.Emit( - GameLib.Event.SHAPE_INSTANCE_CREATED, - { - shape: component - } - ); - - return; - } - - if ( - component instanceof GameLib.D3.PhysicsWorld - ) { - GameLib.Event.Emit( - GameLib.Event.PHYSICS_WORLD_INSTANCE_CREATED, - { - world: component - } - ); - - return; - } - - GameLib.Event.Emit( - GameLib.Event.INSTANCE_CREATED, - { - component: component - } - ) -}; // /** // * Stop listening for this event after the callback returns true diff --git a/src/game-lib-a-component-a.js b/src/game-lib-a-component-a.js index 503cf4c..f0914ca 100644 --- a/src/game-lib-a-component-a.js +++ b/src/game-lib-a-component-a.js @@ -141,7 +141,12 @@ GameLib.Component.prototype.createInstance = function() { this.loaded = true; - GameLib.Event.EmitInstanceEvents(this); + GameLib.Event.Emit( + GameLib.Event.INSTANCE_CREATED, + { + component: this + } + ) } if (this instanceof GameLib.Entity) { @@ -552,12 +557,10 @@ GameLib.Component.prototype.clone = function() { } } - /** - * TODO check below event emit - */ GameLib.Event.Emit( - GameLib.Event.COMPONENT_CREATED, + GameLib.Event.COMPONENT_CLONED, { + parent : this, component : runtimeObject } ); @@ -572,6 +575,32 @@ GameLib.Component.prototype.clone = function() { }; +/** + * Clones only the instance + */ +GameLib.Component.prototype.cloneInstance = function() { + + var clone = null; + + if ( + this.instance && + this.instance.clone && + typeof (this.instance.clone === 'function')) { + + clone = this.instance.clone(); + + GameLib.Event.Emit( + GameLib.Event.INSTANCE_CLONED, + { + component : this, + instance : clone + } + ) + } + + return clone; +}; + GameLib.Component.prototype.getStorageDependencies = function() { var dependencies = {}; diff --git a/src/game-lib-d3-particle.js b/src/game-lib-d3-particle.js index aef19d7..4fecbd0e 100644 --- a/src/game-lib-d3-particle.js +++ b/src/game-lib-d3-particle.js @@ -166,7 +166,7 @@ GameLib.D3.Particle.prototype.clone = function(camera) { instance.lookAt(direction); - instance.userData.direction = this.direction.instance; + instance.userData.direction = this.direction.instance.clone(); instance.userData.elapsed = 0; instance.userData.lifeTime = this.lifeTime; this.mesh.parentScene.instance.add(instance); diff --git a/src/game-lib-d3-scene.js b/src/game-lib-d3-scene.js index 9abde02..cf5ce72 100644 --- a/src/game-lib-d3-scene.js +++ b/src/game-lib-d3-scene.js @@ -145,6 +145,8 @@ GameLib.D3.Scene = function ( */ this.helpers = []; + this.clones = []; + GameLib.Component.call( this, GameLib.Component.COMPONENT_SCENE, @@ -393,7 +395,19 @@ GameLib.D3.Scene.prototype.addObject = function(object) { } } +}; +GameLib.D3.Scene.prototype.addClone = function(component) { + + if (component instanceof GameLib.D3.Mesh || + component instanceof GameLib.D3.Light + ) { + if (this.instance && component.instance) { + this.instance.add(component.instance); + } + + GameLib.Utils.PushUnique(this.clones, component); + } }; /** @@ -405,15 +419,29 @@ GameLib.D3.Scene.prototype.removeObject = function(object) { var index = -1; if (object instanceof GameLib.D3.Mesh) { + index = this.meshes.indexOf(object); if (index !== -1) { this.meshes.splice(index, 1); } + + index = this.clones.indexOf(object); + if (index !== -1) { + this.clones.splice(index, 1); + } + } else if (object instanceof GameLib.D3.Light) { + index = this.lights.indexOf(object); if (index !== -1) { this.lights.splice(index, 1); } + + index = this.clones.indexOf(object); + if (index !== -1) { + this.clones.splice(index, 1); + } + } else { console.warn('Cannot remove this object - what is this ?' + object.toString()); return; diff --git a/src/game-lib-system-linking.js b/src/game-lib-system-linking.js index 597078b..cf398d4 100644 --- a/src/game-lib-system-linking.js +++ b/src/game-lib-system-linking.js @@ -27,6 +27,7 @@ GameLib.System.Linking = function( * Components */ this.componentCreatedSubscription = null; + this.componentClonedSubscription = null; this.registerDependenciesSubscription = null; this.componentRemoveSubscription = null; @@ -41,16 +42,7 @@ GameLib.System.Linking = function( * Instances */ this.instanceCreatedSubscription = null; - this.meshInstanceCreatedSubscription = null; - this.lightInstanceCreatedSubscription = null; - this.sceneInstanceCreatedSubscription = null; - this.imageInstanceCreatedSubscription = null; - this.fontInstanceCreatedSubscription = null; - this.textureInstanceCreatedSubscription = null; - this.materialInstanceCreatedSubscription = null; - this.shapeInstanceCreatedSubscription = null; - this.solverInstanceCreatedSubscription = null; - this.physicsWorldInstanceCreatedSubscription = null; + this.instanceClonedSubscription = null; /** * Meshes @@ -88,6 +80,11 @@ GameLib.System.Linking.prototype.start = function() { this.componentCreated.bind(this) ); + this.componentClonedSubscription = this.subscribe( + GameLib.Event.COMPONENT_CLONED, + this.componentCloned.bind(this) + ); + this.registerDependenciesSubscription = this.subscribe( GameLib.Event.REGISTER_DEPENDENCIES, this.registerDependenciesDirect @@ -124,54 +121,9 @@ GameLib.System.Linking.prototype.start = function() { this.instanceCreated ); - this.meshInstanceCreatedSubscription = this.subscribe( - GameLib.Event.MESH_INSTANCE_CREATED, - this.meshInstanceCreated - ); - - this.lightInstanceCreatedSubscription = this.subscribe( - GameLib.Event.LIGHT_INSTANCE_CREATED, - this.lightInstanceCreated - ); - - this.sceneInstanceCreatedSubscription = this.subscribe( - GameLib.Event.SCENE_INSTANCE_CREATED, - this.sceneInstanceCreated - ); - - this.imageInstanceCreatedSubscription = this.subscribe( - GameLib.Event.IMAGE_INSTANCE_CREATED, - this.imageInstanceCreated - ); - - this.fontInstanceCreatedSubscription = this.subscribe( - GameLib.Event.FONT_INSTANCE_CREATED, - this.fontInstanceCreated - ); - - this.textureInstanceCreatedSubscription = this.subscribe( - GameLib.Event.TEXTURE_INSTANCE_CREATED, - this.textureInstanceCreated - ); - - this.materialInstanceCreatedSubscription = this.subscribe( - GameLib.Event.MATERIAL_INSTANCE_CREATED, - this.materialInstanceCreated - ); - - this.shapeInstanceCreatedSubscription = this.subscribe( - GameLib.Event.SHAPE_INSTANCE_CREATED, - this.shapeInstanceCreated - ); - - this.solverInstanceCreatedSubscription = this.subscribe( - GameLib.Event.SOLVER_INSTANCE_CREATED, - this.solverInstanceCreated - ); - - this.physicsWorldInstanceCreatedSubscription = this.subscribe( - GameLib.Event.PHYSICS_WORLD_INSTANCE_CREATED, - this.physicsWorldInstanceCreated + this.instanceClonedSubscription = this.subscribe( + GameLib.Event.INSTANCE_CLONED, + this.instanceCloned ); /** @@ -517,6 +469,21 @@ GameLib.System.Linking.prototype.componentCreated = function(data) { }; +GameLib.System.Linking.prototype.componentCloned = function(data) { + + this.componentCreated(data); + + if (data.component instanceof GameLib.D3.Mesh) { + + if (!(data.parent instanceof GameLib.D3.Mesh)){ + throw new Error('no scene parent'); + } + + data.parent.parentScene.addClone(data.component); + } + +}; + /** * When you want to register dependencies directly - Component constructor does this when it knows the * component instance cannot be created because it has a bunch of dependencies. So it tells the linking @@ -558,8 +525,6 @@ GameLib.System.Linking.prototype.removeComponent = function(data) { } }; - - GameLib.System.Linking.prototype.imageChanged = function(data) { var materials = GameLib.EntityManager.Instance.queryComponents(GameLib.D3.Material); @@ -576,172 +541,6 @@ GameLib.System.Linking.prototype.imageChanged = function(data) { }; -GameLib.System.Linking.prototype.meshInstanceCreated = function(data) { - - this.resolveDependencies(data.mesh); - - var scenes = GameLib.EntityManager.Instance.queryComponents(GameLib.D3.Scene); - - scenes.map(function(scene){ - if (data.mesh.parentScene === scene.id) { - data.mesh.parentScene = scene; - } - }); - - var meshes = GameLib.EntityManager.Instance.queryComponents(GameLib.D3.Mesh); - - /** - * Link Parent Meshes for other meshes - */ - meshes.map( - function(mesh) { - - if (mesh === data.mesh) { - return; - } - - if (mesh.parentMesh === data.mesh.id) { - mesh.setParentMesh(data.mesh); - } - - if (data.mesh.parentMesh === mesh.id) { - data.mesh.setParentMesh(mesh); - } - }.bind(this) - ); - - /** - * Link Parent Meshes for shapes - */ - var shapes = GameLib.EntityManager.Instance.queryComponents(GameLib.D3.Shape); - shapes.map(function(shape){ - if (shape.parentMesh === data.mesh.id) { - shape.parentMesh = data.mesh; - } - }); - - /** - * Link Parent Meshes for raycast wheels - */ - var raycastWheels = GameLib.EntityManager.Instance.queryComponents(GameLib.D3.RaycastWheel); - raycastWheels.map(function(wheel){ - if (wheel.parentMesh === data.mesh.id) { - wheel.parentMesh = data.mesh; - } - }); - - /** - * Link Parent Meshes for rigidbodies - */ - var rigidBodies = GameLib.EntityManager.Instance.queryComponents(GameLib.D3.RigidBody); - rigidBodies.map(function(rigidBody){ - if (rigidBody.parentMesh === data.mesh.id) { - rigidBody.parentMesh = data.mesh; - } - }); -}; - -GameLib.System.Linking.prototype.lightInstanceCreated = function(data) { - - this.resolveDependencies(data.light); - - var scenes = GameLib.EntityManager.Instance.queryComponents(GameLib.D3.Scene); - - scenes.map(function(scene){ - if (data.light.parentScene === scene) { - scene.addObject(data.light); - - //scene.updateInstance(); - } - }); - -}; - -GameLib.System.Linking.prototype.sceneInstanceCreated = function(data) { - - this.resolveDependencies(data.scene); - - /** - * Add all meshes and lights - */ - var objects = GameLib.EntityManager.Instance.queryComponents([GameLib.D3.Mesh,GameLib.D3.Light]); - objects.map(function(object){ - if ( - object.parentScene === data.scene.id - ) { - object.parentScene = data.scene; - } - }); - - //data.scene.updateInstance(); - -}; - -GameLib.System.Linking.prototype.fontInstanceCreated = function(data) { - /** - * We resolve the dependencies - */ - this.resolveDependencies(data.font); -}; - -GameLib.System.Linking.prototype.imageInstanceCreated = function(data) { - /** - * We resolve the dependencies - */ - this.resolveDependencies(data.image); - - /** - * At this point - updateInstance on the texture will have been called - we need to update the material - */ - var textures = GameLib.EntityManager.Instance.queryComponents([GameLib.D3.Texture]); - - //var materials = GameLib.EntityManager.Instance.queryComponents([GameLib.D3.Material]); - - /** - * Find all textures which use this image - */ - textures.map(function(texture){ - - if (texture.image === data.image || - texture.images.indexOf(data.image) !== -1 - ) { - - /** - * Ok - this image is in use - */ - texture.updateInstance(); - - /** - * Find all materials that use this texture - * This is no longer necessary - because texture updateInstance should notify materials when its image changes - */ - // materials.map(function(material){ - // - // var materialNeedsUpdate = false; - // - // for (var property in material) { - // if (material.hasOwnProperty(property) && - // material[property] === texture) { - // materialNeedsUpdate = true; - // } - // } - // - // if (materialNeedsUpdate) { - // material.updateInstance(); - // } - // - // }); - - - } - - }); -}; - -GameLib.System.Linking.prototype.solverInstanceCreated = function(data) { - console.log(data); -}; - GameLib.System.Linking.prototype.arrayItemAdded = function(data) { if ( data.component instanceof GameLib.D3.PhysicsWorld && @@ -757,103 +556,135 @@ GameLib.System.Linking.prototype.arrayItemAdded = function(data) { } }; +GameLib.System.Linking.prototype.instanceCloned = function(data) { + if ( + data.component.parentScene + ) { + // data.component.parentScene.instance.add(data.instance); + } +}; + GameLib.System.Linking.prototype.instanceCreated = function(data) { this.resolveDependencies(data.component); - if (data.component instanceof GameLib.D3.RaycastVehicle) { - - var worlds = GameLib.EntityManager.Instance.queryComponents(GameLib.D3.PhysicsWorld); - + if (data.component instanceof GameLib.D3.Image) { /** - * First ensure our parent world is linked + * Find all textures which use this image */ - worlds.map( - function(world) { - if (data.component.parentWorld === world.id) { - data.component.parentWorld = world; + GameLib.EntityManager.Instance.queryComponents(GameLib.D3.Texture).map( + function(texture) { + if (texture.image === data.component || + texture.images.indexOf(data.component) !== -1 + ) { + + /** + * Ok - this image is in use - this should notify materials when its image changes + */ + texture.updateInstance(); } } ); - - if (!data.component.parentWorld) { - console.log('no parent world'); - } else { - data.component.instance.addToWorld(data.component.parentWorld.instance); - console.log('raycast vehicle instance added to its parent world'); - } } - if (data.component instanceof GameLib.D3.RigidBody) { - - var meshes = GameLib.EntityManager.Instance.queryComponents(GameLib.D3.Mesh); - - meshes.map( - function(mesh) { - if (data.component.parentMesh === mesh.id) { - data.component.parentMesh = mesh; - } - } - ); - - this.restart(); - } -}; - -GameLib.System.Linking.prototype.shapeInstanceCreated = function(data) { - /** - * When a shape instance is created, just check if the parentMesh is loaded somewhere and set it. - * - * We also do the reverse, when a mesh instance is created, we check if its the parent of any shape. + * Link all scenes */ - var meshes = GameLib.EntityManager.Instance.queryComponents(GameLib.D3.Mesh); - meshes.map( - function(mesh) { - if (data.shape.parentMesh === mesh.id) { - data.shape.parentMesh = mesh; + if (data.component instanceof GameLib.D3.Scene) { + /** + * Check ALL components for 'parentScenes' - this is expensive so it checks the register directly + */ + GameLib.EntityManager.Instance.register.map( + function(component) { + if (component.parentScene === data.component.id) { + component.parentScene = data.component; + } } - } - ); -}; + ); + } - -GameLib.System.Linking.prototype.textureInstanceCreated = function(data) { - this.resolveDependencies(data.texture); -}; - -GameLib.System.Linking.prototype.materialInstanceCreated = function(data) { - this.resolveDependencies(data.material); -}; - -GameLib.System.Linking.prototype.physicsWorldInstanceCreated = function(data) { - - var raycastVehicles = GameLib.EntityManager.Instance.queryComponents(GameLib.D3.RaycastVehicle); - raycastVehicles.map( - function(vehicle) { - if (vehicle.parentWorld === data.world.id) { - vehicle.parentWorld = data.world; + if ( + data.component.parentScene && + typeof data.component.parentScene === 'string' + ) { + GameLib.EntityManager.Instance.queryComponents(GameLib.D3.Scene).map( + function (scene) { + if (data.component.parentScene === scene.id) { + data.component.parentScene = scene; + scene.addObject(data.component); + } } - } - ); + ); + } - var rigidBodies = GameLib.EntityManager.Instance.queryComponents(GameLib.D3.RigidBody); - rigidBodies.map( - function(rigidBody){ - if (rigidBody.parentWorld === data.world.id) { - rigidBody.parentWorld = data.world; - } - } - ); + /** + * Link all meshes + */ + if (data.component instanceof GameLib.D3.Mesh) { - var contactMaterials = GameLib.EntityManager.Instance.queryComponents(GameLib.D3.FrictionContactMaterial); - contactMaterials.map( - function(contactMaterial) { - if (contactMaterial.parentWorld === data.world.id) { - contactMaterial.parentWorld = data.world; - } + /** + * Check if this mesh is a parentMesh to any component- this is an expensive call, so check if we should call it + * Also - it inspects the register directly instead of querying it twice (since it checks ALL components) + */ + if (!data.preventParentMeshCheck) { + GameLib.EntityManager.Instance.register.map( + function (component) { + if (component.parentMesh && + component.parentMesh === data.component.id ) { + + component.parentMesh = data.component; + + /** + * Check if a component has this mesh as a parent + */ + if (component instanceof GameLib.D3.Mesh) { + component.setParentMesh(data.component); + } + } + } + ); } - ); + + } + + /** + * Maybe this component has a parent mesh + */ + if ( + data.component.parentMesh && + typeof data.component.parentMesh === 'string' + ) { + GameLib.EntityManager.Instance.queryComponents(GameLib.D3.Mesh).map( + function (mesh) { + if (data.component.parentMesh === mesh.id) { + + data.component.parentMesh = mesh; + + if (data.component instanceof GameLib.D3.Mesh) { + data.component.setParentMesh(mesh); + } + } + } + ); + } + + if ( + data.component.parentWorld && + typeof data.component.parentWorld === 'string' + ) { + GameLib.EntityManager.Instance.queryComponents(GameLib.D3.PhysicsWorld).map( + function (world) { + if (data.component.parentWorld === world.id) { + data.component.parentWorld = world; + + if (typeof data.component.instance.addToWorld === 'function') { + data.component.instance.addToWorld(world.instance); + console.log('instance added to physics world'); + } + } + } + ); + } }; @@ -1055,6 +886,7 @@ GameLib.System.Linking.prototype.stop = function() { * Components */ this.componentCreatedSubscription.remove(); + this.componentClonedSubscription.remove(); this.registerDependenciesSubscription.remove(); this.componentRemoveSubscription.remove(); @@ -1069,16 +901,7 @@ GameLib.System.Linking.prototype.stop = function() { * Instances */ this.instanceCreatedSubscription.remove(); - this.meshInstanceCreatedSubscription.remove(); - this.lightInstanceCreatedSubscription.remove(); - this.sceneInstanceCreatedSubscription.remove(); - this.imageInstanceCreatedSubscription.remove(); - this.fontInstanceCreatedSubscription.remove(); - this.textureInstanceCreatedSubscription.remove(); - this.materialInstanceCreatedSubscription.remove(); - this.shapeInstanceCreatedSubscription.remove(); - this.solverInstanceCreatedSubscription.remove(); - this.physicsWorldInstanceCreatedSubscription.remove(); + this.instanceClonedSubscription.remove(); /** * Meshes diff --git a/src/game-lib-system-storage.js b/src/game-lib-system-storage.js index 311b80f..20c9225 100644 --- a/src/game-lib-system-storage.js +++ b/src/game-lib-system-storage.js @@ -915,13 +915,7 @@ GameLib.System.Storage.prototype.loadFont = function(data) { ); } else { data.font.instance = font; - data.font.loaded = true; - GameLib.Event.Emit( - GameLib.Event.FONT_INSTANCE_CREATED, - { - font: data.font - } - ); + data.font.createInstance(); } } );