diff --git a/src/game-lib-a-component-a.js b/src/game-lib-a-component-a.js index b7a3d6c..a5d36ab 100644 --- a/src/game-lib-a-component-a.js +++ b/src/game-lib-a-component-a.js @@ -199,6 +199,8 @@ GameLib.Component.GetComponentName = function(number) { case 0x2f : return 'GameLib.D3.Shape.Plane'; break; } + + throw new Error('Unknown component type: ' + number ); }; /** diff --git a/src/game-lib-d3-api-rigid-body.js b/src/game-lib-d3-api-rigid-body.js index 6a9306a..b78ba9c 100644 --- a/src/game-lib-d3-api-rigid-body.js +++ b/src/game-lib-d3-api-rigid-body.js @@ -16,7 +16,7 @@ * @param collisionFilterGroup * @param collisionFilterMask * @param fixedRotation - * @param shape + * @param shapes * @param kinematic * @param parentEntity * @constructor @@ -38,7 +38,7 @@ GameLib.D3.API.RigidBody = function( collisionFilterGroup, collisionFilterMask, fixedRotation, - shape, + shapes, kinematic, parentEntity ) { @@ -123,10 +123,10 @@ GameLib.D3.API.RigidBody = function( } this.fixedRotation = fixedRotation; - if (GameLib.Utils.UndefinedOrNull(shape)) { - shape = null; + if (GameLib.Utils.UndefinedOrNull(shapes)) { + shapes = []; } - this.shape = shape; + this.shapes = shapes; if (GameLib.Utils.UndefinedOrNull(kinematic)) { kinematic = false; @@ -165,7 +165,7 @@ GameLib.D3.API.RigidBody.FromObject = function(objectRigidBody) { objectRigidBody.collisionFilterGroup, objectRigidBody.collisionFilterMask, objectRigidBody.fixedRotation, - objectRigidBody.shape, + objectRigidBody.shapes, objectRigidBody.kinematic, objectRigidBody.parentEntity ); diff --git a/src/game-lib-d3-mesh-0.js b/src/game-lib-d3-mesh-0.js index 6ded589..c8e7aad 100644 --- a/src/game-lib-d3-mesh-0.js +++ b/src/game-lib-d3-mesh-0.js @@ -531,14 +531,22 @@ GameLib.D3.Mesh.prototype.updateInstance = function() { } this.instance.renderOrder = this.renderOrder; - // - // this.instance.geometry.computeBoundingBox(); - // - // if (this.meshType === GameLib.D3.Mesh.MESH_TYPE_NORMAL) { - // this.width = this.instance.geometry.boundingBox.max.x - this.instance.geometry.boundingBox.min.x; - // this.height = this.instance.geometry.boundingBox.max.y - this.instance.geometry.boundingBox.min.y; - // this.depth = this.instance.geometry.boundingBox.max.z - this.instance.geometry.boundingBox.min.z; - // } + +}; + +GameLib.D3.Mesh.prototype.getBoundingBox = function() { + + this.instance.geometry.computeBoundingBox(); + + return new GameLib.Vector3( + this.graphics, + new GameLib.API.Vector3( + this.instance.geometry.boundingBox.max.x - this.instance.geometry.boundingBox.min.x, + this.instance.geometry.boundingBox.max.y - this.instance.geometry.boundingBox.min.y, + this.instance.geometry.boundingBox.max.z - this.instance.geometry.boundingBox.min.z + ) + ); + }; /** diff --git a/src/game-lib-d3-rigid-body.js b/src/game-lib-d3-rigid-body.js index 2825a00..e6b0758 100644 --- a/src/game-lib-d3-rigid-body.js +++ b/src/game-lib-d3-rigid-body.js @@ -38,7 +38,7 @@ GameLib.D3.RigidBody = function ( apiRigidBody.collisionFilterGroup, apiRigidBody.collisionFilterMask, apiRigidBody.fixedRotation, - apiRigidBody.shape, + apiRigidBody.shapes, apiRigidBody.kinematic, apiRigidBody.parentEntity ); @@ -47,7 +47,7 @@ GameLib.D3.RigidBody = function ( this, GameLib.Component.COMPONENT_RIGID_BODY, { - 'shape' : GameLib.D3.Shape + 'shapes' : [GameLib.D3.Shape] } ); }; @@ -166,7 +166,7 @@ GameLib.D3.RigidBody.prototype.toApiObject = function() { this.collisionFilterGroup, this.collisionFilterMask, this.fixedRotation, - GameLib.Utils.IdOrNull(this.shape), + this.shapes.map(function(shape){return GameLib.Utils.IdOrNull(shape)}), this.kinematic, GameLib.Utils.IdOrNull(this.parentEntity) ); @@ -176,17 +176,17 @@ GameLib.D3.RigidBody.prototype.toApiObject = function() { /** * GameLib.D3.RigidBody from Object RigidBody - * @param graphics + * @param physics * @param objectComponent * @returns {GameLib.D3.RigidBody} * @constructor */ -GameLib.D3.RigidBody.FromObject = function(graphics, objectComponent) { +GameLib.D3.RigidBody.FromObject = function(physics, objectComponent) { var apiRigidBody = GameLib.D3.API.RigidBody.FromObject(objectComponent); return new GameLib.D3.RigidBody( - graphics, + physics, apiRigidBody ); }; diff --git a/src/game-lib-d3-shape-0.js b/src/game-lib-d3-shape-0.js index f750c3f..b0f6892 100644 --- a/src/game-lib-d3-shape-0.js +++ b/src/game-lib-d3-shape-0.js @@ -96,16 +96,16 @@ GameLib.D3.Shape.prototype.toApiObject = function() { /** * Converts a standard object mesh to a GameLib.D3.Shape - * @param graphics GameLib.D3.Graphics + * @param physics GameLib.D3.Physics * @param objectShape {Object} * @constructor */ -GameLib.D3.Shape.FromObject = function(graphics, objectShape) { +GameLib.D3.Shape.FromObject = function(physics, objectShape) { var apiShape = GameLib.D3.API.Shape.FromObject(objectShape); return new GameLib.D3.Shape( - graphics, + physics, apiShape ); diff --git a/src/game-lib-d3-shape-box.js b/src/game-lib-d3-shape-box.js index d8f383f..2b8f5d7 100644 --- a/src/game-lib-d3-shape-box.js +++ b/src/game-lib-d3-shape-box.js @@ -3,12 +3,14 @@ * @param physics * @param apiShape GameLib.D3.API.Shape * @param halfExtents + * @param activeMesh * @constructor */ GameLib.D3.Shape.Box = function ( physics, apiShape, - halfExtents + halfExtents, + activeMesh ) { this.physics = physics; this.physics.isNotCannonThrow(); @@ -28,6 +30,12 @@ GameLib.D3.Shape.Box = function ( this.physics, apiShape ); + + if (GameLib.Utils.UndefinedOrNull(activeMesh)) { + activeMesh = null; + } + this.activeMesh = activeMesh; + this.linkedObjects.activeMesh = GameLib.D3.Mesh; }; GameLib.D3.Shape.Box.prototype = Object.create(GameLib.D3.Shape.prototype); @@ -50,9 +58,48 @@ GameLib.D3.Shape.Box.prototype.updateInstance = function() { this.instance.halfExtents.x = this.halfExtents.x; this.instance.halfExtents.y = this.halfExtents.y; this.instance.halfExtents.z = this.halfExtents.z; - this.instance.updateAABB(); this.instance.updateBoundingSphereRadius(); - this.instance.updateEdges(); - this.instance.updateNormals(); - this.instance.updateTree(); + this.instance.updateConvexPolyhedronRepresentation(); +}; + +GameLib.D3.Shape.Box.prototype.toApiObject = function() { + + var apiShape = GameLib.D3.Shape.prototype.toApiObject.call(this); + + apiShape.halfExtents = this.halfExtents.toApiObject(); + apiShape.activeMesh = GameLib.Utils.IdOrNull(this.activeMesh); + + return apiShape; +}; + +GameLib.D3.Shape.Box.prototype.setFromMesh = function() { + + if (this.activeMesh === null) { + console.log('select a mesh first'); + return; + } + + var box = this.activeMesh.getBoundingBox(); + + this.halfExtents.x = box.x / 2; + this.halfExtents.y = box.y / 2; + this.halfExtents.z = box.z / 2; + + this.halfExtents.updateInstance(); +}; + +GameLib.D3.Shape.Box.FromObject = function(physics, objectShape) { + + var apiShape = GameLib.D3.Shape.FromObject(physics, objectShape); + + apiShape.halfExtents = objectShape.halfExtents; + apiShape.activeMesh = objectShape.activeMesh; + + return new GameLib.D3.Shape.Box( + graphics, + apiShape, + apiShape.halfExtents, + apiShape.activeMesh + ); + }; \ No newline at end of file diff --git a/src/game-lib-system-storage.js b/src/game-lib-system-storage.js index ab39822..8effc6d 100644 --- a/src/game-lib-system-storage.js +++ b/src/game-lib-system-storage.js @@ -1,6 +1,7 @@ /** * Storage System takes care loading and linking components and dependencies * @param graphics + * @param physics * @param apiSystem GameLib.API.System * @param apiUrl * @param token @@ -15,6 +16,7 @@ */ GameLib.System.Storage = function( graphics, + physics, apiSystem, apiUrl, token, @@ -29,6 +31,9 @@ GameLib.System.Storage = function( this.graphics = graphics; this.graphics.isNotThreeThrow(); + this.physics = physics; + this.physics.isNotCannonThrow(); + GameLib.System.call( this, apiSystem @@ -251,7 +256,24 @@ GameLib.System.Storage.prototype.loadComponent = function(toProcess, includeDepe runtimeComponent.parentEntity = parentEntity; parentEntity = runtimeComponent; } else { - runtimeComponent = fn(__system.graphics, component); + try { + runtimeComponent = fn(__system.graphics, component); + } catch (error) { + runtimeComponent = null; + } + + if (!runtimeComponent) { + try { + runtimeComponent = fn(__system.physics, component); + } catch (error) { + runtimeComponent = null; + } + } + + if (!runtimeComponent) { + throw new Error('Could not create a runtime component: ', component); + } + runtimeComponent.parentEntity = parentEntity; }