forked from arkin0x/ONOSENDAI
-
Notifications
You must be signed in to change notification settings - Fork 0
/
keyboard.merge.tmp.js
88 lines (81 loc) · 1.97 KB
/
keyboard.merge.tmp.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
// keyboard
const keyState = {};
document.addEventListener('keyup', function (e) {
e.preventDefault();
keyState[e.code] = 0;
});
document.addEventListener('keydown', function (e) {
e.preventDefault();
console.log(e.code);
keyState[e.code] = 1;
});
/**
* updatePosition is called every frame. It reads the keyState and translates
* the camera.
*/
function updatePosition() {
const move = 10.0; // originally set at 1.0
const yMove = move;
const zMove = move;
const xMove = move;
const zRot = (Math.PI / 180) * 30;
let x = 0;
let y = 0;
let z = 0;
let zR = 0;
let yR = 0;
let xR = 0;
Object.keys(keyState).forEach(key => {
const keyFramesDown = keyState[key];
if (!keyState[key]) return;
let doUpdate = true;
switch (key) {
case 'KeyW': // move forward
z -= zMove;
break;
case 'KeyS': // move back
z += zMove;
break;
case 'KeyA': // move left
x -= xMove;
break;
case 'KeyD': // move right
x += xMove;
break;
case 'Space': // move up
y += yMove;
break;
case 'ShiftLeft': // move down
y -= yMove;
break;
case 'KeyQ': // yaw left
if (keyFramesDown === 1) yR += zRot;
break;
case 'KeyE': // yaw right
if (keyFramesDown === 1) yR -= zRot;
break;
case 'KeyZ': // roll left
if (keyFramesDown === 1) zR += zRot;
break;
case 'KeyC': // roll right
if (keyFramesDown === 1) zR -= zRot;
break;
default:
doUpdate = false;
break;
}
if (doUpdate) {
keyState[key]++;
camera.position.setX(camera.position.x + x);
camera.position.setY(camera.position.y + y);
camera.position.setZ(camera.position.z + z);
camera.rotation.setFromVector3(
new THREE.Vector3(
camera.rotation.x + xR,
camera.rotation.y + yR,
camera.rotation.z + zR
)
);
}
});
}