diff --git a/src/game-lib-component-a.js b/src/game-lib-component-a.js index 0347167..85f23fe 100644 --- a/src/game-lib-component-a.js +++ b/src/game-lib-component-a.js @@ -66,16 +66,17 @@ GameLib.D3.Component.COMPONENT_COLOR_LERP = 0x4; GameLib.D3.Component.COMPONENT_FLY_CONTROLS = 0x5; GameLib.D3.Component.COMPONENT_FOLLOW = 0x6; GameLib.D3.Component.COMPONENT_LOOK_AT = 0x7; -GameLib.D3.Component.COMPONENT_MESH_PERMUTATION = 0x8; -GameLib.D3.Component.COMPONENT_PATH_AI = 0x9; -GameLib.D3.Component.COMPONENT_PATH_CONTROLS = 0xA; +GameLib.D3.Component.COMPONENT_MESH = 0x8; +GameLib.D3.Component.COMPONENT_MESH_PERMUTATION = 0x9; +GameLib.D3.Component.COMPONENT_PATH_AI = 0xA; GameLib.D3.Component.COMPONENT_PATH_FOLLOWING = 0xB; -GameLib.D3.Component.COMPONENT_RAYCAST_VEHICLE_CONTROLS = 0xC; -GameLib.D3.Component.COMPONENT_TRIGGER_BOX_BOX = 0xD; -GameLib.D3.Component.COMPONENT_TRIGGER_BOX_SPHERE = 0xE; -GameLib.D3.Component.COMPONENT_TRIGGER_SPHERE_SPHERE = 0xF; -GameLib.D3.Component.COMPONENT_VEHICLE_AI_OBJECT_AVOIDENCE = 0x10; -GameLib.D3.Component.COMPONENT_VEHICLE_AI_PATH_STEERING = 0x11; +GameLib.D3.Component.COMPONENT_PATH_CONTROLS = 0xC; +GameLib.D3.Component.COMPONENT_RAYCAST_VEHICLE_CONTROLS = 0xD; +GameLib.D3.Component.COMPONENT_TRIGGER_BOX_BOX = 0xE; +GameLib.D3.Component.COMPONENT_TRIGGER_BOX_SPHERE = 0xF; +GameLib.D3.Component.COMPONENT_TRIGGER_SPHERE_SPHERE = 0x10; +GameLib.D3.Component.COMPONENT_VEHICLE_AI_OBJECT_AVOIDENCE = 0x11; +GameLib.D3.Component.COMPONENT_VEHICLE_AI_PATH_STEERING = 0x12; GameLib.D3.Component.prototype.toApiComponent = function() { @@ -160,6 +161,12 @@ GameLib.D3.Component.FromObjectComponent = function(graphics, objectComponent) { case GameLib.D3.Component.COMPONENT_PATH_FOLLOWING : component = new GameLib.D3.ComponentPathFollowing(null, null, graphics); break; + case GameLib.D3.Component.COMPONENT_MESH : + component = new GameLib.D3.ComponentMesh(); + break; + case GameLib.D3.Component.COMPONENT_MESH_PERMUTATION : + component = new GameLib.D3.ComponentMeshPermutation(null, null, graphics); + break; default: console.warn('This type of component is not yet read from the database:' + objectComponent.componentType); diff --git a/src/game-lib-component-mesh-permutation.js b/src/game-lib-component-mesh-permutation.js index 772c9f5..3b8f195 100644 --- a/src/game-lib-component-mesh-permutation.js +++ b/src/game-lib-component-mesh-permutation.js @@ -1,72 +1,173 @@ /** - * + * Adjusts a mesh rotation, scale and translation according to parent entity * @param id * @param name - * @param positionOffset - * @param quaternionOffset - * @param scaleOffset + * @param graphics GameLib.D3.Graphics + * @param parentEntity GameLib.D3.Entity + * @param mesh GameLib.D3.Mesh + * @param positionOffset GameLib.D3.Vector3 + * @param quaternionOffset GameLib.D3.Quaternion + * @param scaleOffset GameLib.D3.Vector3 * @constructor */ GameLib.D3.ComponentMeshPermutation = function ComponentMeshPermutation( id, name, + graphics, + parentEntity, + mesh, positionOffset, quaternionOffset, scaleOffset ) { - this.id = id || GameLib.D3.Utils.RandomId(); + this.graphics = graphics; + this.graphics.isNotThreeThrow(); + + GameLib.D3.Component.call( + this, + id, + name, + GameLib.D3.Component.COMPONENT_MESH_PERMUTATION, + parentEntity, + [ + new GameLib.D3.Component.LinkedProperty( + 'mesh', + GameLib.D3.Camera + ) + ] + ); - if (typeof name == 'undefined') { - name = this.constructor.name; + if (GameLib.D3.Utils.UndefinedOrNull(mesh)) { + mesh = null; } - this.name = name; + this.mesh = mesh; - this.parentEntity = null; + /** + * Position + */ + if (GameLib.D3.Utils.UndefinedOrNull(positionOffset)) { + positionOffset = new GameLib.D3.Vector3( + graphics, + this, + new GameLib.D3.API.Vector3(0, 0, 0) + ); + } + this.positionOffset = positionOffset; - this.positionOffset = positionOffset || new GameLib.D3.API.Vector3(0, 0, 0); - this.quaternionOffset = quaternionOffset || new GameLib.D3.API.Quaternion(0, 0, 0, 1); - this.scaleOffset = scaleOffset || new GameLib.D3.API.Vector3(1, 1, 1); + /** + * Rotation + */ + if (GameLib.D3.Utils.UndefinedOrNull(quaternionOffset)) { + quaternionOffset = new GameLib.D3.Quaternion( + graphics, + this, + new GameLib.D3.API.Quaternion() + ); + } + this.quaternionOffset = quaternionOffset; + + /** + * Scale + */ + if (GameLib.D3.Utils.UndefinedOrNull(scaleOffset)) { + scaleOffset = new GameLib.D3.Vector3( + graphics, + this, + new GameLib.D3.API.Vector3(1, 1, 1) + ); + } + this.scaleOffset = scaleOffset; - // Todo: this should be executed somewhere in game-lib-z, so that we don't execute it on every construction of an object. - GameLib.D3.Utils.Extend(GameLib.D3.ComponentMeshPermutation, GameLib.D3.Component); + /** + * Runtime members + */ + this.quaternion = new GameLib.D3.Quaternion( + graphics, + this, + new GameLib.D3.API.Quaternion() + ); + + this.position = new GameLib.D3.Vector3( + graphics, + this, + new GameLib.D3.API.Vector3() + ); + + this.scale = new GameLib.D3.Vector3( + graphics, + this, + new GameLib.D3.API.Vector3() + ); }; -//#ifdef RUNTIME__ -if(typeof THREE != "undefined") { - ComponentMeshPermutation_quaternion = new THREE.Quaternion(); - ComponentMeshPermutation_quaternionCopy = new THREE.Quaternion(); - ComponentMeshPermutation_position = new THREE.Vector3(); - ComponentMeshPermutation_scale = new THREE.Vector3(); - ComponentMeshPermutation_offsetQuaternion = new THREE.Quaternion(); - ComponentMeshPermutation_offsetPosition = new THREE.Vector3(); - ComponentMeshPermutation_offsetScale = new THREE.Vector3(); -} +GameLib.D3.ComponentMeshPermutation.prototype = Object.create(GameLib.D3.Component.prototype); +GameLib.D3.ComponentMeshPermutation.prototype.constructor = GameLib.D3.ComponentMeshPermutation; -///////////////////////// Methods to override ////////////////////////// -GameLib.D3.ComponentMeshPermutation.prototype.onLateUpdate = function( - deltaTime, - parentEntity -) { +/** + * onLateUpdate + * @param deltaTime + */ +GameLib.D3.ComponentMeshPermutation.prototype.onUpdate = function(deltaTime) { - if(parentEntity && parentEntity.mesh) { - ComponentMeshPermutation_quaternion.copy(parentEntity.mesh.quaternion); - ComponentMeshPermutation_quaternionCopy.copy(ComponentMeshPermutation_quaternion); - ComponentMeshPermutation_position.copy(parentEntity.mesh.position); + if (this.parentEntity && this.mesh) { - ComponentMeshPermutation_offsetQuaternion.copy(this.quaternionOffset); - ComponentMeshPermutation_quaternion = ComponentMeshPermutation_quaternion.multiply(ComponentMeshPermutation_offsetQuaternion).normalize(); + this.position.x = this.parentEntity.position.x + this.positionOffset.x; + this.position.y = this.parentEntity.position.y + this.positionOffset.y; + this.position.z = this.parentEntity.position.z + this.positionOffset.z; - ComponentMeshPermutation_offsetPosition.copy(this.positionOffset); - ComponentMeshPermutation_position = ComponentMeshPermutation_position.add(ComponentMeshPermutation_offsetPosition.applyQuaternion(ComponentMeshPermutation_quaternionCopy)); + this.scale.x = this.parentEntity.scale.x * this.scaleOffset.x; + this.scale.y = this.parentEntity.scale.y * this.scaleOffset.y; + this.scale.z = this.parentEntity.scale.z * this.scaleOffset.z; - ComponentMeshPermutation_scale.copy(parentEntity.mesh.scale); + this.quaternion.x = this.parentEntity.quaternion.x;// + this.quaternionOffset.x; + this.quaternion.y = this.parentEntity.quaternion.y;// + this.quaternionOffset.y; + this.quaternion.z = this.parentEntity.quaternion.z;// + this.quaternionOffset.z; + this.quaternion.w = this.parentEntity.quaternion.w;// + this.quaternionOffset.w; - ComponentMeshPermutation_offsetScale.copy(this.scaleOffset); - ComponentMeshPermutation_scale = ComponentMeshPermutation_scale.multiply(ComponentMeshPermutation_offsetScale); + this.quaternion.axis.x = this.quaternionOffset.axis.x; + this.quaternion.axis.y = this.quaternionOffset.axis.y; + this.quaternion.axis.z = this.quaternionOffset.axis.z; - parentEntity.mesh.position.copy(ComponentMeshPermutation_position); - parentEntity.mesh.quaternion.copy(ComponentMeshPermutation_quaternion); - parentEntity.mesh.scale.copy(ComponentMeshPermutation_scale); + this.quaternion.updateInstance(); + + //this.quaternion.setFromAngle(this.quaternionOffset.angle); + + this.quaternion.normalize(); + + this.mesh.position.x = this.position.x; + this.mesh.position.y = this.position.y; + this.mesh.position.z = this.position.z; + + this.mesh.scale.x = this.scale.x; + this.mesh.scale.y = this.scale.y; + this.mesh.scale.z = this.scale.z; + + this.mesh.quaternion.x = this.quaternion.x; + this.mesh.quaternion.y = this.quaternion.y; + this.mesh.quaternion.z = this.quaternion.z; + this.mesh.quaternion.w = this.quaternion.w; + + this.mesh.updateInstance(); + + // this.updateInstance(); + + // quaternion.copy(this.mesh.quaternion); + // quaternionCopy.copy(quaternion); + // position.copy(this.mesh.position); + // + // offsetQuaternion.copy(this.quaternionOffset); + // quaternion = quaternion.multiply(offsetQuaternion).normalize(); + // + // offsetPosition.copy(this.positionOffset); + // position = position.add(offsetPosition.applyQuaternion(quaternionCopy)); + // + // scale.copy(this.mesh.scale); + // + // offsetScale.copy(this.scaleOffset); + // scale = scale.multiply(offsetScale); + // + // this.mesh.position.copy(position); + // this.mesh.quaternion.copy(quaternion); + // this.mesh.scale.copy(scale); } }; -//#endif \ No newline at end of file diff --git a/src/game-lib-component-mesh.js b/src/game-lib-component-mesh.js new file mode 100644 index 0000000..d579879 --- /dev/null +++ b/src/game-lib-component-mesh.js @@ -0,0 +1,67 @@ +/** + * Creates a camera component which just assigns the position and rotation to whatever its parent position and rotation + * is. + * @param id + * @param name String + * @param parentEntity GameLib.D3.Entity + * @param mesh GameLib.D3.Camera + * @constructor + */ +GameLib.D3.ComponentMesh = function ComponentMesh( + id, + name, + parentEntity, + mesh +) { + GameLib.D3.Component.call( + this, + id, + name, + GameLib.D3.Component.COMPONENT_MESH, + parentEntity, + [ + new GameLib.D3.Component.LinkedProperty( + 'mesh', + GameLib.D3.Camera + ) + ] + ); + + if (GameLib.D3.Utils.UndefinedOrNull(mesh)) { + mesh = null; + } + this.mesh = mesh; +}; + +GameLib.D3.ComponentMesh.prototype = Object.create(GameLib.D3.Component.prototype); +GameLib.D3.ComponentMesh.prototype.constructor = GameLib.D3.ComponentMesh; + + +/** + * onLateUpdate override method + * @override + * @callback + */ +GameLib.D3.ComponentMesh.prototype.onLateUpdate = function() { + + if (this.mesh) { + + this.mesh.position.x = this.parentEntity.position.x; + this.mesh.position.y = this.parentEntity.position.y; + this.mesh.position.z = this.parentEntity.position.z; + + this.mesh.scale.x = this.parentEntity.scale.x; + this.mesh.scale.y = this.parentEntity.scale.y; + this.mesh.scale.z = this.parentEntity.scale.z; + + /** + * Caller is responsible for correct quaternion - i.e. normalize quaternion if needed + */ + this.mesh.quaternion.x = this.parentEntity.quaternion.x; + this.mesh.quaternion.y = this.parentEntity.quaternion.y; + this.mesh.quaternion.z = this.parentEntity.quaternion.z; + this.mesh.quaternion.w = this.parentEntity.quaternion.w; + + this.mesh.updateInstance(); + } +}; \ No newline at end of file diff --git a/src/game-lib-entity.js b/src/game-lib-entity.js index d223c3a..c92b45a 100644 --- a/src/game-lib-entity.js +++ b/src/game-lib-entity.js @@ -57,27 +57,6 @@ GameLib.D3.Entity.prototype.update = function( } } ); - - if (this.mesh) { - - this.mesh.position.x = this.position.x; - this.mesh.position.y = this.position.y; - this.mesh.position.z = this.position.z; - - this.mesh.scale.x = this.scale.x; - this.mesh.scale.y = this.scale.y; - this.mesh.scale.z = this.scale.z; - - /** - * Caller is responsible for correct quaternion - i.e. normalize quaternion if needed - */ - this.mesh.quaternion.x = this.quaternion.x; - this.mesh.quaternion.y = this.quaternion.y; - this.mesh.quaternion.z = this.quaternion.z; - this.mesh.quaternion.w = this.quaternion.w; - - this.mesh.updateInstance(); - } }; /**