From 274ab21d3eda3cf6147c7b4fa9fba41efb267b53 Mon Sep 17 00:00:00 2001 From: polygonboutique Date: Fri, 4 Nov 2016 15:50:33 +0100 Subject: [PATCH] follow & look at components --- src/game-lib-component-camera-follow.js | 18 ---------- src/game-lib-component-follow.js | 26 ++++++++++++++ src/game-lib-component-look-at.js | 46 +++++++++++++++++++++++++ 3 files changed, 72 insertions(+), 18 deletions(-) delete mode 100644 src/game-lib-component-camera-follow.js create mode 100644 src/game-lib-component-follow.js create mode 100644 src/game-lib-component-look-at.js diff --git a/src/game-lib-component-camera-follow.js b/src/game-lib-component-camera-follow.js deleted file mode 100644 index 15cd2d2..0000000 --- a/src/game-lib-component-camera-follow.js +++ /dev/null @@ -1,18 +0,0 @@ -GameLib.D3.ComponentCameraFollow = function( - componentId, - targetEntity -) { - this.componentId = componentId || GameLib.D3.Tools.RandomId(); - this.parentEntity = null; - - // 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.ComponentCameraFollow, GameLib.D3.ComponentInterface); -}; - -///////////////////////// Methods to override ////////////////////////// -GameLib.D3.ComponentCameraFollow.prototype.onUpdate = function( - deltaTime, - parentEntity -) { - // -}; \ No newline at end of file diff --git a/src/game-lib-component-follow.js b/src/game-lib-component-follow.js new file mode 100644 index 0000000..9aeec20 --- /dev/null +++ b/src/game-lib-component-follow.js @@ -0,0 +1,26 @@ +GameLib.D3.ComponentFollow = function( + componentId, + targetEntity +) { + this.componentId = componentId || GameLib.D3.Tools.RandomId(); + this.parentEntity = null; + + // 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.ComponentFollow, GameLib.D3.ComponentInterface); + + // + this.targetEntity = targetEntity; + this.moveSpeed = 2.5; +}; + +///////////////////////// Methods to override ////////////////////////// +GameLib.D3.ComponentFollow.prototype.onUpdate = function( + deltaTime, + parentEntity +) { + if(this.targetEntity) { + var target = new THREE.Vector3().copy(this.targetEntity.position); + + parentEntity.position = parentEntity.position.lerp(target, this.moveSpeed * deltaTime); + } +}; \ No newline at end of file diff --git a/src/game-lib-component-look-at.js b/src/game-lib-component-look-at.js new file mode 100644 index 0000000..6d1a4e2 --- /dev/null +++ b/src/game-lib-component-look-at.js @@ -0,0 +1,46 @@ +GameLib.D3.ComponentLookAt = function( + componentId, + targetEntity +) { + this.componentId = componentId || GameLib.D3.Tools.RandomId(); + this.parentEntity = null; + + // 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.ComponentLookAt, GameLib.D3.ComponentInterface); + + // + this.targetEntity = targetEntity; +}; + +///////////////////////// Methods to override ////////////////////////// +GameLib.D3.ComponentLookAt.prototype.onUpdate = function( + deltaTime, + parentEntity +) { + if(this.targetEntity) { + var target = this.targetEntity.position; + var lookAtMatrix = new THREE.Matrix4().lookAt( + new THREE.Vector3( + parentEntity.position.x, + parentEntity.position.y, + parentEntity.position.z + ), + new THREE.Vector3( + target.x, + target.y, + target.z + ), + new THREE.Vector3( + 0, + 1, + 0 + ) + ); + + var quaternion = new THREE.Quaternion().setFromRotationMatrix(lookAtMatrix); + this.parentEntity.quaternion.x = quaternion.x; + this.parentEntity.quaternion.y = quaternion.y; + this.parentEntity.quaternion.z = quaternion.z; + this.parentEntity.quaternion.w = quaternion.w; + } +}; \ No newline at end of file