From bb0a861d35f8afb3e2076ea18a05aa3c65184228 Mon Sep 17 00:00:00 2001 From: "Theunis J. Botha" Date: Fri, 16 Jun 2017 15:49:53 +0200 Subject: [PATCH] proper loading of objects --- {src => bak}/game-lib-api-entity-manager.js | 22 --- {src => bak}/game-lib-d3-api-follow.js | 0 {src => bak}/game-lib-d3-follow.js | 0 {src => bak}/game-lib-d3-selected-object.js | 0 src/game-lib-a-1-event.js | 54 ++--- ...ame-lib-utils.js => game-lib-a-2-utils.js} | 0 src/game-lib-a-component-a.js | 41 +++- src/game-lib-api-dom-element.js | 7 - src/game-lib-api-entity.js | 10 - src/game-lib-api-system.js | 21 +- src/game-lib-d3-api-camera.js | 11 +- src/game-lib-d3-api-composer.js | 17 +- src/game-lib-d3-api-custom-code.js | 29 ++- src/game-lib-d3-api-image-factory.js | 12 +- src/game-lib-d3-api-input-drive.js | 18 +- src/game-lib-d3-api-input-editor.js | 13 +- src/game-lib-d3-api-light.js | 14 +- src/game-lib-d3-api-look-at.js | 15 +- src/game-lib-d3-api-material.js | 29 +-- src/game-lib-d3-api-mesh.js | 17 +- src/game-lib-d3-api-pass.js | 17 +- src/game-lib-d3-api-path-following.js | 18 +- src/game-lib-d3-api-render-target.js | 15 +- src/game-lib-d3-api-renderer.js | 16 +- src/game-lib-d3-api-scene.js | 18 +- src/game-lib-d3-api-skeleton.js | 9 - src/game-lib-d3-api-spline.js | 12 +- src/game-lib-d3-api-texture.js | 9 - src/game-lib-d3-api-viewport.js | 13 +- src/game-lib-d3-camera.js | 22 ++- src/game-lib-d3-composer.js | 89 ++++----- src/game-lib-d3-custom-code.js | 33 ++-- src/game-lib-d3-image-factory.js | 24 ++- src/game-lib-d3-input-drive.js | 19 +- src/game-lib-d3-input-editor.js | 16 +- src/game-lib-d3-light.js | 8 +- src/game-lib-d3-look-at.js | 13 +- src/game-lib-d3-material.js | 21 +- src/game-lib-d3-mesh-0.js | 32 ++- src/game-lib-d3-mesh-sphere.js | 4 +- src/game-lib-d3-pass.js | 11 +- src/game-lib-d3-path-following.js | 14 +- src/game-lib-d3-render-target.js | 10 +- src/game-lib-d3-renderer.js | 9 +- src/game-lib-d3-scene.js | 31 ++- src/game-lib-d3-skeleton.js | 12 +- src/game-lib-d3-spline.js | 5 +- src/game-lib-d3-stats.js | 22 ++- src/game-lib-d3-texture.js | 8 +- src/game-lib-d3-viewport.js | 10 +- src/game-lib-dom-element.js | 5 +- src/game-lib-entity-manager.js | 185 +++++++++++------- src/game-lib-entity.js | 62 +----- src/game-lib-system-0.js | 46 ++--- src/game-lib-z.js | 3 + 55 files changed, 589 insertions(+), 582 deletions(-) rename {src => bak}/game-lib-api-entity-manager.js (71%) rename {src => bak}/game-lib-d3-api-follow.js (100%) rename {src => bak}/game-lib-d3-follow.js (100%) rename {src => bak}/game-lib-d3-selected-object.js (100%) rename src/{game-lib-utils.js => game-lib-a-2-utils.js} (100%) diff --git a/src/game-lib-api-entity-manager.js b/bak/game-lib-api-entity-manager.js similarity index 71% rename from src/game-lib-api-entity-manager.js rename to bak/game-lib-api-entity-manager.js index 960acbb..440d21c 100644 --- a/src/game-lib-api-entity-manager.js +++ b/bak/game-lib-api-entity-manager.js @@ -13,29 +13,7 @@ GameLib.API.EntityManager = function( // systems, parentEntity ) { - GameLib.Component.call( - this, - GameLib.Component.COMPONENT_ENTITY_MANAGER, - { - 'entities' : [GameLib.Entity] - }, - 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(entities)) { - entities = []; - } - this.entities = entities; // if (GameLib.Utils.UndefinedOrNull(systems)) { // systems = []; diff --git a/src/game-lib-d3-api-follow.js b/bak/game-lib-d3-api-follow.js similarity index 100% rename from src/game-lib-d3-api-follow.js rename to bak/game-lib-d3-api-follow.js diff --git a/src/game-lib-d3-follow.js b/bak/game-lib-d3-follow.js similarity index 100% rename from src/game-lib-d3-follow.js rename to bak/game-lib-d3-follow.js diff --git a/src/game-lib-d3-selected-object.js b/bak/game-lib-d3-selected-object.js similarity index 100% rename from src/game-lib-d3-selected-object.js rename to bak/game-lib-d3-selected-object.js diff --git a/src/game-lib-a-1-event.js b/src/game-lib-a-1-event.js index 78c71e0..ca6340a 100644 --- a/src/game-lib-a-1-event.js +++ b/src/game-lib-a-1-event.js @@ -31,6 +31,7 @@ GameLib.Event.LOADED = 0xf; GameLib.Event.SAVE_SUCCESS = 0x10; GameLib.Event.SAVE_FAILURE = 0x11; GameLib.Event.LOGGED_IN = 0x12; +GameLib.Event.COMPONENT_CREATED = 0x13; /** * Subscribe to some events @@ -42,62 +43,65 @@ GameLib.Event.prototype.subscribe = function( callback ) { + var fn = callback.bind(this); + if (GameLib.Event.Subscriptions.hasOwnProperty(eventName)) { - GameLib.Event.Subscriptions[eventName].push(callback.bind(this)); + GameLib.Event.Subscriptions[eventName].push(fn); } else { GameLib.Event.Subscriptions[eventName] = []; - GameLib.Event.Subscriptions[eventName].push(callback.bind(this)); + GameLib.Event.Subscriptions[eventName].push(fn); } + var index = GameLib.Event.Subscriptions[eventName].indexOf(fn); + + /** + * Return a handle to the caller to allow us to unsubscribe to this event + */ + return { + fn : fn, + remove : function() { + GameLib.Event.Subscriptions[eventName].splice(index, 1); + } + } }; /** - * Unsubscribe to some events - * TODO: incomplete + * * @param eventName - * @param callback + * @param data */ -GameLib.Event.prototype.unsubscribe = function( - eventName, - callback -) { - - var index = -1; - - if (GameLib.Event.Subscriptions.hasOwnProperty(eventName)) { - index = GameLib.Event.Subscriptions[eventName].indexOf(callback.bind(this)); - } - - if (index === -1) { - console.warn('could not unsubscribe from event - please implement this sometime (bind creates new function - store this pointer somewhere)'); - } else { - GameLib.Event.Subscriptions[eventName].splice(index, 1); - } - -}; - /** * Publish some event happened with some data * @param eventName * @param data + * @returns {number} of callbacks executed */ GameLib.Event.prototype.publish = function( eventName, data ) { - GameLib.Event.Emit(eventName, data); + return GameLib.Event.Emit(eventName, data); }; /** * Static method call + * @param eventName + * @param data + * @returns {number} of callbacks executed + * @constructor */ GameLib.Event.Emit = function(eventName, data) { + var count = 0; + if (GameLib.Event.Subscriptions.hasOwnProperty(eventName)) { GameLib.Event.Subscriptions[eventName].map( function(callback) { callback(data); + count++; } ) } + + return count; }; \ No newline at end of file diff --git a/src/game-lib-utils.js b/src/game-lib-a-2-utils.js similarity index 100% rename from src/game-lib-utils.js rename to src/game-lib-a-2-utils.js diff --git a/src/game-lib-a-component-a.js b/src/game-lib-a-component-a.js index 85d7bd8..8cb9fba 100644 --- a/src/game-lib-a-component-a.js +++ b/src/game-lib-a-component-a.js @@ -4,11 +4,13 @@ * @param componentType * @param linkedObjects * @param parentEntity + * @param traverse */ GameLib.Component = function( componentType, linkedObjects, - parentEntity + parentEntity, + traverse ) { if (GameLib.Utils.UndefinedOrNull(linkedObjects)) { linkedObjects = {}; @@ -32,11 +34,42 @@ GameLib.Component = function( this.loaded = false; + if (GameLib.Utils.UndefinedOrNull(traverse)) { + traverse = true; + } + this.traverse = traverse; + + this.dependencies = this.getDependencies(); + + this.publish( + GameLib.Event.COMPONENT_CREATED, + { + component : this + } + ); }; GameLib.Component.prototype = Object.create(GameLib.API.Component.prototype); GameLib.Component.prototype.constructor = GameLib.Component; +GameLib.Component.prototype.getDependencies = function() { + + var dependencies = []; + + for (var property in this.linkedObjects) { + if (this.linkedObjects.hasOwnProperty(property)){ + if ( + this.hasOwnProperty(property) && + typeof this[property] === 'string' + ) { + dependencies.push(this[property]); + } + } + } + + return dependencies; +}; + GameLib.Component.prototype.toString = function() { return this.id; }; @@ -72,6 +105,7 @@ GameLib.Component.COMPONENT_IMAGE_FACTORY = 0x1c; GameLib.Component.COMPONENT_STATS = 0x1d; GameLib.Component.COMPONENT_GUI = 0x1e; GameLib.Component.COMPONENT_IMAGE = 0x1f; +GameLib.Component.COMPONENT_ENTITY = 0x20; /** * Returns string name for component number @@ -112,6 +146,7 @@ GameLib.Component.GetComponentName = function(number) { case 0x1d : return 'GameLib.D3.Stats'; case 0x1e : return 'GameLib.GUI'; case 0x1f : return 'GameLib.D3.Image'; + case 0x20 : return 'GameLib.Entity'; break; } }; @@ -126,6 +161,10 @@ GameLib.Component.prototype.toApiObject = function() { GameLib.Component.prototype.buildIdToObject = function() { + if (!this.traverse) { + return; + } + if (this.built) { return; } diff --git a/src/game-lib-api-dom-element.js b/src/game-lib-api-dom-element.js index d1c9f4b..c626443 100644 --- a/src/game-lib-api-dom-element.js +++ b/src/game-lib-api-dom-element.js @@ -18,13 +18,6 @@ GameLib.API.DomElement = function( } this.parentEntity = parentEntity; - GameLib.Component.call( - this, - GameLib.Component.COMPONENT_DOM_ELEMENT, - null, - parentEntity - ); - if (GameLib.Utils.UndefinedOrNull(id)) { id = GameLib.Utils.RandomId(); } diff --git a/src/game-lib-api-entity.js b/src/game-lib-api-entity.js index ea13880..be578d5 100644 --- a/src/game-lib-api-entity.js +++ b/src/game-lib-api-entity.js @@ -26,16 +26,6 @@ GameLib.API.Entity = function( this.activeComponent = null; - GameLib.Component.call( - this, - GameLib.Component.COMPONENT_ENTITY, - { - 'components' : [GameLib.Component], - 'activeComponent' : GameLib.Component - }, - parentEntity - ); - if (GameLib.Utils.UndefinedOrNull(id)) { id = GameLib.Utils.RandomId(); } diff --git a/src/game-lib-api-system.js b/src/game-lib-api-system.js index 02ca5d4..d93725c 100644 --- a/src/game-lib-api-system.js +++ b/src/game-lib-api-system.js @@ -3,7 +3,6 @@ * @param id String * @param name String * @param systemType - * @param entityManager * @param parentEntity * @constructor */ @@ -11,19 +10,9 @@ GameLib.API.System = function ( id, name, systemType, - entityManager, parentEntity ) { - GameLib.Component.call( - this, - GameLib.Component.COMPONENT_SYSTEM, - { - 'entityManager' : GameLib.EntityManager - }, - parentEntity - ); - if (GameLib.Utils.UndefinedOrNull(id)) { id = GameLib.Utils.RandomId(); } @@ -39,10 +28,11 @@ GameLib.API.System = function ( } this.systemType = systemType; - if (GameLib.Utils.UndefinedOrNull(entityManager)) { - entityManager = null; - } - this.entityManager = entityManager; + if (GameLib.Utils.UndefinedOrNull(parentEntity)) { + parentEntity = null; + } + this.parentEntity = parentEntity; + }; GameLib.API.System.prototype = Object.create(GameLib.Component.prototype); @@ -58,7 +48,6 @@ GameLib.API.System.FromObject = function(objectComponent) { objectComponent.id, objectComponent.name, objectComponent.systemType, - objectComponent.entityManager, objectComponent.parentEntity ); }; diff --git a/src/game-lib-d3-api-camera.js b/src/game-lib-d3-api-camera.js index e2dafe0..277a74f 100644 --- a/src/game-lib-d3-api-camera.js +++ b/src/game-lib-d3-api-camera.js @@ -42,12 +42,6 @@ GameLib.D3.API.Camera = function( eyeSeparation, focalLength ) { - GameLib.Component.call( - this, - GameLib.Component.COMPONENT_CAMERA, - null, - parentEntity - ); if (GameLib.Utils.UndefinedOrNull(id)) { id = GameLib.Utils.RandomId(); @@ -146,6 +140,11 @@ GameLib.D3.API.Camera = function( focalLength = 150; } this.focalLength = focalLength; + + if (GameLib.Utils.UndefinedOrNull(parentEntity)) { + parentEntity = null; + } + this.parentEntity = parentEntity; }; GameLib.D3.API.Camera.prototype = Object.create(GameLib.Component.prototype); diff --git a/src/game-lib-d3-api-composer.js b/src/game-lib-d3-api-composer.js index 0c4f94f..951c705 100644 --- a/src/game-lib-d3-api-composer.js +++ b/src/game-lib-d3-api-composer.js @@ -16,18 +16,6 @@ GameLib.D3.API.Composer = function ( passes, parentEntity ) { - - GameLib.Component.call( - this, - GameLib.Component.COMPONENT_COMPOSER, - { - 'renderer' : GameLib.D3.Renderer, - 'renderTarget' : GameLib.D3.RenderTarget, - 'passes' : [GameLib.D3.Pass] - }, - parentEntity - ); - if (GameLib.Utils.UndefinedOrNull(id)) { id = GameLib.Utils.RandomId(); } @@ -52,6 +40,11 @@ GameLib.D3.API.Composer = function ( passes = []; } this.passes = passes; + + if (GameLib.Utils.UndefinedOrNull(parentEntity)) { + parentEntity = null; + } + this.parentEntity = parentEntity; }; GameLib.D3.API.Composer.prototype = Object.create(GameLib.Component.prototype); diff --git a/src/game-lib-d3-api-custom-code.js b/src/game-lib-d3-api-custom-code.js index 6e2f44a..c684878 100644 --- a/src/game-lib-d3-api-custom-code.js +++ b/src/game-lib-d3-api-custom-code.js @@ -1,31 +1,21 @@ /** - * This component makes the parentEntity (ex. car) follow the path provided by the spline - * @param id String - * @param name String - * @param code String - * @param domElementId + * Custom Code Component + * @param id + * @param name * @param parentEntity + * @param code + * @param domElementId * @param args * @constructor */ GameLib.D3.API.CustomCode = function ( id, name, + parentEntity, code, domElementId, - parentEntity, args ) { - - GameLib.Component.call( - this, - GameLib.Component.COMPONENT_CUSTOM_CODE, - { - 'args' : [GameLib.Component] - }, - parentEntity - ); - if (GameLib.Utils.UndefinedOrNull(id)) { id = GameLib.Utils.RandomId(); } @@ -36,6 +26,11 @@ GameLib.D3.API.CustomCode = function ( } this.name = name; + if (GameLib.Utils.UndefinedOrNull(parentEntity)) { + parentEntity = null; + } + this.parentEntity = parentEntity; + if (GameLib.Utils.UndefinedOrNull(code)) { code = ''; } @@ -65,9 +60,9 @@ GameLib.D3.API.CustomCode.FromObject = function(objectComponent) { return new GameLib.D3.API.CustomCode( objectComponent.id, objectComponent.name, + objectComponent.parentEntity, objectComponent.code, objectComponent.domElementId, - objectComponent.parentEntity, objectComponent.args ); }; diff --git a/src/game-lib-d3-api-image-factory.js b/src/game-lib-d3-api-image-factory.js index 3bc952a..48425d9 100644 --- a/src/game-lib-d3-api-image-factory.js +++ b/src/game-lib-d3-api-image-factory.js @@ -12,13 +12,6 @@ GameLib.D3.API.ImageFactory = function( baseUrl, parentEntity ) { - GameLib.Component.call( - this, - GameLib.Component.COMPONENT_IMAGE_FACTORY, - null, - parentEntity - ); - if (GameLib.Utils.UndefinedOrNull(id)) { id = GameLib.Utils.RandomId(); } @@ -34,6 +27,11 @@ GameLib.D3.API.ImageFactory = function( console.warn('No baseURL defined for image factory'); } this.baseUrl = baseUrl; + + if (GameLib.Utils.UndefinedOrNull(parentEntity)) { + parentEntity = null; + } + this.parentEntity = parentEntity; }; GameLib.D3.API.ImageFactory.prototype = Object.create(GameLib.Component.prototype); diff --git a/src/game-lib-d3-api-input-drive.js b/src/game-lib-d3-api-input-drive.js index 464400d..bca249a 100644 --- a/src/game-lib-d3-api-input-drive.js +++ b/src/game-lib-d3-api-input-drive.js @@ -30,19 +30,6 @@ GameLib.D3.API.Input.Drive = function ( distanceGrain, rotationFactor ) { - GameLib.Component.call( - this, - GameLib.Component.COMPONENT_INPUT_DRIVE, - { - 'pathFollowingComponent' : GameLib.D3.PathFollowing, - 'wheelFL' : GameLib.D3.Mesh, - 'wheelFR' : GameLib.D3.Mesh, - 'wheelRL' : GameLib.D3.Mesh, - 'wheelRR' : GameLib.D3.Mesh - }, - parentEntity - ); - if (GameLib.Utils.UndefinedOrNull(id)) { id = GameLib.Utils.RandomId(); } @@ -63,6 +50,11 @@ GameLib.D3.API.Input.Drive = function ( } this.pathFollowingComponent = pathFollowingComponent; + if (GameLib.Utils.UndefinedOrNull(parentEntity)) { + parentEntity = null; + } + this.parentEntity = parentEntity; + if (GameLib.Utils.UndefinedOrNull(wheelFL)) { wheelFL = null; } diff --git a/src/game-lib-d3-api-input-editor.js b/src/game-lib-d3-api-input-editor.js index b50564d..ddea8c6 100644 --- a/src/game-lib-d3-api-input-editor.js +++ b/src/game-lib-d3-api-input-editor.js @@ -14,14 +14,6 @@ GameLib.D3.API.Input.Editor = function ( camera, parentEntity ) { - GameLib.Component.call( - this, - GameLib.Component.COMPONENT_INPUT_EDITOR, - { - 'camera' : GameLib.D3.Camera - }, - parentEntity - ); if (GameLib.Utils.UndefinedOrNull(id)) { id = GameLib.Utils.RandomId(); @@ -43,6 +35,11 @@ GameLib.D3.API.Input.Editor = function ( } this.camera = camera; + if (GameLib.Utils.UndefinedOrNull(parentEntity)) { + parentEntity = null; + } + this.parentEntity = parentEntity; + }; GameLib.D3.API.Input.Editor.prototype = Object.create(GameLib.Component.prototype); diff --git a/src/game-lib-d3-api-light.js b/src/game-lib-d3-api-light.js index bb00829..eee33fc 100644 --- a/src/game-lib-d3-api-light.js +++ b/src/game-lib-d3-api-light.js @@ -38,15 +38,6 @@ GameLib.D3.API.Light = function( parentScene, parentEntity ) { - GameLib.Component.call( - this, - GameLib.Component.COMPONENT_LIGHT, - { - 'parentScene' : GameLib.D3.Scene - }, - parentEntity - ); - if (GameLib.Utils.UndefinedOrNull(id)) { id = GameLib.Utils.RandomId(); } @@ -126,6 +117,11 @@ GameLib.D3.API.Light = function( parentScene = null; } this.parentScene = parentScene; + + if (GameLib.Utils.UndefinedOrNull(parentEntity)){ + parentEntity = null; + } + this.parentEntity = parentEntity; }; GameLib.D3.API.Light.prototype = Object.create(GameLib.Component.prototype); diff --git a/src/game-lib-d3-api-look-at.js b/src/game-lib-d3-api-look-at.js index 0149087..776d604 100644 --- a/src/game-lib-d3-api-look-at.js +++ b/src/game-lib-d3-api-look-at.js @@ -19,16 +19,6 @@ GameLib.D3.API.LookAt = function ( parentEntity ) { - GameLib.Component.call( - this, - GameLib.Component.COMPONENT_LOOK_AT, - { - 'currentComponent' : GameLib.Component, - 'targetComponent' : GameLib.Component - }, - parentEntity - ); - if (GameLib.Utils.UndefinedOrNull(id)) { id = GameLib.Utils.RandomId(); } @@ -66,6 +56,11 @@ GameLib.D3.API.LookAt = function ( this.currentRotation = new GameLib.API.Quaternion(); this.targetPosition = new GameLib.API.Vector3(); + + if(GameLib.Utils.UndefinedOrNull(parentEntity)) { + parentEntity = null; + } + this.parentEntity = parentEntity; }; GameLib.D3.API.LookAt.prototype = Object.create(GameLib.Component.prototype); diff --git a/src/game-lib-d3-api-material.js b/src/game-lib-d3-api-material.js index a4f9522..f367d62 100644 --- a/src/game-lib-d3-api-material.js +++ b/src/game-lib-d3-api-material.js @@ -69,7 +69,6 @@ * @param roughnessMap * @param specularMap * @param parentEntity - * @param selected * @constructor */ GameLib.D3.API.Material = function( @@ -144,26 +143,6 @@ GameLib.D3.API.Material = function( parentEntity ) { - GameLib.Component.call( - this, - GameLib.Component.COMPONENT_MATERIAL, - { - 'alphaMap' : GameLib.D3.Texture, - 'aoMap' : GameLib.D3.Texture, - 'bumpMap' : GameLib.D3.Texture, - 'diffuseMap' : GameLib.D3.Texture, - 'displacementMap' : GameLib.D3.Texture, - 'emissiveMap' : GameLib.D3.Texture, - 'environmentMap' : GameLib.D3.Texture, - 'lightMap' : GameLib.D3.Texture, - 'metalnessMap' : GameLib.D3.Texture, - 'normalMap' : GameLib.D3.Texture, - 'roughnessMap' : GameLib.D3.Texture, - 'specularMap' : GameLib.D3.Texture - }, - parentEntity - ); - if (GameLib.Utils.UndefinedOrNull(id)) { id = GameLib.Utils.RandomId(); } @@ -504,6 +483,11 @@ GameLib.D3.API.Material = function( } this.specularMap = specularMap; + if (GameLib.Utils.UndefinedOrNull(parentEntity)) { + parentEntity = null; + } + this.parentEntity = parentEntity; + this.needsUpdate = false; }; @@ -647,7 +631,6 @@ GameLib.D3.API.Material.FromObject = function(objectMaterial) { apiNormalMap, apiRoughnessMap, apiSpecularMap, - objectMaterial.parentEntity, - objectMaterial.selected + objectMaterial.parentEntity ) }; diff --git a/src/game-lib-d3-api-mesh.js b/src/game-lib-d3-api-mesh.js index 8fb9c07..1642ab8 100644 --- a/src/game-lib-d3-api-mesh.js +++ b/src/game-lib-d3-api-mesh.js @@ -48,18 +48,6 @@ GameLib.D3.API.Mesh = function( parentEntity, renderOrder ) { - GameLib.Component.call( - this, - GameLib.Component.COMPONENT_MESH, - { - 'parentMesh' : GameLib.D3.Mesh, - 'parentScene' : GameLib.D3.Scene, - 'materials' : [GameLib.D3.Material], - 'skeleton' : GameLib.D3.Skeleton - }, - parentEntity - ); - if (GameLib.Utils.UndefinedOrNull(id)) { id = GameLib.Utils.RandomId(); } @@ -164,6 +152,11 @@ GameLib.D3.API.Mesh = function( renderOrder = 0; } this.renderOrder = renderOrder; + + if (GameLib.Utils.UndefinedOrNull(parentEntity)) { + parentEntity = null; + } + this.parentEntity = parentEntity; }; GameLib.D3.API.Mesh.prototype = Object.create(GameLib.Component.prototype); diff --git a/src/game-lib-d3-api-pass.js b/src/game-lib-d3-api-pass.js index 570f6ae..a99d920 100644 --- a/src/game-lib-d3-api-pass.js +++ b/src/game-lib-d3-api-pass.js @@ -18,17 +18,6 @@ GameLib.D3.API.Pass = function ( renderToScreen, parentEntity ) { - - GameLib.Component.call( - this, - GameLib.Component.COMPONENT_PASS, - { - 'camera' : GameLib.D3.Camera, - 'scene' : GameLib.D3.Scene - }, - parentEntity - ); - if (GameLib.Utils.UndefinedOrNull(id)) { id = GameLib.Utils.RandomId(); } @@ -56,7 +45,7 @@ GameLib.D3.API.Pass = function ( if (GameLib.Utils.UndefinedOrNull(renderToScreen)) { - if (this.passType == GameLib.D3.Pass.PASS_TYPE_RENDER) { + if (this.passType === GameLib.D3.Pass.PASS_TYPE_RENDER) { renderToScreen = false; } else if (GameLib.D3.Pass.PASS_TYPE_COPY_SHADER) { renderToScreen = true; @@ -68,6 +57,10 @@ GameLib.D3.API.Pass = function ( } this.renderToScreen = renderToScreen; + if (GameLib.Utils.UndefinedOrNull(parentEntity)) { + parentEntity = null; + } + this.parentEntity = parentEntity; }; GameLib.D3.API.Pass.prototype = Object.create(GameLib.Component.prototype); diff --git a/src/game-lib-d3-api-path-following.js b/src/game-lib-d3-api-path-following.js index cdb46ea..41fbd47 100644 --- a/src/game-lib-d3-api-path-following.js +++ b/src/game-lib-d3-api-path-following.js @@ -48,18 +48,6 @@ GameLib.D3.API.PathFollowing = function ( rotationVector, parentEntity ) { - - GameLib.Component.call( - this, - GameLib.Component.COMPONENT_PATH_FOLLOWING, - { - 'spline': GameLib.D3.Spline, - 'mesh' : GameLib.D3.Mesh, - 'raytraceMesh' : GameLib.D3.Mesh - }, - parentEntity - ); - if (GameLib.Utils.UndefinedOrNull(id)) { id = GameLib.Utils.RandomId(); } @@ -164,6 +152,12 @@ GameLib.D3.API.PathFollowing = function ( rotationVector = new GameLib.API.Quaternion(); } this.rotationVector = rotationVector; + + if (GameLib.Utils.UndefinedOrNull(parentEntity)) { + parentEntity = null; + } + this.parentEntity = parentEntity; + }; GameLib.D3.API.PathFollowing.prototype = Object.create(GameLib.Component.prototype); diff --git a/src/game-lib-d3-api-render-target.js b/src/game-lib-d3-api-render-target.js index 4ef3b40..affdc5c 100644 --- a/src/game-lib-d3-api-render-target.js +++ b/src/game-lib-d3-api-render-target.js @@ -24,16 +24,6 @@ GameLib.D3.API.RenderTarget = function ( texture, parentEntity ) { - - GameLib.Component.call( - this, - GameLib.Component.COMPONENT_RENDER_TARGET, - { - 'texture' : GameLib.D3.Texture - }, - parentEntity - ); - if (GameLib.Utils.UndefinedOrNull(id)) { id = GameLib.Utils.RandomId(); } @@ -79,6 +69,11 @@ GameLib.D3.API.RenderTarget = function ( } this.texture = texture; + if (GameLib.Utils.UndefinedOrNull(parentEntity)) { + parentEntity = null; + } + this.parentEntity = parentEntity; + }; GameLib.D3.API.RenderTarget.prototype = Object.create(GameLib.Component.prototype); diff --git a/src/game-lib-d3-api-renderer.js b/src/game-lib-d3-api-renderer.js index d78a1cf..9f6987c 100644 --- a/src/game-lib-d3-api-renderer.js +++ b/src/game-lib-d3-api-renderer.js @@ -24,16 +24,6 @@ GameLib.D3.API.Renderer = function ( clearColor, parentEntity ) { - - GameLib.Component.call( - this, - GameLib.Component.COMPONENT_RENDERER, - { - 'domElement' : GameLib.DomElement - }, - parentEntity - ); - if (GameLib.Utils.UndefinedOrNull(id)) { id = GameLib.Utils.RandomId(); } @@ -78,6 +68,12 @@ GameLib.D3.API.Renderer = function ( clearColor = new GameLib.API.Color(0.58, 0.58, 0.58); } this.clearColor = clearColor; + + if (GameLib.Utils.UndefinedOrNull(parentEntity)) { + parentEntity = null; + } + this.parentEntity = parentEntity; + }; GameLib.D3.API.Renderer.prototype = Object.create(GameLib.Component.prototype); diff --git a/src/game-lib-d3-api-scene.js b/src/game-lib-d3-api-scene.js index 9e42b77..e8df9e3 100644 --- a/src/game-lib-d3-api-scene.js +++ b/src/game-lib-d3-api-scene.js @@ -30,19 +30,6 @@ GameLib.D3.API.Scene = function( activeCamera, parentEntity ) { - GameLib.Component.call( - this, - GameLib.Component.COMPONENT_SCENE, - { - 'meshes' : [GameLib.D3.Mesh], - 'lights' : [GameLib.D3.Light], - 'textures' : [GameLib.D3.Texture], - 'materials' : [GameLib.D3.Material], - 'images' : [GameLib.D3.Image], - 'activeCamera' : GameLib.D3.Camera - }, - parentEntity - ); if (GameLib.Utils.UndefinedOrNull(id)) { id = GameLib.Utils.RandomId(); @@ -104,6 +91,11 @@ GameLib.D3.API.Scene = function( } this.activeCamera = activeCamera; + if (GameLib.Utils.UndefinedOrNull(parentEntity)) { + parentEntity = null; + } + this.parentEntity = parentEntity; + }; GameLib.D3.API.Scene.prototype = Object.create(GameLib.Component.prototype); diff --git a/src/game-lib-d3-api-skeleton.js b/src/game-lib-d3-api-skeleton.js index f5271d1..7b723a4 100644 --- a/src/game-lib-d3-api-skeleton.js +++ b/src/game-lib-d3-api-skeleton.js @@ -29,15 +29,6 @@ GameLib.D3.API.Skeleton = function ( } this.parentEntity = parentEntity; - GameLib.Component.call( - this, - GameLib.Component.COMPONENT_SKELETON, - { - 'bones' : [GameLib.D3.Bone] - }, - parentEntity - ); - if (GameLib.Utils.UndefinedOrNull(id)) { id = GameLib.Utils.RandomId(); } diff --git a/src/game-lib-d3-api-spline.js b/src/game-lib-d3-api-spline.js index be613de..ffef0e2 100644 --- a/src/game-lib-d3-api-spline.js +++ b/src/game-lib-d3-api-spline.js @@ -12,13 +12,6 @@ GameLib.D3.API.Spline = function( vertices, parentEntity ) { - GameLib.Component.call( - this, - GameLib.Component.COMPONENT_SPLINE, - null, - parentEntity - ); - if (GameLib.Utils.UndefinedOrNull(id)) { id = GameLib.Utils.RandomId(); } @@ -33,6 +26,11 @@ GameLib.D3.API.Spline = function( vertices = []; } this.vertices = vertices; + + if (GameLib.Utils.UndefinedOrNull(parentEntity)) { + parentEntity = null; + } + this.parentEntity = parentEntity; }; GameLib.D3.API.Spline.prototype = Object.create(GameLib.Component.prototype); diff --git a/src/game-lib-d3-api-texture.js b/src/game-lib-d3-api-texture.js index 0c220b1..0119465 100644 --- a/src/game-lib-d3-api-texture.js +++ b/src/game-lib-d3-api-texture.js @@ -53,15 +53,6 @@ GameLib.D3.API.Texture = function( } this.parentEntity = parentEntity; - GameLib.Component.call( - this, - GameLib.Component.COMPONENT_TEXTURE, - { - 'image' : GameLib.D3.Image - }, - parentEntity - ); - if (GameLib.Utils.UndefinedOrNull(id)) { id = GameLib.Utils.RandomId(); } diff --git a/src/game-lib-d3-api-viewport.js b/src/game-lib-d3-api-viewport.js index 3e93eb8..d8b987f 100644 --- a/src/game-lib-d3-api-viewport.js +++ b/src/game-lib-d3-api-viewport.js @@ -20,14 +20,6 @@ GameLib.D3.API.Viewport = function( scenes, parentEntity ) { - GameLib.Component.call( - this, - GameLib.Component.COMPONENT_VIEWPORT, - { - 'scenes' : [GameLib.D3.Scene] - }, - parentEntity - ); if (GameLib.Utils.UndefinedOrNull(id)) { id = GameLib.Utils.RandomId(); @@ -63,6 +55,11 @@ GameLib.D3.API.Viewport = function( scenes = []; } this.scenes = scenes; + + if (GameLib.Utils.UndefinedOrNull(parentEntity)) { + parentEntity = null; + } + this.parentEntity = parentEntity; }; GameLib.D3.API.Viewport.prototype = Object.create(GameLib.Component.prototype); diff --git a/src/game-lib-d3-camera.js b/src/game-lib-d3-camera.js index 6901c98..f177fdb 100644 --- a/src/game-lib-d3-camera.js +++ b/src/game-lib-d3-camera.js @@ -16,9 +16,9 @@ GameLib.D3.Camera = function( apiCamera = {}; } - if (apiCamera instanceof GameLib.D3.Camera) { - return apiCamera; - } + if (apiCamera instanceof GameLib.D3.Camera) { + return apiCamera; + } GameLib.D3.API.Camera.call( this, @@ -76,9 +76,12 @@ GameLib.D3.Camera = function( throw new Error('lookAt not instance of API.Vector3'); } - this.instance = this.createInstance(); + GameLib.Component.call( + this, + GameLib.Component.COMPONENT_CAMERA + ); - this.needsUpdate = false; + //this.needsUpdate = false; } ; GameLib.D3.Camera.prototype = Object.create(GameLib.D3.API.Camera.prototype); @@ -94,6 +97,11 @@ GameLib.D3.Camera.CAMERA_TYPE_STEREO = 0x3; */ GameLib.D3.Camera.prototype.createInstance = function(update) { + if (!this.loaded) { + console.log('Attempted to create an instance but the runtime object is not fully loaded : ' + this.name); + return null; + } + var instance = null; if (update) { @@ -101,14 +109,14 @@ GameLib.D3.Camera.prototype.createInstance = function(update) { } if (!instance) { - if (this.cameraType == GameLib.D3.Camera.CAMERA_TYPE_PERSPECTIVE ) { + if (this.cameraType === GameLib.D3.Camera.CAMERA_TYPE_PERSPECTIVE ) { instance = new THREE.PerspectiveCamera( this.fov, this.aspect, this.near, this.far ); - } else if (this.cameraType == GameLib.D3.Camera.CAMERA_TYPE_ORTHOGONAL) { + } else if (this.cameraType === GameLib.D3.Camera.CAMERA_TYPE_ORTHOGONAL) { instance = new THREE.OrthographicCamera( this.minX, this.maxX, diff --git a/src/game-lib-d3-composer.js b/src/game-lib-d3-composer.js index 29afe03..2cf5982 100644 --- a/src/game-lib-d3-composer.js +++ b/src/game-lib-d3-composer.js @@ -30,37 +30,15 @@ GameLib.D3.Composer = function ( apiComposer.parentEntity ); - if (this.renderer instanceof GameLib.D3.API.Renderer) { - this.renderer = new GameLib.D3.Renderer( - this.graphics, - this.renderer - ) - } - - if (this.renderTarget instanceof GameLib.D3.API.RenderTarget) { - this.renderTarget = new GameLib.D3.RenderTarget( - this.graphics, - this.renderTarget - ) - } - - this.passes = this.passes.map( - function (apiPass) { - if (apiPass instanceof GameLib.D3.API.Pass) { - return GameLib.D3.Pass( - this.graphics, - apiPass - ) - } else { - console.warn('apiPass not of type API.Pass'); - throw new Error('apiPass not of type API.Pass'); - } - }.bind(this) + GameLib.Component.call( + this, + GameLib.Component.COMPONENT_COMPOSER, + { + 'renderer' : GameLib.D3.Renderer, + 'renderTarget' : GameLib.D3.RenderTarget, + 'passes' : [GameLib.D3.Pass] + } ); - - this.buildIdToObject(); - - this.instance = this.createInstance(); }; GameLib.D3.Composer.prototype = Object.create(GameLib.D3.API.Composer.prototype); @@ -73,31 +51,38 @@ GameLib.D3.Composer.prototype.constructor = GameLib.D3.Composer; */ GameLib.D3.Composer.prototype.createInstance = function(update) { - var instance = null; - - if (update) { - instance = this.instance; + if (!this.loaded) { + console.log('Attempted to create an instance but the runtime object is not fully loaded : ' + this.name); + return null; } - if (this.renderer && - this.renderTarget) { + //TODO : Fix this too - make it nice (no circular references) + // var instance = null; + // + // if (update) { + // instance = this.instance; + // } - if (!THREE.EffectComposer) { - console.warn('No THREE.EffectComposer'); - throw new Error('No THREE.EffectComposer'); - } - - instance = new THREE.EffectComposer( - this.renderer.instance, - this.renderTarget.instance - ); - - this.passes.map( - function(pass) { - this.instance.addPass(pass.instance); - }.bind(this) - ); - } + //TODO : Fix this still (renderer and renderTarget should be objects) + // if (this.renderer && + // this.renderTarget) { + // + // if (!THREE.EffectComposer) { + // console.warn('No THREE.EffectComposer'); + // throw new Error('No THREE.EffectComposer'); + // } + // + // instance = new THREE.EffectComposer( + // this.renderer.instance, + // this.renderTarget.instance + // ); + // + // this.passes.map( + // function(pass) { + // this.instance.addPass(pass.instance); + // }.bind(this) + // ); + // } return instance; }; diff --git a/src/game-lib-d3-custom-code.js b/src/game-lib-d3-custom-code.js index 42cda9a..aea2677 100644 --- a/src/game-lib-d3-custom-code.js +++ b/src/game-lib-d3-custom-code.js @@ -19,15 +19,19 @@ GameLib.D3.CustomCode = function( this, apiCustomCode.id, apiCustomCode.name, + apiCustomCode.parentEntity, apiCustomCode.code, apiCustomCode.domElementId, - apiCustomCode.parentEntity, apiCustomCode.args ); - this.buildIdToObject(); - - this.instance = this.createInstance(); + GameLib.Component.call( + this, + GameLib.Component.COMPONENT_CUSTOM_CODE, + { + 'args' : [GameLib.Component] + } + ); }; GameLib.D3.CustomCode.prototype = Object.create(GameLib.D3.API.CustomCode.prototype); @@ -39,6 +43,11 @@ GameLib.D3.CustomCode.prototype.constructor = GameLib.D3.CustomCode; */ GameLib.D3.CustomCode.prototype.createInstance = function(update) { + if (!this.loaded) { + console.log('Attempted to create an instance but the runtime object is not fully loaded : ' + this.name); + return null; + } + var instance = function(deltaTime) { this.args['deltaTime'] = deltaTime; var f = new Function(this.code).apply(this.parentEntity, this.args); @@ -61,22 +70,18 @@ GameLib.D3.CustomCode.prototype.updateInstance = function() { */ GameLib.D3.CustomCode.prototype.toApiObject = function() { - var apiArgs = []; - - if (this.args) { - apiArgs = this.args.map( - function(arg) { - return GameLib.Utils.IdOrNull(arg); - } - ) - } + var apiArgs = this.args.map( + function(arg) { + return GameLib.Utils.IdOrNull(arg); + } + ); return new GameLib.D3.API.CustomCode( this.id, this.name, + GameLib.Utils.IdOrNull(this.parentEntity), this.code, this.domElementId, - GameLib.Utils.IdOrNull(this.parentEntity), apiArgs ); diff --git a/src/game-lib-d3-image-factory.js b/src/game-lib-d3-image-factory.js index 607f111..875b8e2 100644 --- a/src/game-lib-d3-image-factory.js +++ b/src/game-lib-d3-image-factory.js @@ -22,11 +22,6 @@ GameLib.D3.ImageFactory = function ( return apiImageFactory; } - if (GameLib.Utils.UndefinedOrNull(progressCallback)) { - progressCallback = null; - } - this.progressCallback = progressCallback; - GameLib.D3.API.ImageFactory.call( this, apiImageFactory.id, @@ -35,18 +30,29 @@ GameLib.D3.ImageFactory = function ( apiImageFactory.parentEntity ); - this.buildIdToObject(); + if (GameLib.Utils.UndefinedOrNull(progressCallback)) { + progressCallback = null; + } + this.progressCallback = progressCallback; + + GameLib.Component.call( + this, + GameLib.Component.COMPONENT_IMAGE_FACTORY + ); this.promiseList = {}; - - this.instance = this.createInstance(); }; GameLib.D3.ImageFactory.prototype = Object.create(GameLib.D3.API.ImageFactory.prototype); GameLib.D3.ImageFactory.prototype.constructor = GameLib.D3.ImageFactory; GameLib.D3.ImageFactory.prototype.createInstance = function(update) { - + + if (!this.loaded) { + console.log('Attempted to create an instance but the runtime object is not fully loaded : ' + this.name); + return null; + } + var instance = null; if (update) { diff --git a/src/game-lib-d3-input-drive.js b/src/game-lib-d3-input-drive.js index f05a843..72167cd 100644 --- a/src/game-lib-d3-input-drive.js +++ b/src/game-lib-d3-input-drive.js @@ -49,9 +49,17 @@ GameLib.D3.Input.Drive = function ( this.keyRight = false; - this.buildIdToObject(); - - this.instance = this.createInstance(); + GameLib.Component.call( + this, + GameLib.Component.COMPONENT_INPUT_DRIVE, + { + 'pathFollowingComponent' : GameLib.D3.PathFollowing, + 'wheelFL' : GameLib.D3.Mesh, + 'wheelFR' : GameLib.D3.Mesh, + 'wheelRL' : GameLib.D3.Mesh, + 'wheelRR' : GameLib.D3.Mesh + } + ); }; GameLib.D3.Input.Drive.prototype = Object.create(GameLib.D3.API.Input.Drive.prototype); @@ -59,6 +67,11 @@ GameLib.D3.Input.Drive.prototype.constructor = GameLib.D3.Input.Drive; GameLib.D3.Input.Drive.prototype.createInstance = function(update) { + if (!this.loaded) { + console.log('Attempted to create an instance but the runtime object is not fully loaded : ' + this.name); + return null; + } + if (update) { return this.instance; } diff --git a/src/game-lib-d3-input-editor.js b/src/game-lib-d3-input-editor.js index 4ac9947..1f14e34 100644 --- a/src/game-lib-d3-input-editor.js +++ b/src/game-lib-d3-input-editor.js @@ -68,15 +68,25 @@ GameLib.D3.Input.Editor = function ( this.graphics ); - this.buildIdToObject(); - - this.instance = this.createInstance(); + GameLib.Component.call( + this, + GameLib.Component.COMPONENT_INPUT_EDITOR, + { + 'camera' : GameLib.D3.Camera + } + ); }; GameLib.D3.Input.Editor.prototype = Object.create(GameLib.D3.API.Input.Editor.prototype); GameLib.D3.Input.Editor.prototype.constructor = GameLib.D3.Input.Editor; GameLib.D3.Input.Editor.prototype.createInstance = function(update) { + + if (!this.loaded) { + console.log('Attempted to create an instance but the runtime object is not fully loaded : ' + this.name); + return null; + } + var instance = null; if (update) { diff --git a/src/game-lib-d3-light.js b/src/game-lib-d3-light.js index 1116bf3..3407526 100644 --- a/src/game-lib-d3-light.js +++ b/src/game-lib-d3-light.js @@ -75,7 +75,13 @@ GameLib.D3.Light = function( this ); - this.instance = this.createInstance(); + GameLib.Component.call( + this, + GameLib.Component.COMPONENT_LIGHT, + { + 'parentScene' : GameLib.D3.Scene + } + ); }; GameLib.D3.Light.prototype = Object.create(GameLib.D3.API.Light.prototype); diff --git a/src/game-lib-d3-look-at.js b/src/game-lib-d3-look-at.js index f6d468b..d7619b1 100644 --- a/src/game-lib-d3-look-at.js +++ b/src/game-lib-d3-look-at.js @@ -60,12 +60,23 @@ GameLib.D3.LookAt = function ( this ); - this.buildIdToObject(); + GameLib.Component.call( + this, + GameLib.Component.COMPONENT_LOOK_AT, + { + 'currentComponent' : GameLib.Component, + 'targetComponent' : GameLib.Component + } + ); }; GameLib.D3.LookAt.prototype = Object.create(GameLib.D3.API.LookAt.prototype); GameLib.D3.LookAt.prototype.constructor = GameLib.D3.LookAt; +GameLib.D3.LookAt.prototype.createInstance = function() { + console.log('LookAt.createInstance()'); +}; + GameLib.D3.LookAt.prototype.toApiObject = function() { var apiLookAt = new GameLib.D3.API.LookAt( diff --git a/src/game-lib-d3-material.js b/src/game-lib-d3-material.js index 50de915..94ba832 100644 --- a/src/game-lib-d3-material.js +++ b/src/game-lib-d3-material.js @@ -221,9 +221,24 @@ GameLib.D3.Material = function( } } - this.buildIdToObject(); - - this.instance = this.createInstance(); + GameLib.Component.call( + this, + GameLib.Component.COMPONENT_MATERIAL, + { + 'alphaMap' : GameLib.D3.Texture, + 'aoMap' : GameLib.D3.Texture, + 'bumpMap' : GameLib.D3.Texture, + 'diffuseMap' : GameLib.D3.Texture, + 'displacementMap' : GameLib.D3.Texture, + 'emissiveMap' : GameLib.D3.Texture, + 'environmentMap' : GameLib.D3.Texture, + 'lightMap' : GameLib.D3.Texture, + 'metalnessMap' : GameLib.D3.Texture, + 'normalMap' : GameLib.D3.Texture, + 'roughnessMap' : GameLib.D3.Texture, + 'specularMap' : GameLib.D3.Texture + } + ); this.updateTextures(); }; diff --git a/src/game-lib-d3-mesh-0.js b/src/game-lib-d3-mesh-0.js index e8e9430..ac93ebb 100644 --- a/src/game-lib-d3-mesh-0.js +++ b/src/game-lib-d3-mesh-0.js @@ -122,16 +122,16 @@ GameLib.D3.Mesh = function ( this ); - this.buildIdToObject(); - - this.instance = this.createInstance(); - - this.instance.geometry.computeBoundingBox(); - - this.width = this.instance.geometry.boundingBox.max.x - this.instance.geometry.boundingBox.min.x; - this.height = this.instance.geometry.boundingBox.max.y - this.instance.geometry.boundingBox.min.y; - this.depth = this.instance.geometry.boundingBox.max.z - this.instance.geometry.boundingBox.min.z; - + GameLib.Component.call( + this, + GameLib.Component.COMPONENT_MESH, + { + 'parentMesh' : GameLib.D3.Mesh, + 'parentScene' : GameLib.D3.Scene, + 'materials' : [GameLib.D3.Material], + 'skeleton' : GameLib.D3.Skeleton + } + ); }; GameLib.D3.Mesh.prototype = Object.create(GameLib.D3.API.Mesh.prototype); @@ -328,6 +328,12 @@ GameLib.D3.Mesh.prototype.createInstance = function(update) { this.instance.renderOrder = this.renderOrder; + this.instance.geometry.computeBoundingBox(); + + this.width = this.instance.geometry.boundingBox.max.x - this.instance.geometry.boundingBox.min.x; + this.height = this.instance.geometry.boundingBox.max.y - this.instance.geometry.boundingBox.min.y; + this.depth = this.instance.geometry.boundingBox.max.z - this.instance.geometry.boundingBox.min.z; + } else { var instance = null; @@ -432,6 +438,12 @@ GameLib.D3.Mesh.prototype.createInstance = function(update) { instance.renderOrder = this.renderOrder; + instance.geometry.computeBoundingBox(); + + this.width = instance.geometry.boundingBox.max.x - instance.geometry.boundingBox.min.x; + this.height = instance.geometry.boundingBox.max.y - instance.geometry.boundingBox.min.y; + this.depth = instance.geometry.boundingBox.max.z - instance.geometry.boundingBox.min.z; + this.subscribe( GameLib.Event.MATERIAL_LOADED, function(data) { diff --git a/src/game-lib-d3-mesh-sphere.js b/src/game-lib-d3-mesh-sphere.js index 3bbe709..6bbd744 100644 --- a/src/game-lib-d3-mesh-sphere.js +++ b/src/game-lib-d3-mesh-sphere.js @@ -73,11 +73,9 @@ GameLib.D3.Mesh.Sphere.prototype.toApiObject = function() { * Converts a standard object mesh to a GameLib.D3.Mesh * @param graphics GameLib.D3.Graphics * @param objectMesh {Object} - * @param computeNormals boolean to indicate whether or not to recalculate normals - * @param imageFactory GameLib.D3.ImageFactory * @constructor */ -GameLib.D3.Mesh.Sphere.FromObject = function(graphics, objectMesh, computeNormals, imageFactory) { +GameLib.D3.Mesh.Sphere.FromObject = function(graphics, objectMesh) { var apiMesh = GameLib.D3.API.Mesh.FromObject(objectMesh); diff --git a/src/game-lib-d3-pass.js b/src/game-lib-d3-pass.js index 0dfd386..d0f1753 100644 --- a/src/game-lib-d3-pass.js +++ b/src/game-lib-d3-pass.js @@ -31,9 +31,14 @@ GameLib.D3.Pass = function ( apiPass.parentEntity ); - this.buildIdToObject(); - - this.instance = this.createInstance(); + GameLib.Component.call( + this, + GameLib.Component.COMPONENT_PASS, + { + 'camera' : GameLib.D3.Camera, + 'scene' : GameLib.D3.Scene + } + ); }; GameLib.D3.Pass.prototype = Object.create(GameLib.D3.API.Pass.prototype); diff --git a/src/game-lib-d3-path-following.js b/src/game-lib-d3-path-following.js index e826509..2a0076c 100644 --- a/src/game-lib-d3-path-following.js +++ b/src/game-lib-d3-path-following.js @@ -109,12 +109,24 @@ GameLib.D3.PathFollowing = function ( this.my = new GameLib.Utils.MovingAverage(10); this.mz = new GameLib.Utils.MovingAverage(10); - this.buildIdToObject(); + GameLib.Component.call( + this, + GameLib.Component.COMPONENT_PATH_FOLLOWING, + { + 'spline': GameLib.D3.Spline, + 'mesh' : GameLib.D3.Mesh, + 'raytraceMesh' : GameLib.D3.Mesh + } + ); }; GameLib.D3.PathFollowing.prototype = Object.create(GameLib.D3.API.PathFollowing.prototype); GameLib.D3.PathFollowing.prototype.constructor = GameLib.D3.PathFollowing; +GameLib.D3.PathFollowing.prototype.createInstance = function() { + console.log('GameLib.D3.PathFollowing.prototype.createInstance()'); +}; + GameLib.D3.PathFollowing.prototype.toApiObject = function() { var apiPathFollowing = new GameLib.D3.API.PathFollowing( diff --git a/src/game-lib-d3-render-target.js b/src/game-lib-d3-render-target.js index a877bea..b44591c 100644 --- a/src/game-lib-d3-render-target.js +++ b/src/game-lib-d3-render-target.js @@ -33,9 +33,13 @@ GameLib.D3.RenderTarget = function ( apiRenderTarget.texture ); - this.buildIdToObject(); - - this.instance = this.createInstance(); + GameLib.Component.call( + this, + GameLib.Component.COMPONENT_RENDER_TARGET, + { + 'texture' : GameLib.D3.Texture + } + ); }; GameLib.D3.RenderTarget.prototype = Object.create(GameLib.D3.API.RenderTarget.prototype); diff --git a/src/game-lib-d3-renderer.js b/src/game-lib-d3-renderer.js index 2e9f4a1..ae69fae 100644 --- a/src/game-lib-d3-renderer.js +++ b/src/game-lib-d3-renderer.js @@ -46,7 +46,14 @@ GameLib.D3.Renderer = function ( ); } - this.instance = this.createInstance(); + GameLib.Component.call( + this, + GameLib.Component.COMPONENT_RENDERER, + { + 'domElement' : GameLib.DomElement + } + ); + }; GameLib.D3.Renderer.prototype = Object.create(GameLib.D3.API.Renderer.prototype); diff --git a/src/game-lib-d3-scene.js b/src/game-lib-d3-scene.js index 411aa32..5f8e60b 100644 --- a/src/game-lib-d3-scene.js +++ b/src/game-lib-d3-scene.js @@ -100,7 +100,7 @@ GameLib.D3.Scene = function ( apiTexture ); - this.idToObject[texture.id] = texture; + // this.idToObject[texture.id] = texture; return texture; } else { @@ -121,7 +121,7 @@ GameLib.D3.Scene = function ( apiMaterial ); - this.idToObject[material.id] = material; + // this.idToObject[material.id] = material; return material; @@ -143,7 +143,7 @@ GameLib.D3.Scene = function ( apiImage ); - this.idToObject[image.id] = image; + // this.idToObject[image.id] = image; return image; @@ -162,13 +162,26 @@ GameLib.D3.Scene = function ( ); } - this.idToObject[this.id] = this; + /** + * TODO : Refactor (linking ?) + * @type {GameLib.D3.Scene} + */ + // this.idToObject[this.id] = this; + // + // this.linkObjects(this.idToObject); - this.linkObjects(this.idToObject); - - this.buildIdToObject(); - - this.instance = this.createInstance(); + GameLib.Component.call( + this, + GameLib.Component.COMPONENT_SCENE, + { + 'meshes' : [GameLib.D3.Mesh], + 'lights' : [GameLib.D3.Light], + 'textures' : [GameLib.D3.Texture], + 'materials' : [GameLib.D3.Material], + 'images' : [GameLib.D3.Image], + 'activeCamera' : GameLib.D3.Camera + } + ); }; GameLib.D3.Scene.prototype = Object.create(GameLib.D3.API.Scene.prototype); diff --git a/src/game-lib-d3-skeleton.js b/src/game-lib-d3-skeleton.js index 29a15de..37ee126 100644 --- a/src/game-lib-d3-skeleton.js +++ b/src/game-lib-d3-skeleton.js @@ -83,9 +83,13 @@ GameLib.D3.Skeleton = function Skeleton( }.bind(this) ); - this.buildIdToObject(); - - this.instance = this.createInstance(); + GameLib.Component.call( + this, + GameLib.Component.COMPONENT_SKELETON, + { + 'bones' : [GameLib.D3.Bone] + } + ); }; GameLib.D3.Skeleton.prototype = Object.create(GameLib.D3.API.Skeleton.prototype); @@ -122,7 +126,7 @@ GameLib.D3.Skeleton.prototype.createInstance = function(update) { return result; } - if (bone.parentBoneIds.length == 0) { + if (bone.parentBoneIds.length === 0) { return bone.instance; } diff --git a/src/game-lib-d3-spline.js b/src/game-lib-d3-spline.js index 61bdc29..47dc941 100644 --- a/src/game-lib-d3-spline.js +++ b/src/game-lib-d3-spline.js @@ -37,7 +37,10 @@ GameLib.D3.Spline = function ( } ); - this.instance = this.createInstance(); + GameLib.Component.call( + this, + GameLib.Component.COMPONENT_SPLINE + ); }; GameLib.D3.Spline.prototype = Object.create(GameLib.D3.API.Spline.prototype); diff --git a/src/game-lib-d3-stats.js b/src/game-lib-d3-stats.js index ceccd86..e8c6255 100644 --- a/src/game-lib-d3-stats.js +++ b/src/game-lib-d3-stats.js @@ -19,15 +19,6 @@ GameLib.D3.Stats = function( } this.stats = stats; - GameLib.Component.call( - this, - GameLib.Component.COMPONENT_STATS, - { - 'domElement': GameLib.DomElement - }, - parentEntity - ); - if (GameLib.Utils.UndefinedOrNull(id)) { id = GameLib.Utils.RandomId(); } @@ -44,7 +35,18 @@ GameLib.D3.Stats = function( } this.domElement = domElement; - this.instance = this.createInstance(); + if (GameLib.Utils.UndefinedOrNull(parentEntity)) { + parentEntity = null; + } + this.parentEntity = parentEntity; + + GameLib.Component.call( + this, + GameLib.Component.COMPONENT_STATS, + { + 'domElement': GameLib.DomElement + } + ); }; GameLib.D3.Stats.prototype = Object.create(GameLib.Component.prototype); diff --git a/src/game-lib-d3-texture.js b/src/game-lib-d3-texture.js index 792dfe8..a6765ea 100644 --- a/src/game-lib-d3-texture.js +++ b/src/game-lib-d3-texture.js @@ -65,7 +65,13 @@ GameLib.D3.Texture = function( ); } - this.instance = this.createInstance(); + GameLib.Component.call( + this, + GameLib.Component.COMPONENT_TEXTURE, + { + 'image' : GameLib.D3.Image + } + ); }; GameLib.D3.Texture.prototype = Object.create(GameLib.D3.API.Texture.prototype); diff --git a/src/game-lib-d3-viewport.js b/src/game-lib-d3-viewport.js index 8c84224..44ddcc3 100644 --- a/src/game-lib-d3-viewport.js +++ b/src/game-lib-d3-viewport.js @@ -32,9 +32,13 @@ GameLib.D3.Viewport = function ( apiViewport.parentEntity ); - this.buildIdToObject(); - - this.instance = this.createInstance(); + GameLib.Component.call( + this, + GameLib.Component.COMPONENT_VIEWPORT, + { + 'scenes' : [GameLib.D3.Scene] + } + ); }; GameLib.D3.Viewport.prototype = Object.create(GameLib.D3.API.Viewport.prototype); diff --git a/src/game-lib-dom-element.js b/src/game-lib-dom-element.js index 02132d9..eea64bd 100644 --- a/src/game-lib-dom-element.js +++ b/src/game-lib-dom-element.js @@ -17,7 +17,10 @@ GameLib.DomElement = function (apiDomElement) { apiDomElement.parentEntity ); - this.instance = this.createInstance(); + GameLib.Component.call( + this, + GameLib.Component.COMPONENT_DOM_ELEMENT + ); }; GameLib.DomElement.prototype = Object.create(GameLib.API.DomElement.prototype); diff --git a/src/game-lib-entity-manager.js b/src/game-lib-entity-manager.js index 6c5765c..cefc552 100644 --- a/src/game-lib-entity-manager.js +++ b/src/game-lib-entity-manager.js @@ -1,85 +1,38 @@ /** * GameLib.EntityManager - * @param graphics GameLib.D3.Graphics - * @param apiEntityManager GameLib.API.EntityManager * @constructor */ -GameLib.EntityManager = function( - graphics, - apiEntityManager -) { +GameLib.EntityManager = function() { - this.graphics = graphics; - this.graphics.isNotThreeThrow(); + this.id = GameLib.Utils.RandomId(); - if (GameLib.Utils.UndefinedOrNull(apiEntityManager)) { - apiEntityManager = {}; - } + this.name = 'Entity Manager (' + this.id + ')'; - if (apiEntityManager instanceof GameLib.EntityManager) { - return apiEntityManager; - } + this.entities = []; - GameLib.API.EntityManager.call( + GameLib.Component.call( this, - apiEntityManager.id, - apiEntityManager.name, - apiEntityManager.entities, - // apiEntityManager.systems, - apiEntityManager.parentEntity + GameLib.Component.COMPONENT_ENTITY_MANAGER, + { + 'entities' : [GameLib.Entity] + }, + null ); - this.entities = this.entities.map( - function(apiEntity) { + this.loaded = []; - if (apiEntity instanceof GameLib.API.Entity) { - return new GameLib.Entity( - this.graphics, - apiEntity - ) - } else { - console.warn('Entity not of type API.Entity'); - throw new Error('Entity not of type API.Entity'); - } + this.dependencies = {}; - }.bind(this) - ); - - // this.systems = this.systems.map( - // function(apiSystem) { - // - // if (apiSystem instanceof GameLib.API.System) { - // return new GameLib.System( - // this.graphics, - // apiSystem - // ) - // } else { - // console.warn('System not of type API.System'); - // throw new Error('System not of type API.System'); - // } - // - // }.bind(this) - // ); - - this.buildIdToObject(); - - this.instance = this.createInstance(); + this.subscriptions = []; this.registerCallbacks(); }; -GameLib.EntityManager.prototype = Object.create(GameLib.API.EntityManager.prototype); +GameLib.EntityManager.prototype = Object.create(GameLib.Component.prototype); GameLib.EntityManager.prototype.constructor = GameLib.EntityManager; -/** - * Creates an Entity Manager instance - * @returns {*} - */ GameLib.EntityManager.prototype.createInstance = function() { - /** - * Fuck the current ECS bullshit on the internet - both tiny-ecs and ecsjs SUCKS ASS - */ - return null; + return GameLib.EntityManager.Instance; }; /** @@ -303,14 +256,11 @@ GameLib.EntityManager.prototype.toApiObject = function() { * @param objectEntityManager Object * @constructor */ -GameLib.EntityManager.FromObject = function(graphics, objectEntityManager) { +GameLib.EntityManager.FromObject = function(objectEntityManager) { var apiEntityManager = GameLib.API.EntityManager.FromObject(objectEntityManager); - var entityManager = new GameLib.EntityManager( - graphics, - apiEntityManager - ); + var entityManager = new GameLib.EntityManager(apiEntityManager); return entityManager; }; @@ -428,21 +378,108 @@ GameLib.EntityManager.prototype.onParentEntityChange = function(data) { }; +GameLib.EntityManager.prototype.componentCreated = function(data) { + + console.log('component created : ' + data.component.name); + + /** + * If we notify ourselves - ignore it + */ + if (data.component === this) { + return; + } + + /** + * Store this component into our 'loaded' list + */ + this.loaded.push(data.component); + + /** + * Store the dependencies too + */ + data.component.dependencies.map(function(id) { + + if (GameLib.Utils.UndefinedOrNull(this.dependencies[id])) { + this.dependencies[id] = []; + } + + this.dependencies[id].push(data.component); + + }.bind(this)); + + /** + * Now find all the dependencies of this component + */ + var dependencies = this.dependencies[data.component.id]; + if (GameLib.Utils.UndefinedOrNull(dependencies)) { + + /** + * We have no dependencies, so mark our component as loaded and create an instance + */ + data.component.loaded = true; + data.component.instance = data.component.createInstance(); + + /** + * If we have none, we are done and can exit + */ + return; + } + + /** + * Otherwise, now - for each dependency - update 'idToObject' and check if its loaded + */ + dependencies.map(function(component){ + component.idToObject[data.component.id] = data.component; + + var loaded = true; + + for (var property in component.idToObject) { + if ( + component.idToObject.hasOwnProperty(property) && + component.idToObject[property] === null + ) { + loaded = false + } + } + + component.loaded = loaded; + + if (component.loaded) { + /** + * Our component is fully loaded, time to create an instance of it + */ + component.instance = component.createInstance(); + } + + }) + +}; + /** * */ GameLib.EntityManager.prototype.registerCallbacks = function() { - this.subscribe( - GameLib.Event.PARENT_SCENE_CHANGE, - this.onParentSceneChange + this.subscriptions.push( + this.subscribe( + GameLib.Event.PARENT_SCENE_CHANGE, + this.onParentSceneChange + ) ); - this.subscribe( - GameLib.Event.PARENT_ENTITY_CHANGE, - this.onParentEntityChange - ); + this.subscriptions.push( + this.subscribe( + GameLib.Event.PARENT_ENTITY_CHANGE, + this.onParentEntityChange + ) + ); + this.subscriptions.push( + this.subscribe( + GameLib.Event.COMPONENT_CREATED, + this.componentCreated + ) + ); }; /** diff --git a/src/game-lib-entity.js b/src/game-lib-entity.js index aa8ad0f..0d0dbf2 100644 --- a/src/game-lib-entity.js +++ b/src/game-lib-entity.js @@ -31,60 +31,14 @@ GameLib.Entity = function ( this.componentToCreate = 0; - // this.components = this.components.map( - // - // function (apiComponent) { - // - // if (apiComponent instanceof GameLib.D3.API.PathFollowing) { - // return new GameLib.D3.PathFollowing(this.graphics, apiComponent); - // } - // - // if (apiComponent instanceof GameLib.D3.API.Renderer) { - // return new GameLib.D3.Renderer(this.graphics, apiComponent); - // } - // - // if (apiComponent instanceof GameLib.D3.API.RenderTarget) { - // return new GameLib.D3.RenderTarget(this.graphics, apiComponent); - // } - // - // if (apiComponent instanceof GameLib.D3.API.Pass) { - // return new GameLib.D3.Pass(this.graphics, apiComponent); - // } - // - // if (apiComponent instanceof GameLib.D3.API.Composer) { - // return new GameLib.D3.Composer(this.graphics, apiComponent); - // } - // - // if (apiComponent instanceof GameLib.D3.API.Camera) { - // return new GameLib.D3.Camera(this.graphics, apiComponent); - // } - // - // if (apiComponent instanceof GameLib.D3.API.LookAt) { - // return new GameLib.D3.LookAt(this.graphics, apiComponent); - // } - // - // if (apiComponent instanceof GameLib.D3.API.Follow) { - // return new GameLib.D3.Follow(this.graphics, apiComponent); - // } - // - // if (apiComponent instanceof GameLib.D3.API.Input.Editor) { - // return new GameLib.D3.Input.Editor(this.graphics, apiComponent); - // } - // - // if (apiComponent instanceof GameLib.D3.API.Input.Drive) { - // return new GameLib.D3.Input.Drive(this.graphics, apiComponent); - // } - // - // if (apiComponent instanceof GameLib.D3.API.Spline) { - // return new GameLib.D3.Spline(this.graphics, apiComponent); - // } - // - // return apiComponent; - // - // }.bind(this) - // ); - - this.instance = this.createInstance(); + GameLib.Component.call( + this, + GameLib.Component.COMPONENT_ENTITY, + { + 'components' : [GameLib.Component], + 'activeComponent' : GameLib.Component + } + ); }; GameLib.Entity.prototype = Object.create(GameLib.API.Entity.prototype); diff --git a/src/game-lib-system-0.js b/src/game-lib-system-0.js index 54813ad..1f2d3bc 100644 --- a/src/game-lib-system-0.js +++ b/src/game-lib-system-0.js @@ -25,20 +25,17 @@ GameLib.System = function( apiSystem.id, apiSystem.name, apiSystem.systemType, - apiSystem.entityManager, apiSystem.parentEntity ); - if (this.entityManager instanceof GameLib.API.EntityManager) { - this.entityManager = new GameLib.EntityManager( - this.graphics, - this.entityManager - ); - } - this.saveCallback = null; this.loadCallback = null; + GameLib.Component.call( + this, + GameLib.Component.COMPONENT_SYSTEM + ); + }; GameLib.System.prototype = Object.create(GameLib.API.System.prototype); @@ -52,6 +49,10 @@ GameLib.System.SYSTEM_TYPE_STORAGE = 0x8; GameLib.System.SYSTEM_TYPE_GUI = 0x10; GameLib.System.SYSTEM_TYPE_ALL = 0xFFFF; +GameLib.System.prototype.createInstance = function() { + console.log('GameLib.System.prototype.createInstance();'); +}; + /** * @callback * @override @@ -63,16 +64,16 @@ GameLib.System.prototype.start = function() { /** * Hookup all editor input capabilities */ - var entities = this.entityManager.query([GameLib.D3.Input.Editor]); + var entities = GameLib.EntityManager.Instance.query([GameLib.D3.Input.Editor]); entities.map(function(entity){ var component = entity.getFirstComponent(GameLib.D3.Input.Editor); - component.mouseDown = component.onMouseDown(entity, this.entityManager).bind(component); + component.mouseDown = component.onMouseDown(entity, GameLib.EntityManager.Instance).bind(component); component.mouseMove = component.onMouseMove(entity).bind(component); - component.keyDown = component.onKeyDown(entity, this.entityManager).bind(component); - component.keyUp = component.onKeyUp(entity, this.entityManager).bind(component); + component.keyDown = component.onKeyDown(entity, GameLib.EntityManager.Instance).bind(component); + component.keyUp = component.onKeyUp(entity, GameLib.EntityManager.Instance).bind(component); component.domElement.instance.addEventListener('mousedown', component.mouseDown, false); component.domElement.instance.addEventListener('mousemove', component.mouseMove, false); @@ -129,7 +130,7 @@ GameLib.System.prototype.start = function() { if (this.systemType === GameLib.System.SYSTEM_TYPE_RENDER) { - this.renderEntities = this.entityManager.query( + this.renderEntities = GameLib.EntityManager.Instance.query( [ GameLib.D3.Viewport, GameLib.D3.Scene, @@ -149,7 +150,7 @@ GameLib.System.prototype.start = function() { if (this.systemType === GameLib.System.SYSTEM_TYPE_GUI) { - var guis = this.entityManager.queryComponents(GameLib.GUI); + var guis = GameLib.EntityManager.Instance.queryComponents(GameLib.GUI); guis.map(function(gui){ gui.domElement.instance.parentElement.appendChild(gui.instance.domElement); @@ -158,12 +159,12 @@ GameLib.System.prototype.start = function() { } if (this.systemType === GameLib.System.SYSTEM_TYPE_ANIMATION) { - // this.pathFollowingObjects = this.entityManager.query([GameLib.D3.PathFollowing]); - // this.followObjects = this.entityManager.query([GameLib.D3.Follow]); - // this.meshObjects = this.entityManager.query([GameLib.D3.Mesh]); - // this.lookAtObjects = this.entityManager.query([GameLib.D3.LookAt]); - // this.cameraObjects = this.entityManager.query([GameLib.D3.Camera]); - // this.lightObjects = this.entityManager.query([GameLib.D3.Light]); + // this.pathFollowingObjects = GameLib.EntityManager.Instance.query([GameLib.D3.PathFollowing]); + // this.followObjects = GameLib.EntityManager.Instance.query([GameLib.D3.Follow]); + // this.meshObjects = GameLib.EntityManager.Instance.query([GameLib.D3.Mesh]); + // this.lookAtObjects = GameLib.EntityManager.Instance.query([GameLib.D3.LookAt]); + // this.cameraObjects = GameLib.EntityManager.Instance.query([GameLib.D3.Camera]); + // this.lightObjects = GameLib.EntityManager.Instance.query([GameLib.D3.Light]); } if (this.systemType === GameLib.System.SYSTEM_TYPE_STORAGE) { @@ -312,7 +313,7 @@ GameLib.System.prototype.stop = function() { /** * Now remove all editor input capabilities */ - var entities = this.entityManager.query([GameLib.D3.Input.Editor]); + var entities = GameLib.EntityManager.Instance.query([GameLib.D3.Input.Editor]); entities.map(function(entity){ var component = entity.getFirstComponent(GameLib.D3.Input.Editor); @@ -330,7 +331,7 @@ GameLib.System.prototype.stop = function() { if (this.systemType === GameLib.System.SYSTEM_TYPE_GUI) { console.log('stopping GUI system'); - var guis = this.entityManager.queryComponents(GameLib.GUI); + var guis = GameLib.EntityManager.Instance.queryComponents(GameLib.GUI); guis.map(function(gui){ gui.domElement.instance.parentElement.removeChild(gui.instance.domElement); @@ -383,7 +384,6 @@ GameLib.System.prototype.toApiObject = function() { this.id, this.name, this.systemType, - GameLib.Utils.IdOrNull(this.entityManager), GameLib.Utils.IdOrNull(this.parentEntity) ); }; diff --git a/src/game-lib-z.js b/src/game-lib-z.js index 771958c..1bdf272 100644 --- a/src/game-lib-z.js +++ b/src/game-lib-z.js @@ -1,3 +1,6 @@ + +GameLib.EntityManager.Instance = new GameLib.EntityManager(); + if (typeof module !== 'undefined') { module.exports = GameLib; } \ No newline at end of file