follow & look at components

beta.r3js.org
polygonboutique 2016-11-04 15:50:33 +01:00
parent a0a9d6e45d
commit 274ab21d3e
3 changed files with 72 additions and 18 deletions

View File

@ -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
) {
//
};

View File

@ -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);
}
};

View File

@ -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;
}
};