diff --git a/src/game-lib-d3-api-helper.js b/bak/game-lib-d3-api-helper.js similarity index 100% rename from src/game-lib-d3-api-helper.js rename to bak/game-lib-d3-api-helper.js diff --git a/src/game-lib-a.js b/src/game-lib-a-0.js similarity index 100% rename from src/game-lib-a.js rename to src/game-lib-a-0.js diff --git a/src/game-lib-api-component.js b/src/game-lib-a-api-component.js similarity index 98% rename from src/game-lib-api-component.js rename to src/game-lib-a-api-component.js index 88c0134..91146ec 100644 --- a/src/game-lib-api-component.js +++ b/src/game-lib-a-api-component.js @@ -39,7 +39,7 @@ GameLib.API.Component = function( GameLib.API.Component.FromObjectComponent = function(objectComponent) { if (objectComponent instanceof Object) { - if (objectComponent.componentType == GameLib.Component.COMPONENT_TYPE_PATH_FOLLOWING) { + if (objectComponent.componentType == GameLib.Component.COMPONENT_PATH_FOLLOWING) { return GameLib.D3.API.PathFollowing.FromObjectComponent(objectComponent); } diff --git a/src/game-lib-component-a.js b/src/game-lib-a-component-a.js similarity index 66% rename from src/game-lib-component-a.js rename to src/game-lib-a-component-a.js index 81d021a..4674bc5 100644 --- a/src/game-lib-component-a.js +++ b/src/game-lib-a-component-a.js @@ -20,13 +20,15 @@ GameLib.Component = function( parentEntity ); + this.idToObject = {}; + this.linkedObjects.parentEntity = GameLib.Entity; }; GameLib.Component.prototype = Object.create(GameLib.API.Component.prototype); GameLib.Component.prototype.constructor = GameLib.Component; -GameLib.Component.COMPONENT_TYPE_PATH_FOLLOWING = 0x1; +GameLib.Component.COMPONENT_PATH_FOLLOWING = 0x1; GameLib.Component.COMPONENT_MATERIAL = 0x2; GameLib.Component.COMPONENT_RENDERER = 0x3; GameLib.Component.COMPONENT_LOOK_AT = 0x5; @@ -48,6 +50,10 @@ GameLib.Component.COMPONENT_SYSTEM = 0x14; GameLib.Component.COMPONENT_GRAPHICS = 0x15; GameLib.Component.COMPONENT_HELPER = 0x16; GameLib.Component.COMPONENT_CUSTOM_CODE = 0x17; +GameLib.Component.COMPONENT_MOUSE = 0x18; +GameLib.Component.COMPONENT_SKELETON = 0x19; +GameLib.Component.COMPONENT_TEXTURE = 0x1a; +GameLib.Component.COMPONENT_ENTITY_MANAGER = 0x1b; /** * Components are linked at runtime - for storing, we just store the ID @@ -55,4 +61,24 @@ GameLib.Component.COMPONENT_CUSTOM_CODE = 0x17; */ GameLib.Component.prototype.toApiComponent = function() { return this.id; -}; \ No newline at end of file +}; + +GameLib.Component.prototype.buildIdToObject = function() { + + this.idToObject = {}; + + for (var property in this.linkedObjects) { + if ( + this.linkedObjects.hasOwnProperty(property) && + this.hasOwnProperty(property) + ) { + if (this.linkedObjects[property] instanceof Array) { + this.idToObject = GameLib.Utils.LoadIdsFromArrayToIdObject(this[property], this.idToObject); + } else { + this.idToObject = GameLib.Utils.LoadIdsFromObjectToIdObject(this[property], this.idToObject); + } + } + } + + this.idToObject[this.id] = this; +}; diff --git a/src/game-lib-api-entity-manager.js b/src/game-lib-api-entity-manager.js index f193b78..7634d74 100644 --- a/src/game-lib-api-entity-manager.js +++ b/src/game-lib-api-entity-manager.js @@ -1,17 +1,46 @@ /** * Entity API Object (for storing / loading entities to and from API) * @constructor + * @param id + * @param name * @param entities GameLib.API.Entity[] + * @param parentEntity */ GameLib.API.EntityManager = function( - entities + id, + name, + entities, + parentEntity ) { + GameLib.Component.call( + this, + GameLib.Component.COMPONENT_ENTITY_MANAGER, + { + 'entities' : [GameLib.Entity] + }, + null, + 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; }; +GameLib.API.EntityManager.prototype = Object.create(GameLib.Component.prototype); +GameLib.API.EntityManager.prototype.constructor = GameLib.API.EntityManager; + /** * Creates an API entity manager from an Object entity manager * @param objectEntityManager @@ -26,6 +55,9 @@ GameLib.API.EntityManager.FromObjectEntityManager = function(objectEntityManager ); return new GameLib.API.EntityManager( - apiEntities + objectEntityManager.id, + objectEntityManager.name, + apiEntities, + objectEntityManager.parentEntity ); }; diff --git a/src/game-lib-api-entity.js b/src/game-lib-api-entity.js index b0624cf..368bf45 100644 --- a/src/game-lib-api-entity.js +++ b/src/game-lib-api-entity.js @@ -16,7 +16,7 @@ GameLib.API.Entity = function( this.id = id; if (GameLib.Utils.UndefinedOrNull(name)) { - name = this.constructor.name; + name = 'Entity (' + this.id + ')'; } this.name = name; diff --git a/src/game-lib-api-mouse.js b/src/game-lib-api-mouse.js index 33f14c2..c62c75d 100644 --- a/src/game-lib-api-mouse.js +++ b/src/game-lib-api-mouse.js @@ -4,15 +4,30 @@ * @param name * @param x * @param y + * @param parentEntity * @constructor */ GameLib.API.Mouse = function( id, name, x, - y + y, + parentEntity ) { + if (GameLib.Utils.UndefinedOrNull(parentEntity)) { + parentEntity = null; + } + this.parentEntity = parentEntity; + + GameLib.Component.call( + this, + GameLib.Component.COMPONENT_MOUSE, + null, + null, + parentEntity + ); + if (GameLib.Utils.UndefinedOrNull(id)) { id = GameLib.Utils.RandomId(); } @@ -35,6 +50,9 @@ GameLib.API.Mouse = function( }; +GameLib.API.Mouse.prototype = Object.create(GameLib.Component.prototype); +GameLib.API.Mouse.prototype.constructor = GameLib.API.Mouse; + /** * Returns an API mouse from an Object mouse * @param objectMouse @@ -45,6 +63,7 @@ GameLib.API.Mouse.FromObjectMouse = function (objectMouse) { objectMouse.id, objectMouse.name, objectMouse.x, - objectMouse.y + objectMouse.y, + objectMouse.parentEntity ) }; diff --git a/src/game-lib-api-system.js b/src/game-lib-api-system.js index 8d0ee8a..08bd704 100644 --- a/src/game-lib-api-system.js +++ b/src/game-lib-api-system.js @@ -44,6 +44,9 @@ GameLib.API.System = function ( this.entityManager = entityManager; }; +GameLib.API.System.prototype = Object.create(GameLib.Component.prototype); +GameLib.API.System.prototype.constructor = GameLib.API.System; + /** * Object to GameLib.D3.API.System * @param objectComponent diff --git a/src/game-lib-d3-api-camera.js b/src/game-lib-d3-api-camera.js index 1a86ad4..af84cbd 100644 --- a/src/game-lib-d3-api-camera.js +++ b/src/game-lib-d3-api-camera.js @@ -56,7 +56,7 @@ GameLib.D3.API.Camera = function( this.id = id; if (GameLib.Utils.UndefinedOrNull(cameraType)) { - cameraType = GameLib.Component.COMPONENT_CAMERA; + cameraType = GameLib.D3.Camera.CAMERA_TYPE_PERSPECTIVE; } this.cameraType = cameraType; diff --git a/src/game-lib-d3-api-editor.js b/src/game-lib-d3-api-editor.js index 00e0d88..8c73e43 100644 --- a/src/game-lib-d3-api-editor.js +++ b/src/game-lib-d3-api-editor.js @@ -22,7 +22,8 @@ GameLib.D3.API.Editor = function( this, GameLib.Component.COMPONENT_EDITOR, { - 'game' : GameLib.D3.Game + 'game' : GameLib.D3.Game, + 'viewports' : [GameLib.D3.Viewport] }, null, parentEntity diff --git a/src/game-lib-d3-api-game.js b/src/game-lib-d3-api-game.js index 6beb951..fe04bcd 100644 --- a/src/game-lib-d3-api-game.js +++ b/src/game-lib-d3-api-game.js @@ -44,7 +44,7 @@ GameLib.D3.API.Game = function( 'cameras' : [GameLib.D3.Camera], 'renderers' : [GameLib.D3.Renderer], 'composers' : [GameLib.D3.Composer], - 'systems' : [GameLib.D3.System], + 'systems' : [GameLib.System], 'viewports' : [GameLib.D3.Viewport], 'entityManager' : GameLib.EntityManager, 'mouse' : GameLib.Mouse @@ -125,7 +125,7 @@ GameLib.D3.API.Game = function( this.entityManager = entityManager; if (GameLib.Utils.UndefinedOrNull(mouse)) { - mouse = null; + mouse = new GameLib.API.Mouse(); } this.mouse = mouse; }; diff --git a/src/game-lib-d3-api-pass.js b/src/game-lib-d3-api-pass.js index f092e4e..b50c50c 100644 --- a/src/game-lib-d3-api-pass.js +++ b/src/game-lib-d3-api-pass.js @@ -45,6 +45,11 @@ GameLib.D3.API.Pass = function ( } this.passType = passType; + if (GameLib.Utils.UndefinedOrNull(camera)) { + camera = null; + } + this.camera = camera; + if (GameLib.Utils.UndefinedOrNull(scene)) { scene = null; } @@ -64,7 +69,6 @@ GameLib.D3.API.Pass = function ( } this.renderToScreen = renderToScreen; - }; 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 8586a08..b733f20 100644 --- a/src/game-lib-d3-api-path-following.js +++ b/src/game-lib-d3-api-path-following.js @@ -51,7 +51,7 @@ GameLib.D3.API.PathFollowing = function ( GameLib.Component.call( this, - GameLib.Component.COMPONENT_TYPE_PATH_FOLLOWING, + GameLib.Component.COMPONENT_PATH_FOLLOWING, { 'spline': GameLib.D3.Spline, 'mesh' : GameLib.D3.Mesh, diff --git a/src/game-lib-d3-api-render-target.js b/src/game-lib-d3-api-render-target.js index 9d2e840..3f201b7 100644 --- a/src/game-lib-d3-api-render-target.js +++ b/src/game-lib-d3-api-render-target.js @@ -29,7 +29,7 @@ GameLib.D3.API.RenderTarget = function ( this, GameLib.Component.COMPONENT_RENDER_TARGET, { - texture : GameLib.D3.Texture + 'texture' : GameLib.D3.Texture }, null, parentEntity diff --git a/src/game-lib-d3-api-skeleton.js b/src/game-lib-d3-api-skeleton.js index 7ddcb12..00dde18 100644 --- a/src/game-lib-d3-api-skeleton.js +++ b/src/game-lib-d3-api-skeleton.js @@ -9,6 +9,7 @@ * @param boneTextureHeight Number * @param boneMatrices GameLib.API.Matrix4[] * @param boneTexture null (not implemented) + * @param parentEntity * @constructor */ GameLib.D3.API.Skeleton = function ( @@ -20,8 +21,24 @@ GameLib.D3.API.Skeleton = function ( boneTextureWidth, boneTextureHeight, boneMatrices, - boneTexture + boneTexture, + parentEntity ) { + if (GameLib.Utils.UndefinedOrNull(parentEntity)) { + parentEntity = null; + } + this.parentEntity = parentEntity; + + GameLib.Component.call( + this, + GameLib.Component.COMPONENT_SKELETON, + { + 'bones' : [GameLib.D3.Bone] + }, + false, + parentEntity + ); + if (GameLib.Utils.UndefinedOrNull(id)) { id = GameLib.Utils.RandomId(); } @@ -77,6 +94,9 @@ GameLib.D3.API.Skeleton = function ( this.boneTexture = boneTexture; }; +GameLib.D3.API.Skeleton.prototype = Object.create(GameLib.Component.prototype); +GameLib.D3.API.Skeleton.prototype.constructor = GameLib.D3.API.Skeleton; + /** * Creates an API skeleton from an Object skeleton * @param objectSkeleton @@ -104,6 +124,7 @@ GameLib.D3.API.Skeleton.FromObjectSkeleton = function(objectSkeleton) { return GameLib.D3.API.Matrix4.FromObjectMatrix(boneMatrix); } ), - objectSkeleton.boneTexture + objectSkeleton.boneTexture, + objectSkeleton.parentEntity ); }; \ No newline at end of file diff --git a/src/game-lib-d3-api-texture.js b/src/game-lib-d3-api-texture.js index 2c1548c..a515942 100644 --- a/src/game-lib-d3-api-texture.js +++ b/src/game-lib-d3-api-texture.js @@ -21,6 +21,7 @@ * @param unpackAlignment * @param premultiplyAlpha * @param encoding + * @param parentEntity * @constructor */ GameLib.D3.API.Texture = function( @@ -44,8 +45,22 @@ GameLib.D3.API.Texture = function( mipmaps, unpackAlignment, premultiplyAlpha, - encoding + encoding, + parentEntity ) { + if (GameLib.Utils.UndefinedOrNull(parentEntity)) { + parentEntity = null; + } + this.parentEntity = parentEntity; + + GameLib.Component.call( + this, + GameLib.Component.COMPONENT_TEXTURE, + null, + null, + parentEntity + ); + if (GameLib.Utils.UndefinedOrNull(id)) { id = GameLib.Utils.RandomId(); } @@ -179,6 +194,7 @@ GameLib.D3.API.Texture.FromObjectTexture = function(objectTexture) { objectTexture.mipmaps, objectTexture.unpackAlignment, objectTexture.premultiplyAlpha, - objectTexture.encoding + objectTexture.encoding, + objectTexture.parentEntity ) }; diff --git a/src/game-lib-d3-camera.js b/src/game-lib-d3-camera.js index 1f1f027..87d759b 100644 --- a/src/game-lib-d3-camera.js +++ b/src/game-lib-d3-camera.js @@ -39,23 +39,38 @@ GameLib.D3.Camera = function( apiCamera.focalLength ); - this.position = new GameLib.Vector3( - graphics, - this.position, - this - ); + if (this.position instanceof GameLib.API.Vector3) { + this.position = new GameLib.Vector3( + graphics, + this.position, + this + ); + } else { + console.warn('position not instance of API.Vector3'); + throw new Error('position not instance of API.Vector3'); + } - this.quaternion = new GameLib.Quaternion( - graphics, - this.quaternion, - this - ); + if (this.quaternion instanceof GameLib.API.Quaternion) { + this.quaternion = new GameLib.Quaternion( + graphics, + this.quaternion, + this + ); + } else { + console.warn('quaternion not instance of API.Quaternion'); + throw new Error('quaternion not instance of API.Quaternion'); + } - this.lookAt = new GameLib.Vector3( - graphics, - this.lookAt, - this - ); + if (this.lookAt instanceof GameLib.API.Vector3) { + this.lookAt = new GameLib.Vector3( + graphics, + this.lookAt, + this + ); + } else { + console.warn('lookAt not instance of API.Vector3'); + throw new Error('lookAt not instance of API.Vector3'); + } this.instance = this.createInstance(); @@ -99,9 +114,7 @@ GameLib.D3.Camera.prototype.createInstance = function(update) { this.maxZ ); } else if (this.cameraType == GameLib.D3.Camera.CAMERA_TYPE_STEREO) { - if (!instance) { - instance = new THREE.StereoCamera(); - } + instance = new THREE.StereoCamera(); } } @@ -133,6 +146,11 @@ GameLib.D3.Camera.prototype.createInstance = function(update) { } } + if (!instance) { + console.log('Unsupported camera type : ' + this.cameraType); + throw new Error('Unsupported camera type : ' + this.cameraType); + } + instance.position.x = this.position.x; instance.position.y = this.position.y; instance.position.z = this.position.z; diff --git a/src/game-lib-d3-composer.js b/src/game-lib-d3-composer.js index f7925dc..9c54254 100644 --- a/src/game-lib-d3-composer.js +++ b/src/game-lib-d3-composer.js @@ -26,6 +26,8 @@ GameLib.D3.Composer = function ( apiComposer.parentEntity ); + this.buildIdToObject(); + this.instance = this.createInstance(); }; @@ -43,7 +45,10 @@ GameLib.D3.Composer.prototype.createInstance = function(update) { if (update) { instance = this.instance; - } else { + } + + if (this.renderer && + this.renderTarget) { if (!THREE.EffectComposer) { console.warn('No THREE.EffectComposer'); diff --git a/src/game-lib-d3-custom-code.js b/src/game-lib-d3-custom-code.js index 5d587ec..c4bf130 100644 --- a/src/game-lib-d3-custom-code.js +++ b/src/game-lib-d3-custom-code.js @@ -21,6 +21,8 @@ GameLib.D3.CustomCode = function( apiCustomCode.args ); + this.buildIdToObject(); + this.instance = this.createInstance(); }; diff --git a/src/game-lib-d3-editor.js b/src/game-lib-d3-editor.js index 059086a..d9fa0df 100644 --- a/src/game-lib-d3-editor.js +++ b/src/game-lib-d3-editor.js @@ -3,6 +3,8 @@ * @param graphics GameLib.D3.Graphics * @param apiEditor GameLib.D3.API.Editor * @param onSelectionChanged + * @param onSelectObject + * @param onDeSelectObject * @constructor */ GameLib.D3.Editor = function( @@ -75,6 +77,8 @@ GameLib.D3.Editor = function( } this.onDeSelectObject = onDeSelectObject; + this.buildIdToObject(); + this.instance = this.createInstance(); }; @@ -172,16 +176,7 @@ GameLib.D3.Editor.prototype.selectObject = function(object) { this.selectedObjects.push( new GameLib.D3.SelectedObject( this.graphics, - object, - new GameLib.D3.Helper( - this.graphics, - new GameLib.D3.API.Helper( - null, - null, - null, - object - ) - ) + object ) ); @@ -220,4 +215,4 @@ GameLib.D3.Editor.prototype.deSelectObject = function(object) { this.onDeSelectObject(results); } -}; \ No newline at end of file +}; diff --git a/src/game-lib-d3-follow.js b/src/game-lib-d3-follow.js index 2a6edaa..f2d9b00 100644 --- a/src/game-lib-d3-follow.js +++ b/src/game-lib-d3-follow.js @@ -56,6 +56,9 @@ GameLib.D3.Follow = function ( this.rotated, this ); + + this.buildIdToObject(); + }; GameLib.D3.Follow.prototype = Object.create(GameLib.D3.API.Follow.prototype); diff --git a/src/game-lib-d3-game.js b/src/game-lib-d3-game.js index c768893..ed66ecf 100644 --- a/src/game-lib-d3-game.js +++ b/src/game-lib-d3-game.js @@ -144,7 +144,29 @@ GameLib.D3.Game = function ( }.bind(this) ); + if (this.entityManager instanceof GameLib.API.EntityManager) { + this.entityManager = new GameLib.EntityManager( + this.graphics, + this.entityManager + ); + } else { + console.warn('EntityManager not of type API.EntityManager'); + throw new Error('EntityManager not of type API.EntityManager'); + } + + if (this.mouse instanceof GameLib.API.Mouse) { + this.mouse = new GameLib.Mouse( + this.graphics, + this.mouse + ); + } else { + console.warn('mouse not of type API.Mouse'); + throw new Error('mouse not of type API.Mouse'); + } + this.buildIdToObject(); + + this.entityManager.linkObjects(this.idToObject); }; GameLib.D3.Game.prototype = Object.create(GameLib.D3.API.Game.prototype); @@ -198,55 +220,6 @@ GameLib.D3.Game.prototype.updateInstance = function() { this.instance = this.createInstance(true); }; -GameLib.D3.Game.prototype.buildIdToObject = function() { - - this.idToObject = {}; - - if (this.entityManager instanceof GameLib.API.EntityManager) { - - this.entityManager = new GameLib.EntityManager( - this.graphics, - this.entityManager - ); - - this.entityManager.entities.map( - function (entity) { - this.idToObject[entity.id] = entity; - entity.components.map( - function(component) { - if (component instanceof GameLib.Component) { - this.idToObject[component.id] = component; - } else { - console.warn('Component not of type Component'); - throw new Error('Component not of type Component'); - } - }.bind(this) - ) - }.bind(this) - ); - - this.entityManager.linkObjects(this.idToObject); - - } else { - console.warn('EntityManager not of type API.EntityManager'); - throw new Error('EntityManager not of type API.EntityManager'); - } - - this.scenes.map( - function(scene) { - - var idToObject = scene.idToObject; - - for (var property in idToObject) { - if (idToObject.hasOwnProperty(property)) { - this.idToObject[property] = idToObject[property]; - } - } - }.bind(this) - ) - -}; - /** * Converts a GameLib.D3.Game to a new GameLib.D3.API.Game * @returns {GameLib.D3.API.Game} diff --git a/src/game-lib-d3-helper.js b/src/game-lib-d3-helper.js index ad1ecaa..3357d8f 100644 --- a/src/game-lib-d3-helper.js +++ b/src/game-lib-d3-helper.js @@ -1,46 +1,83 @@ /** * Helpers for displaying outlines or making 'invisible' scene objects visible * @param graphics GameLib.D3.Graphics - * @param apiHelper GameLib.D3.API.Helper + * @param id + * @param name + * @param object + * @param helperType * @constructor */ GameLib.D3.Helper = function Helper( graphics, - apiHelper + id, + name, + object, + helperType ) { this.graphics = graphics; this.graphics.isNotThreeThrow(); - if (GameLib.Utils.UndefinedOrNull(apiHelper)) { - apiHelper = {}; + if (GameLib.Utils.UndefinedOrNull(id)) { + id = GameLib.Utils.RandomId(); } + this.id = id; - GameLib.D3.API.Helper.call( - this, - apiHelper.id, - apiHelper.name, - apiHelper.helperType, - apiHelper.object, - apiHelper.parentEntity - ); + if (GameLib.Utils.UndefinedOrNull(name)) { + name = 'Helper (' + id + ')'; + } + this.name = name; + + if (GameLib.Utils.UndefinedOrNull(object)) { + console.warn('Cannot create a helper for an Object which does not exist'); + throw new Error('Cannot create a helper for an Object which does not exist'); + } + this.object = object; + + if (GameLib.Utils.UndefinedOrNull(helperType)) { + + helperType = GameLib.D3.Helper.HELPER_TYPE_NONE; + + if ( + object instanceof GameLib.D3.Mesh && + object.meshType != GameLib.D3.Mesh.TYPE_CURVE + ) { + helperType = GameLib.D3.Helper.HELPER_TYPE_WIREFRAME; + } + + if (object instanceof GameLib.D3.Light) { + if (object.lightType == GameLib.D3.Light.LIGHT_TYPE_DIRECTIONAL) { + helperType = GameLib.D3.Helper.HELPER_TYPE_DIRECTIONAL_LIGHT; + } + + if (object.lightType == GameLib.D3.Light.LIGHT_TYPE_POINT) { + helperType = GameLib.D3.Helper.HELPER_TYPE_POINT_LIGHT; + } + + if (object.lightType == GameLib.D3.Light.LIGHT_TYPE_SPOT) { + helperType = GameLib.D3.Helper.HELPER_TYPE_SPOT_LIGHT; + } + } + + if (object instanceof GameLib.D3.Skeleton) { + helperType = GameLib.D3.Helper.HELPER_TYPE_SKELETON; + } + } + this.helperType = helperType; this.instance = this.createInstance(); }; -GameLib.D3.Helper.prototype = Object.create(GameLib.D3.API.Helper.prototype); -GameLib.D3.Helper.prototype.constructor = GameLib.D3.Helper; - /** * Helper types * @type {string} */ -GameLib.D3.Helper.HELPER_TYPE_EDGES = 'edges'; -GameLib.D3.Helper.HELPER_TYPE_DIRECTIONAL_LIGHT = 'directional-light'; -GameLib.D3.Helper.HELPER_TYPE_SPOT_LIGHT = 'spot-light'; -GameLib.D3.Helper.HELPER_TYPE_POINT_LIGHT = 'point-light'; -GameLib.D3.Helper.HELPER_TYPE_WIREFRAME = 'wireframe'; -GameLib.D3.Helper.HELPER_TYPE_SKELETON = 'skeleton'; - +GameLib.D3.Helper.HELPER_TYPE_NONE = 0x0; +GameLib.D3.Helper.HELPER_TYPE_EDGES = 0x1; +GameLib.D3.Helper.HELPER_TYPE_DIRECTIONAL_LIGHT = 0x2; +GameLib.D3.Helper.HELPER_TYPE_SPOT_LIGHT = 0x3; +GameLib.D3.Helper.HELPER_TYPE_POINT_LIGHT = 0x4; +GameLib.D3.Helper.HELPER_TYPE_WIREFRAME = 0x5; +GameLib.D3.Helper.HELPER_TYPE_SKELETON = 0x6; /** * Creates a helper instance @@ -71,17 +108,13 @@ GameLib.D3.Helper.prototype.createInstance = function(update) { } if (this.helperType == GameLib.D3.Helper.HELPER_TYPE_WIREFRAME) { - instance = new THREE.WireframeHelper(this.object.instance, 0x007700); + instance = new THREE.WireframeGeometry(this.object.instance, 0x007700); } if (this.helperType == GameLib.D3.Helper.HELPER_TYPE_SKELETON) { instance = new THREE.SkeletonHelper(this.object.instance); } - if (!instance) { - throw new Error('Unsupported helper type: ' + this.helperType); - } - return instance; }; @@ -91,37 +124,3 @@ GameLib.D3.Helper.prototype.createInstance = function(update) { GameLib.D3.Helper.prototype.updateInstance = function() { this.instance = this.createInstance(true); }; - -/** - * Converts a GameLib.D3.Helper to a new GameLib.D3.API.Helper - * @returns {GameLib.D3.API.Helper} - */ -GameLib.D3.Helper.prototype.toApiHelper = function() { - - return new GameLib.D3.API.Helper( - this.id, - this.name, - this.helperType, - GameLib.Utils.IdOrNull(this.object), - GameLib.Utils.IdOrNull(this.parentEntity) - ); - -}; - -/** - * Converts from an Object Helper to a GameLib.D3.Helper - * @param graphics GameLib.D3.Graphics - * @param objectHelper Object - * @returns {GameLib.D3.Helper} - * @constructor - */ -GameLib.D3.Helper.FromObjectHelper = function(graphics, objectHelper) { - - var apiHelper = GameLib.D3.API.Helper.FromObjectHelper(objectHelper); - - return new GameLib.D3.Helper( - graphics, - apiHelper - ); - -}; diff --git a/src/game-lib-d3-input-drive.js b/src/game-lib-d3-input-drive.js index dde8548..87bfb96 100644 --- a/src/game-lib-d3-input-drive.js +++ b/src/game-lib-d3-input-drive.js @@ -45,6 +45,8 @@ GameLib.D3.Input.Drive = function ( this.keyRight = false; + this.buildIdToObject(); + this.instance = this.createInstance(); }; diff --git a/src/game-lib-d3-input-editor.js b/src/game-lib-d3-input-editor.js index b242fb1..9f291ff 100644 --- a/src/game-lib-d3-input-editor.js +++ b/src/game-lib-d3-input-editor.js @@ -67,6 +67,8 @@ GameLib.D3.Input.Editor = function ( this.graphics ); + this.buildIdToObject(); + this.instance = this.createInstance(); }; diff --git a/src/game-lib-d3-look-at.js b/src/game-lib-d3-look-at.js index b196faa..0acb04d 100644 --- a/src/game-lib-d3-look-at.js +++ b/src/game-lib-d3-look-at.js @@ -55,6 +55,8 @@ GameLib.D3.LookAt = function ( this.targetPosition, this ); + + this.buildIdToObject(); }; GameLib.D3.LookAt.prototype = Object.create(GameLib.D3.API.LookAt.prototype); diff --git a/src/game-lib-d3-material.js b/src/game-lib-d3-material.js index fe8993a..3770831 100644 --- a/src/game-lib-d3-material.js +++ b/src/game-lib-d3-material.js @@ -272,6 +272,8 @@ GameLib.D3.Material = function Material( } } + this.buildIdToObject(); + this.instance = this.createInstance(); }; diff --git a/src/game-lib-d3-mesh.js b/src/game-lib-d3-mesh.js index b890342..8d0939d 100644 --- a/src/game-lib-d3-mesh.js +++ b/src/game-lib-d3-mesh.js @@ -137,6 +137,8 @@ GameLib.D3.Mesh = function ( this ); + this.buildIdToObject(); + this.instance = this.createInstance(false); this.instance.geometry.computeBoundingBox(); diff --git a/src/game-lib-d3-pass.js b/src/game-lib-d3-pass.js index dfc9d27..9e084b6 100644 --- a/src/game-lib-d3-pass.js +++ b/src/game-lib-d3-pass.js @@ -27,6 +27,8 @@ GameLib.D3.Pass = function ( apiPass.parentEntity ); + this.buildIdToObject(); + this.instance = this.createInstance(); }; @@ -47,10 +49,11 @@ GameLib.D3.Pass.prototype.createInstance = function(update) { if (update) { instance = this.instance; - } else { + } - if (this.passType == GameLib.D3.Pass.PASS_TYPE_RENDER) { + if (this.passType == GameLib.D3.Pass.PASS_TYPE_RENDER) { + if (this.scene && this.camera) { if (!THREE.RenderPass) { console.warn('No THREE.RenderPass'); throw new Error('No THREE.RenderPass'); @@ -60,20 +63,25 @@ GameLib.D3.Pass.prototype.createInstance = function(update) { this.scene.instance, this.camera.instance ); - } else if (this.passType == GameLib.D3.Pass.PASS_TYPE_COPY_SHADER) { - if (!THREE.CopyShader) { - console.warn('No THREE.CopyShader'); - throw new Error('No THREE.CopyShader'); - } - - instance = THREE.CopyShader; - } else { - console.warn('Render pass not supported yet: ' + this.passType); - throw new Error('Render pass not supported yet: ' + this.passType); } + + } else if (this.passType == GameLib.D3.Pass.PASS_TYPE_COPY_SHADER) { + + if (!THREE.CopyShader) { + console.warn('No THREE.CopyShader'); + throw new Error('No THREE.CopyShader'); + } + + instance = THREE.CopyShader; + + } else { + console.warn('Render pass not supported yet: ' + this.passType); + throw new Error('Render pass not supported yet: ' + this.passType); } - instance.renderToScreen = this.renderToScreen; + if (instance) { + instance.renderToScreen = this.renderToScreen; + } return instance; }; diff --git a/src/game-lib-d3-path-following.js b/src/game-lib-d3-path-following.js index 56a2f60..224b1c7 100644 --- a/src/game-lib-d3-path-following.js +++ b/src/game-lib-d3-path-following.js @@ -105,6 +105,7 @@ GameLib.D3.PathFollowing = function ( this.my = new GameLib.Utils.MovingAverage(10); this.mz = new GameLib.Utils.MovingAverage(10); + this.buildIdToObject(); }; GameLib.D3.PathFollowing.prototype = Object.create(GameLib.D3.API.PathFollowing.prototype); diff --git a/src/game-lib-d3-render-target.js b/src/game-lib-d3-render-target.js index d5cbfcf..8d95d75 100644 --- a/src/game-lib-d3-render-target.js +++ b/src/game-lib-d3-render-target.js @@ -29,6 +29,8 @@ GameLib.D3.RenderTarget = function ( apiRenderTarget.texture ); + this.buildIdToObject(); + this.instance = this.createInstance(); }; diff --git a/src/game-lib-d3-scene.js b/src/game-lib-d3-scene.js index 36d4e9c..601be15 100644 --- a/src/game-lib-d3-scene.js +++ b/src/game-lib-d3-scene.js @@ -46,48 +46,24 @@ GameLib.D3.Scene = function ( } this.imageFactory = imageFactory; - this.idToObject = {}; this.meshes = this.meshes.map( function(apiMesh) { - var mesh = new GameLib.D3.Mesh( - this.graphics, - apiMesh, - this.computeNormals, - this.imageFactory - ); + if (apiMesh instanceof GameLib.D3.API.Mesh) { - this.idToObject[mesh.id] = mesh; + return new GameLib.D3.Mesh( + this.graphics, + apiMesh, + this.computeNormals, + this.imageFactory + ); - if (mesh.skeleton) { - - this.idToObject[mesh.skeleton.id] = mesh.skeleton; - - mesh.skeleton.bones.map( - function (bone) { - this.idToObject[bone.id] = bone; - }.bind(this) - ) + } else { + console.warn('apiMesh not an instance of API.Mesh'); + throw new Error('apiMesh not an instance of API.Mesh'); } - mesh.materials.map( - function(material) { - this.idToObject[material.id] = material; - - for (var property in material) { - if (material.hasOwnProperty(property)) { - if (material[property] instanceof GameLib.D3.Texture) { - this.idToObject[material[property].id] = material[property]; - } - } - } - - }.bind(this) - ); - - return mesh; - }.bind(this) ); @@ -113,22 +89,21 @@ GameLib.D3.Scene = function ( function(apiLight) { if (apiLight instanceof GameLib.D3.API.Light) { - var light = new GameLib.D3.Light( + + return new GameLib.D3.Light( this.graphics, apiLight ); - this.idToObject[light.id] = light; - - return light; } else { - + console.warn('apiLight not an instance of API.Light'); + throw new Error('apiLight not an instance of API.Light'); } }.bind(this) ); - this.idToObject[this.id] = this; + this.buildIdToObject(); this.instance = this.createInstance(); }; diff --git a/src/game-lib-d3-selected-object.js b/src/game-lib-d3-selected-object.js index f430675..20f3405 100644 --- a/src/game-lib-d3-selected-object.js +++ b/src/game-lib-d3-selected-object.js @@ -23,14 +23,11 @@ GameLib.D3.SelectedObject = function SelectedObject( if (GameLib.Utils.UndefinedOrNull(helper)) { helper = new GameLib.D3.Helper( - graphics, - new GameLib.D3.API.Helper( - null, - null, - null, - object - ) - ); + this.graphics, + null, + null, + object + ) } this.helper = helper; diff --git a/src/game-lib-d3-skeleton.js b/src/game-lib-d3-skeleton.js index 5145072..43c7203 100644 --- a/src/game-lib-d3-skeleton.js +++ b/src/game-lib-d3-skeleton.js @@ -25,7 +25,8 @@ GameLib.D3.Skeleton = function Skeleton( apiSkeleton.boneTextureWidth, apiSkeleton.boneTextureHeight, apiSkeleton.boneMatrices, - apiSkeleton.boneTexture + apiSkeleton.boneTexture, + apiSkeleton.parentEntity ); this.bones = this.bones.map( @@ -78,6 +79,8 @@ GameLib.D3.Skeleton = function Skeleton( }.bind(this) ); + this.buildIdToObject(); + this.instance = this.createInstance(); }; @@ -192,7 +195,8 @@ GameLib.D3.Skeleton.prototype.toApiSkeleton = function() { return boneMatrix.toApiMatrix(); } ), - this.boneTexture + this.boneTexture, + this.parentEntity ); return apiSkeleton; diff --git a/src/game-lib-d3-texture.js b/src/game-lib-d3-texture.js index 10dee71..65468c3 100644 --- a/src/game-lib-d3-texture.js +++ b/src/game-lib-d3-texture.js @@ -47,7 +47,8 @@ GameLib.D3.Texture = function Texture( apiTexture.mipmaps, apiTexture.unpackAlignment, apiTexture.premultiplyAlpha, - apiTexture.encoding + apiTexture.encoding, + apiTexture.parentEntity ); this.offset = new GameLib.Vector2( @@ -273,7 +274,8 @@ GameLib.D3.Texture.prototype.toApiTexture = function() { this.mipmaps, this.unpackAlignment, this.premultiplyAlpha, - this.encoding + this.encoding, + this.parentEntity ) }; diff --git a/src/game-lib-d3-viewport.js b/src/game-lib-d3-viewport.js index e855c75..c28c16d 100644 --- a/src/game-lib-d3-viewport.js +++ b/src/game-lib-d3-viewport.js @@ -57,7 +57,9 @@ GameLib.D3.Viewport = function ( this.camera ) } - + + this.buildIdToObject(); + this.instance = this.createInstance(); }; diff --git a/src/game-lib-entity-manager.js b/src/game-lib-entity-manager.js index 52deea0..8304b41 100644 --- a/src/game-lib-entity-manager.js +++ b/src/game-lib-entity-manager.js @@ -18,7 +18,10 @@ GameLib.EntityManager = function( GameLib.API.EntityManager.call( this, - apiEntityManager.entities + apiEntityManager.id, + apiEntityManager.name, + apiEntityManager.entities, + apiEntityManager.parentEntity ); this.entities = this.entities.map( @@ -37,6 +40,8 @@ GameLib.EntityManager = function( }.bind(this) ); + this.buildIdToObject(); + this.instance = this.createInstance(); }; @@ -60,9 +65,15 @@ GameLib.EntityManager.prototype.createInstance = function() { */ GameLib.EntityManager.prototype.createEntity = function(name) { - var apiEntity = new GameLib.API.Entity(null, name); + var apiEntity = new GameLib.API.Entity( + null, + name + ); - var entity = new GameLib.Entity(this.graphics, apiEntity); + var entity = new GameLib.Entity( + this.graphics, + apiEntity + ); this.entities.push(entity); @@ -168,7 +179,10 @@ GameLib.EntityManager.prototype.toApiEntityManager = function() { ); var apiEntityManager = new GameLib.API.EntityManager( - apiEntities + this.id, + this.name, + apiEntities, + this.parentEntity ); return apiEntityManager; diff --git a/src/game-lib-entity.js b/src/game-lib-entity.js index 856f2d6..6297704 100644 --- a/src/game-lib-entity.js +++ b/src/game-lib-entity.js @@ -23,6 +23,8 @@ GameLib.Entity = function ( apiEntity.components ); + this.componentToCreate = 0; + this.components = this.components.map( function (apiComponent) { @@ -92,7 +94,12 @@ GameLib.Entity.prototype.createInstance = function() { * Adds a component to this entity through the instance (should notify the entity manager instance) * @param component */ -GameLib.Entity.prototype.addComponent = function(component) { +GameLib.Entity.prototype.addComponent = function() { + + if (this.componentToCreate == GameLib.Component.COMPONENT_CUSTOM_CODE) { + var component = new GameLib.D3.CustomCode(); + } + this.components.push(component); component.parentEntity = this; }; diff --git a/src/game-lib-mouse.js b/src/game-lib-mouse.js index 98d6752..f0a6d7d 100644 --- a/src/game-lib-mouse.js +++ b/src/game-lib-mouse.js @@ -9,51 +9,42 @@ GameLib.Mouse = function (graphics, apiMouse) { this.graphics = graphics; this.graphics.isNotThreeThrow(); - - GameLib.API.Mouse.call( this, apiMouse.id, apiMouse.name, apiMouse.x, - apiMouse.y + apiMouse.y, + apiMouse.parentEntity ); - this.parentObject = parentObject; - - // this.instance = this.createInstance(); + this.instance = this.createInstance(); }; +GameLib.Mouse.prototype = Object.create(GameLib.API.Mouse.prototype); +GameLib.Mouse.prototype.constructor = GameLib.Mouse; + /** * Creates an instance mouse * @param update * @returns {*} */ GameLib.Mouse.prototype.createInstance = function(update) { - // - // var instance = null; - // - // if (update) { - // instance = this.instance; - // instance.x = this.x; - // instance.y = this.y; - // } else { - // instance = new THREE.Mouse(this.x, this.y); - // } - // - // return instance; + + var instance = null; + + if (update) { + instance = this.instance; + } + + return instance; }; /** * Updates the instance vector, calls updateInstance on the parent object */ GameLib.Mouse.prototype.updateInstance = function() { - this.createInstance(true); - - if (this.parentObject.updateInstance) { - this.parentObject.updateInstance(); - } }; /** @@ -65,6 +56,7 @@ GameLib.Mouse.prototype.toApiMouse = function() { this.id, this.name, this.x, - this.y + this.y, + this.parentEntity ); }; diff --git a/src/game-lib-utils.js b/src/game-lib-utils.js index fe195b3..2f2f77b 100644 --- a/src/game-lib-utils.js +++ b/src/game-lib-utils.js @@ -35,6 +35,50 @@ GameLib.Utils.Extend = function( // } // }; +GameLib.Utils.LoadIdsFromArrayToIdObject = function(array, idToObject) { + array.map( + function(object) { + + if (object.buildIdToObject) { + object.buildIdToObject(); + var _idToObject = object.idToObject; + + for (var property in _idToObject) { + if (_idToObject.hasOwnProperty(property)) { + idToObject[property] = _idToObject[property]; + } + } + } + + idToObject[object.id] = object; + } + ); + + return idToObject; +}; + +GameLib.Utils.LoadIdsFromObjectToIdObject = function(object, idToObject) { + + if (!object) { + return idToObject; + } + + if (object.buildIdToObject) { + object.buildIdToObject(); + var _idToObject = object.idToObject; + + for (var property in _idToObject) { + if (_idToObject.hasOwnProperty(property)) { + idToObject[property] = _idToObject[property]; + } + } + } + + idToObject[object.id] = object; + + return idToObject; +}; + GameLib.Utils.UndefinedOrNull = function ( variable ) { diff --git a/src/game-lib-z.js b/src/game-lib-z.js index d839557..f586a6c 100644 --- a/src/game-lib-z.js +++ b/src/game-lib-z.js @@ -1,5 +1,8 @@ -GameLib.API.System.prototype = Object.create(GameLib.Component.prototype); -GameLib.API.System.prototype.constructor = GameLib.API.System; + + + + + if (typeof module !== 'undefined') { module.exports = GameLib;