From d0ac06f2b617cd79153efe20cc5bc5963ab92dfc Mon Sep 17 00:00:00 2001 From: "Theunis J. Botha" Date: Tue, 17 Jan 2017 13:24:45 +0100 Subject: [PATCH] constructor updates - inheritance getting stable --- {src => bak}/game-lib-d3-physics.js | 0 {src => bak}/game-lib-d3-raycast-vehicle.js | 0 {src => bak}/game-lib-d3-raycast-wheel.js | 0 .../game-lib-d3-rigid-body-vehicle.js | 0 {src => bak}/game-lib-d3-rigid-body.js | 0 {src => bak}/game-lib-d3-rigid-wheel.js | 0 {src => bak}/game-lib-d3-shape.js | 0 {src => bak}/game-lib-d3-sky-box.js | 0 {src => bak}/game-lib-d3-solver.js | 0 {src => bak}/game-lib-d3-world.js | 0 {src => bak}/game-lib-matrix-3.js | 0 src/game-lib-api-matrix4.js | 68 ++--- src/game-lib-api-quaternion-a.js | 32 ++- src/game-lib-api-vector2.js | 2 +- src/game-lib-color.js | 17 +- src/game-lib-d3-api-input-editor.js | 8 + src/game-lib-d3-bone.js | 16 +- src/game-lib-d3-camera.js | 12 +- src/game-lib-d3-follow.js | 20 +- src/game-lib-d3-input-editor.js | 63 +++-- src/game-lib-d3-light.js | 31 ++- src/game-lib-d3-look-at.js | 24 +- src/game-lib-d3-material.js | 11 +- src/game-lib-d3-mesh.js | 62 +++-- src/game-lib-d3-pass.js | 4 + src/game-lib-d3-path-following.js | 44 +-- src/game-lib-d3-raycaster.js | 61 +++-- src/game-lib-d3-render-target.js | 4 + src/game-lib-d3-renderer.js | 4 + src/game-lib-d3-scene.js | 101 ++++--- src/game-lib-d3-skeleton.js | 54 ++-- src/game-lib-d3-spline.js | 10 +- src/game-lib-d3-texture.js | 19 +- src/game-lib-d3-vertex.js | 8 +- src/game-lib-d3-viewport.js | 6 +- src/game-lib-entity-manager.js | 4 + src/game-lib-entity.js | 4 + src/game-lib-matrix-4.js | 150 +++++------ src/game-lib-mouse.js | 5 +- src/game-lib-quaternion.js | 36 +-- src/game-lib-vector2.js | 251 +++++++++++++----- src/game-lib-vector3.js | 49 ++-- src/game-lib-vector4.js | 26 +- 43 files changed, 772 insertions(+), 434 deletions(-) rename {src => bak}/game-lib-d3-physics.js (100%) rename {src => bak}/game-lib-d3-raycast-vehicle.js (100%) rename {src => bak}/game-lib-d3-raycast-wheel.js (100%) rename {src => bak}/game-lib-d3-rigid-body-vehicle.js (100%) rename {src => bak}/game-lib-d3-rigid-body.js (100%) rename {src => bak}/game-lib-d3-rigid-wheel.js (100%) rename {src => bak}/game-lib-d3-shape.js (100%) rename {src => bak}/game-lib-d3-sky-box.js (100%) rename {src => bak}/game-lib-d3-solver.js (100%) rename {src => bak}/game-lib-d3-world.js (100%) rename {src => bak}/game-lib-matrix-3.js (100%) diff --git a/src/game-lib-d3-physics.js b/bak/game-lib-d3-physics.js similarity index 100% rename from src/game-lib-d3-physics.js rename to bak/game-lib-d3-physics.js diff --git a/src/game-lib-d3-raycast-vehicle.js b/bak/game-lib-d3-raycast-vehicle.js similarity index 100% rename from src/game-lib-d3-raycast-vehicle.js rename to bak/game-lib-d3-raycast-vehicle.js diff --git a/src/game-lib-d3-raycast-wheel.js b/bak/game-lib-d3-raycast-wheel.js similarity index 100% rename from src/game-lib-d3-raycast-wheel.js rename to bak/game-lib-d3-raycast-wheel.js diff --git a/src/game-lib-d3-rigid-body-vehicle.js b/bak/game-lib-d3-rigid-body-vehicle.js similarity index 100% rename from src/game-lib-d3-rigid-body-vehicle.js rename to bak/game-lib-d3-rigid-body-vehicle.js diff --git a/src/game-lib-d3-rigid-body.js b/bak/game-lib-d3-rigid-body.js similarity index 100% rename from src/game-lib-d3-rigid-body.js rename to bak/game-lib-d3-rigid-body.js diff --git a/src/game-lib-d3-rigid-wheel.js b/bak/game-lib-d3-rigid-wheel.js similarity index 100% rename from src/game-lib-d3-rigid-wheel.js rename to bak/game-lib-d3-rigid-wheel.js diff --git a/src/game-lib-d3-shape.js b/bak/game-lib-d3-shape.js similarity index 100% rename from src/game-lib-d3-shape.js rename to bak/game-lib-d3-shape.js diff --git a/src/game-lib-d3-sky-box.js b/bak/game-lib-d3-sky-box.js similarity index 100% rename from src/game-lib-d3-sky-box.js rename to bak/game-lib-d3-sky-box.js diff --git a/src/game-lib-d3-solver.js b/bak/game-lib-d3-solver.js similarity index 100% rename from src/game-lib-d3-solver.js rename to bak/game-lib-d3-solver.js diff --git a/src/game-lib-d3-world.js b/bak/game-lib-d3-world.js similarity index 100% rename from src/game-lib-d3-world.js rename to bak/game-lib-d3-world.js diff --git a/src/game-lib-matrix-3.js b/bak/game-lib-matrix-3.js similarity index 100% rename from src/game-lib-matrix-3.js rename to bak/game-lib-matrix-3.js diff --git a/src/game-lib-api-matrix4.js b/src/game-lib-api-matrix4.js index 45a8c0b..15ea6ab 100644 --- a/src/game-lib-api-matrix4.js +++ b/src/game-lib-api-matrix4.js @@ -56,6 +56,40 @@ GameLib.API.Matrix4 = function ApiMatrix4( this.up = new GameLib.API.Vector4(); }; +/** + * Returns an API matrix from an Object matrix + * @param objectMatrix + * @constructor + */ +GameLib.API.Matrix4.FromObjectMatrix = function(objectMatrix) { + return new GameLib.API.Matrix4( + new GameLib.API.Vector4( + objectMatrix.rows[0].x, + objectMatrix.rows[0].y, + objectMatrix.rows[0].z, + objectMatrix.rows[0].w + ), + new GameLib.API.Vector4( + objectMatrix.rows[1].x, + objectMatrix.rows[1].y, + objectMatrix.rows[1].z, + objectMatrix.rows[1].w + ), + new GameLib.API.Vector4( + objectMatrix.rows[2].x, + objectMatrix.rows[2].y, + objectMatrix.rows[2].z, + objectMatrix.rows[2].w + ), + new GameLib.API.Vector4( + objectMatrix.rows[3].x, + objectMatrix.rows[3].y, + objectMatrix.rows[3].z, + objectMatrix.rows[3].w + ) + ) +}; + GameLib.API.Matrix4.prototype.rotationMatrixX = function (radians) { this.identity(); this.rows[1] = new GameLib.API.Vector4(0, Math.cos(radians), -1 * Math.sin(radians), 0); @@ -130,37 +164,3 @@ GameLib.API.Matrix4.prototype.identity = function () { new GameLib.API.Vector4(0, 0, 0, 1) ]; }; - -/** - * Returns an API matrix from an Object matrix - * @param objectMatrix - * @constructor - */ -GameLib.API.Matrix4.FromObjectMatrix = function(objectMatrix) { - return new GameLib.API.Matrix4( - new GameLib.API.Vector4( - objectMatrix.rows[0].x, - objectMatrix.rows[0].y, - objectMatrix.rows[0].z, - objectMatrix.rows[0].w - ), - new GameLib.API.Vector4( - objectMatrix.rows[1].x, - objectMatrix.rows[1].y, - objectMatrix.rows[1].z, - objectMatrix.rows[1].w - ), - new GameLib.API.Vector4( - objectMatrix.rows[2].x, - objectMatrix.rows[2].y, - objectMatrix.rows[2].z, - objectMatrix.rows[2].w - ), - new GameLib.API.Vector4( - objectMatrix.rows[3].x, - objectMatrix.rows[3].y, - objectMatrix.rows[3].z, - objectMatrix.rows[3].w - ) - ) -}; diff --git a/src/game-lib-api-quaternion-a.js b/src/game-lib-api-quaternion-a.js index 939742d..4692128 100644 --- a/src/game-lib-api-quaternion-a.js +++ b/src/game-lib-api-quaternion-a.js @@ -1,4 +1,21 @@ -GameLib.API.Quaternion = function (x, y, z, w, axis, angle) { +/** + * Quaternion + * @param x + * @param y + * @param z + * @param w + * @param axis + * @param angle + * @constructor + */ +GameLib.API.Quaternion = function ( + x, + y, + z, + w, + axis, + angle +) { if (GameLib.Utils.UndefinedOrNull(x)) { x = 0; @@ -202,16 +219,19 @@ GameLib.API.Quaternion.prototype.slerp = function (quaternion, t) { * @constructor */ GameLib.API.Quaternion.FromObjectQuaternion = function (objectQuaternion) { + + var apiAxis = null; + + if (objectQuaternion.axis) { + apiAxis = GameLib.API.Vector3.FromObjectVector(objectQuaternion.axis); + } + return new GameLib.API.Quaternion( objectQuaternion.x, objectQuaternion.y, objectQuaternion.z, objectQuaternion.w, - new GameLib.API.Vector3( - objectQuaternion.axis.x, - objectQuaternion.axis.y, - objectQuaternion.axis.z - ), + apiAxis, objectQuaternion.angle ) }; \ No newline at end of file diff --git a/src/game-lib-api-vector2.js b/src/game-lib-api-vector2.js index a8054f1..fca51f1 100644 --- a/src/game-lib-api-vector2.js +++ b/src/game-lib-api-vector2.js @@ -1,4 +1,4 @@ -GameLib.API.Vector2 = function ApiVector2(x, y) { +GameLib.API.Vector2 = function (x, y) { if (GameLib.Utils.UndefinedOrNull(x)) { x = 0; diff --git a/src/game-lib-color.js b/src/game-lib-color.js index e03aa39..55989fe 100644 --- a/src/game-lib-color.js +++ b/src/game-lib-color.js @@ -6,11 +6,20 @@ * @param grain Number * @constructor */ -GameLib.Color = function (graphics, parentObject, apiColor, grain) { +GameLib.Color = function ( + graphics, + apiColor, + parentObject, + grain +) { this.graphics = graphics; this.graphics.isNotThreeThrow(); + if (GameLib.Utils.UndefinedOrNull(apiColor)) { + apiColor = {}; + } + GameLib.API.Color.call( this, apiColor.r, @@ -19,6 +28,9 @@ GameLib.Color = function (graphics, parentObject, apiColor, grain) { apiColor.a ); + if (GameLib.Utils.UndefinedOrNull(parentObject)) { + parentObject = null; + } this.parentObject = parentObject; if (GameLib.Utils.UndefinedOrNull(grain)) { @@ -60,7 +72,8 @@ GameLib.Color.prototype.updateInstance = function() { this.createInstance(true); - if (this.parentObject.updateInstance) { + if (this.parentObject && + this.parentObject.updateInstance) { this.parentObject.updateInstance(); } }; diff --git a/src/game-lib-d3-api-input-editor.js b/src/game-lib-d3-api-input-editor.js index 29f1c60..2c3ea95 100644 --- a/src/game-lib-d3-api-input-editor.js +++ b/src/game-lib-d3-api-input-editor.js @@ -10,6 +10,7 @@ * @param heightOffset * @param containerWidthOffset * @param containerHeightOffset + * @param selectDelayMs * @param parentEntity * @constructor */ @@ -24,6 +25,7 @@ GameLib.D3.API.Input.Editor = function ( heightOffset, containerWidthOffset, containerHeightOffset, + selectDelayMs, parentEntity ) { GameLib.Component.call( @@ -86,6 +88,11 @@ GameLib.D3.API.Input.Editor = function ( containerHeightOffset = 80; } this.containerHeightOffset = containerHeightOffset; + + if (GameLib.Utils.UndefinedOrNull(selectDelayMs)) { + selectDelayMs = 300; + } + this.selectDelayMs = selectDelayMs; }; GameLib.D3.API.Input.Editor.prototype = Object.create(GameLib.Component.prototype); @@ -109,6 +116,7 @@ GameLib.D3.API.Input.Editor.FromObjectComponent = function(objectComponent) { objectComponent.heightOffset, objectComponent.containerWidthOffset, objectComponent.containerHeightOffset, + objectComponent.selectDelayMs, objectComponent.parentEntity ); }; diff --git a/src/game-lib-d3-bone.js b/src/game-lib-d3-bone.js index 4a6bace..7052063 100644 --- a/src/game-lib-d3-bone.js +++ b/src/game-lib-d3-bone.js @@ -29,26 +29,26 @@ GameLib.D3.Bone = function ( this.position = new GameLib.Vector3( graphics, - this, - this.position + this.position, + this ); this.quaternion = new GameLib.Quaternion( graphics, - this, - this.quaternion + this.quaternion, + this ); this.scale = new GameLib.Vector3( graphics, - this, - this.scale + this.scale, + this ); this.up = new GameLib.Vector3( graphics, - this, - this.up + this.up, + this ); this.instance = this.createInstance(); diff --git a/src/game-lib-d3-camera.js b/src/game-lib-d3-camera.js index 2bbc078..1f1f027 100644 --- a/src/game-lib-d3-camera.js +++ b/src/game-lib-d3-camera.js @@ -41,20 +41,20 @@ GameLib.D3.Camera = function( this.position = new GameLib.Vector3( graphics, - this, - this.position + this.position, + this ); this.quaternion = new GameLib.Quaternion( graphics, - this, - this.quaternion + this.quaternion, + this ); this.lookAt = new GameLib.Vector3( graphics, - this, - this.lookAt + this.lookAt, + this ); this.instance = this.createInstance(); diff --git a/src/game-lib-d3-follow.js b/src/game-lib-d3-follow.js index 249f52c..2a6edaa 100644 --- a/src/game-lib-d3-follow.js +++ b/src/game-lib-d3-follow.js @@ -29,32 +29,32 @@ GameLib.D3.Follow = function ( this.targetPositionOffset = new GameLib.Vector3( this.graphics, - this, - this.targetPositionOffset + this.targetPositionOffset, + this ); this.target = new GameLib.Vector3( this.graphics, - this, - this.target + this.target, + this ); this.targetToParent = new GameLib.Vector3( this.graphics, - this, - this.targetToParent + this.targetToParent, + this ); this.rotatedTargetOffset = new GameLib.Vector3( this.graphics, - this, - this.rotatedTargetOffset + this.rotatedTargetOffset, + this ); this.rotated = new GameLib.Quaternion( this.graphics, - this, - this.rotated + this.rotated, + this ); }; diff --git a/src/game-lib-d3-input-editor.js b/src/game-lib-d3-input-editor.js index 96af077..bb16dfa 100644 --- a/src/game-lib-d3-input-editor.js +++ b/src/game-lib-d3-input-editor.js @@ -30,6 +30,7 @@ GameLib.D3.Input.Editor = function ( apiInputEditor.heightOffset, apiInputEditor.containerWidthOffset, apiInputEditor.containerHeightOffset, + apiInputEditor.selectDelayMs, apiInputEditor.parentEntity ); @@ -58,6 +59,10 @@ GameLib.D3.Input.Editor = function ( this.keyPress = this.onKeyPress.bind(this); this.contextMenu = this.onContextMenu.bind(this); + this.raycaster = new GameLib.D3.Raycaster( + this.graphics + ); + this.instance = this.createInstance(); }; @@ -145,6 +150,7 @@ GameLib.D3.Input.Editor.prototype.toApiComponent = function() { this.heightOffset, this.containerWidthOffset, this.containerHeightOffset, + this.selectDelayMs, GameLib.Utils.IdOrNull(this.parentEntity) ); @@ -206,8 +212,9 @@ GameLib.D3.Input.Editor.prototype.onKeyPress = function(event) { this.editor.allSelected = !this.editor.allSelected; - if (this.editor.allSelected) { + this.editor.selectedObjects = []; + if (this.editor.allSelected) { this.editor.games.map( function(game) { @@ -224,13 +231,10 @@ GameLib.D3.Input.Editor.prototype.onKeyPress = function(event) { }.bind(this) ); - - } else { - this.editor.selectedObjects = []; } - if (this.editor.onSelectionChange) { - this.editor.onSelectionChange(this.editor); + if (this.editor.onSelectionChanged) { + this.editor.onSelectionChanged(this.editor); } } @@ -300,20 +304,27 @@ GameLib.D3.Input.Editor.prototype.onMouseDown = function(event) { event.stopPropagation(); } - var meshInstances = []; + var meshCollections = this.editor.game.scenes.map( + function(scene) { + return scene.meshes; + } + ); - for (var m = 0; m < this.scene.meshes.length; m++) { - meshInstances.push(this.scene.meshes[m].instance); - } + var meshes = meshCollections.reduce( + function(result, meshCollection) { + return result.concat(meshCollection); + }, + [] + ); - var intersects = this.scene.raycaster.instance.intersectObjects(meshInstances); + var intersects = this.raycaster.getIntersectedObjects(meshes); if (intersects.length > 0) { var index = -1; - for (var s = 0; s < this.selectedObjects.length; s++) { - if (this.selectedObjects[s].object.instance == intersects[0].object) { + for (var s = 0; s < this.editor.selectedObjects.length; s++) { + if (this.editor.selectedObjects[s].object == intersects[0]) { index = s; break; } @@ -323,26 +334,21 @@ GameLib.D3.Input.Editor.prototype.onMouseDown = function(event) { /** * The object is not selected, select it */ - this.selectObject(intersects[0].object.gameLibObject); - - this.buildSelectedObjects(); - - this.buildGUI(); + this.selectObject(intersects[0]); } else { /** * De-select the objec */ - var delta = Date.now() - this.selectedObjects[index].lastUpdate; - if (delta > 300) { - - this.unselectObject(intersects[0].object.gameLibObject); - - this.buildSelectedObjects(); - - this.buildGUI(); + var delta = Date.now() - this.editor.selectedObjects[index].lastUpdate; + if (delta > this.selectDelayMs) { + this.unselectObject(intersects[0]); } } + + if (this.editor.onSelectionChanged) { + this.editor.onSelectionChanged(this.editor); + } } return false; @@ -363,9 +369,10 @@ GameLib.D3.Input.Editor.prototype.onMouseDown = function(event) { * @param event */ GameLib.D3.Input.Editor.prototype.onMouseMove = function(event) { - var clientX = event.clientX - 400; - this.scene.mouse.x = ((clientX/ (window.innerWidth - 400))) * 2 - 1; + var clientX = event.clientX - this.widthOffset; + + this.scene.mouse.x = ((clientX / (window.innerWidth - this.widthOffset))) * 2 - 1; this.scene.mouse.y = -(event.clientY / window.innerHeight) * 2 + 1; this.scene.raycaster.instance.setFromCamera( diff --git a/src/game-lib-d3-light.js b/src/game-lib-d3-light.js index 7e3956b..c9bb59d 100644 --- a/src/game-lib-d3-light.js +++ b/src/game-lib-d3-light.js @@ -11,6 +11,10 @@ GameLib.D3.Light = function Light( this.graphics = graphics; this.graphics.isNotThreeThrow(); + if (GameLib.Utils.UndefinedOrNull(apiLight)) { + apiLight = {}; + } + GameLib.D3.API.Light.call( this, apiLight.id, @@ -33,33 +37,32 @@ GameLib.D3.Light = function Light( this.color = new GameLib.Color( graphics, - this, this.color, - 0.01 + this ); this.position = new GameLib.Vector3( graphics, - this, - this.position + this.position, + this ); this.targetPosition = new GameLib.Vector3( graphics, - this, - this.targetPosition + this.targetPosition, + this ); this.scale = new GameLib.Vector3( graphics, - this, - this.scale + this.scale, + this ); this.quaternion = new GameLib.Quaternion( graphics, - this, - this.quaternion + this.quaternion, + this ); this.instance = this.createInstance(); @@ -92,21 +95,21 @@ GameLib.D3.Light.prototype.createInstance = function(update) { if (!instance) { if (this.lightType == GameLib.D3.Light.LIGHT_TYPE_AMBIENT) { - instance = new this.graphics.instance.AmbientLight( + instance = new THREE.AmbientLight( this.color.instance, this.intensity ); } if (this.lightType == GameLib.D3.Light.LIGHT_TYPE_DIRECTIONAL) { - instance = new this.graphics.instance.DirectionalLight( + instance = new THREE.DirectionalLight( this.color.instance, this.intensity ); } if (this.lightType == GameLib.D3.Light.LIGHT_TYPE_POINT) { - instance = new this.graphics.instance.PointLight( + instance = new THREE.PointLight( this.color.instance, this.intensity ); @@ -115,7 +118,7 @@ GameLib.D3.Light.prototype.createInstance = function(update) { } if (this.lightType == GameLib.D3.Light.LIGHT_TYPE_SPOT ) { - instance = new this.graphics.instance.SpotLight( + instance = new THREE.SpotLight( this.color.instance, this.intensity ); diff --git a/src/game-lib-d3-look-at.js b/src/game-lib-d3-look-at.js index 4521d0b..b196faa 100644 --- a/src/game-lib-d3-look-at.js +++ b/src/game-lib-d3-look-at.js @@ -11,6 +11,10 @@ GameLib.D3.LookAt = function ( this.graphics = graphics; this.graphics.isNotThreeThrow(); + if (GameLib.Utils.UndefinedOrNull(apiLookAt)) { + apiLookAt = {}; + } + GameLib.D3.API.LookAt.call( this, apiLookAt.id, @@ -24,32 +28,32 @@ GameLib.D3.LookAt = function ( this.targetPositionOffset = new GameLib.Vector3( this.graphics, - this, - this.targetPositionOffset + this.targetPositionOffset, + this ); this.lookAtMatrix = new GameLib.Matrix4( this.graphics, - this, - this.lookAtMatrix + this.lookAtMatrix, + this ); this.up = new GameLib.Vector3( this.graphics, - this, - this.up + this.up, + this ); this.currentRotation = new GameLib.Quaternion( this.graphics, - this, - this.currentRotation + this.currentRotation, + this ); this.targetPosition = new GameLib.Vector3( this.graphics, - this, - this.targetPosition + this.targetPosition, + this ); }; diff --git a/src/game-lib-d3-material.js b/src/game-lib-d3-material.js index 01ad7ad..b447528 100644 --- a/src/game-lib-d3-material.js +++ b/src/game-lib-d3-material.js @@ -15,7 +15,16 @@ GameLib.D3.Material = function Material( this.graphics = graphics; this.graphics.isNotThreeThrow(); - + + if (GameLib.Utils.UndefinedOrNull(apiMaterial)) { + apiMaterial = {}; + } + + if (GameLib.Utils.UndefinedOrNull(imageFactory)) { + console.warn('Cannot create a material without specifying an ImageFactory'); + throw new Error('Cannot create a material without specifying an ImageFactory'); + } + GameLib.D3.API.Material.call( this, apiMaterial.id, diff --git a/src/game-lib-d3-mesh.js b/src/game-lib-d3-mesh.js index fcf4bb5..b2da058 100644 --- a/src/game-lib-d3-mesh.js +++ b/src/game-lib-d3-mesh.js @@ -15,6 +15,20 @@ GameLib.D3.Mesh = function ( this.graphics = graphics; this.graphics.isNotThreeThrow(); + if (GameLib.Utils.UndefinedOrNull(apiMesh)) { + apiMesh = {}; + } + + if (GameLib.Utils.UndefinedOrNull(computeNormals)) { + computeNormals = true; + } + this.computeNormals = computeNormals; + + if (GameLib.Utils.UndefinedOrNull(imageFactory)) { + console.warn('Cannot create a mesh without specifying an ImageFactory'); + throw new Error('Cannot create a mesh without specifying an ImageFactory'); + } + GameLib.D3.API.Mesh.call( this, apiMesh.id, @@ -43,11 +57,18 @@ GameLib.D3.Mesh = function ( this.materials = this.materials.map( function (apiMaterial) { - return new GameLib.D3.Material( - this.graphics, - apiMaterial, - imageFactory - ) + + if (apiMaterial instanceof GameLib.D3.API.Material) { + return new GameLib.D3.Material( + this.graphics, + apiMaterial, + imageFactory + ) + } else { + console.warn('API material not of instance API.Material'); + throw new Error('API material not of instance API.Material'); + } + }.bind(this) ); @@ -70,57 +91,52 @@ GameLib.D3.Mesh = function ( this.position = new GameLib.Vector3( this.graphics, - this, this.position, - 0.001 + this ); this.scale = new GameLib.Vector3( this.graphics, - this, this.scale, - 0.001 + this ); this.up = new GameLib.Vector3( this.graphics, - this, this.up, - 0.001 + this ); this.quaternion = new GameLib.Quaternion( this.graphics, - this, - this.quaternion + this.quaternion, + this ); this.localPosition = new GameLib.Vector3( this.graphics, - this, - this.localPosition + this.localPosition, + this ); this.localRotation = new GameLib.Vector3( this.graphics, - this, - this.localRotation + this.localRotation, + this ); this.localScale = new GameLib.Vector3( this.graphics, - this, - this.localScale + this.localScale, + this ); this.modelMatrix = new GameLib.Matrix4( this.graphics, - this, - this.modelMatrix + this.modelMatrix, + this ); - this.computeNormals = computeNormals; - this.instance = this.createInstance(false); this.instance.geometry.computeBoundingBox(); diff --git a/src/game-lib-d3-pass.js b/src/game-lib-d3-pass.js index b77a999..dfc9d27 100644 --- a/src/game-lib-d3-pass.js +++ b/src/game-lib-d3-pass.js @@ -12,6 +12,10 @@ GameLib.D3.Pass = function ( this.graphics = graphics; this.graphics.isNotThreeThrow(); + if (GameLib.Utils.UndefinedOrNull(apiPass)) { + apiPass = {}; + } + GameLib.D3.API.Pass.call( this, apiPass.id, diff --git a/src/game-lib-d3-path-following.js b/src/game-lib-d3-path-following.js index 9e47952..56a2f60 100644 --- a/src/game-lib-d3-path-following.js +++ b/src/game-lib-d3-path-following.js @@ -12,6 +12,10 @@ GameLib.D3.PathFollowing = function ( this.graphics = graphics; this.graphics.isNotThreeThrow(); + if (GameLib.Utils.UndefinedOrNull(apiPathFollowing)) { + apiPathFollowing = {}; + } + GameLib.D3.API.PathFollowing.call( this, apiPathFollowing.id, @@ -40,26 +44,26 @@ GameLib.D3.PathFollowing = function ( this.baseOffset = new GameLib.Vector3( this.graphics, - this, - this.baseOffset + this.baseOffset, + this ); this.maxOffset = new GameLib.Vector3( this.graphics, - this, - this.maxOffset + this.maxOffset, + this ); this.targetOffset = new GameLib.Vector3( this.graphics, - this, - this.targetOffset + this.targetOffset, + this ); this.currentOffset = new GameLib.Vector3( this.graphics, - this, - this.currentOffset + this.currentOffset, + this ); this.raycaster = new GameLib.D3.Raycaster( @@ -69,42 +73,38 @@ GameLib.D3.PathFollowing = function ( this.currentPosition = new GameLib.Vector3( this.graphics, - this, - this.currentPosition + this.currentPosition, + this ); this.futurePosition = new GameLib.Vector3( this.graphics, - this, - this.futurePosition + this.futurePosition, + this ); this.up = new GameLib.Vector3( this.graphics, - this, - this.up + this.up, + this ); this.rotationMatrix = new GameLib.Matrix4( this.graphics, - this, - this.rotationMatrix + this.rotationMatrix, + this ); this.rotationVector = new GameLib.Quaternion( this.graphics, - this, - this.rotationVector + this.rotationVector, + this ); this.mx = new GameLib.Utils.MovingAverage(10); this.my = new GameLib.Utils.MovingAverage(10); this.mz = new GameLib.Utils.MovingAverage(10); - this.posx = new GameLib.Utils.MovingAverage(10); - this.posy = new GameLib.Utils.MovingAverage(10); - this.posz = new GameLib.Utils.MovingAverage(10); - }; GameLib.D3.PathFollowing.prototype = Object.create(GameLib.D3.API.PathFollowing.prototype); diff --git a/src/game-lib-d3-raycaster.js b/src/game-lib-d3-raycaster.js index a511888..4fe3ebb 100644 --- a/src/game-lib-d3-raycaster.js +++ b/src/game-lib-d3-raycaster.js @@ -1,7 +1,6 @@ /** * Raycaster for GameLib.D3 * @param graphics GameLib.D3.Graphics - * @param parentObject * @param apiRaycaster * @constructor */ @@ -13,6 +12,10 @@ GameLib.D3.Raycaster = function( this.graphics = graphics; this.graphics.isNotThreeThrow(); + if (GameLib.Utils.UndefinedOrNull(apiRaycaster)) { + apiRaycaster = {}; + } + GameLib.D3.API.Raycaster.call( this, apiRaycaster.id, @@ -23,14 +26,14 @@ GameLib.D3.Raycaster = function( this.position = new GameLib.Vector3( this.graphics, - this, - this.position + this.position, + this ); this.direction = new GameLib.Vector3( this.graphics, - this, - this.direction + this.direction, + this ); this.instance = this.createInstance(); @@ -61,6 +64,10 @@ GameLib.D3.Raycaster.prototype.createInstance = function(update) { return instance; }; +GameLib.D3.Raycaster.prototype.updateInstance = function() { + this.instance = this.createInstance(true); +}; + GameLib.D3.Raycaster.prototype.toApiRaycaster = function() { return new GameLib.D3.API.Raycaster( this.id, @@ -76,7 +83,6 @@ GameLib.D3.Raycaster.FromObjectRaycaster = function(graphics, parentObject, obje var raycaster = new GameLib.D3.Raycaster( graphics, - parentObject, apiRaycaster ); @@ -102,8 +108,6 @@ GameLib.D3.Raycaster.prototype.set = function( this.position.updateInstance(); this.direction.updateInstance(); - - this.createInstance(true); }; /** @@ -118,8 +122,6 @@ GameLib.D3.Raycaster.prototype.setDirection = function( this.direction.z = direction.z; this.direction.updateInstance(); - - this.createInstance(true); }; /** @@ -134,10 +136,36 @@ GameLib.D3.Raycaster.prototype.setPosition = function( this.position.z = position.z; this.position.updateInstance(); - - this.createInstance(true); }; +/** + * Gets all interesected GameLib.D3.Mesh objects + * @param meshes [GameLib.D3.Mesh] + */ +GameLib.D3.Raycaster.prototype.getIntersectedObjects = function(meshes) { + + var meshInstances = meshes.map( + function (mesh) { + return mesh.instance; + } + ); + + var instanceIdToMesh = meshes.reduce( + function (result, mesh) { + result[mesh.instance.id] = mesh; + }, + {} + ); + + var intersects = this.instance.intersectObjects(meshInstances); + + return intersects.map(function (intersect) { + return instanceIdToMesh[intersect.object.id]; + }); + +}; + + /** * Returns the face normal (if any) of an intersection between current ray position, direction and a provided mesh * @param mesh GameLib.D3.Mesh @@ -152,14 +180,15 @@ GameLib.D3.Raycaster.prototype.getFaceNormal = function(mesh) { ); if (intersect && intersect.length > 0) { + normal = new GameLib.Vector3( this.graphics, - this, new GameLib.API.Vector3( intersect[0].face.normal.x, intersect[0].face.normal.y, intersect[0].face.normal.z - ) + ), + this ); } @@ -182,12 +211,12 @@ GameLib.D3.Raycaster.prototype.getIntersectPoint = function(mesh) { if (intersect && intersect.length > 0) { point = new GameLib.Vector3( this.graphics, - this, new GameLib.API.Vector3( intersect[0].point.x, intersect[0].point.y, intersect[0].point.z - ) + ), + this ); } diff --git a/src/game-lib-d3-render-target.js b/src/game-lib-d3-render-target.js index 43562f2..d5cbfcf 100644 --- a/src/game-lib-d3-render-target.js +++ b/src/game-lib-d3-render-target.js @@ -12,6 +12,10 @@ GameLib.D3.RenderTarget = function ( this.graphics = graphics; this.graphics.isNotThreeThrow(); + if (GameLib.Utils.UndefinedOrNull(apiRenderTarget)) { + apiRenderTarget = {}; + } + GameLib.D3.API.RenderTarget.call( this, apiRenderTarget.id, diff --git a/src/game-lib-d3-renderer.js b/src/game-lib-d3-renderer.js index bef0954..1d32d69 100644 --- a/src/game-lib-d3-renderer.js +++ b/src/game-lib-d3-renderer.js @@ -12,6 +12,10 @@ GameLib.D3.Renderer = function ( this.graphics = graphics; this.graphics.isNotThreeThrow(); + if (GameLib.Utils.UndefinedOrNull(apiRenderer)) { + apiRenderer = {}; + } + GameLib.D3.API.Renderer.call( this, apiRenderer.id, diff --git a/src/game-lib-d3-scene.js b/src/game-lib-d3-scene.js index b77f11a..24b70f4 100644 --- a/src/game-lib-d3-scene.js +++ b/src/game-lib-d3-scene.js @@ -16,6 +16,23 @@ GameLib.D3.Scene = function Scene( this.graphics = graphics; this.graphics.isNotThreeThrow(); + if (GameLib.Utils.UndefinedOrNull(apiScene)) { + apiScene = {}; + } + + if (GameLib.Utils.UndefinedOrNull(imageFactory)) { + imageFactory = GameLib.D3.ImageFactory( + this.graphics, + this.baseUrl + ); + } + this.imageFactory = imageFactory; + + if (GameLib.Utils.UndefinedOrNull(computeNormals)) { + computeNormals = true; + } + this.computeNormals = computeNormals; + GameLib.D3.API.Scene.call( this, apiScene.id, @@ -33,14 +50,6 @@ GameLib.D3.Scene = function Scene( apiScene.parentEntity ); - if (GameLib.Utils.UndefinedOrNull(imageFactory)) { - imageFactory = GameLib.D3.ImageFactory( - this.graphics, - this.baseUrl - ); - } - this.imageFactory = imageFactory; - this.idToObject = {}; this.meshes = this.meshes.map( @@ -49,7 +58,7 @@ GameLib.D3.Scene = function Scene( var mesh = new GameLib.D3.Mesh( this.graphics, apiMesh, - computeNormals, + this.computeNormals, imageFactory ); @@ -72,62 +81,78 @@ GameLib.D3.Scene = function Scene( ); this.position = new GameLib.Vector3( - graphics, - this, - this.position + this.graphics, + this.position, + this ); this.quaternion = new GameLib.Quaternion( - graphics, - this, - this.quaternion + this.graphics, + this.quaternion, + this ); this.scale = new GameLib.Vector3( - graphics, - this, - this.scale + this.graphics, + this.scale, + this ); this.lights = this.lights.map( function(apiLight) { - var light = new GameLib.D3.Light( - this.graphics, - apiLight - ); - this.idToObject[light.id] = light; + if (apiLight instanceof GameLib.D3.API.Light) { + var light = new GameLib.D3.Light( + this.graphics, + apiLight + ); + + this.idToObject[light.id] = light; + + return light; + } else { + + } - return light; }.bind(this) ); this.materials = this.materials.map( function(apiMaterial) { - var material = new GameLib.D3.Material( - this.graphics, - apiMaterial, - this.imageFactory - ); + if (apiMaterial instanceof GameLib.D3.API.Material) { + var material = new GameLib.D3.Material( + this.graphics, + apiMaterial, + this.imageFactory + ); - this.idToObject[material.id] = material; + this.idToObject[material.id] = material; - return material; + return material; + } else { + console.warn('apiMaterial not an instance of API.Material'); + throw new Error('apiMaterial not an instance of API.Material'); + } }.bind(this) ); this.textures = this.textures.map( function(apiTexture) { - var texture = new GameLib.D3.Texture( - this.graphics, - apiTexture, - this.imageFactory - ); + if (apiTexture instanceof GameLib.D3.API.Texture) { + var texture = new GameLib.D3.Texture( + this.graphics, + apiTexture, + this.imageFactory + ); - this.idToObject[texture.id] = texture; + this.idToObject[texture.id] = texture; - return texture; + return texture; + } else { + console.warn('apiTexture not an instance of API.Texture'); + throw new Error('apiTexture not an instance of API.Texture'); + } }.bind(this) ); diff --git a/src/game-lib-d3-skeleton.js b/src/game-lib-d3-skeleton.js index 86edc73..5145072 100644 --- a/src/game-lib-d3-skeleton.js +++ b/src/game-lib-d3-skeleton.js @@ -10,7 +10,11 @@ GameLib.D3.Skeleton = function Skeleton( ) { this.graphics = graphics; this.graphics.isNotThreeThrow(); - + + if (GameLib.Utils.UndefinedOrNull(apiSkeleton)) { + apiSkeleton = {}; + } + GameLib.D3.API.Skeleton.call( this, apiSkeleton.id, @@ -26,31 +30,51 @@ GameLib.D3.Skeleton = function Skeleton( this.bones = this.bones.map( function(apiBone) { - return new GameLib.D3.Bone( - this.graphics, - apiBone - ) + + if (apiBone instanceof GameLib.D3.API.Bone) { + return new GameLib.D3.Bone( + this.graphics, + apiBone + ) + } else { + console.warn('apiBone not an instance of API.Bone'); + throw new Error('apiBone not an instance of API.Bone'); + } + }.bind(this) ); this.boneInverses = this.boneInverses.map( function(boneInverse) { - return new GameLib.Matrix4( - this.graphics, - this, - boneInverse - ); + + if (boneInverse instanceof GameLib.API.Matrix4) { + return new GameLib.Matrix4( + this.graphics, + boneInverse, + this + ); + } else { + console.warn('boneInverse not an instance of API.Matrix4'); + throw new Error('boneInverse not an instance of API.Matrix4'); + } + }.bind(this) ); this.boneMatrices = this.boneMatrices.map( function(boneMatrices) { - return new GameLib.Matrix4( - this.graphics, - this, - boneMatrices - ); + if (boneMatrices instanceof GameLib.API.Matrix4) { + return new GameLib.Matrix4( + this.graphics, + boneMatrices, + this + ); + } else { + console.warn('boneMatrices not an instance of API.Matrix4'); + throw new Error('boneMatrices not an instance of API.Matrix4'); + } + }.bind(this) ); diff --git a/src/game-lib-d3-spline.js b/src/game-lib-d3-spline.js index c7a420c..ea6c272 100644 --- a/src/game-lib-d3-spline.js +++ b/src/game-lib-d3-spline.js @@ -11,6 +11,10 @@ GameLib.D3.Spline = function ( this.graphics = graphics; this.graphics.isNotThreeThrow(); + if (GameLib.Utils.UndefinedOrNull(apiSpline)) { + apiSpline = {}; + } + GameLib.D3.API.Spline.call( this, apiSpline.id, @@ -23,8 +27,8 @@ GameLib.D3.Spline = function ( function (vertex) { return new GameLib.Vector3( graphics, - this, - vertex + vertex, + this ) } ); @@ -108,8 +112,8 @@ GameLib.D3.Spline.prototype.getPointAt = function(proper) { var point = this.instance.getPointAt(proper); return new GameLib.Vector3( this.graphics, - this, new GameLib.API.Vector3(point.x, point.y, point.z), + this, 0.1 ); }; diff --git a/src/game-lib-d3-texture.js b/src/game-lib-d3-texture.js index 05ba6f2..10dee71 100644 --- a/src/game-lib-d3-texture.js +++ b/src/game-lib-d3-texture.js @@ -15,7 +15,16 @@ GameLib.D3.Texture = function Texture( ) { this.graphics = graphics; this.graphics.isNotThreeThrow(); - + + if (GameLib.Utils.UndefinedOrNull(apiTexture)) { + apiTexture = {}; + } + + if (GameLib.Utils.UndefinedOrNull(imageFactory)) { + console.warn('Cannot create a material without specifying an ImageFactory'); + throw new Error('Cannot create a material without specifying an ImageFactory'); + } + GameLib.D3.API.Texture.call( this, apiTexture.id, @@ -43,14 +52,14 @@ GameLib.D3.Texture = function Texture( this.offset = new GameLib.Vector2( graphics, - this, - this.offset + this.offset, + this ); this.repeat = new GameLib.Vector2( graphics, - this, - this.repeat + this.repeat, + this ); // this.parentMaterial = parentMaterial; diff --git a/src/game-lib-d3-vertex.js b/src/game-lib-d3-vertex.js index 821a2a8..27a62f8 100644 --- a/src/game-lib-d3-vertex.js +++ b/src/game-lib-d3-vertex.js @@ -11,6 +11,10 @@ GameLib.D3.Vertex = function Vertex( this.graphics = graphics; this.graphics.isNotThreeThrow(); + if (GameLib.Utils.UndefinedOrNull(apiVertex)) { + apiVertex = {}; + } + GameLib.D3.API.Vertex.call( this, apiVertex.position, @@ -19,8 +23,8 @@ GameLib.D3.Vertex = function Vertex( this.position = new GameLib.Vector3( this.graphics, - null, - this.position + this.position, + null ); this.boneWeights = this.boneWeights.map( diff --git a/src/game-lib-d3-viewport.js b/src/game-lib-d3-viewport.js index 0ce61c4..e855c75 100644 --- a/src/game-lib-d3-viewport.js +++ b/src/game-lib-d3-viewport.js @@ -11,7 +11,11 @@ GameLib.D3.Viewport = function ( this.graphics = graphics; this.graphics.isNotThreeThrow(); - + + if (GameLib.Utils.UndefinedOrNull(apiViewport)) { + apiViewport = {}; + } + GameLib.D3.API.Viewport.call( this, apiViewport.id, diff --git a/src/game-lib-entity-manager.js b/src/game-lib-entity-manager.js index 3140a45..52deea0 100644 --- a/src/game-lib-entity-manager.js +++ b/src/game-lib-entity-manager.js @@ -12,6 +12,10 @@ GameLib.EntityManager = function( this.graphics = graphics; this.graphics.isNotThreeThrow(); + if (GameLib.Utils.UndefinedOrNull(apiEntityManager)) { + apiEntityManager = {}; + } + GameLib.API.EntityManager.call( this, apiEntityManager.entities diff --git a/src/game-lib-entity.js b/src/game-lib-entity.js index 92799f5..856f2d6 100644 --- a/src/game-lib-entity.js +++ b/src/game-lib-entity.js @@ -12,6 +12,10 @@ GameLib.Entity = function ( this.graphics = graphics; this.graphics.isNotThreeThrow(); + if (GameLib.Utils.UndefinedOrNull(apiEntity)) { + apiEntity = {}; + } + GameLib.API.Entity.call( this, apiEntity.id, diff --git a/src/game-lib-matrix-4.js b/src/game-lib-matrix-4.js index a4924ab..46fb2be 100644 --- a/src/game-lib-matrix-4.js +++ b/src/game-lib-matrix-4.js @@ -8,11 +8,18 @@ */ GameLib.Matrix4 = function( graphics, - parentObject, apiMatrix4, + parentObject, grain ) { + this.graphics = graphics; + this.graphics.isNotThreeThrow(); + + if (GameLib.Utils.UndefinedOrNull(apiMatrix4)) { + apiMatrix4 = {}; + } + GameLib.API.Matrix4.call( this, apiMatrix4.rows[0], @@ -21,10 +28,9 @@ GameLib.Matrix4 = function( apiMatrix4.rows[3] ); - this.graphics = graphics; - - this.graphics.isNotThreeThrow(); - + if (GameLib.Utils.UndefinedOrNull(parentObject)) { + parentObject = null; + } this.parentObject = parentObject; if (GameLib.Utils.UndefinedOrNull(grain)) { @@ -32,53 +38,41 @@ GameLib.Matrix4 = function( } this.grain = grain; - this.rows[0] = new GameLib.Vector4( - this.graphics, - this, - this.rows[0], - grain - ); - - this.rows[1] = new GameLib.Vector4( - this.graphics, - this, - this.rows[1], - grain - ); - - this.rows[2] = new GameLib.Vector4( - this.graphics, - this, - this.rows[2], - grain - ); - - this.rows[3] = new GameLib.Vector4( - this.graphics, - this, - this.rows[3], - grain + this.rows = this.rows.map( + function(row) { + if (row instanceof GameLib.API.Vector4) { + return new GameLib.Vector4( + this.graphics, + row, + this, + this.grain + ); + } else { + console.warn('Attempted conversion of wrong instance'); + throw new Error('Attempted conversion of wrong instance'); + } + }.bind(this) ); this.forward = new GameLib.Vector4( this.graphics, - this, this.forward, - grain + this, + this.grain ); this.left = new GameLib.Vector4( this.graphics, - this, this.left, - grain + this, + this.grain ); this.up = new GameLib.Vector4( this.graphics, - this, this.up, - grain + this, + this.grain ); this.instance = this.createInstance(); @@ -136,10 +130,9 @@ GameLib.Matrix4.prototype.updateInstance = function() { this.createInstance(true); - if (this.parentObject) { - if (this.parentObject.updateInstance) { - this.parentObject.updateInstance(); - } + if (this.parentObject && + this.parentObject.updateInstance) { + this.parentObject.updateInstance(); } }; @@ -148,12 +141,18 @@ GameLib.Matrix4.prototype.updateInstance = function() { * @returns {*} */ GameLib.Matrix4.prototype.toApiMatrix = function () { - return new GameLib.API.Matrix4( - this.rows[0].toApiVector(), - this.rows[1].toApiVector(), - this.rows[2].toApiVector(), - this.rows[3].toApiVector() - ) + + return this.rows.map( + function(row) { + if (row instanceof GameLib.Vector4) { + return row.toApiVector(); + } else { + console.warn('Incompatible conversion to API matrix for vector: ', row); + throw new Error('Incompatible conversion to API matrix for a vector'); + } + } + ); + }; /** @@ -166,40 +165,7 @@ GameLib.Matrix4.prototype.toApiMatrix = function () { */ GameLib.Matrix4.FromObjectMatrix = function(graphics, objectMatrix, parentObject) { - if (GameLib.Utils.UndefinedOrNull(objectMatrix)) { - return new GameLib.Matrix4( - graphics, - parentObject, - new GameLib.API.Matrix4() - ) - } - - var apiMatrix = new GameLib.API.Matrix4( - new GameLib.API.Vector4( - objectMatrix.rows[0].x, - objectMatrix.rows[0].y, - objectMatrix.rows[0].z, - objectMatrix.rows[0].w - ), - new GameLib.API.Vector4( - objectMatrix.rows[1].x, - objectMatrix.rows[1].y, - objectMatrix.rows[1].z, - objectMatrix.rows[1].w - ), - new GameLib.API.Vector4( - objectMatrix.rows[2].x, - objectMatrix.rows[2].y, - objectMatrix.rows[2].z, - objectMatrix.rows[2].w - ), - new GameLib.API.Vector4( - objectMatrix.rows[3].x, - objectMatrix.rows[3].y, - objectMatrix.rows[3].z, - objectMatrix.rows[3].w - ) - ); + var apiMatrix = new GameLib.API.Matrix4.FromObjectMatrix(objectMatrix); return new GameLib.Matrix4( graphics, @@ -208,6 +174,13 @@ GameLib.Matrix4.FromObjectMatrix = function(graphics, objectMatrix, parentObject ) }; +/** + * Lookat + * @param position + * @param target + * @param up + * @returns {GameLib.Matrix4} + */ GameLib.Matrix4.prototype.lookAt = function (position, target, up) { var pv = new GameLib.API.Vector3(position.x, position.y, position.z); @@ -256,35 +229,42 @@ GameLib.Matrix4.prototype.lookAt = function (position, target, up) { return this; }; +/** + * Identity + */ GameLib.Matrix4.prototype.identity = function () { this.rows = [ new GameLib.Vector4( this.graphics, - this, new GameLib.API.Vector4(1,0,0,0), + this, this.grain ), new GameLib.Vector4( this.graphics, - this, new GameLib.API.Vector4(0,1,0,0), + this, this.grain ), new GameLib.Vector4( this.graphics, - this, new GameLib.API.Vector4(0,0,1,0), + this, this.grain ), new GameLib.Vector4( this.graphics, - this, new GameLib.API.Vector4(0,0,0,1), + this, this.grain ) ]; }; +/** + * Transpose + * @returns {GameLib.Matrix4} + */ GameLib.Matrix4.prototype.transpose = function () { this.temp[0].x = this.rows[0].x; diff --git a/src/game-lib-mouse.js b/src/game-lib-mouse.js index c0f4d28..98d6752 100644 --- a/src/game-lib-mouse.js +++ b/src/game-lib-mouse.js @@ -1,15 +1,16 @@ /** * Runtime mouse for updating instance objects * @param graphics GameLib.D3.Graphics - * @param parentObject GameLib.D3.* * @param apiMouse GameLib.API.Mouse * @constructor */ -GameLib.Mouse = function (graphics, parentObject, apiMouse) { +GameLib.Mouse = function (graphics, apiMouse) { this.graphics = graphics; this.graphics.isNotThreeThrow(); + + GameLib.API.Mouse.call( this, apiMouse.id, diff --git a/src/game-lib-quaternion.js b/src/game-lib-quaternion.js index 4708b03..823307d 100644 --- a/src/game-lib-quaternion.js +++ b/src/game-lib-quaternion.js @@ -8,39 +8,39 @@ */ GameLib.Quaternion = function ( graphics, - parentObject, apiQuaternion, + parentObject, grain ) { this.graphics = graphics; this.graphics.isNotThreeThrow(); + if (GameLib.Utils.UndefinedOrNull(apiQuaternion)) { + apiQuaternion = {}; + } + GameLib.API.Quaternion.call( this, apiQuaternion.x, apiQuaternion.y, apiQuaternion.z, apiQuaternion.w, - new GameLib.API.Vector3( - apiQuaternion.axis.x, - apiQuaternion.axis.y, - apiQuaternion.axis.z - ), + apiQuaternion.axis, apiQuaternion.angle ); - this.axis = new GameLib.Vector3( - this.graphics, - this, - this.axis, - 0.001 - ); - if (GameLib.Utils.UndefinedOrNull(parentObject)) { parentObject = null; } this.parentObject = parentObject; + this.axis = new GameLib.Vector3( + this.graphics, + this.axis, + this, + this.grain + ); + if (GameLib.Utils.UndefinedOrNull(grain)) { grain = 0.001; } @@ -68,7 +68,12 @@ GameLib.Quaternion.prototype.createInstance = function(update) { instance.z = this.z; instance.w = this.w; } else { - instance = new THREE.Quaternion(this.x, this.y, this.z, this.w); + instance = new THREE.Quaternion( + this.x, + this.y, + this.z, + this.w + ); } return instance; @@ -81,7 +86,8 @@ GameLib.Quaternion.prototype.updateInstance = function() { this.createInstance(true); - if (this.parentObject.updateInstance) { + if (this.parentObject && + this.parentObject.updateInstance) { this.parentObject.updateInstance(); } }; diff --git a/src/game-lib-vector2.js b/src/game-lib-vector2.js index 87ed63b..8db932b 100644 --- a/src/game-lib-vector2.js +++ b/src/game-lib-vector2.js @@ -6,20 +6,28 @@ * @param grain Number * @constructor */ -GameLib.Vector2 = function (graphics, parentObject, apiVector2, grain) { - - for (var property in apiVector2) { - if (apiVector2.hasOwnProperty(property)) { - this[property] = apiVector2[property]; - } - } - - GameLib.Utils.Extend(GameLib.Vector2, GameLib.API.Vector2); - +GameLib.Vector2 = function ( + graphics, + apiVector2, + parentObject, + grain +) { this.graphics = graphics; - this.graphics.isNotThreeThrow(); + if (GameLib.Utils.UndefinedOrNull(apiVector2)) { + apiVector2 = {}; + } + + GameLib.Vector2.call( + this, + apiVector2.x, + apiVector2.y + ); + + if (GameLib.Utils.UndefinedOrNull(parentObject)) { + parentObject = null; + } this.parentObject = parentObject; if (GameLib.Utils.UndefinedOrNull(grain)) { @@ -30,6 +38,10 @@ GameLib.Vector2 = function (graphics, parentObject, apiVector2, grain) { this.instance = this.createInstance(); }; +GameLib.Vector2.prototype = Object.create(GameLib.API.Vector2.prototype); +GameLib.Vector2.prototype.constructor = GameLib.Vector2; + + /** * Creates an instance vector2 * @param update @@ -44,7 +56,7 @@ GameLib.Vector2.prototype.createInstance = function(update) { instance.x = this.x; instance.y = this.y; } else { - instance = new this.graphics.instance.Vector2(this.x, this.y); + instance = new THREE.Vector2(this.x, this.y); } return instance; @@ -57,7 +69,8 @@ GameLib.Vector2.prototype.updateInstance = function() { this.createInstance(true); - if (this.parentObject.updateInstance) { + if (this.parentObject && + this.parentObject.updateInstance) { this.parentObject.updateInstance(); } }; @@ -73,99 +86,209 @@ GameLib.Vector2.prototype.toApiVector = function() { ); }; +/** + * Copy + * TODO: Test + * @param v optional + * @returns {GameLib.Vector2} + */ GameLib.Vector2.prototype.copy = function (v) { - if (!GameLib.Utils.UndefinedOrNull(v)) { + + if (GameLib.Utils.UndefinedOrNull(v)) { + + return new GameLib.Vector2( + this.graphics, + new GameLib.API.Vector2( + this.x, + this.y + ), + this.parentObject, + this.grain + ); + + } else { + this.x = v.x; this.y = v.y; + return this; - } else { - return new GameLib.Vector2( - this.x, - this.y - ); } }; +/** + * Equals + * TODO: Test + * @param v + * @returns {boolean} + */ GameLib.Vector2.prototype.equals = function(v) { - return (((this.x == v.x) && - (this.y == v.y)) || - ((this.y == v.x) && - (this.x == v.y))); + + if ((this.x == v.x) && + (this.y == v.y)) { + return true; + } else { + return false; + } + }; +/** + * Add + * TODO: Test + * @param v + */ GameLib.Vector2.prototype.add = function(v) { - return new GameLib.Vector2( - this.x + v.x, - this.y + v.y - ); + + if ( + v instanceof GameLib.API.Vector2 || + v instanceof GameLib.API.Vector3 || + v instanceof GameLib.API.Vector4 || + v instanceof GameLib.API.Quaternion + ) { + this.x += v.x; + this.y += v.y; + } else { + console.warn('Could not add Vector2'); + throw new Error('Could not add Vector2'); + } + }; +/** + * Subtract + * TODO: Test + * @param v + */ GameLib.Vector2.prototype.subtract = function(v) { - return new GameLib.Vector2( - this.x - v.x, - this.y - v.y - ); + + if ( + v instanceof GameLib.API.Vector2 || + v instanceof GameLib.API.Vector3 || + v instanceof GameLib.API.Vector4 || + v instanceof GameLib.API.Quaternion + ) { + this.x -= v.x; + this.y -= v.y; + } else { + console.warn('Could not subtract Vector2'); + throw new Error('Could not subtract Vector2'); + } + }; +/** + * Multiply + * TODO: Test + * @param v + */ GameLib.Vector2.prototype.multiply = function(v) { - if (v instanceof GameLib.Vector2) { - return new GameLib.Vector2( - this.x * v.x, - this.y * v.y - ); - } else if (isNumber(v)) { - return new GameLib.Vector2( - this.x * v, - this.y * v - ); + + if ( + v instanceof GameLib.API.Vector2 || + v instanceof GameLib.API.Vector3 || + v instanceof GameLib.API.Vector4 || + v instanceof GameLib.API.Quaternion + ) { + this.x *= v.x; + this.y *= v.y; + } else if (typeof v == 'number') { + this.x *= v; + this.y *= v; + } else { + console.warn('Could not multiply Vector2'); + throw new Error('Could not multiply Vector2'); } + }; +/** + * Divide + * TODO: Test + * @param v + */ GameLib.Vector2.prototype.divide = function(v) { - if (v instanceof GameLib.Vector2) { - return new GameLib.Vector2( - this.x * (1.0 / v.x), - this.y * (1.0 / v.y) - ); - } else if (isNumber(v)) { - var invS = 1.0 / v; - return new GameLib.Vector2( - this.x * invS, - this.y * invS - ); + + if ( + v instanceof GameLib.API.Vector2 || + v instanceof GameLib.API.Vector3 || + v instanceof GameLib.API.Vector4 || + v instanceof GameLib.API.Quaternion + ) { + this.x *= (1.0 / v.x); + this.y *= (1.0 / v.y); + } else if (typeof v == 'number') { + this.x *= 1.0 / v; + this.y *= 1.0 / v; + } else { + console.warn('Could not divide Vector2'); + throw new Error('Could not divide Vector2'); } + }; -GameLib.Vector2.prototype.set = function(x, y) { - this.x = x; - this.y = y; -}; - +/** + * Clamp + * TODO: Test + * @param min GameLib.API.Vector2 + * @param max GameLib.API.Vector2 + * @returns {GameLib.Vector2} + */ GameLib.Vector2.prototype.clamp = function(min, max) { - return new GameLib.Vector2( - Math.max(min.x, Math.min(max.x, this.x)), - Math.max(min.y, Math.min(max.y, this.y)) + + this.x = Math.max(min.x, Math.min(max.x, this.x)); + this.y = Math.max(min.y, Math.min(max.y, this.y)); + + return this; + +}; + +/** + * Length + * TODO: Test + * @returns {number} + */ +GameLib.Vector2.prototype.length = function() { + return Math.sqrt( + this.x * this.x + this.y * this.y ); }; -GameLib.Vector2.prototype.length = function() { - return Math.sqrt(this.x * this.x + this.y * this.y); -}; - +/** + * Dot product + * TODO: Test + * @param v + * @returns {number} + */ GameLib.Vector2.prototype.dot = function(v) { return this.x * v.x + this.y * v.y; }; +/** + * Normalize + * TODO: Test + */ GameLib.Vector2.prototype.normalize = function() { return this.multiply(1.0 / this.length()); }; +/** + * TODO: Test + * Angle between this vector and origin + * @returns {number} + */ GameLib.Vector2.prototype.angle = function() { var angle = Math.atan2(this.y, this.x); if ( angle < 0 ) angle += 2 * Math.PI; return angle; }; +/** + * Interpolate to v from here + * TODO: Test + * @param v + * @param alpha + * @returns {GameLib.Vector2} + */ GameLib.Vector2.prototype.lerp = function ( v, alpha ) { return new GameLib.Vector2( this.x + ( v.x - this.x ) * alpha, diff --git a/src/game-lib-vector3.js b/src/game-lib-vector3.js index 5f688e4..00b9085 100644 --- a/src/game-lib-vector3.js +++ b/src/game-lib-vector3.js @@ -1,25 +1,34 @@ /** * Runtime apiVector3 for updating instance objects * @param graphics GameLib.D3.Graphics - * @param parentObject GameLib.D3.* * @param apiVector3 GameLib.API.Vector3 + * @param parentObject GameLib.* * @param grain Number * @constructor */ -GameLib.Vector3 = function (graphics, parentObject, apiVector3, grain) { - - for (var property in apiVector3) { - if (apiVector3.hasOwnProperty(property)) { - this[property] = apiVector3[property]; - } - } - - GameLib.Utils.Extend(GameLib.Vector3, GameLib.API.Vector3); - +GameLib.Vector3 = function ( + graphics, + apiVector3, + parentObject, + grain +) { this.graphics = graphics; - this.graphics.isNotThreeThrow(); + if (GameLib.Utils.UndefinedOrNull(apiVector3)) { + apiVector3 = {}; + } + + GameLib.Vector3.call( + this, + apiVector3.x, + apiVector3.y, + apiVector3.z + ); + + if (GameLib.Utils.UndefinedOrNull(parentObject)) { + parentObject = null; + } this.parentObject = parentObject; if (GameLib.Utils.UndefinedOrNull(grain)) { @@ -30,6 +39,9 @@ GameLib.Vector3 = function (graphics, parentObject, apiVector3, grain) { this.instance = this.createInstance(); }; +GameLib.Vector3.prototype = Object.create(GameLib.API.Vector3.prototype); +GameLib.Vector3.prototype.constructor = GameLib.Vector3; + /** * Creates an instance vector3 * @param update @@ -45,7 +57,11 @@ GameLib.Vector3.prototype.createInstance = function(update) { instance.y = this.y; instance.z = this.z; } else { - instance = new THREE.Vector3(this.x, this.y, this.z); + instance = new THREE.Vector3( + this.x, + this.y, + this.z + ); } return instance; @@ -58,7 +74,8 @@ GameLib.Vector3.prototype.updateInstance = function() { this.createInstance(true); - if (this.parentObject.updateInstance) { + if (this.parentObject && + this.parentObject.updateInstance) { this.parentObject.updateInstance(); } }; @@ -75,17 +92,17 @@ GameLib.Vector3.prototype.toApiVector = function() { }; /** - * Converts runtime vector to API Vector + * Creates a new copy of this Vector3 */ GameLib.Vector3.prototype.copy = function() { return new GameLib.Vector3( this.graphics, - this.parentObject, new GameLib.API.Vector3( this.x, this.y, this.z ), + this.parentObject, this.grain ) }; diff --git a/src/game-lib-vector4.js b/src/game-lib-vector4.js index 4a3f4d1..654b921 100644 --- a/src/game-lib-vector4.js +++ b/src/game-lib-vector4.js @@ -1,12 +1,24 @@ /** * Runtime apiVector4 for updating instance objects * @param graphics GameLib.D3.Graphics - * @param parentObject GameLib.D3.* * @param apiVector4 GameLib.API.Vector4 + * @param parentObject GameLib.* * @param grain Number * @constructor */ -GameLib.Vector4 = function (graphics, parentObject, apiVector4, grain) { +GameLib.Vector4 = function ( + graphics, + apiVector4, + parentObject, + grain +) { + + this.graphics = graphics; + this.graphics.isNotThreeThrow(); + + if (GameLib.Utils.UndefinedOrNull(apiVector4)) { + apiVector4 = {}; + } GameLib.API.Vector4.call( this, @@ -16,10 +28,9 @@ GameLib.Vector4 = function (graphics, parentObject, apiVector4, grain) { apiVector4.w ); - this.graphics = graphics; - - this.graphics.isNotThreeThrow(); - + if (GameLib.Utils.UndefinedOrNull(parentObject)) { + parentObject = null; + } this.parentObject = parentObject; if (GameLib.Utils.UndefinedOrNull(grain)) { @@ -62,7 +73,8 @@ GameLib.Vector4.prototype.updateInstance = function() { this.createInstance(true); - if (this.parentObject.updateInstance) { + if (this.parentObject && + this.parentObject.updateInstance) { this.parentObject.updateInstance(); } };