diff --git a/src/game-lib-a-2-utils.js b/src/game-lib-a-2-utils.js index a9b2498..146c651 100644 --- a/src/game-lib-a-2-utils.js +++ b/src/game-lib-a-2-utils.js @@ -61,66 +61,12 @@ GameLib.Utils.ObjectIdWithNameInArray = function(name, array) { // }; GameLib.Utils.LoadIdsFromArrayToIdObject = function(array, idToObject) { - array.map( - function(object) { - if (object instanceof Object) { - - if (object.buildIdToObject) { - object.buildIdToObject(); - var _idToObject = object.idToObject; - - for (var property in _idToObject) { - if (_idToObject.hasOwnProperty(property)) { - idToObject[property] = _idToObject[property]; - } - } - } - - if (object.id) { - idToObject[object.id] = object; - } else { - console.warn('Object with no ID passed: ' + object) - } - - } else if (typeof object === 'string') { - // console.warn('Linked object found:' + object); - } else { - console.warn('Unhandled type of object: ', object); - } - } - ); - - return idToObject; }; GameLib.Utils.LoadIdsFromObjectToIdObject = function(object, idToObject) { - if (object instanceof Object) { - if (object.buildIdToObject) { - object.buildIdToObject(); - var _idToObject = object.idToObject; - - for (var property in _idToObject) { - if (_idToObject.hasOwnProperty(property)) { - idToObject[property] = _idToObject[property]; - } - } - } - - if (object.id) { - idToObject[object.id] = object; - } else { - console.warn('Object with no ID passed: ' + object) - } - } else if (typeof object === 'string') { - // console.warn('Linked object found:' + object); - } else { - console.warn('Unhandled type of object: ', object); - } - - return idToObject; }; GameLib.Utils.InterpolateArray = function(data, fitCount) { @@ -301,7 +247,7 @@ GameLib.Utils.InvertMeshWindingOrder = function(mesh) { ); mesh.computeNormals = true; - mesh.instance = mesh.createInstance(false); + mesh.createInstance(); }; /** diff --git a/src/game-lib-a-component-a.js b/src/game-lib-a-component-a.js index 52d2c4f..bb17561 100644 --- a/src/game-lib-a-component-a.js +++ b/src/game-lib-a-component-a.js @@ -37,24 +37,27 @@ GameLib.Component = function( } ); - // GameLib.Event.Emit( - // GameLib.Event.COMPONENT_CREATED, - // { - // component : this - // } - // ); - if (this.dependencies.length === 0) { delete this.dependencies; - this.instance = this.createInstance(); + /** + * Build ID to object should run through all sub components - + * if one is found which is not loaded, this component is not loaded fully + */ + this.buildIdToObject(); - if (this.instance) { - this.loaded = true; - this.buildIdToObject(); - GameLib.Event.EmitInstanceEvents(this); + /** + * Don't try to create an instance of this object until it is fully loaded + */ + if (this.loaded) { + try { + this.createInstance(); + } catch (error) { + console.error(error); + } } + } else { GameLib.Event.Emit( GameLib.Event.REGISTER_DEPENDENCIES, @@ -72,6 +75,12 @@ GameLib.Component.prototype.constructor = GameLib.Component; GameLib.Component.prototype.createInstance = function() { + console.log('create instance : '+ this.name); + + if (this.instance) { + GameLib.Event.EmitInstanceEvents(this); + } + }; GameLib.Component.prototype.getDependencies = function() { @@ -188,6 +197,7 @@ GameLib.Component.COMPONENT_CONTROLS_MOUSE = 0x3a; GameLib.Component.COMPONENT_MESH_TEXT = 0x3b; GameLib.Component.COMPONENT_FONT = 0x3c; GameLib.Component.COMPONENT_CANVAS = 0x3d; +GameLib.Component.COMPONENT_BONE = 0x3e; /** * Returns string name for component number @@ -258,6 +268,7 @@ GameLib.Component.GetComponentName = function(number) { case 0x3b : return 'GameLib.D3.Mesh.Text'; case 0x3c : return 'GameLib.D3.Font'; case 0x3d : return 'GameLib.D3.Canvas'; + case 0x3e : return 'GameLib.D3.Bone'; break; } @@ -286,6 +297,8 @@ GameLib.Component.prototype.buildIdToObject = function() { this.idToObject = {}; + var loaded = true; + for (var property in this.linkedObjects) { if ( this.linkedObjects.hasOwnProperty(property) && @@ -295,16 +308,107 @@ GameLib.Component.prototype.buildIdToObject = function() { property !== 'parentScene' && property !== 'parentMesh' && property !== 'parentWorld' && - property !== 'parentEntityManager' + property !== 'parentEntityManager' && + loaded ) { + if (this.linkedObjects[property] instanceof Array) { - this.idToObject = GameLib.Utils.LoadIdsFromArrayToIdObject(this[property], this.idToObject); + + /** + * Remove null objects (can happen) + */ + this[property] = this[property].filter( + function(object) { + if (object === null) { + console.log('null object found and removed'); + return false; + } + return true; + } + ); + + this[property].map( + + function(object) { + + if (object instanceof Object) { + + if (object.buildIdToObject) { + + object.buildIdToObject(); + + if (object.loaded === false) { + loaded = false; + /** + * Don't continue processing + */ + } else { + var _idToObject = object.idToObject; + + for (var property in _idToObject) { + if (_idToObject.hasOwnProperty(property)) { + this.idToObject[property] = _idToObject[property]; + } + } + } + } + + if (object.id) { + this.idToObject[object.id] = object; + } else { + console.warn('Object with no ID passed: ' + object) + } + + } else if (typeof object === 'string') { + loaded = false; + } else { + console.warn('Unhandled type of object: ', object); + } + }.bind(this) + ); + } else { - this.idToObject = GameLib.Utils.LoadIdsFromObjectToIdObject(this[property], this.idToObject); + + var object = this[property]; + + if (object instanceof Object) { + + if (object.buildIdToObject) { + + object.buildIdToObject(); + + if (object.loaded === false) { + loaded = false; + } else { + var _idToObject = object.idToObject; + + for (var objectProperty in _idToObject) { + if (_idToObject.hasOwnProperty(objectProperty)) { + this.idToObject[objectProperty] = _idToObject[objectProperty]; + } + } + } + } + + if (object.id) { + this.idToObject[object.id] = object; + } else { + console.warn('Object with no ID passed: ' + object) + } + + } else if (typeof object === 'string') { + + loaded = false; + + } else { + console.warn('Unhandled type of object: ', object); + } } } } + this.loaded = loaded; + this.idToObject[this.id] = this; this.built = false; @@ -324,6 +428,7 @@ GameLib.Component.prototype.generateNewIds = function() { this.idToObject[property].name = this.idToObject[property].name.replace(oldId,newId); } } + }; GameLib.Component.prototype.clone = function() { diff --git a/src/game-lib-clock.js b/src/game-lib-clock.js index fc97860..c6c95e1 100644 --- a/src/game-lib-clock.js +++ b/src/game-lib-clock.js @@ -43,9 +43,9 @@ GameLib.Clock.prototype.constructor = GameLib.Clock; */ GameLib.Clock.prototype.createInstance = function() { - var instance = new THREE.Clock(); + this.instance = new THREE.Clock(); - return instance; + GameLib.Component.prototype.createInstance.call(this); }; /** diff --git a/src/game-lib-color.js b/src/game-lib-color.js index 3d9b408..db62f8a 100644 --- a/src/game-lib-color.js +++ b/src/game-lib-color.js @@ -42,7 +42,7 @@ GameLib.Color = function ( } this.grain = grain; - this.instance = this.createInstance(); + this.createInstance(); }; GameLib.Color.prototype = Object.create(GameLib.API.Color.prototype); @@ -50,23 +50,14 @@ GameLib.Color.prototype.constructor = GameLib.Color; /** * Creates an instance color - * @param update * @returns {*} */ -GameLib.Color.prototype.createInstance = function(update) { - - var instance = null; - - if (update) { - instance = this.instance; - instance.r = this.r; - instance.g = this.g; - instance.b = this.b; - } else { - instance = new THREE.Color(this.r, this.g, this.b); - } - - return instance; +GameLib.Color.prototype.createInstance = function() { + this.instance = new THREE.Color( + this.r, + this.g, + this.b + ); }; /** @@ -74,7 +65,9 @@ GameLib.Color.prototype.createInstance = function(update) { */ GameLib.Color.prototype.updateInstance = function() { - this.createInstance(true); + this.instance.r = this.r; + this.instance.g = this.g; + this.instance.b = this.b; if (this.parentObject && this.parentObject.updateInstance) { diff --git a/src/game-lib-d3-api-texture.js b/src/game-lib-d3-api-texture.js index 31a610a..1bd2a5f 100644 --- a/src/game-lib-d3-api-texture.js +++ b/src/game-lib-d3-api-texture.js @@ -4,6 +4,7 @@ * @param typeId * @param name * @param image + * @param images * @param wrapS * @param wrapT * @param repeat @@ -30,6 +31,7 @@ GameLib.D3.API.Texture = function( typeId, name, image, + images, wrapS, wrapT, repeat, @@ -75,6 +77,11 @@ GameLib.D3.API.Texture = function( } this.image = image; + if (GameLib.Utils.UndefinedOrNull(images)) { + images = []; + } + this.images = images; + if (GameLib.Utils.UndefinedOrNull(wrapS)) { wrapS = GameLib.D3.Texture.TYPE_REPEAT_WRAPPING; } @@ -182,6 +189,7 @@ GameLib.D3.API.Texture.FromObject = function(objectTexture) { objectTexture.typeId, objectTexture.name, objectTexture.image, + objectTexture.images, objectTexture.wrapS, objectTexture.wrapT, GameLib.API.Vector2.FromObject(objectTexture.repeat), diff --git a/src/game-lib-d3-bone.js b/src/game-lib-d3-bone.js index d880b7b..afd939e 100644 --- a/src/game-lib-d3-bone.js +++ b/src/game-lib-d3-bone.js @@ -55,7 +55,10 @@ GameLib.D3.Bone = function ( this ); - this.instance = this.createInstance(); + GameLib.Component.call( + this, + GameLib.Component.COMPONENT_BONE + ); }; GameLib.D3.Bone.prototype = Object.create(GameLib.D3.API.Bone.prototype); @@ -68,28 +71,28 @@ GameLib.D3.Bone.prototype.constructor = GameLib.D3.Bone; */ GameLib.D3.Bone.prototype.createInstance = function() { - var instance = new THREE.Bone(); + this.instance = new THREE.Bone(); - instance.name = this.name; + this.instance.name = this.name; - instance.position.x = this.position.x; - instance.position.y = this.position.y; - instance.position.z = this.position.z; + this.instance.position.x = this.position.x; + this.instance.position.y = this.position.y; + this.instance.position.z = this.position.z; - instance.quaternion.x = this.quaternion.x; - instance.quaternion.y = this.quaternion.y; - instance.quaternion.z = this.quaternion.z; - instance.quaternion.w = this.quaternion.w; + this.instance.quaternion.x = this.quaternion.x; + this.instance.quaternion.y = this.quaternion.y; + this.instance.quaternion.z = this.quaternion.z; + this.instance.quaternion.w = this.quaternion.w; - instance.scale.x = this.scale.x; - instance.scale.y = this.scale.y; - instance.scale.z = this.scale.z; + this.instance.scale.x = this.scale.x; + this.instance.scale.y = this.scale.y; + this.instance.scale.z = this.scale.z; - instance.up.x = this.up.x; - instance.up.y = this.up.y; - instance.up.z = this.up.z; - - return instance; + this.instance.up.x = this.up.x; + this.instance.up.y = this.up.y; + this.instance.up.z = this.up.z; + + GameLib.Component.prototype.createInstance.call(this); }; /** diff --git a/src/game-lib-d3-broadphase.js b/src/game-lib-d3-broadphase.js index 2281973..419ec06 100644 --- a/src/game-lib-d3-broadphase.js +++ b/src/game-lib-d3-broadphase.js @@ -43,20 +43,18 @@ GameLib.D3.Broadphase.prototype.constructor = GameLib.D3.Broadphase; */ GameLib.D3.Broadphase.prototype.createInstance = function() { - var instance = null; - if (this.broadphaseType === GameLib.D3.Broadphase.BROADPHASE_TYPE_NAIVE) { - instance = new CANNON.NaiveBroadphase(); + this.instance = new CANNON.NaiveBroadphase(); } else if (this.broadphaseType === GameLib.D3.Broadphase.BROADPHASE_TYPE_GRID) { - instance = new CANNON.GridBroadphase(); + this.instance = new CANNON.GridBroadphase(); } else if (this.broadphaseType === GameLib.D3.Broadphase.BROADPHASE_TYPE_SAP) { - instance = new CANNON.SAPBroadphase(); + this.instance = new CANNON.SAPBroadphase(); } else { console.warn('Unsupported broadphase type: ' + this.broadphaseType); throw new Error('Unsupported broadphase type: ' + this.broadphaseType); } - return instance; + GameLib.Component.prototype.createInstance.call(this); }; /** @@ -66,19 +64,19 @@ GameLib.D3.Broadphase.prototype.updateInstance = function() { if (this.broadphaseType === GameLib.D3.Broadphase.BROADPHASE_TYPE_NAIVE) { if (!(this.instance instanceof CANNON.NaiveBroadphase)) { - this.instance = new CANNON.NaiveBroadphase(); + this.createInstance(); } } if (this.broadphaseType === GameLib.D3.Broadphase.BROADPHASE_TYPE_GRID) { if (!(this.instance instanceof CANNON.GridBroadphase)) { - this.instance = new CANNON.GridBroadphase(); + this.createInstance(); } } if (this.broadphaseType === GameLib.D3.Broadphase.BROADPHASE_TYPE_SAP) { if (!(this.instance instanceof CANNON.SAPBroadphase)) { - this.instance = new CANNON.SAPBroadphase(); + this.createInstance(); } } }; diff --git a/src/game-lib-d3-camera.js b/src/game-lib-d3-camera.js index 1aa79da..c87ce64 100644 --- a/src/game-lib-d3-camera.js +++ b/src/game-lib-d3-camera.js @@ -83,7 +83,6 @@ GameLib.D3.Camera = function( GameLib.Component.COMPONENT_CAMERA ); - //this.needsUpdate = false; } ; GameLib.D3.Camera.prototype = Object.create(GameLib.D3.API.Camera.prototype); @@ -99,17 +98,15 @@ GameLib.D3.Camera.CAMERA_TYPE_STEREO = 0x3; */ GameLib.D3.Camera.prototype.createInstance = function() { - var instance = null; - if (this.cameraType === GameLib.D3.Camera.CAMERA_TYPE_PERSPECTIVE ) { - instance = new THREE.PerspectiveCamera( + this.instance = new THREE.PerspectiveCamera( this.fov, this.aspect, this.near, this.far ); } else if (this.cameraType === GameLib.D3.Camera.CAMERA_TYPE_ORTHOGONAL) { - instance = new THREE.OrthographicCamera( + this.instance = new THREE.OrthographicCamera( this.minX + this.offsetX, this.maxX + this.offsetX, this.maxY, @@ -118,28 +115,25 @@ GameLib.D3.Camera.prototype.createInstance = function() { this.maxZ ); } else if (this.cameraType === GameLib.D3.Camera.CAMERA_TYPE_STEREO) { - instance = new THREE.StereoCamera(); + this.instance = new THREE.StereoCamera(); + } else { + throw new Error('unsupported camera type : ' + this.cameraType); } - if (!instance) { - console.log('Unsupported camera type : ' + this.cameraType); - throw new Error('Unsupported camera type : ' + this.cameraType); - } + this.instance.position.x = this.position.x; + this.instance.position.y = this.position.y; + this.instance.position.z = this.position.z; - instance.position.x = this.position.x; - instance.position.y = this.position.y; - instance.position.z = this.position.z; + this.instance.quaternion.x = this.quaternion.x; + this.instance.quaternion.y = this.quaternion.y; + this.instance.quaternion.z = this.quaternion.z; + this.instance.quaternion.w = this.quaternion.w; - instance.quaternion.x = this.quaternion.x; - instance.quaternion.y = this.quaternion.y; - instance.quaternion.z = this.quaternion.z; - instance.quaternion.w = this.quaternion.w; + this.instance.lookAt(this.lookAt.instance); - instance.lookAt(this.lookAt.instance); + this.instance.updateProjectionMatrix(); - instance.updateProjectionMatrix(); - - return instance; + GameLib.Component.prototype.createInstance.call(this); }; /** @@ -151,16 +145,14 @@ GameLib.D3.Camera.prototype.updateInstance = function() { this.cameraType === GameLib.D3.Camera.CAMERA_TYPE_ORTHOGONAL && this.instance instanceof THREE.PerspectiveCamera ) { - this.instance = this.createInstance(); - return; + this.createInstance(); } if ( this.cameraType === GameLib.D3.Camera.CAMERA_TYPE_PERSPECTIVE && this.instance instanceof THREE.OrthographicCamera ) { - this.instance = this.createInstance(); - return; + this.createInstance(); } if (this.cameraType === GameLib.D3.Camera.CAMERA_TYPE_ORTHOGONAL) { diff --git a/src/game-lib-d3-canvas.js b/src/game-lib-d3-canvas.js index 00e3444..5f13567 100644 --- a/src/game-lib-d3-canvas.js +++ b/src/game-lib-d3-canvas.js @@ -43,12 +43,12 @@ GameLib.D3.Canvas.prototype.constructor = GameLib.D3.Canvas; */ GameLib.D3.Canvas.prototype.createInstance = function() { - var instance = document.createElement('canvas'); + this.instance = document.createElement('canvas'); - instance.width = this.width; - instance.height = this.height; + this.instance.width = this.width; + this.instance.height = this.height; - return instance; + GameLib.Component.prototype.createInstance.call(this); }; /** @@ -57,8 +57,7 @@ GameLib.D3.Canvas.prototype.createInstance = function() { GameLib.D3.Canvas.prototype.updateInstance = function() { if (GameLib.Utils.UndefinedOrNull(this.instance)) { - this.instance = document.createElement('canvas'); - this.loaded = true; + this.createInstance(); } this.instance.width = this.width; diff --git a/src/game-lib-d3-coder.js b/src/game-lib-d3-coder.js index 4fdf77b..8f07707 100644 --- a/src/game-lib-d3-coder.js +++ b/src/game-lib-d3-coder.js @@ -25,7 +25,7 @@ GameLib.D3.Coder = function Coder( } this.coderType = coderType; - this.instance = this.createInstance(); + this.createInstance(); }; /** @@ -38,17 +38,13 @@ GameLib.D3.Coder.CODER_TYPE_CODE_MIRROR = 0x1; * @returns {THREE.Coder} */ GameLib.D3.Coder.prototype.createInstance = function() { - - var instance = CodeMirror; - - return instance; + this.instance = CodeMirror; }; /** * Updates the instance with the current state */ GameLib.D3.Coder.prototype.updateInstance = function() { - this.instance = CodeMirror; }; /** diff --git a/src/game-lib-d3-composer.js b/src/game-lib-d3-composer.js index 2cf5982..942842c 100644 --- a/src/game-lib-d3-composer.js +++ b/src/game-lib-d3-composer.js @@ -46,15 +46,9 @@ GameLib.D3.Composer.prototype.constructor = GameLib.D3.Composer; /** * Creates a Composer instance - * @param update * @returns {*} */ -GameLib.D3.Composer.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; - } +GameLib.D3.Composer.prototype.createInstance = function() { //TODO : Fix this too - make it nice (no circular references) // var instance = null; @@ -83,15 +77,18 @@ GameLib.D3.Composer.prototype.createInstance = function(update) { // }.bind(this) // ); // } + console.log('GameLib.D3.Composer.prototype.createInstance() - fix me'); - return instance; + GameLib.Component.prototype.createInstance.call(this); + + //return instance; }; /** * Updates Composer instance */ GameLib.D3.Composer.prototype.updateInstance = function() { - this.instance = this.createInstance(true); + console.log('GameLib.D3.Composer.prototype.updateInstance() - fix me'); }; /** diff --git a/src/game-lib-d3-controls-0.js b/src/game-lib-d3-controls-0.js index c314f90..aa9cfee 100644 --- a/src/game-lib-d3-controls-0.js +++ b/src/game-lib-d3-controls-0.js @@ -77,7 +77,7 @@ GameLib.D3.Controls.CONTROLS_TYPE_MOUSE = 0x3; * Creates a mesh instance or updates it */ GameLib.D3.Controls.prototype.createInstance = function() { - console.log('default controls create instance'); + GameLib.Component.prototype.createInstance.call(this); }; /** diff --git a/src/game-lib-d3-controls-editor.js b/src/game-lib-d3-controls-editor.js index 726f6fd..4f64f95 100644 --- a/src/game-lib-d3-controls-editor.js +++ b/src/game-lib-d3-controls-editor.js @@ -59,7 +59,8 @@ GameLib.D3.Controls.Editor.prototype.constructor = GameLib.D3.Controls.Editor; * @returns {THREE.EditorControls} */ GameLib.D3.Controls.Editor.prototype.createInstance = function() { - console.log('delaying controls instance creation - call GameLib.D3.Controls.Editor.delayInstance() to create the instance'); + console.log('delaying controls instance creation - call GameLib.D3.Controls.Editor.delayedInstance() to create the instance'); + GameLib.D3.Controls.prototype.createInstance.call(this); }; GameLib.D3.Controls.Editor.prototype.delayedInstance = function() { @@ -74,12 +75,10 @@ GameLib.D3.Controls.Editor.prototype.delayedInstance = function() { throw new Error('No dom element at time of instance'); } - var instance = new THREE.EditorControls( + this.instance = new THREE.EditorControls( this.camera.instance, this.domElement.instance ); - - return instance; }; /** @@ -89,12 +88,12 @@ GameLib.D3.Controls.Editor.prototype.updateInstance = function() { console.warn('an update instance was called on editor controls - which, if not called from within a running system at the right time will affect the order of input event handling and cause system instability'); - if (this.instance) { - this.instance.dispose(); - delete this.instance; - } - - this.instance = this.delayedInstance(); + // if (this.instance) { + // this.instance.dispose(); + // delete this.instance; + // } + // + // this.delayedInstance(); GameLib.D3.Controls.prototype.updateInstance.call(this); }; diff --git a/src/game-lib-d3-controls-keyboard.js b/src/game-lib-d3-controls-keyboard.js index f0ae1cc..8dd5b7b 100644 --- a/src/game-lib-d3-controls-keyboard.js +++ b/src/game-lib-d3-controls-keyboard.js @@ -32,9 +32,10 @@ GameLib.D3.Controls.Keyboard.prototype.constructor = GameLib.D3.Controls.Keyboar */ GameLib.D3.Controls.Keyboard.prototype.createInstance = function() { /** - * Return true to indicate no dependencies to other components + * Set instance to true to indicate no dependencies to other components */ - return true; + this.instance = true; + GameLib.D3.Controls.prototype.createInstance.call(this); }; /** diff --git a/src/game-lib-d3-controls-mouse.js b/src/game-lib-d3-controls-mouse.js index 4b5b2a1..5515433 100644 --- a/src/game-lib-d3-controls-mouse.js +++ b/src/game-lib-d3-controls-mouse.js @@ -32,9 +32,10 @@ GameLib.D3.Controls.Mouse.prototype.constructor = GameLib.D3.Controls.Mouse; */ GameLib.D3.Controls.Mouse.prototype.createInstance = function() { /** - * Return true to indicate no dependencies to other components + * Set instance to true to indicate no dependencies to other components */ - return true; + this.instance = true; + GameLib.D3.Controls.prototype.createInstance.call(this); }; /** diff --git a/src/game-lib-d3-controls-touch.js b/src/game-lib-d3-controls-touch.js index dd310f3..6aba3dc 100644 --- a/src/game-lib-d3-controls-touch.js +++ b/src/game-lib-d3-controls-touch.js @@ -2,6 +2,7 @@ * Touch Controls * @param graphics GameLib.D3.Graphics * @param apiControls GameLib.D3.API.Controls + * @param sensitivity * @constructor */ GameLib.D3.Controls.Touch = function ( @@ -38,9 +39,10 @@ GameLib.D3.Controls.Touch.prototype.constructor = GameLib.D3.Controls.Touch; */ GameLib.D3.Controls.Touch.prototype.createInstance = function() { /** - * Return true to indicate no dependencies to other components + * Set instance to true to indicate no dependencies to other components */ - return true; + this.instance = true; + GameLib.D3.Controls.prototype.createInstance.call(this); }; /** diff --git a/src/game-lib-d3-custom-code.js b/src/game-lib-d3-custom-code.js index 2ccbb0d..15be82c 100644 --- a/src/game-lib-d3-custom-code.js +++ b/src/game-lib-d3-custom-code.js @@ -36,23 +36,26 @@ GameLib.D3.CustomCode.prototype = Object.create(GameLib.D3.API.CustomCode.protot GameLib.D3.CustomCode.prototype.constructor = GameLib.D3.CustomCode; GameLib.D3.CustomCode.prototype.createInstance = function() { + try { - var instance = new Function('data', this.code).bind(this); - return instance; + this.instance = new Function('data', this.code).bind(this); } catch (error) { /** - * Return true here to indicate that even though the compilation failed, the instance will be fine and + * Set the instance to true here to indicate that even though the compilation failed, the instance will be fine and * this component loaded fine. */ - return true; + this.instance = new Function('data', "console.log('compilation failed for : " + this.name + "');").bind(this); } + + GameLib.Component.prototype.createInstance.call(this); }; /** * Updates the instance with the current state */ GameLib.D3.CustomCode.prototype.updateInstance = function() { - try { + + try { this.instance = new Function('data', this.code).bind(this); this.publish( GameLib.Event.COMPILE_SUCCESS, @@ -61,6 +64,7 @@ GameLib.D3.CustomCode.prototype.updateInstance = function() { } ) } catch (error) { + this.instance = new Function('data', "console.log('compilation update failed for : " + this.name + "');").bind(this); this.publish( GameLib.Event.COMPILE_FAILED, { @@ -68,6 +72,7 @@ GameLib.D3.CustomCode.prototype.updateInstance = function() { } ) } + }; /** diff --git a/src/game-lib-d3-font.js b/src/game-lib-d3-font.js index 849164a..504ac46 100644 --- a/src/game-lib-d3-font.js +++ b/src/game-lib-d3-font.js @@ -49,13 +49,16 @@ GameLib.D3.Font.prototype.createInstance = function() { } ); - return null; + this.instance = null; + + GameLib.Component.prototype.createInstance.call(this); }; /** * Updates the instance with the current state */ GameLib.D3.Font.prototype.updateInstance = function() { + GameLib.Event.Emit( GameLib.Event.LOAD_FONT, { diff --git a/src/game-lib-d3-friction-contact-material.js b/src/game-lib-d3-friction-contact-material.js index 294471b..54e137d 100644 --- a/src/game-lib-d3-friction-contact-material.js +++ b/src/game-lib-d3-friction-contact-material.js @@ -52,7 +52,7 @@ GameLib.D3.FrictionContactMaterial.prototype.constructor = GameLib.D3.FrictionCo */ GameLib.D3.FrictionContactMaterial.prototype.createInstance = function() { - var instance = new CANNON.ContactMaterial( + this.instance = new CANNON.ContactMaterial( null, null, { @@ -62,13 +62,13 @@ GameLib.D3.FrictionContactMaterial.prototype.createInstance = function() { } ); - instance.materials = this.materials.map( + this.instance.materials = this.materials.map( function(material){ return material.instance; } ); - return instance; + GameLib.Component.prototype.createInstance.call(this); }; /** diff --git a/src/game-lib-d3-friction-material.js b/src/game-lib-d3-friction-material.js index 86f59a2..e4a3792 100644 --- a/src/game-lib-d3-friction-material.js +++ b/src/game-lib-d3-friction-material.js @@ -39,18 +39,18 @@ GameLib.D3.FrictionMaterial.prototype = Object.create(GameLib.D3.API.FrictionMat GameLib.D3.FrictionMaterial.prototype.constructor = GameLib.D3.FrictionMaterial; /** - * - * @returns {*} + * create instance */ GameLib.D3.FrictionMaterial.prototype.createInstance = function() { - var instance = new CANNON.Material(this.name); + this.instance = new CANNON.Material( + this.name + ); - instance.friction = this.friction; + this.instance.friction = this.friction; + this.instance.restitution = this.restitution; - instance.restitution = this.restitution; - - return instance; + GameLib.Component.prototype.createInstance.call(this); }; /** diff --git a/src/game-lib-d3-graphics.js b/src/game-lib-d3-graphics.js index 476391b..a77d373 100644 --- a/src/game-lib-d3-graphics.js +++ b/src/game-lib-d3-graphics.js @@ -25,7 +25,7 @@ GameLib.D3.Graphics = function Graphics( } this.graphicsType = graphicsType; - this.instance = this.createInstance(); + this.createInstance(); }; /** @@ -38,8 +38,7 @@ GameLib.D3.Graphics.GRAPHICS_TYPE_THREE = 0x1; * @returns {THREE.Graphics} */ GameLib.D3.Graphics.prototype.createInstance = function() { - var instance = THREE; - return instance; + this.instance = THREE; }; /** diff --git a/src/game-lib-d3-helper.js b/src/game-lib-d3-helper.js index 2491c8e..4ef2ffc 100644 --- a/src/game-lib-d3-helper.js +++ b/src/game-lib-d3-helper.js @@ -71,11 +71,13 @@ GameLib.D3.Helper = function( } this.parentEntity = parentEntity; - this.instance = this.createInstance(); - - // GameLib.Component.call( - // this, - // GameLib.Component.COMPONENT_HELPER + this.createInstance(); + /** + * A helper as a component - does this make sense at all? + */ + // GameLib.Component.call( + // this, + // GameLib.Component.COMPONENT_HELPER // ); }; @@ -99,41 +101,47 @@ GameLib.D3.Helper.HELPER_TYPE_SKELETON = 0x6; */ GameLib.D3.Helper.prototype.createInstance = function() { - var instance = null; - if (this.helperType === GameLib.D3.Helper.HELPER_TYPE_EDGES) { - var edges = new THREE.EdgesGeometry(this.object.instance.geometry); - instance = new THREE.LineSegments(edges, new THREE.LineBasicMaterial({color:0x00ff00, linewidth:2})) + this.instance = new THREE.LineSegments( + new THREE.EdgesGeometry( + this.object.instance.geometry + ), + new THREE.LineBasicMaterial( + { + color:0x00ff00, + linewidth:2 + } + ) + ) } if (this.helperType === GameLib.D3.Helper.HELPER_TYPE_DIRECTIONAL_LIGHT) { - instance = new THREE.DirectionalLightHelper(this.object.instance); + this.instance = new THREE.DirectionalLightHelper(this.object.instance); } if (this.helperType === GameLib.D3.Helper.HELPER_TYPE_POINT_LIGHT) { - instance = new THREE.PointLightHelper(this.object.instance, 1); + this.instance = new THREE.PointLightHelper(this.object.instance, 1); } if (this.helperType === GameLib.D3.Helper.HELPER_TYPE_SPOT_LIGHT) { - instance = new THREE.SpotLightHelper(this.object.instance); + this.instance = new THREE.SpotLightHelper(this.object.instance); } if (this.helperType === GameLib.D3.Helper.HELPER_TYPE_WIREFRAME) { - instance = new THREE.WireframeGeometry(this.object.instance, 0x00FF00); + this.instance = new THREE.WireframeGeometry(this.object.instance, 0x00FF00); } if (this.helperType === GameLib.D3.Helper.HELPER_TYPE_SKELETON) { - instance = new THREE.SkeletonHelper(this.object.instance); + this.instance = new THREE.SkeletonHelper(this.object.instance); } - return instance; }; /** * Updates the instance with the current state */ GameLib.D3.Helper.prototype.updateInstance = function() { - this.instance.position.copy(this.object.instance.position); + this.instance.position.copy(this.object.instance.position); if (this.object.instance.parentMesh && this.object.instance.parentMesh.instance) { this.instance.position.add(this.object.instance.parentMesh.instance.position); diff --git a/src/game-lib-d3-image.js b/src/game-lib-d3-image.js index 77b437d..4058eb0 100644 --- a/src/game-lib-d3-image.js +++ b/src/game-lib-d3-image.js @@ -47,16 +47,36 @@ GameLib.D3.Image.prototype.constructor = GameLib.D3.Image; */ GameLib.D3.Image.prototype.createInstance = function() { - console.log('image create instance'); + this.instance = document.createElement('img'); - return null; + this.instance.onload = function() { + + GameLib.Event.Emit( + GameLib.Event.IMAGE_INSTANCE_CREATED, + { + image: this + } + ); + + }.bind(this); + + this.instance.src = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAEAAAABACAIAAAAlC+aJAAAACXBIWXMAAAsTAAALEwEAmpwYAAAAB3RJTUUH4QoWEQMQBXD4hQAAABl0RVh0Q29tbWVudABDcmVhdGVkIHdpdGggR0lNUFeBDhcAAABRSURBVGje7c8xDQAwCAAwmA3koA/PU8FB0jpo1nRc9uI4AQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBgX0fjEoBa8xN1z4AAAAASUVORK5CYII='; + + GameLib.Event.Emit( + GameLib.Event.LOAD_IMAGE, + { + image : this + } + ); + + GameLib.Component.prototype.createInstance.call(this); }; /** * Updates the instance with the current state */ GameLib.D3.Image.prototype.updateInstance = function() { - console.log('update image instance'); + this.createInstance(); }; /** diff --git a/src/game-lib-d3-light.js b/src/game-lib-d3-light.js index f43bd59..b5c8883 100644 --- a/src/game-lib-d3-light.js +++ b/src/game-lib-d3-light.js @@ -103,13 +103,11 @@ GameLib.D3.Light.LIGHT_TYPE_SPOT = 0x4; */ GameLib.D3.Light.prototype.createInstance = function() { - var instance = null; - if ( this.lightType === GameLib.D3.Light.LIGHT_TYPE_AMBIENT || this.lightType === 'AmbientLight' ) { - instance = new THREE.AmbientLight( + this.instance = new THREE.AmbientLight( this.color.instance, this.intensity ); @@ -117,7 +115,7 @@ GameLib.D3.Light.prototype.createInstance = function() { this.lightType === GameLib.D3.Light.LIGHT_TYPE_DIRECTIONAL || this.lightType === 'DirectionalLight' ) { - instance = new THREE.DirectionalLight( + this.instance = new THREE.DirectionalLight( this.color.instance, this.intensity ); @@ -125,110 +123,16 @@ GameLib.D3.Light.prototype.createInstance = function() { this.lightType === GameLib.D3.Light.LIGHT_TYPE_POINT || this.lightType === 'PointLight' ) { - instance = new THREE.PointLight( - this.color.instance, - this.intensity - ); - instance.distance = this.distance; - instance.decay = this.decay; - } else if ( - this.lightType === GameLib.D3.Light.LIGHT_TYPE_SPOT || - this.lightType === 'SpotLight' - ) { - instance = new THREE.SpotLight( - this.color.instance, - this.intensity - ); - instance.distance = this.distance; - instance.angle = this.angle; - instance.penumbra = this.penumbra; - instance.decay = this.decay; - } else { - console.warn('unsupported light type: ' + this.lightType); - return null; - } - - instance.name = this.name; - - instance.position.x = this.position.x; - instance.position.y = this.position.y; - instance.position.z = this.position.z; - - instance.scale.x = this.scale.x; - instance.scale.y = this.scale.y; - instance.scale.z = this.scale.z; - - if (instance.target) { - instance.target.position.x = this.targetPosition.x; - instance.target.position.y = this.targetPosition.y; - instance.target.position.z = this.targetPosition.z; - } - - instance.quaternion.x = this.quaternion.x; - instance.quaternion.y = this.quaternion.y; - instance.quaternion.z = this.quaternion.z; - instance.quaternion.w = this.quaternion.w; - - instance.intensity = this.intensity; - - instance.color.set(this.color.toHex()); - - return instance; - -}; - -/** - * Updates the instance with the current state - */ -GameLib.D3.Light.prototype.updateInstance = function() { - var oldInstance = null; - - if (GameLib.Utils.UndefinedOrNull(this.instance)) { - console.warn('cannot update an non-existent light'); - return - } - - if ( - this.lightType === GameLib.D3.Light.LIGHT_TYPE_AMBIENT && - !(this.instance instanceof THREE.AmbientLight) - ) { - oldInstance = this.instance; - - this.instance = new THREE.AmbientLight( - this.color.instance, - this.intensity - ); - - } else if ( - this.lightType === GameLib.D3.Light.LIGHT_TYPE_DIRECTIONAL && - !(this.instance instanceof THREE.DirectionalLight) - ) { - oldInstance = this.instance; - - this.instance = new THREE.DirectionalLight( - this.color.instance, - this.intensity - ); - - } else if ( - this.lightType === GameLib.D3.Light.LIGHT_TYPE_POINT && - !(this.instance instanceof THREE.PointLight) - ) { - oldInstance = this.instance; - this.instance = new THREE.PointLight( this.color.instance, this.intensity ); this.instance.distance = this.distance; this.instance.decay = this.decay; - } else if ( - this.lightType === GameLib.D3.Light.LIGHT_TYPE_SPOT && - !(this.instance instanceof THREE.SpotLight) + this.lightType === GameLib.D3.Light.LIGHT_TYPE_SPOT || + this.lightType === 'SpotLight' ) { - oldInstance = this.instance; - this.instance = new THREE.SpotLight( this.color.instance, this.intensity @@ -237,16 +141,87 @@ GameLib.D3.Light.prototype.updateInstance = function() { this.instance.angle = this.angle; this.instance.penumbra = this.penumbra; this.instance.decay = this.decay; - } else { - /** - * Light type not supported or light types match - */ + console.warn('unsupported light type: ' + this.lightType); + return; } - if (oldInstance && this.parentScene) { - this.parentScene.instance.remove(oldInstance); - this.parentScene.instance.add(this.instance); + this.instance.name = this.name; + + this.instance.position.x = this.position.x; + this.instance.position.y = this.position.y; + this.instance.position.z = this.position.z; + + this.instance.scale.x = this.scale.x; + this.instance.scale.y = this.scale.y; + this.instance.scale.z = this.scale.z; + + if (this.instance.target) { + this.instance.target.position.x = this.targetPosition.x; + this.instance.target.position.y = this.targetPosition.y; + this.instance.target.position.z = this.targetPosition.z; + } + + this.instance.quaternion.x = this.quaternion.x; + this.instance.quaternion.y = this.quaternion.y; + this.instance.quaternion.z = this.quaternion.z; + this.instance.quaternion.w = this.quaternion.w; + + this.instance.intensity = this.intensity; + + this.instance.color.set(this.color.toHex()); + + GameLib.Component.prototype.createInstance.call(this); +}; + +/** + * Updates the instance with the current state + */ +GameLib.D3.Light.prototype.updateInstance = function() { + + if (GameLib.Utils.UndefinedOrNull(this.instance)) { + console.warn('cannot update an non-existent light'); + return + } + + var lightTypeChange = false; + + if ( + this.lightType === GameLib.D3.Light.LIGHT_TYPE_AMBIENT && + !(this.instance instanceof THREE.AmbientLight) + ) { + + lightTypeChange = true; + + } else if ( + this.lightType === GameLib.D3.Light.LIGHT_TYPE_DIRECTIONAL && + !(this.instance instanceof THREE.DirectionalLight) + ) { + + lightTypeChange = true; + + } else if ( + this.lightType === GameLib.D3.Light.LIGHT_TYPE_POINT && + !(this.instance instanceof THREE.PointLight) + ) { + + lightTypeChange = true; + + } else if ( + this.lightType === GameLib.D3.Light.LIGHT_TYPE_SPOT && + !(this.instance instanceof THREE.SpotLight) + ) { + lightTypeChange = true; + } + + if (lightTypeChange) { + try { + this.parentScene.instance.remove(this.instance); + this.createInstance(); + this.parentScene.instance.add(this.instance); + } catch (error) { + console.error(error); + } } this.instance.name = this.name; diff --git a/src/game-lib-d3-look-at.js b/src/game-lib-d3-look-at.js index 50eb4f6..7a15acc 100644 --- a/src/game-lib-d3-look-at.js +++ b/src/game-lib-d3-look-at.js @@ -74,10 +74,17 @@ 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('GameLib.D3.LookAt.prototype.createInstance()'); - return true; + this.instance = true; + GameLib.Component.prototype.createInstance.call(this); }; +GameLib.D3.LookAt.prototype.updateInstance = function() { +}; + +/** + * to API object + * @returns {GameLib.D3.API.LookAt} + */ 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 a5db792..aa5fc84 100644 --- a/src/game-lib-d3-material.js +++ b/src/game-lib-d3-material.js @@ -831,41 +831,35 @@ GameLib.D3.Material.prototype.updateMeshBasicMaterialInstance = function() { */ GameLib.D3.Material.prototype.createInstance = function() { - // console.log('material create instance'); - - var instance = null; - if (this.materialType === GameLib.D3.Material.MATERIAL_TYPE_STANDARD) { - instance = this.createStandardMaterialInstance(); + this.instance = this.createStandardMaterialInstance(); } else if (this.materialType === GameLib.D3.Material.MATERIAL_TYPE_POINTS) { - instance = this.createPointsMaterialInstance(); + this.instance = this.createPointsMaterialInstance(); } else if (this.materialType === GameLib.D3.Material.MATERIAL_TYPE_PHONG) { - instance = this.createPhongMaterialInstance(); + this.instance = this.createPhongMaterialInstance(); } else if (this.materialType === GameLib.D3.Material.MATERIAL_TYPE_BASIC) { - instance = this.createMeshBasicMaterialInstance(); + this.instance = this.createMeshBasicMaterialInstance(); } else if (this.materialType === GameLib.D3.Material.MATERIAL_TYPE_LINE_BASIC) { - instance = this.createLineBasicMaterialInstance(); + this.instance = this.createLineBasicMaterialInstance(); } else { console.warn("material type is not implemented yet: " + this.materialType); } - instance.needsUpdate = true; - - this.instance = instance; + this.instance.needsUpdate = true; this.updateTextures(); - return instance; + GameLib.Component.prototype.createInstance.call(this); }; /** @@ -884,35 +878,35 @@ GameLib.D3.Material.prototype.updateInstance = function() { if (this.materialType === GameLib.D3.Material.MATERIAL_TYPE_STANDARD) { if (!(this.instance instanceof THREE.MeshStandardMaterial)) { - this.instance = this.createStandardMaterialInstance(); + this.createInstance(); typeChange = true; } else { this.updateStandardMaterialInstance(); } } else if (this.materialType === GameLib.D3.Material.MATERIAL_TYPE_POINTS) { if (!(this.instance instanceof THREE.PointsMaterial)) { - this.instance = this.createPointsMaterialInstance(); + this.createInstance(); typeChange = true; } else { this.updatePointsMaterialInstance(); } } else if (this.materialType === GameLib.D3.Material.MATERIAL_TYPE_PHONG) { if (!(this.instance instanceof THREE.MeshPhongMaterial)) { - this.instance = this.createPhongMaterialInstance(); + this.createInstance(); typeChange = true; } else { this.updatePhongMaterialInstance(); } } else if (this.materialType === GameLib.D3.Material.MATERIAL_TYPE_BASIC) { if (!(this.instance instanceof THREE.MeshBasicMaterial)) { - this.instance = this.createMeshBasicMaterialInstance(); + this.createInstance(); typeChange = true; } else { this.updateMeshBasicMaterialInstance(); } } else if (this.materialType === GameLib.D3.Material.MATERIAL_TYPE_LINE_BASIC) { if (!(this.instance instanceof THREE.LineBasicMaterial)) { - this.instance = this.createLineBasicMaterialInstance(); + this.createInstance(); typeChange = true; } else { this.updateLineBasicMaterialInstance(); diff --git a/src/game-lib-d3-mesh-0.js b/src/game-lib-d3-mesh-0.js index af07415..8eccf12 100644 --- a/src/game-lib-d3-mesh-0.js +++ b/src/game-lib-d3-mesh-0.js @@ -440,17 +440,15 @@ GameLib.D3.Mesh.prototype.createInstance = function() { var geometry = this.createInstanceGeometry(); - var instance = null; - if (this.skeleton) { if (this.materials.length === 1) { - instance = new THREE.SkinnedMesh( + this.instance = new THREE.SkinnedMesh( geometry, this.materials[0].instance ) } else { - instance = new THREE.SkinnedMesh( + this.instance = new THREE.SkinnedMesh( geometry, this.materials.map( function(material) { @@ -460,16 +458,17 @@ GameLib.D3.Mesh.prototype.createInstance = function() { ) } - instance.add(this.skeleton.rootBoneInstance); - instance.bind(this.skeleton.instance); + this.instance.add(this.skeleton.rootBoneInstance); + this.instance.bind(this.skeleton.instance); + } else { if (this.materials.length === 1) { - instance = new THREE.Mesh( + this.instance = new THREE.Mesh( geometry, this.materials[0].instance ) } else { - instance = new THREE.Mesh( + this.instance = new THREE.Mesh( geometry, this.materials.map( function(material) { @@ -480,22 +479,22 @@ GameLib.D3.Mesh.prototype.createInstance = function() { } } - instance.name = this.name; + this.instance.name = this.name; if (this.parentMesh && this.parentMesh.loaded) { - this.parentMesh.add(instance, this); + this.parentMesh.add(this.instance, this); } - instance.position.x = this.position.x; - instance.position.y = this.position.y; - instance.position.z = this.position.z; + this.instance.position.x = this.position.x; + this.instance.position.y = this.position.y; + this.instance.position.z = this.position.z; if (this.useQuaternion) { - instance.quaternion.x = this.quaternion.x; - instance.quaternion.y = this.quaternion.y; - instance.quaternion.z = this.quaternion.z; - instance.quaternion.w = this.quaternion.w; - instance.quaternion.setFromAxisAngle( + this.instance.quaternion.x = this.quaternion.x; + this.instance.quaternion.y = this.quaternion.y; + this.instance.quaternion.z = this.quaternion.z; + this.instance.quaternion.w = this.quaternion.w; + this.instance.quaternion.setFromAxisAngle( new THREE.Vector3( this.quaternion.axis.x, this.quaternion.axis.y, @@ -504,22 +503,22 @@ GameLib.D3.Mesh.prototype.createInstance = function() { this.quaternion.angle ); } else { - instance.rotation.x = this.rotation.x; - instance.rotation.y = this.rotation.y; - instance.rotation.z = this.rotation.z; + this.instance.rotation.x = this.rotation.x; + this.instance.rotation.y = this.rotation.y; + this.instance.rotation.z = this.rotation.z; } - instance.scale.x = this.scale.x; - instance.scale.y = this.scale.y; - instance.scale.z = this.scale.z; + this.instance.scale.x = this.scale.x; + this.instance.scale.y = this.scale.y; + this.instance.scale.z = this.scale.z; - instance.up.x = this.up.x; - instance.up.y = this.up.y; - instance.up.z = this.up.z; + this.instance.up.x = this.up.x; + this.instance.up.y = this.up.y; + this.instance.up.z = this.up.z; - instance.renderOrder = this.renderOrder; + this.instance.renderOrder = this.renderOrder; - return instance; + GameLib.Component.prototype.createInstance.call(this); }; /** @@ -1297,7 +1296,16 @@ GameLib.D3.Mesh.prototype.getChildrenComponents = function() { typeof material[property][tProperty] !== 'string' && tProperty !== 'parentEntity' ) { - GameLib.Utils.PushUnique(components, material[property][tProperty]); + + if (material[property][tProperty] instanceof Array) { + material[property][tProperty].map( + function(object) { + GameLib.Utils.PushUnique(components, object); + } + ) + } else { + GameLib.Utils.PushUnique(components, material[property][tProperty]); + } } } } diff --git a/src/game-lib-d3-mesh-box.js b/src/game-lib-d3-mesh-box.js index a62c458..dccb366 100644 --- a/src/game-lib-d3-mesh-box.js +++ b/src/game-lib-d3-mesh-box.js @@ -56,13 +56,11 @@ GameLib.D3.Mesh.Box.prototype.createInstance = function() { this.updateVerticesFromGeometryInstance(geometry); } - var instance = GameLib.D3.Mesh.prototype.createInstance.call(this); + GameLib.D3.Mesh.prototype.createInstance.call(this); - instance.userData.width = this.width; - instance.userData.height = this.height; - instance.userData.depth = this.depth; - - return instance; + this.instance.userData.width = this.width; + this.instance.userData.height = this.height; + this.instance.userData.depth = this.depth; }; GameLib.D3.Mesh.Box.prototype.updateInstance = function() { diff --git a/src/game-lib-d3-mesh-cylinder.js b/src/game-lib-d3-mesh-cylinder.js index d1bec8e..24dfb62 100644 --- a/src/game-lib-d3-mesh-cylinder.js +++ b/src/game-lib-d3-mesh-cylinder.js @@ -95,18 +95,16 @@ GameLib.D3.Mesh.Cylinder.prototype.createInstance = function() { this.updateVerticesFromGeometryInstance(geometry); } - var instance = GameLib.D3.Mesh.prototype.createInstance.call(this); + GameLib.D3.Mesh.prototype.createInstance.call(this); - instance.userData.radiusTop = this.radiusTop; - instance.userData.radiusBottom = this.radiusBottom; - instance.userData.height = this.height; - instance.userData.radiusSegments = this.radiusSegments; - instance.userData.heightSegments = this.heightSegments; - instance.userData.openEnded = this.openEnded; - instance.userData.thetaStart = this.thetaStart; - instance.userData.thetaLength = this.thetaLength; - - return instance; + this.instance.userData.radiusTop = this.radiusTop; + this.instance.userData.radiusBottom = this.radiusBottom; + this.instance.userData.height = this.height; + this.instance.userData.radiusSegments = this.radiusSegments; + this.instance.userData.heightSegments = this.heightSegments; + this.instance.userData.openEnded = this.openEnded; + this.instance.userData.thetaStart = this.thetaStart; + this.instance.userData.thetaLength = this.thetaLength; }; GameLib.D3.Mesh.Cylinder.prototype.updateInstance = function() { diff --git a/src/game-lib-d3-mesh-plane.js b/src/game-lib-d3-mesh-plane.js index 16b5634..06e1da8 100644 --- a/src/game-lib-d3-mesh-plane.js +++ b/src/game-lib-d3-mesh-plane.js @@ -107,30 +107,26 @@ GameLib.D3.Mesh.Plane.prototype.createInstance = function() { /** * Now construct the mesh instance */ - var instance = GameLib.D3.Mesh.prototype.createInstance.call(this); + GameLib.D3.Mesh.prototype.createInstance.call(this); /** * Apply some plane specific data to the instance */ - instance.userData.width = this.width; - instance.userData.height = this.height; - instance.userData.widthSegments = this.widthSegments; - instance.userData.heightSegments = this.heightSegments; - instance.userData.heightMapScale = this.heightMapScale; - instance.userData.isHeightMap = this.isHeightMap; - instance.userData.isClippingPlane = this.isClippingPlane; - instance.userData.distanceFromOrigin = this.distanceFromOrigin; + this.instance.userData.width = this.width; + this.instance.userData.height = this.height; + this.instance.userData.widthSegments = this.widthSegments; + this.instance.userData.heightSegments = this.heightSegments; + this.instance.userData.heightMapScale = this.heightMapScale; + this.instance.userData.isHeightMap = this.isHeightMap; + this.instance.userData.isClippingPlane = this.isClippingPlane; + this.instance.userData.distanceFromOrigin = this.distanceFromOrigin; if (this.isClippingPlane) { - instance.clipping = new THREE.Plane( + this.instance.clipping = new THREE.Plane( geometry.faces[0].normal, this.distanceFromOrigin ); - - } - - return instance; }; /** diff --git a/src/game-lib-d3-mesh-sphere.js b/src/game-lib-d3-mesh-sphere.js index aedb4fd..3c7af56 100644 --- a/src/game-lib-d3-mesh-sphere.js +++ b/src/game-lib-d3-mesh-sphere.js @@ -55,13 +55,12 @@ GameLib.D3.Mesh.Sphere.prototype.createInstance = function() { this.updateVerticesFromGeometryInstance(geometry); } - var instance = GameLib.D3.Mesh.prototype.createInstance.call(this); + GameLib.D3.Mesh.prototype.createInstance.call(this); - instance.userData.radius = this.radius; - instance.userData.widthSegments = this.widthSegments; - instance.userData.heightSegments = this.heightSegments; + this.instance.userData.radius = this.radius; + this.instance.userData.widthSegments = this.widthSegments; + this.instance.userData.heightSegments = this.heightSegments; - return instance; }; GameLib.D3.Mesh.Sphere.prototype.updateInstance = function() { diff --git a/src/game-lib-d3-mesh-text.js b/src/game-lib-d3-mesh-text.js index 781ee97..393e345 100644 --- a/src/game-lib-d3-mesh-text.js +++ b/src/game-lib-d3-mesh-text.js @@ -109,18 +109,16 @@ GameLib.D3.Mesh.Text.prototype.createInstance = function() { this.updateVerticesFromGeometryInstance(geometry); } - var instance = GameLib.D3.Mesh.prototype.createInstance.call(this); + GameLib.D3.Mesh.prototype.createInstance.call(this); - instance.userData.font = this.font; - instance.userData.size = this.size; - instance.userData.height = this.height; - instance.userData.curveSegments = this.curveSegments; - instance.userData.bevelEnabled = this.bevelEnabled; - instance.userData.bevelThickness = this.bevelThickness; - instance.userData.bevelSize = this.bevelSize; - instance.userData.bevelSegments = this.bevelSegments; - - return instance; + this.instance.userData.font = this.font; + this.instance.userData.size = this.size; + this.instance.userData.height = this.height; + this.instance.userData.curveSegments = this.curveSegments; + this.instance.userData.bevelEnabled = this.bevelEnabled; + this.instance.userData.bevelThickness = this.bevelThickness; + this.instance.userData.bevelSize = this.bevelSize; + this.instance.userData.bevelSegments = this.bevelSegments; }; GameLib.D3.Mesh.Text.prototype.updateInstance = function() { diff --git a/src/game-lib-d3-pass.js b/src/game-lib-d3-pass.js index 5f2a9f4..19e46e8 100644 --- a/src/game-lib-d3-pass.js +++ b/src/game-lib-d3-pass.js @@ -49,57 +49,65 @@ GameLib.D3.Pass.PASS_TYPE_COPY_SHADER = 0x2; /** * Create Pass instance - * @param update * @returns {*} */ -GameLib.D3.Pass.prototype.createInstance = function(update) { - - var instance = null; - - if (update) { - instance = this.instance; - } +GameLib.D3.Pass.prototype.createInstance = function() { 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'); - } - - instance = new THREE.RenderPass( - this.scene.instance, - this.camera.instance - ); + if (GameLib.Utils.UndefinedOrNull(this.scene)) { + throw new Error('no scene object'); } + if (GameLib.Utils.UndefinedOrNull(this.camera)) { + throw new Error('no camera object'); + } + + if (GameLib.Utils.UndefinedOrNull(THREE.RenderPass)) { + throw new Error('no render pass library') + } + + this.instance = new THREE.RenderPass( + 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'); + if (GameLib.Utils.UndefinedOrNull(THREE.CopyShader)) { + throw new Error('no copyshader library') } - instance = THREE.CopyShader; + this.instance = THREE.CopyShader; } else { console.warn('Render pass not supported yet: ' + this.passType); throw new Error('Render pass not supported yet: ' + this.passType); } - if (instance) { - instance.renderToScreen = this.renderToScreen; - } + this.instance.renderToScreen = this.renderToScreen; + + GameLib.Component.prototype.createInstance.call(this); - return instance; }; /** * Update Pass instance */ GameLib.D3.Pass.prototype.updateInstance = function() { - this.instance = this.createInstance(true); + + if ( + this.passType === GameLib.D3.Pass.PASS_TYPE_RENDER && !(this.instance instanceof THREE.RenderPass)){ + this.createInstance(); + } + + if ( + this.passType === GameLib.D3.Pass.PASS_TYPE_COPY_SHADER && !(this.instance instanceof THREE.CopyShader)){ + this.createInstance(); + } + + this.instance.renderToScreen = this.renderToScreen; + }; /** diff --git a/src/game-lib-d3-path-following.js b/src/game-lib-d3-path-following.js index aebc63a..2218fdd 100644 --- a/src/game-lib-d3-path-following.js +++ b/src/game-lib-d3-path-following.js @@ -127,6 +127,7 @@ GameLib.D3.PathFollowing.prototype.createInstance = function() { console.log('GameLib.D3.PathFollowing.prototype.createInstance()'); + GameLib.Component.prototype.createInstance.call(this); }; diff --git a/src/game-lib-d3-physics-world.js b/src/game-lib-d3-physics-world.js index 2f11a36..b3f29de 100644 --- a/src/game-lib-d3-physics-world.js +++ b/src/game-lib-d3-physics-world.js @@ -109,58 +109,70 @@ GameLib.D3.PhysicsWorld.prototype.constructor = GameLib.D3.PhysicsWorld; */ GameLib.D3.PhysicsWorld.prototype.createInstance = function() { - if (this.broadphase && this.broadphase.instance && - this.solver && this.solver.instance) { - - var instance = new CANNON.World(); - instance.broadphase = this.broadphase.instance; - instance.solver = this.solver.instance; - instance.gravity = this.gravity.instance; - instance.allowSleep = this.allowSleep; - - this.contactMaterials.map( - function(contactMaterial) { - if (contactMaterial && contactMaterial.instance) { - instance.addContactMaterial(contactMaterial.instance); - } else { - console.warn('contact materials not loaded at time of PhysicsWorld.createInstance()'); - } - } - ); - - this.rigidBodies.map( - function(rigidBody) { - if (rigidBody && rigidBody.instance) { - /** - * Ensure the rigidBody knows its parentWorld - */ - rigidBody.parentWorld = this; - - /** - * Add the rigid body to the instance world - */ - instance.add(rigidBody.instance); - } else { - console.warn('rigidbodies not loaded at time of PhysicsWorld.createInstance()'); - } - } - ); - - instance.defaultContactMaterial.friction = this.defaultContactMaterial.friction; - instance.defaultContactMaterial.restitution = this.defaultContactMaterial.restitution; - instance.defaultContactMaterial.contactEquationStiffness = this.defaultContactMaterial.contactEquationStiffness; - instance.defaultContactMaterial.contactEquationRelaxation = this.defaultContactMaterial.contactEquationRelaxation; - instance.defaultContactMaterial.frictionEquationStiffness = this.defaultContactMaterial.frictionEquationStiffness; - instance.defaultContactMaterial.frictionEquationRelaxation = this.defaultContactMaterial.frictionEquationRelaxation; - - return instance; - - } else { - console.warn('broadphase or solver not loaded during PhysicsWorld.createInstance()'); + if (GameLib.Utils.UndefinedOrNull(this.broadphase)) { + throw new Error('no broadphase'); } + if (GameLib.Utils.UndefinedOrNull(this.broadphase.instance)) { + throw new Error('no broadphase instance'); + } - return false; + if (GameLib.Utils.UndefinedOrNull(this.solver)) { + throw new Error('no solver'); + } + + if (GameLib.Utils.UndefinedOrNull(this.solver.instance)) { + throw new Error('no solver instance'); + } + + this.instance = new CANNON.World(); + this.instance.broadphase = this.broadphase.instance; + this.instance.solver = this.solver.instance; + this.instance.gravity = this.gravity.instance; + this.instance.allowSleep = this.allowSleep; + + this.contactMaterials.map( + function (contactMaterial) { + + if (GameLib.Utils.UndefinedOrNull(contactMaterial)) { + throw new Error('no contact material'); + } + + if (GameLib.Utils.UndefinedOrNull(contactMaterial.instance)) { + throw new Error('no contact material instance'); + } + + this.instance.addContactMaterial(contactMaterial.instance); + + }.bind(this) + ); + + this.rigidBodies.map( + function (rigidBody) { + + if (GameLib.Utils.UndefinedOrNull(rigidBody)) { + throw new Error('no rigidbody'); + } + + if (GameLib.Utils.UndefinedOrNull(rigidBody.instance)) { + throw new Error('no rigidbody instance'); + } + + rigidBody.parentWorld = this; + + this.instance.add(rigidBody.instance); + + }.bind(this) + ); + + this.instance.defaultContactMaterial.friction = this.defaultContactMaterial.friction; + this.instance.defaultContactMaterial.restitution = this.defaultContactMaterial.restitution; + this.instance.defaultContactMaterial.contactEquationStiffness = this.defaultContactMaterial.contactEquationStiffness; + this.instance.defaultContactMaterial.contactEquationRelaxation = this.defaultContactMaterial.contactEquationRelaxation; + this.instance.defaultContactMaterial.frictionEquationStiffness = this.defaultContactMaterial.frictionEquationStiffness; + this.instance.defaultContactMaterial.frictionEquationRelaxation = this.defaultContactMaterial.frictionEquationRelaxation; + + GameLib.Component.prototype.createInstance.call(this); }; GameLib.D3.PhysicsWorld.prototype.addRigidBody = function(rigidBody) { diff --git a/src/game-lib-d3-physics.js b/src/game-lib-d3-physics.js index e23047f..66924ab 100644 --- a/src/game-lib-d3-physics.js +++ b/src/game-lib-d3-physics.js @@ -25,7 +25,7 @@ GameLib.D3.Physics = function Physics( } this.physicsType = physicsType; - this.instance = this.createInstance(); + this.createInstance(); }; /** @@ -37,9 +37,8 @@ GameLib.D3.Physics.PHYSICS_TYPE_CANNON = 0x1; /** * @returns {THREE.Physics} */ -GameLib.D3.Physics.prototype.createInstance = function(update) { - var instance = CANNON; - return instance; +GameLib.D3.Physics.prototype.createInstance = function() { + this.instance = CANNON; }; /** diff --git a/src/game-lib-d3-raycast-vehicle.js b/src/game-lib-d3-raycast-vehicle.js index 4d2d9ef..b738454 100644 --- a/src/game-lib-d3-raycast-vehicle.js +++ b/src/game-lib-d3-raycast-vehicle.js @@ -87,24 +87,47 @@ GameLib.D3.RaycastVehicle.prototype.createInstance = function() { * delayedInstance somehow... * @type {GameLib.D3.RaycastVehicle|GameLib.D3.API.RaycastVehicle|*} */ - var instance = false; - if (this.chassis && this.chassis.instance) { - - instance = new CANNON.RaycastVehicle({ - chassisBody: this.chassis.instance - }); - - this.raycastWheels.map(function(wheel){ - instance.addWheel(wheel.instance); - }); - - if (this.parentWorld && this.parentWorld.instance) { - instance.addToWorld(this.parentWorld.instance); - } + if (GameLib.Utils.UndefinedOrNull(this.chassis)) { + throw new Error('no chassis'); } - return instance; + if (GameLib.Utils.UndefinedOrNull(this.chassis.instance)) { + throw new Error('no chassis instance'); + } + + if (GameLib.Utils.UndefinedOrNull(this.parentWorld)) { + throw new Error('no parent world'); + } + + if (GameLib.Utils.UndefinedOrNull(this.parentWorld.instance)) { + throw new Error('no parent world instance'); + } + + this.instance = new CANNON.RaycastVehicle({ + chassisBody: this.chassis.instance + }); + + this.raycastWheels.map( + function(wheel){ + + if (GameLib.Utils.UndefinedOrNull(wheel)) { + throw new Error('no wheel'); + } + + if (GameLib.Utils.UndefinedOrNull(wheel.instance)) { + throw new Error('no wheel instance'); + } + + this.instance.addWheel(wheel.instance); + + }.bind(this) + ); + + this.instance.addToWorld(this.parentWorld.instance); + + GameLib.Component.prototype.createInstance.call(this); + }; GameLib.D3.RaycastVehicle.prototype.updateInstance = function() { diff --git a/src/game-lib-d3-raycast-wheel.js b/src/game-lib-d3-raycast-wheel.js index 71e7c28..4e749a4 100644 --- a/src/game-lib-d3-raycast-wheel.js +++ b/src/game-lib-d3-raycast-wheel.js @@ -78,7 +78,7 @@ GameLib.D3.RaycastWheel.prototype.constructor = GameLib.D3.RaycastWheel; */ GameLib.D3.RaycastWheel.prototype.createInstance = function() { - var instance = { + this.instance = { radius: this.radius, directionLocal: this.directionLocal.instance, suspensionStiffness: this.suspensionStiffness, @@ -95,7 +95,7 @@ GameLib.D3.RaycastWheel.prototype.createInstance = function() { useCustomSlidingRotationalSpeed: this.useCustomSlidingRotationalSpeed }; - return instance; + GameLib.Component.prototype.createInstance.call(this); }; GameLib.D3.RaycastWheel.prototype.updateInstance = function() { diff --git a/src/game-lib-d3-raycaster.js b/src/game-lib-d3-raycaster.js index fbb1e01..00282d8 100644 --- a/src/game-lib-d3-raycaster.js +++ b/src/game-lib-d3-raycaster.js @@ -53,24 +53,29 @@ GameLib.D3.Raycaster.prototype.constructor = GameLib.D3.Raycaster; * Creates or updates a raycaster instance */ GameLib.D3.Raycaster.prototype.createInstance = function() { - var instance = new THREE.Raycaster(); - - instance.set( - this.position.instance, - this.direction.instance - ); - - return instance; -}; - -GameLib.D3.Raycaster.prototype.updateInstance = function() { - + this.instance = new THREE.Raycaster(); this.instance.set( this.position.instance, this.direction.instance ); - return null; + GameLib.Component.prototype.createInstance.call(this); +}; + +GameLib.D3.Raycaster.prototype.updateInstance = function() { + + this.position.instance.x = this.position.x; + this.position.instance.y = this.position.y; + this.position.instance.z = this.position.z; + + this.direction.instance.x = this.direction.x; + this.direction.instance.y = this.direction.y; + this.direction.instance.z = this.direction.z; + + this.instance.set( + this.position.instance, + this.direction.instance + ); }; GameLib.D3.Raycaster.prototype.toApiObject = function() { diff --git a/src/game-lib-d3-render-target.js b/src/game-lib-d3-render-target.js index 5848e57..7c24e94 100644 --- a/src/game-lib-d3-render-target.js +++ b/src/game-lib-d3-render-target.js @@ -48,23 +48,25 @@ GameLib.D3.RenderTarget.prototype.constructor = GameLib.D3.RenderTarget; */ GameLib.D3.RenderTarget.prototype.createInstance = function() { - var instance = null; - - if (this.texture && this.texture.loaded) { - - instance = new THREE.WebGLRenderTarget( - this.width, - this.height, - { - stencilBuffer : this.stencilBuffer - } - ); - - instance.texture = this.texture.instance; + if (GameLib.Utils.UndefinedOrNull(this.texture)) { + throw new Error('no texture'); } + if (GameLib.Utils.UndefinedOrNull(this.texture.instance)) { + throw new Error('no texture instance'); + } - return instance; + this.instance = new THREE.WebGLRenderTarget( + this.width, + this.height, + { + stencilBuffer : this.stencilBuffer + } + ); + + this.instance.texture = this.texture.instance; + + GameLib.Component.prototype.createInstance.call(this); }; /** @@ -82,14 +84,13 @@ GameLib.D3.RenderTarget.prototype.updateInstance = function() { this.instance.texture = null; } } else { - this.instance = this.createInstance(); - - if (this.instance) { + try { + this.createInstance(); this.loaded = true; + } catch (error) { + console.error(error); } } - - }; /** diff --git a/src/game-lib-d3-renderer.js b/src/game-lib-d3-renderer.js index bcdb141..e413c2b 100644 --- a/src/game-lib-d3-renderer.js +++ b/src/game-lib-d3-renderer.js @@ -158,12 +158,22 @@ GameLib.D3.Renderer.prototype.constructor = GameLib.D3.Renderer; */ GameLib.D3.Renderer.prototype.createInstance = function() { - var instance = new THREE.WebGLRenderer({ - canvas : this.domElement.instance - }); + if (GameLib.Utils.UndefinedOrNull(this.domElement)) { + throw new Error('no dom element'); + } + + if (GameLib.Utils.UndefinedOrNull(this.domElement.instance)) { + throw new Error('no dom element instance'); + } + + this.instance = new THREE.WebGLRenderer( + { + canvas : this.domElement.instance + } + ); if (this.clippingPlanes.length > 0) { - instance.clippingPlanes = this.clippingPlanes.map( + this.instance.clippingPlanes = this.clippingPlanes.map( function(clippingPlane) { if (!clippingPlane.isClippingPlane || !clippingPlane.instance || !clippingPlane.instance.clipping) { @@ -175,14 +185,14 @@ GameLib.D3.Renderer.prototype.createInstance = function() { ) } - instance.localClippingEnabled = this.localClipping; + this.instance.localClippingEnabled = this.localClipping; - instance.setSize( + this.instance.setSize( this.width, this.height ); - instance.setClearColor( + this.instance.setClearColor( new THREE.Color( this.clearColor.r, this.clearColor.g, @@ -191,13 +201,14 @@ GameLib.D3.Renderer.prototype.createInstance = function() { 1 - this.clearColor.a ); - instance.domElement.width = this.width; - instance.domElement.height = this.height; + this.instance.domElement.width = this.width; + this.instance.domElement.height = this.height; - instance.autoClear = this.autoClear; - instance.preserveDrawingBuffer = this.preserveDrawingBuffer; + this.instance.autoClear = this.autoClear; + this.instance.preserveDrawingBuffer = this.preserveDrawingBuffer; + + GameLib.Component.prototype.createInstance.call(this); - return instance; }; /** @@ -206,14 +217,14 @@ GameLib.D3.Renderer.prototype.createInstance = function() { GameLib.D3.Renderer.prototype.updateInstance = function() { if (!this.instance) { - this.instance = this.createInstance(); + try { + this.createInstance(); + } catch (error) { + console.error(error.message); + return; + } } - if (!this.instance) { - console.warn('could not create renderer instance'); - return; - } - this.instance.localClippingEnabled = this.localClipping; this.instance.setSize( diff --git a/src/game-lib-d3-rigid-body.js b/src/game-lib-d3-rigid-body.js index ad6552e..27bb093 100644 --- a/src/game-lib-d3-rigid-body.js +++ b/src/game-lib-d3-rigid-body.js @@ -97,7 +97,7 @@ GameLib.D3.RigidBody.prototype.constructor = GameLib.D3.RigidBody; */ GameLib.D3.RigidBody.prototype.createInstance = function() { - var instance = new CANNON.Body( + this.instance = new CANNON.Body( { mass : this.mass, friction : this.friction, @@ -117,30 +117,44 @@ GameLib.D3.RigidBody.prototype.createInstance = function() { } ); - instance.addEventListener("sleepy",function(event){ - console.log(this.name + " is feeling sleepy..."); - }.bind(this)); + this.instance.addEventListener( + "sleepy", + function() { + console.log(this.name + " is feeling sleepy..."); + }.bind(this) + ); - instance.addEventListener("sleep",function(event){ - console.log(this.name + " fell asleep!"); - }.bind(this)); + this.instance.addEventListener( + "sleep", + function() { + console.log(this.name + " fell asleep!"); + }.bind(this) + ); - instance.addEventListener("wakeup",function(event){ - console.log(this.name + " woke up!"); - }.bind(this)); + this.instance.addEventListener( + "wakeup", + function() { + console.log(this.name + " woke up!"); + }.bind(this) + ); - this.shapes.map(function(shape){ - - if (shape.loaded) { - instance.addShape(shape.instance) - } else { - console.warn('todo : listen for shape created and add here'); - throw new Error('todo : listen for shape created and add here'); - } - - }); + this.shapes.map( + function(shape) { - return instance; + if (GameLib.Utils.UndefinedOrNull(shape)) { + throw new Error('no shape'); + } + + if (GameLib.Utils.UndefinedOrNull(shape.instance)) { + throw new Error('no shape instance'); + } + + this.instance.addShape(shape.instance) + + }.bind(this) + ); + + GameLib.Component.prototype.createInstance.call(this); }; /** @@ -159,7 +173,10 @@ GameLib.D3.RigidBody.prototype.updateInstance = function() { this.quaternion.axis.instance.y = this.quaternion.axis.y; this.quaternion.axis.instance.z = this.quaternion.axis.z; - this.instance.quaternion.setFromAxisAngle(this.quaternion.axis.instance, this.quaternion.angle); + this.instance.quaternion.setFromAxisAngle( + this.quaternion.axis.instance, + this.quaternion.angle + ); this.quaternion.x = this.instance.quaternion.x; this.quaternion.y = this.instance.quaternion.y; diff --git a/src/game-lib-d3-scene.js b/src/game-lib-d3-scene.js index a27f9d0..8bdc307 100644 --- a/src/game-lib-d3-scene.js +++ b/src/game-lib-d3-scene.js @@ -151,36 +151,50 @@ GameLib.D3.Scene.prototype.constructor = GameLib.D3.Scene; */ GameLib.D3.Scene.prototype.createInstance = function() { - var instance = new THREE.Scene(); + this.instance = new THREE.Scene(); - instance.name = this.name; + this.instance.name = this.name; this.meshes.map( function(mesh) { - if (mesh && mesh.instance) { - instance.add(mesh.instance); - } else { - console.warn('invalid mesh at point of scene create instance'); + + if (GameLib.Utils.UndefinedOrNull(mesh)) { + throw new Error('no mesh'); } + + if (GameLib.Utils.UndefinedOrNull(mesh.instance)) { + throw new Error('no mesh instance'); + } + + this.instance.add(mesh.instance); + }.bind(this) ); this.lights.map( function(light) { - if (light && light.instance) { - instance.add(light.instance); - } else { - console.warn('invalid light at point of scene create instance'); + + + if (GameLib.Utils.UndefinedOrNull(light)) { + throw new Error('no light'); } - } + + if (GameLib.Utils.UndefinedOrNull(light.instance)) { + throw new Error('no light instance'); + } + + this.instance.add(light.instance); + + }.bind(this) ); - return instance; + GameLib.Component.prototype.createInstance.call(this); + }; GameLib.D3.Scene.prototype.updateInstance = function() { - this.instance.name = this.name; + this.instance.name = this.name; /** * Add missing meshes diff --git a/src/game-lib-d3-shape-0.js b/src/game-lib-d3-shape-0.js index 58e0865..a2f06d0 100644 --- a/src/game-lib-d3-shape-0.js +++ b/src/game-lib-d3-shape-0.js @@ -79,7 +79,7 @@ GameLib.D3.Shape.prototype.constructor = GameLib.D3.Shape; * Creates a shape instance or updates it */ GameLib.D3.Shape.prototype.createInstance = function() { - throw new Error('Do not instantiate this class directly - use a child class instead'); + GameLib.Component.prototype.createInstance.call(this); }; /** diff --git a/src/game-lib-d3-shape-box.js b/src/game-lib-d3-shape-box.js index 4b35ced..8113d23 100644 --- a/src/game-lib-d3-shape-box.js +++ b/src/game-lib-d3-shape-box.js @@ -45,11 +45,20 @@ GameLib.D3.Shape.Box.prototype.constructor = GameLib.D3.Shape.Box; */ GameLib.D3.Shape.Box.prototype.createInstance = function() { - var instance = new CANNON.Box( + if (GameLib.Utils.UndefinedOrNull(this.halfExtents)) { + throw new Error('no halfExtents'); + } + + if (GameLib.Utils.UndefinedOrNull(this.halfExtents.instance)) { + throw new Error('no halfExtents instance'); + } + + this.instance = new CANNON.Box( this.halfExtents.instance ); - return instance; + GameLib.D3.Shape.prototype.createInstance.call(this); + }; GameLib.D3.Shape.Box.prototype.updateInstance = function() { diff --git a/src/game-lib-d3-shape-convex-hull-0.js b/src/game-lib-d3-shape-convex-hull-0.js index fbc86cb..ca48c73 100644 --- a/src/game-lib-d3-shape-convex-hull-0.js +++ b/src/game-lib-d3-shape-convex-hull-0.js @@ -107,19 +107,62 @@ GameLib.D3.Shape.ConvexHull.prototype.createInstance = function() { var faceNormals = []; - var instance = new CANNON.ConvexPolyhedron( - this.vertices.map(function(vertex){ - return vertex.position.instance; - }), - this.faces.map(function(face){ - faceNormals.push(face.normal.instance); - return [face.v0index, face.v1index, face.v2index] - }) + this.instance = new CANNON.ConvexPolyhedron( + this.vertices.map( + function(vertex) { + + if (GameLib.Utils.UndefinedOrNull(vertex)) { + throw new Error('no vertex'); + } + + if (GameLib.Utils.UndefinedOrNull(vertex.position)) { + throw new Error('no vertex position'); + } + + if (GameLib.Utils.UndefinedOrNull(vertex.position.instance)) { + throw new Error('no vertex position instance'); + } + + return vertex.position.instance; + } + ), + this.faces.map( + function(face) { + + if (GameLib.Utils.UndefinedOrNull(face)) { + throw new Error('no face'); + } + + if (GameLib.Utils.UndefinedOrNull(face.normal)) { + throw new Error('no face normal'); + } + + if (GameLib.Utils.UndefinedOrNull(face.normal.instance)) { + throw new Error('no face normal instance'); + } + + if (GameLib.Utils.UndefinedOrNull(face.v0index)) { + throw new Error('no face v0index'); + } + + if (GameLib.Utils.UndefinedOrNull(face.v1index)) { + throw new Error('no face v1index'); + } + + if (GameLib.Utils.UndefinedOrNull(face.v2index)) { + throw new Error('no face v2index'); + } + + faceNormals.push(face.normal.instance); + + return [face.v0index, face.v1index, face.v2index]; + } + ) ); - instance.faceNormals = faceNormals; + this.instance.faceNormals = faceNormals; - return instance; + GameLib.D3.Shape.prototype.createInstance.call(this); }; /** diff --git a/src/game-lib-d3-shape-convex-hull-cylinder.js b/src/game-lib-d3-shape-convex-hull-cylinder.js index a9617d8..4e35554 100644 --- a/src/game-lib-d3-shape-convex-hull-cylinder.js +++ b/src/game-lib-d3-shape-convex-hull-cylinder.js @@ -55,14 +55,14 @@ GameLib.D3.Shape.ConvexHull.Cylinder.prototype.constructor = GameLib.D3.Shape.Co */ GameLib.D3.Shape.ConvexHull.Cylinder.prototype.createInstance = function() { - var instance = new CANNON.Cylinder( + this.instance = new CANNON.Cylinder( this.radiusTop, this.radiusBottom, this.height, this.numSegments ); - return instance; + GameLib.D3.Shape.prototype.createInstance.call(this); }; GameLib.D3.Shape.ConvexHull.Cylinder.prototype.updateInstance = function() { diff --git a/src/game-lib-d3-shape-height-map.js b/src/game-lib-d3-shape-height-map.js index 98ae00b..42c9f47 100644 --- a/src/game-lib-d3-shape-height-map.js +++ b/src/game-lib-d3-shape-height-map.js @@ -55,14 +55,16 @@ GameLib.D3.Shape.HeightMap.prototype.constructor = GameLib.D3.Shape.HeightMap; */ GameLib.D3.Shape.HeightMap.prototype.createInstance = function() { - var instance = new CANNON.Heightfield( + //TODO: initialize properly and throw when errors + + this.instance = new CANNON.Heightfield( this.heightData, { elemSize : this.elementSize } ); - return instance; + GameLib.D3.Shape.prototype.createInstance.call(this); }; /** diff --git a/src/game-lib-d3-shape-plane.js b/src/game-lib-d3-shape-plane.js index 26f1c95..d39a7e4 100644 --- a/src/game-lib-d3-shape-plane.js +++ b/src/game-lib-d3-shape-plane.js @@ -26,8 +26,11 @@ GameLib.D3.Shape.Plane.prototype.constructor = GameLib.D3.Shape.Plane; * @returns {GameLib.D3.Shape.Plane|*|SEA3D.Plane} */ GameLib.D3.Shape.Plane.prototype.createInstance = function() { - var instance = new CANNON.Plane(); - return instance; + /** + * A plane is just a plane at z = 0, to rotate it put it inside a rigid body and rotate the body + */ + this.instance = new CANNON.Plane(); + GameLib.D3.Shape.prototype.createInstance.call(this); }; GameLib.D3.Shape.Plane.prototype.updateInstance = function() { diff --git a/src/game-lib-d3-shape-sphere.js b/src/game-lib-d3-shape-sphere.js index 792a0b1..3054d48 100644 --- a/src/game-lib-d3-shape-sphere.js +++ b/src/game-lib-d3-shape-sphere.js @@ -34,11 +34,11 @@ GameLib.D3.Shape.Sphere.prototype.constructor = GameLib.D3.Shape.Sphere; */ GameLib.D3.Shape.Sphere.prototype.createInstance = function() { - var instance = new CANNON.Sphere( + this.instance = new CANNON.Sphere( this.radius ); - return instance; + GameLib.D3.Shape.prototype.createInstance.call(this); }; GameLib.D3.Shape.Sphere.prototype.updateInstance = function() { diff --git a/src/game-lib-d3-shape-tri-mesh.js b/src/game-lib-d3-shape-tri-mesh.js index 27334f1..cdb05eb 100644 --- a/src/game-lib-d3-shape-tri-mesh.js +++ b/src/game-lib-d3-shape-tri-mesh.js @@ -41,12 +41,13 @@ GameLib.D3.Shape.TriMesh.prototype.constructor = GameLib.D3.Shape.TriMesh; */ GameLib.D3.Shape.TriMesh.prototype.createInstance = function() { - var instance = new CANNON.TriMesh( + this.instance = new CANNON.TriMesh( this.vertices, this.indices ); - return instance; + GameLib.D3.Shape.prototype.createInstance.call(this); + }; /** diff --git a/src/game-lib-d3-skeleton.js b/src/game-lib-d3-skeleton.js index 37ee126..4966d8c 100644 --- a/src/game-lib-d3-skeleton.js +++ b/src/game-lib-d3-skeleton.js @@ -102,24 +102,23 @@ GameLib.D3.Skeleton.prototype.constructor = GameLib.D3.Skeleton; */ GameLib.D3.Skeleton.prototype.createInstance = function(update) { - var instance = null; + var boneInstances = this.bones.map ( + function (bone) { - if (update) { - //TODO - update instance with bone info - instance = this.instance; - } else { - instance = new THREE.Skeleton( - this.bones.map ( - function (bone) { - return bone.instance; - } - ) - ); - } + if (GameLib.Utils.UndefinedOrNull(bone)) { + throw new Error('no bone'); + } - instance.useVertexTexture = this.useVertexTexture; + if (GameLib.Utils.UndefinedOrNull(bone.instance)) { + throw new Error('no bone instance'); + } + + return bone.instance; + } + ); var parentBoneInstance = this.bones.reduce( + function (result, bone) { if (result) { @@ -135,13 +134,16 @@ GameLib.D3.Skeleton.prototype.createInstance = function(update) { null ); - if (!parentBoneInstance) { - console.warn('Did not find the main parent bone - skeleton will be broken'); - return; + if (GameLib.Utils.UndefinedOrNull(parentBoneInstance)) { + throw new Error('could not find parent bone instance'); } + this.instance = new THREE.Skeleton(boneInstances); + this.rootBoneInstance = parentBoneInstance; + this.instance.useVertexTexture = this.useVertexTexture; + this.boneIdToBone = {}; this.bones.map( @@ -150,6 +152,9 @@ GameLib.D3.Skeleton.prototype.createInstance = function(update) { }.bind(this) ); + /** + * TODO: check if this code does what its supposed to + */ this.bones.map( function (__parentBoneInstance) { return function(bone) { @@ -162,18 +167,18 @@ GameLib.D3.Skeleton.prototype.createInstance = function(update) { }(parentBoneInstance).bind(this) ); - instance.update(); + this.instance.update(); - instance.calculateInverses(); + this.instance.calculateInverses(); - return instance; + GameLib.Component.prototype.createInstance.call(this); }; /** * Updates the instance */ GameLib.D3.Skeleton.prototype.updateInstance = function() { - this.instance = this.createInstance(true); + }; /** diff --git a/src/game-lib-d3-solver.js b/src/game-lib-d3-solver.js index 38c8dd0..e9752d9 100644 --- a/src/game-lib-d3-solver.js +++ b/src/game-lib-d3-solver.js @@ -45,21 +45,18 @@ GameLib.D3.Solver.prototype.constructor = GameLib.D3.Solver; */ GameLib.D3.Solver.prototype.createInstance = function() { - var instance = null; - if (this.solverType === GameLib.D3.Solver.GS_SOLVER) { - instance = new CANNON.GSSolver(); + this.instance = new CANNON.GSSolver(); } else if (this.solverType === GameLib.D3.Solver.SPLIT_SOLVER) { - instance = new CANNON.SplitSolver(); + this.instance = new CANNON.SplitSolver(); } else { - console.warn('Unsupported solver type: ' + this.solverType); - throw new Error('Unsupported solver type: ' + this.solverType); + throw new Error('unsupported solver type: ' + this.solverType); } - instance.tolerance = this.tolerance; - instance.iterations = this.iterations; + this.instance.tolerance = this.tolerance; + this.instance.iterations = this.iterations; - return instance; + GameLib.Component.prototype.createInstance.call(this); }; /** diff --git a/src/game-lib-d3-spline.js b/src/game-lib-d3-spline.js index 2eda74a..28fe7f3 100644 --- a/src/game-lib-d3-spline.js +++ b/src/game-lib-d3-spline.js @@ -48,17 +48,27 @@ GameLib.D3.Spline.prototype.constructor = GameLib.D3.Spline; /** * Creates an instance spline - * @param update boolean */ GameLib.D3.Spline.prototype.createInstance = function() { var vertices = this.vertices.map( function (vertex) { + + if (GameLib.Utils.UndefinedOrNull(vertex)) { + throw new Error('no vertex') + } + + if (GameLib.Utils.UndefinedOrNull(vertex.instance)) { + throw new Error('no vertex instance') + } + return vertex.instance; } ); - return new THREE.CatmullRomCurve3(vertices); + this.instance = THREE.CatmullRomCurve3(vertices); + + GameLib.Component.prototype.createInstance.call(this); }; /** @@ -68,6 +78,15 @@ GameLib.D3.Spline.prototype.updateInstance = function() { var vertices = this.vertices.map( function (vertex) { + + if (GameLib.Utils.UndefinedOrNull(vertex)) { + throw new Error('no vertex') + } + + if (GameLib.Utils.UndefinedOrNull(vertex.instance)) { + throw new Error('no vertex instance') + } + return vertex.instance; } ); diff --git a/src/game-lib-d3-stats.js b/src/game-lib-d3-stats.js index a440da5..6923427 100644 --- a/src/game-lib-d3-stats.js +++ b/src/game-lib-d3-stats.js @@ -60,7 +60,8 @@ GameLib.D3.Stats.prototype.resize = function() { * Creates a helper instance */ GameLib.D3.Stats.prototype.createInstance = function() { - return new this.stats(); + this.instance = this.stats(); + GameLib.Component.prototype.createInstance.call(this); }; /** diff --git a/src/game-lib-d3-texture.js b/src/game-lib-d3-texture.js index c150c5e..3278c6c 100644 --- a/src/game-lib-d3-texture.js +++ b/src/game-lib-d3-texture.js @@ -26,6 +26,7 @@ GameLib.D3.Texture = function( apiTexture.typeId, apiTexture.name, apiTexture.image, + apiTexture.images, apiTexture.wrapS, apiTexture.wrapT, apiTexture.repeat, @@ -66,6 +67,17 @@ GameLib.D3.Texture = function( ); } + this.images = this.images.map( + function(image) { + if (image instanceof GameLib.D3.API.Image) { + return new GameLib.D3.Image( + this.graphics, + image + ); + } + }.bind(this) + ); + if (this.canvas instanceof GameLib.D3.API.Canvas) { this.canvas = new GameLib.D3.Canvas( this.graphics, @@ -78,6 +90,7 @@ GameLib.D3.Texture = function( GameLib.Component.COMPONENT_TEXTURE, { 'image' : GameLib.D3.Image, + 'images' : [GameLib.D3.Image], 'canvas' : GameLib.D3.Canvas } ); @@ -157,6 +170,7 @@ GameLib.D3.Texture.TYPE_RGBD_ENCODING = 3006; // MAXRANGE IS 256. GameLib.D3.Texture.TEXTURE_TYPE_NORMAL = 0x1; GameLib.D3.Texture.TEXTURE_TYPE_CUBE = 0x2; +GameLib.D3.Texture.TEXTURE_TYPE_CANVAS = 0x3; /** * Creates an instance of our texture object @@ -164,66 +178,89 @@ GameLib.D3.Texture.TEXTURE_TYPE_CUBE = 0x2; */ GameLib.D3.Texture.prototype.createInstance = function() { - // console.log('texture create instance'); - - var instance = null; - if (this.typeId === GameLib.D3.Texture.TEXTURE_TYPE_CUBE) { - if (this.image && this.image.instance) { + if (this.images.length !== 6) { + throw new Error('not enough images for cube texture'); + } + + var imageInstances = this.images.map( + function(image) { + + if (GameLib.Utils.UndefinedOrNull(image)) { + throw new Error('no image'); + } + + if (GameLib.Utils.UndefinedOrNull(image.instance)) { + throw new Error('no image instance'); + } + + return image.instance + } + ); + + this.instance = new THREE.CubeTexture(imageInstances); + + } else if (this.typeId === GameLib.D3.Texture.TEXTURE_TYPE_NORMAL) { + + if (GameLib.Utils.UndefinedOrNull(this.image)) { + + this.instance = new THREE.Texture(); - instance = new THREE.CubeTexture( - [ - this.image.instance, - this.image.instance, - this.image.instance, - this.image.instance, - this.image.instance, - this.image.instance - ] - ); - instance.needsUpdate = true; } else { - instance = new THREE.CubeTexture(); - } - if (this.mapping !== GameLib.D3.Texture.TYPE_CUBE_REFLECTION_MAPPING && - this.mapping !== GameLib.D3.Texture.TYPE_CUBE_REFRACTION_MAPPING - ) { - this.mapping = GameLib.D3.Texture.TYPE_CUBE_REFLECTION_MAPPING; - } + if (GameLib.Utils.UndefinedOrNull(this.image.instance)) { + throw new Error('no image instance'); + } - } else { - - if (this.image && this.image.instance) { - instance = new THREE.Texture( + this.instance = new THREE.Texture( this.image.instance ); - instance.needsUpdate = true; - } else if (this.canvas && this.canvas.instance) { - instance = new THREE.Texture(this.canvas.instance); + + } + + } else if (this.typeId === GameLib.D3.Texture.TEXTURE_TYPE_CANVAS) { + + if (GameLib.Utils.UndefinedOrNull(this.canvas)) { + + this.instance = new THREE.Texture(); + } else { - instance = new THREE.Texture(); + + if (GameLib.Utils.UndefinedOrNull(this.canvas.instance)) { + throw new Error('no canvas instance'); + } + + this.instance = new THREE.Texture( + this.canvas.instance + ); + } - if (this.mapping !== GameLib.D3.Texture.TYPE_UV_MAPPING) { - this.mapping = GameLib.D3.Texture.TYPE_UV_MAPPING; - } } - // - instance.name = this.name; - instance.flipY = this.flipY; - // instance.encoding = this.encoding; - instance.offset.x = this.offset.x; - instance.offset.y = this.offset.y; - instance.repeat.x = this.repeat.x; - instance.repeat.y = this.repeat.y; - //instance.mapping = this.mapping; - // instance.format = this.format; - instance.wrapS = this.wrapS; - instance.wrapT = this.wrapT; - return instance; + /** + * Some settings we copy from the instance + */ + this.mapping = this.instance.mapping; + this.encoding = this.instance.encoding; + this.format = this.instance.format; + + /** + * Others we apply to the instance + */ + this.instance.name = this.name; + this.instance.flipY = this.flipY; + this.instance.offset.x = this.offset.x; + this.instance.offset.y = this.offset.y; + this.instance.repeat.x = this.repeat.x; + this.instance.repeat.y = this.repeat.y; + this.instance.wrapS = this.wrapS; + this.instance.wrapT = this.wrapT; + + this.instance.needsUpdate = true; + + GameLib.Component.prototype.createInstance.call(this); }; /** @@ -231,69 +268,63 @@ GameLib.D3.Texture.prototype.createInstance = function() { */ GameLib.D3.Texture.prototype.updateInstance = function() { - // console.log('texture update instance'); - - var imageChanged = false; - - if (!this.instance) { - console.error('no texture instance'); - return; - } - - if (this.image && this.image.instance) { - - if (this.typeId === GameLib.D3.Texture.TEXTURE_TYPE_NORMAL) { - - /** - * Image overrides canvas - */ - if (this.instance.image !== this.image.instance) { - - this.instance = new THREE.Texture( - this.image.instance - ); - this.mapping = this.instance.mapping; - - imageChanged = true; - } - - } else if (this.typeId === GameLib.D3.Texture.TEXTURE_TYPE_CUBE) { - if (this.instance.image[0] !== this.image.instance) { - this.instance = new THREE.CubeTexture( - [ - this.image.instance, - this.image.instance, - this.image.instance, - this.image.instance, - this.image.instance, - this.image.instance - ] - ); - this.mapping = this.instance.mapping; - - imageChanged = true; - } - } - } else if ( - this.canvas && this.canvas.instance - ) { - if (this.typeId === GameLib.D3.Texture.TEXTURE_TYPE_NORMAL) { - console.log('update canvas instance here'); + if (GameLib.Utils.UndefinedOrNull(this.instance)) { + try { + this.createInstance(); + return; + } catch (error) { + console.error(error); } } - this.instance.name = this.name; - this.instance.flipY = this.flipY; - // this.instance.encoding = this.encoding; - this.instance.offset.x = this.offset.x; - this.instance.offset.y = this.offset.y; - this.instance.repeat.x = this.repeat.x; - this.instance.repeat.y = this.repeat.y; - this.instance.mapping = this.mapping; - //this.instance.format = this.format; - this.instance.wrapS = this.wrapS; - this.instance.wrapT = this.wrapT; - this.instance.needsUpdate = true; + var imageChanged = false; + + if (this.typeId === GameLib.D3.Texture.TEXTURE_TYPE_NORMAL) { + + if ( + GameLib.Utils.UndefinedOrNull(this.image) && + this.instance.image + ) { + try { + this.createInstance(); + imageChanged = true; + } catch (error) { + console.error(error); + } + } + + if (this.image && this.image.instance && this.instance.image !== this.image.instance) { + try { + this.createInstance(); + imageChanged = true; + } catch (error) { + console.error(error); + } + } + + } else if (this.typeId === GameLib.D3.Texture.TEXTURE_TYPE_CANVAS) { + + console.log('todo : canvas change check here'); + + } else if (this.typeId === GameLib.D3.Texture.TEXTURE_TYPE_CUBE) { + + console.log('todo : cube images change check here'); + + } + + this.instance.name = this.name; + this.instance.flipY = this.flipY; + this.instance.encoding = this.encoding; + this.instance.offset.x = this.offset.x; + this.instance.offset.y = this.offset.y; + this.instance.repeat.x = this.repeat.x; + this.instance.repeat.y = this.repeat.y; + this.instance.mapping = this.mapping; + this.instance.format = this.format; + this.instance.wrapS = this.wrapS; + this.instance.wrapT = this.wrapT; + + this.instance.needsUpdate = true; if (imageChanged) { this.publish( @@ -311,16 +342,16 @@ GameLib.D3.Texture.prototype.updateInstance = function() { */ GameLib.D3.Texture.prototype.toApiObject = function() { - var apiImage = null; - if (this.image) { - apiImage = this.image.id - } - var apiTexture = new GameLib.D3.API.Texture( this.id, this.typeId, this.name, - apiImage, + GameLib.Utils.IdOrNull(this.image), + this.images.map( + function(image) { + return GameLib.Utils.IdOrNull(image) + } + ), this.wrapS, this.wrapT, this.repeat.toApiObject(), diff --git a/src/game-lib-d3-viewport.js b/src/game-lib-d3-viewport.js index c89768d..587b644 100644 --- a/src/game-lib-d3-viewport.js +++ b/src/game-lib-d3-viewport.js @@ -45,14 +45,14 @@ GameLib.D3.Viewport.prototype.constructor = GameLib.D3.Viewport; * @returns {boolean} */ GameLib.D3.Viewport.prototype.createInstance = function() { - return true; + this.instance = true; + GameLib.Component.prototype.createInstance.call(this); }; /** * */ GameLib.D3.Viewport.prototype.updateInstance = function() { - return true; }; /** diff --git a/src/game-lib-dom-element.js b/src/game-lib-dom-element.js index dd769ba..10a8814 100644 --- a/src/game-lib-dom-element.js +++ b/src/game-lib-dom-element.js @@ -35,7 +35,8 @@ GameLib.DomElement.prototype.constructor = GameLib.DomElement; * @returns {*} */ GameLib.DomElement.prototype.createInstance = function() { - return document.getElementById(this.domElementId); + this.instance = document.getElementById(this.domElementId); + GameLib.Component.prototype.createInstance.call(this); }; /** diff --git a/src/game-lib-entity-manager.js b/src/game-lib-entity-manager.js index 007cd82..e9a846e 100644 --- a/src/game-lib-entity-manager.js +++ b/src/game-lib-entity-manager.js @@ -40,7 +40,8 @@ GameLib.EntityManager.prototype = Object.create(GameLib.Component.prototype); GameLib.EntityManager.prototype.constructor = GameLib.EntityManager; GameLib.EntityManager.prototype.createInstance = function() { - return GameLib.EntityManager.Instance; + this.instance = GameLib.EntityManager.Instance; + GameLib.Component.prototype.createInstance.call(this); }; GameLib.EntityManager.prototype.registerComponent = function(data) { diff --git a/src/game-lib-entity.js b/src/game-lib-entity.js index 221f20d..b130658 100644 --- a/src/game-lib-entity.js +++ b/src/game-lib-entity.js @@ -45,7 +45,8 @@ GameLib.Entity.prototype.createInstance = function() { /** * FUCK ecsjs and tiny-ecs - no client-side support and shitty code (only takes constructors as args) */ - return true; + this.instance = true; + GameLib.Component.prototype.createInstance.call(this); }; /** @@ -189,7 +190,7 @@ GameLib.Entity.prototype.removeComponent = function(component) { * Updates an entity instance */ GameLib.Entity.prototype.updateInstance = function() { - this.instance = this.createInstance(true); + console.log('entity update instance called'); }; /** diff --git a/src/game-lib-gui.js b/src/game-lib-gui.js index a0408b1..63f5e09 100644 --- a/src/game-lib-gui.js +++ b/src/game-lib-gui.js @@ -63,7 +63,8 @@ GameLib.GUI.prototype.constructor = GameLib.GUI; * Creates a helper instance */ GameLib.GUI.prototype.createInstance = function() { - return new dat.GUI( { autoPlace: false } ); + this.instance = new dat.GUI( { autoPlace: false } ); + GameLib.Component.prototype.createInstance.call(this); }; /** diff --git a/src/game-lib-matrix-4.js b/src/game-lib-matrix-4.js index 423aec2..d3d30f9 100644 --- a/src/game-lib-matrix-4.js +++ b/src/game-lib-matrix-4.js @@ -79,7 +79,7 @@ GameLib.Matrix4 = function( this.grain ); - this.instance = this.createInstance(); + this.createInstance(); }; GameLib.Matrix4.prototype = Object.create(GameLib.API.Matrix4.prototype); @@ -91,21 +91,13 @@ GameLib.Matrix4.prototype.constructor = GameLib.Matrix4; */ GameLib.Matrix4.prototype.createInstance = function(update) { - var instance = null; - - if (update) { - instance = this.instance; - } - - if (!instance) { - instance = new THREE.Matrix4(); - } + this.instance = new THREE.Matrix4(); /** * We transpose our matrix when we send it to three since we use a different ordering system * They say they use */ - instance.set( + this.instance.set( this.rows[0].x, this.rows[1].x, this.rows[2].x, @@ -123,8 +115,6 @@ GameLib.Matrix4.prototype.createInstance = function(update) { this.rows[2].w, this.rows[3].w ); - - return instance; }; /** @@ -132,7 +122,24 @@ GameLib.Matrix4.prototype.createInstance = function(update) { */ GameLib.Matrix4.prototype.updateInstance = function() { - this.createInstance(true); + this.instance.set( + this.rows[0].x, + this.rows[1].x, + this.rows[2].x, + this.rows[3].x, + this.rows[0].y, + this.rows[1].y, + this.rows[2].y, + this.rows[3].y, + this.rows[0].z, + this.rows[1].z, + this.rows[2].z, + this.rows[3].z, + this.rows[0].w, + this.rows[1].w, + this.rows[2].w, + this.rows[3].w + ); if (this.parentObject && this.parentObject.updateInstance) { diff --git a/src/game-lib-mouse.js b/src/game-lib-mouse.js index bfc272f..7dff512 100644 --- a/src/game-lib-mouse.js +++ b/src/game-lib-mouse.js @@ -41,7 +41,8 @@ GameLib.Mouse.prototype.constructor = GameLib.Mouse; * @returns {*} */ GameLib.Mouse.prototype.createInstance = function(update) { - return true; + this.instance = true; + GameLib.Component.prototype.createInstance.call(this); }; /** diff --git a/src/game-lib-quaternion.js b/src/game-lib-quaternion.js index aab20db..656fd4f 100644 --- a/src/game-lib-quaternion.js +++ b/src/game-lib-quaternion.js @@ -67,7 +67,7 @@ GameLib.Quaternion = function ( } this.grain = grain; - this.instance = this.createInstance(); + this.createInstance(); }; GameLib.Quaternion.prototype = Object.create(GameLib.API.Quaternion.prototype); @@ -75,40 +75,27 @@ GameLib.Quaternion.prototype.constructor = GameLib.Quaternion; /** * Creates an instance quaternion - * @param update * @returns {*} */ -GameLib.Quaternion.prototype.createInstance = function(update) { +GameLib.Quaternion.prototype.createInstance = function() { - var instance = null; - - if (update) { - instance = this.instance; - instance.x = this.x; - instance.y = this.y; - instance.z = this.z; - instance.w = this.w; - } else { - if (this.graphics) { - instance = new THREE.Quaternion( - this.x, - this.y, - this.z, - this.w - ); - } - - if (this.physics) { - instance = new CANNON.Quaternion( - this.x, - this.y, - this.z, - this.w - ); - } + if (this.graphics) { + this.instance = new THREE.Quaternion( + this.x, + this.y, + this.z, + this.w + ); } - return instance; + if (this.physics) { + this.instance = new CANNON.Quaternion( + this.x, + this.y, + this.z, + this.w + ); + } }; /** @@ -116,7 +103,10 @@ GameLib.Quaternion.prototype.createInstance = function(update) { */ GameLib.Quaternion.prototype.updateInstance = function() { - this.createInstance(true); + this.instance.x = this.x; + this.instance.y = this.y; + this.instance.z = this.z; + this.instance.w = this.w; if (this.parentObject && this.parentObject.updateInstance) { diff --git a/src/game-lib-system-0.js b/src/game-lib-system-0.js index 0d73be5..f7ebd9f 100644 --- a/src/game-lib-system-0.js +++ b/src/game-lib-system-0.js @@ -51,7 +51,8 @@ GameLib.System.SYSTEM_TYPE_VISUALIZATION = 0x100; GameLib.System.SYSTEM_TYPE_ALL = 0xFFFF; GameLib.System.prototype.createInstance = function() { - //console.log('GameLib.System.prototype.createInstance();'); + this.instance = true; + GameLib.Component.prototype.createInstance.call(this); }; /** @@ -59,72 +60,8 @@ GameLib.System.prototype.createInstance = function() { * @override */ GameLib.System.prototype.start = function() { - this.started = true; - console.log('starting ' + this.name); - - // if (this.systemType === GameLib.System.SYSTEM_TYPE_ANIMATION) { - // // 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]); - // } - // - // this.update(); - -}; - -/** - * @callback - * @override - */ -GameLib.System.prototype.update = function(deltaTime) { - - - if (this.systemType === GameLib.System.SYSTEM_TYPE_ANIMATION) { - - this.pathFollowingObjects.forEach( - function(object) { - object.update(deltaTime); - } - ); - - this.followObjects.forEach( - function(object) { - object.update(deltaTime); - } - ); - - this.lookAtObjects.forEach( - function(object) { - object.update(deltaTime); - } - ); - - this.meshObjects.forEach( - function(object) { - object.updateInstance(); - } - ); - - this.cameraObjects.forEach( - function(object) { - object.updateInstance(); - } - ); - - this.lightObjects.forEach( - function(object) { - object.updateInstance(); - } - ); - } - - - }; /** @@ -132,20 +69,8 @@ GameLib.System.prototype.update = function(deltaTime) { * @override */ GameLib.System.prototype.stop = function() { - this.started = false; - console.log('stopping ' + this.name); - - // - // if (this.systemType === GameLib.System.SYSTEM_TYPE_ANIMATION) { - // // this.pathFollowingObjects = []; - // // this.followObjects = []; - // // this.meshObjects = []; - // // this.lookAtObjects = []; - // // this.cameraObjects = []; - // // this.lightObjects = []; - // } }; /** diff --git a/src/game-lib-system-gui.js b/src/game-lib-system-gui.js index 1ec3754..2856bbf 100644 --- a/src/game-lib-system-gui.js +++ b/src/game-lib-system-gui.js @@ -992,7 +992,8 @@ GameLib.System.GUI.prototype.buildControl = function(folder, componentTemplate, property, { 'normal': GameLib.D3.Texture.TEXTURE_TYPE_NORMAL, - 'cube': GameLib.D3.Texture.TEXTURE_TYPE_CUBE + 'cube': GameLib.D3.Texture.TEXTURE_TYPE_CUBE, + 'canvas': GameLib.D3.Texture.TEXTURE_TYPE_CANVAS } ) ); diff --git a/src/game-lib-system-input.js b/src/game-lib-system-input.js index 56f8520..150fce2 100644 --- a/src/game-lib-system-input.js +++ b/src/game-lib-system-input.js @@ -164,7 +164,7 @@ GameLib.System.Input.prototype.start = function() { false ); - editorControl.instance = editorControl.delayedInstance(); + editorControl.delayedInstance(); editorControl.domElement.instance.addEventListener( 'mousewheel', diff --git a/src/game-lib-system-linking.js b/src/game-lib-system-linking.js index 75517a3..297ef05 100644 --- a/src/game-lib-system-linking.js +++ b/src/game-lib-system-linking.js @@ -267,18 +267,22 @@ GameLib.System.Linking.prototype.resolveDependencies = function(component) { * because the component constructor or linking system did not know at time of 'createInstance' * that it required another object to fully become active */ - if (!parentComponent.instance) { + if (GameLib.Utils.UndefinedOrNull(parentComponent.instance)) { - parentComponent.instance = parentComponent.createInstance(); + try { - if (parentComponent.instance) { - parentComponent.loaded = true; parentComponent.buildIdToObject(); - if (this.resolved.indexOf(parentComponent) === -1) { - this.resolved.push(parentComponent); + + if (parentComponent.loaded) { + parentComponent.createInstance(); } - GameLib.Event.EmitInstanceEvents(parentComponent); + + GameLib.Utils.PushUnique(this.resolved, parentComponent); + + } catch (error) { + console.error(error); } + } else { /** * These dependencies have already been met - the parentComponent properties have changed. @@ -308,16 +312,18 @@ GameLib.System.Linking.prototype.resolveDependencies = function(component) { delete parentComponent.dependencies; - parentComponent.instance = parentComponent.createInstance(); + try { - if (parentComponent.instance) { - parentComponent.loaded = true; parentComponent.buildIdToObject(); - if (this.resolved.indexOf(parentComponent) === -1) { - this.resolved.push(parentComponent); + + if (parentComponent.loaded) { + parentComponent.createInstance(); } - GameLib.Event.EmitInstanceEvents(parentComponent); + GameLib.Utils.PushUnique(this.resolved, parentComponent); + + } catch (error) { + console.error(error); } } @@ -390,6 +396,7 @@ GameLib.System.Linking.prototype.registerDependencies = function(component) { component.dependencies.length > 0) { component.dependencies = component.dependencies.reduce( + function(result, id) { /** @@ -397,16 +404,15 @@ GameLib.System.Linking.prototype.registerDependencies = function(component) { */ var processedComponent = GameLib.EntityManager.Instance.findComponentById(id); - if (processedComponent && processedComponent.loaded) { + if (processedComponent) { + // if (processedComponent && processedComponent.loaded) { /** * Link the component */ this.link(component, {component: processedComponent}); - if (this.resolved.indexOf(processedComponent) === -1) { - this.resolved.push(processedComponent); - } + GameLib.Utils.PushUnique(this.resolved, processedComponent); } else { @@ -437,6 +443,7 @@ GameLib.System.Linking.prototype.registerDependencies = function(component) { } return result; + }.bind(this), [] ); @@ -445,11 +452,16 @@ GameLib.System.Linking.prototype.registerDependencies = function(component) { delete component.dependencies; - component.instance = component.createInstance(); - if (component.instance) { - component.loaded = true; - component.buildIdToObject(); - GameLib.Event.EmitInstanceEvents(component); + component.buildIdToObject(); + + if (component.loaded) { + try { + component.createInstance(); + //TODO: test below + GameLib.Utils.PushUnique(this.resolved, component); + } catch (error) { + console.log(error); + } } } } @@ -527,17 +539,16 @@ GameLib.System.Linking.prototype.imageNotFound = function(data) { if (texture.dependencies.length === 0) { delete texture.dependencies; - texture.instance = texture.createInstance(); + texture.buildIdToObject(); - if (texture.instance) { - texture.loaded = true; - - this.resolveDependencies(texture); - - texture.buildIdToObject(); - - GameLib.Event.EmitInstanceEvents(texture); - } + if (texture.loaded) { + try { + texture.createInstance(); + this.resolveDependencies(texture); + } catch (error) { + console.error(error); + } + } } } }.bind(this)); @@ -688,34 +699,47 @@ GameLib.System.Linking.prototype.imageInstanceCreated = function(data) { */ var textures = GameLib.EntityManager.Instance.queryComponents([GameLib.D3.Texture]); - var materials = GameLib.EntityManager.Instance.queryComponents([GameLib.D3.Material]); + //var materials = GameLib.EntityManager.Instance.queryComponents([GameLib.D3.Material]); - /** - * For each material that uses this texture - */ + /** + * Find all textures which use this image + */ textures.map(function(texture){ - materials.map(function(material){ + if (texture.image === data.image || + texture.images.indexOf(data.image) !== -1 + ) { - var materialNeedsUpdate = false; + /** + * Ok - this image is in use + */ + texture.updateInstance(); - for (var property in material) { - if (material.hasOwnProperty(property) && - material[property] === texture) { - materialNeedsUpdate = true; - } - } + /** + * Find all materials that use this texture + * This is no longer necessary - because texture updateInstance should notify materials when its image changes + */ + // materials.map(function(material){ + // + // var materialNeedsUpdate = false; + // + // for (var property in material) { + // if (material.hasOwnProperty(property) && + // material[property] === texture) { + // materialNeedsUpdate = true; + // } + // } + // + // if (materialNeedsUpdate) { + // material.updateInstance(); + // } + // + // }); - if (materialNeedsUpdate) { - material.updateInstance(); - } - }); + } }); - - - }; GameLib.System.Linking.prototype.solverInstanceCreated = function(data) { diff --git a/src/game-lib-system-storage.js b/src/game-lib-system-storage.js index 93d8721..5b68b27 100644 --- a/src/game-lib-system-storage.js +++ b/src/game-lib-system-storage.js @@ -480,15 +480,6 @@ GameLib.System.Storage.prototype.loadComponent = function(apiUrl, toProcess, inc } } - if (runtimeComponent instanceof GameLib.D3.Image) { - GameLib.Event.Emit( - GameLib.Event.LOAD_IMAGE, - { - image : runtimeComponent - } - ) - } - if (runtimeComponent) { loaded.push(runtimeComponent); } @@ -811,9 +802,11 @@ GameLib.System.Storage.prototype.imageUploadComplete = function(data) { * Overrride this image if possible * @type {GameLib.D3.Image} */ - image = GameLib.D3.Image.FromObject(this.graphics, imageData); + GameLib.D3.Image.FromObject(this.graphics, imageData); + } else { image = runtimeImages.reduce( + function(result, runtimeImage){ if (imageData.id === runtimeImage.id) { @@ -825,17 +818,10 @@ GameLib.System.Storage.prototype.imageUploadComplete = function(data) { }, null ); + + image.updateInstance(); } - - GameLib.Event.Emit( - GameLib.Event.LOAD_IMAGE, - { - image : image, - createTexture : true - } - ); - }.bind(this)); }; @@ -856,13 +842,6 @@ GameLib.System.Storage.prototype.processBlenderData = function(data) { var image = GameLib.D3.Image.FromObject(this.graphics, imageData); - GameLib.Event.Emit( - GameLib.Event.LOAD_IMAGE, - { - image : image - } - ); - GameLib.Event.Emit( GameLib.Event.COMPONENT_CREATED, { @@ -1052,7 +1031,7 @@ GameLib.System.Storage.prototype.loadImage = function(data) { window.URL.revokeObjectURL(url); image.instance = img; - image.loaded = true; + image.publish( GameLib.Event.IMAGE_INSTANCE_CREATED, { diff --git a/src/game-lib-vector2.js b/src/game-lib-vector2.js index 4ce59fc..aad03f6 100644 --- a/src/game-lib-vector2.js +++ b/src/game-lib-vector2.js @@ -39,7 +39,7 @@ GameLib.Vector2 = function ( } this.grain = grain; - this.instance = this.createInstance(); + this.createInstance(); }; GameLib.Vector2.prototype = Object.create(GameLib.API.Vector2.prototype); @@ -48,22 +48,10 @@ GameLib.Vector2.prototype.constructor = GameLib.Vector2; /** * Creates an instance vector2 - * @param update * @returns {*} */ -GameLib.Vector2.prototype.createInstance = function(update) { - - var instance = null; - - if (update) { - instance = this.instance; - instance.x = this.x; - instance.y = this.y; - } else { - instance = new THREE.Vector2(this.x, this.y); - } - - return instance; +GameLib.Vector2.prototype.createInstance = function() { + this.instance = new THREE.Vector2(this.x, this.y); }; /** @@ -71,7 +59,8 @@ GameLib.Vector2.prototype.createInstance = function(update) { */ GameLib.Vector2.prototype.updateInstance = function() { - this.createInstance(true); + this.instance.x = this.x; + this.instance.y = this.y; if (this.parentObject && this.parentObject.updateInstance) { diff --git a/src/game-lib-vector3.js b/src/game-lib-vector3.js index 75f3b9f..57adaa2 100644 --- a/src/game-lib-vector3.js +++ b/src/game-lib-vector3.js @@ -52,7 +52,7 @@ GameLib.Vector3 = function ( } this.grain = grain; - this.instance = this.createInstance(); + this.createInstance(); }; GameLib.Vector3.prototype = Object.create(GameLib.API.Vector3.prototype); @@ -64,23 +64,20 @@ GameLib.Vector3.prototype.constructor = GameLib.Vector3; */ GameLib.Vector3.prototype.createInstance = function() { - var instance = null; - if (this.graphics) { - instance = new THREE.Vector3( + this.instance = new THREE.Vector3( this.x, this.y, this.z ); } else if (this.physics) { - instance = new CANNON.Vec3( + this.instance = new CANNON.Vec3( this.x, this.y, this.z ) } - return instance; }; /** diff --git a/src/game-lib-vector4.js b/src/game-lib-vector4.js index de9820c..02eb117 100644 --- a/src/game-lib-vector4.js +++ b/src/game-lib-vector4.js @@ -42,32 +42,23 @@ GameLib.Vector4 = function ( } this.grain = grain; - this.instance = this.createInstance(); + this.createInstance(); }; GameLib.Vector4.prototype = Object.create(GameLib.API.Vector4.prototype); GameLib.Vector4.prototype.constructor = GameLib.Vector4; /** - * Creates an instance vector3 - * @param update + * Creates an instance vector4 * @returns {*} */ -GameLib.Vector4.prototype.createInstance = function(update) { - - var instance = null; - - if (update) { - instance = this.instance; - instance.x = this.x; - instance.y = this.y; - instance.z = this.z; - instance.w = this.w; - } else { - instance = new THREE.Quaternion(this.x, this.y, this.z, this.w); - } - - return instance; +GameLib.Vector4.prototype.createInstance = function() { + this.instance = new THREE.Quaternion( + this.x, + this.y, + this.z, + this.w + ); }; /** @@ -75,7 +66,10 @@ GameLib.Vector4.prototype.createInstance = function(update) { */ GameLib.Vector4.prototype.updateInstance = function() { - this.createInstance(true); + this.instance.x = this.x; + this.instance.y = this.y; + this.instance.z = this.z; + this.instance.w = this.w; if (this.parentObject && this.parentObject.updateInstance) {