game lib controls

beta.r3js.org
Theunis J. Botha 2016-10-07 10:51:37 +02:00
parent 70b2957343
commit e39ffd7383
2 changed files with 238 additions and 0 deletions

227
game-lib-controls.js Normal file
View File

@ -0,0 +1,227 @@
function Controls() {}
Controls.FlyControls = function(
camera,
THREE,
canvas
) {
this.flySpeed = 25;
this.canvas = canvas;
this.THREE = THREE;
this.yaw = 0;
this.pitch = 0;
this.canRotate = false;
this.moveForward = false;
this.moveBackward = false;
this.moveLeft = false;
this.moveRight = false;
this.moveUp = false;
this.moveDown = false;
// Lock cursor
this.havePointerLock = 'pointerLockElement' in document || 'mozPointerLockElement' in document || 'webkitPointerLockElement' in document;
this.element = document.body;
if (this.havePointerLock) {
this.element.requestPointerLock = this.element.requestPointerLock || this.element.mozRequestPointerLock || this.element.webkitRequestPointerLock;
document.exitPointerLock = document.exitPointerLock || document.mozExitPointerLock || document.webkitExitPointerLock;
}
this.mouseUpCallback = this.onMouseUp.bind(this);
this.mouseDownCallback = this.onMouseDown.bind(this);
this.mouseMoveCallback = this.onMouseMove.bind(this);
this.keyDownCallback = this.onKeyDown.bind(this);
this.keyUpCallback = this.onKeyUp.bind(this);
//this.mouseClickCallback = this.onMouseClick.bind(this);
this.camera = camera;
// Add listeners
this.canvas.addEventListener('keydown', this.keyDownCallback, false);
this.canvas.addEventListener('keyup', this.keyUpCallback, false);
this.canvas.addEventListener('mousedown', this.mouseDownCallback, false);
this.canvas.addEventListener('mouseup', this.mouseUpCallback, false);
};
Controls.FlyControls.prototype.onMouseClick = function(event) {
if (this.havePointerLock) {
if (event.button == 0) {
this.canRotate = true;
this.element.requestPointerLock();
} else if(event.button == 2) {
this.canRotate = false;
document.exitPointerLock();
}
}
};
Controls.FlyControls.prototype.onMouseDown = function(event) {
if (event.button == 0) {
this.canRotate = true;
this.canvas.addEventListener('mousemove', this.mouseMoveCallback, false);
}
};
Controls.FlyControls.prototype.onMouseUp = function(event) {
if (event.button == 0) {
this.canRotate = false;
this.canvas.removeEventListener('mousemove', this.mouseMoveCallback);
}
};
Controls.FlyControls.prototype.getForward = function() {
var direction = new this.THREE.Vector3(0, 0, 1);
var rotation = new this.THREE.Euler(0, 0, 0, "YXZ");
rotation.set(this.pitch, this.yaw, 0, "YXZ");
var forward = direction.applyEuler(rotation).normalize();
return forward;
};
Controls.FlyControls.prototype.applyRotation = function(deltaTime) {
this.camera.rotation.set(this.pitch, this.yaw, 0, "YXZ");
};
Controls.FlyControls.prototype.applyTranslation = function(deltaTime) {
var direction = new this.THREE.Vector3(0, 0, -1);
var rotation = new this.THREE.Euler(0, 0, 0, "YXZ");
rotation.set(this.pitch, this.yaw, 0, "YXZ");
direction = direction.applyEuler(rotation);
if(this.moveForward) {
var newPos = direction.normalize();
this.camera.position.x += newPos.x * (deltaTime * this.flySpeed);
this.camera.position.y += newPos.y * (deltaTime * this.flySpeed);
this.camera.position.z += newPos.z * (deltaTime * this.flySpeed);
} else if(this.moveBackward) {
var newPos = direction.normalize();
this.camera.position.x -= newPos.x * (deltaTime * this.flySpeed);
this.camera.position.y -= newPos.y * (deltaTime * this.flySpeed);
this.camera.position.z -= newPos.z * (deltaTime * this.flySpeed);
}
if(this.moveLeft) {
var forward = direction.normalize();
var right = forward.cross(new this.THREE.Vector3(0, 1, 0));
var newPos = right;
this.camera.position.x -= newPos.x * (deltaTime * this.flySpeed);
this.camera.position.y -= newPos.y * (deltaTime * this.flySpeed);
this.camera.position.z -= newPos.z * (deltaTime * this.flySpeed);
} else if(this.moveRight) {
var forward = direction.normalize();
var right = forward.cross(new this.THREE.Vector3(0, 1, 0));
var newPos = right;
this.camera.position.x += newPos.x * (deltaTime * this.flySpeed);
this.camera.position.y += newPos.y * (deltaTime * this.flySpeed);
this.camera.position.z += newPos.z * (deltaTime * this.flySpeed);
}
// Absolute Y-Axis
if(this.moveUp) {
this.camera.position.y += (deltaTime * this.flySpeed);
} else if(this.moveDown) {
this.camera.position.y -= (deltaTime * this.flySpeed);
}
};
Controls.FlyControls.prototype.update = function(deltaTime) {
};
Controls.FlyControls.prototype.onMouseMove = function ( event ) {
if (this.canRotate) {
var movementX = event.movementX || event.mozMovementX || event.webkitMovementX || 0;
var movementY = event.movementY || event.mozMovementY || event.webkitMovementY || 0;
this.yaw -= movementX * 0.002;
this.pitch -= movementY * 0.002;
}
// Save mouseCoords
// sys.mouseCoords.set( ( event.clientX / window.innerWidth ) * 2 - 1,
// -( event.clientY / window.innerHeight ) * 2 + 1 );
};
Controls.FlyControls.prototype.onKeyDown = function ( event ) {
switch ( event.keyCode ) {
case 87: // w
this.moveForward = true;
break;
case 65: // a
this.moveLeft = true;
break;
case 83: // s
this.moveBackward = true;
break;
case 68: // d
this.moveRight = true;
break;
case 32: // space
this.moveUp = true;
break;
case 16:
this.moveDown = true;
break;
}
};
Controls.FlyControls.prototype.onKeyUp = function ( event ) {
switch ( event.keyCode ) {
case 38: // up
case 87: // w
this.moveForward = false;
break;
case 37: // left
case 65: // a
this.moveLeft = false;
break;
case 40: // down
case 83: // s
this.moveBackward = false;
break;
case 39: // right
case 68: // d
this.moveRight = false;
break;
case 32: // space
this.moveUp = false;
break;
case 16:
this.moveDown = false;
break;
}
};
if (typeof module !== 'undefined') {
module.exports = Controls;
}

View File

@ -2,6 +2,10 @@ if (typeof require != 'undefined') {
var Maths3D = require('./game-lib-maths.js');
}
if (typeof require != 'undefined') {
var Controls = require('./game-lib-controls.js');
}
function GameLib() {}
/**
@ -34,6 +38,13 @@ if (typeof require == 'undefined' &&
console.warn("You need a proper Maths3D library in order to use this library");
}
if (typeof require == 'undefined' &&
typeof Controls == 'undefined') {
console.warn("You need a proper Control library in order to use this library");
}
GameLib.D3.Controls = Controls;
GameLib.D3.Vector2 = Maths3D.Vector2;
GameLib.D3.Vector3 = Maths3D.Vector3;
GameLib.D3.Vector4 = Maths3D.Vector4;