-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSpectatorControls.js
155 lines (150 loc) · 4.48 KB
/
SpectatorControls.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
import * as THREE from 'three';
// actions
const FORWARD = 1 << 0;
const LEFT = 1 << 1;
const RIGHT = 1 << 2;
const BACK = 1 << 3;
const UP = 1 << 4;
const DOWN = 1 << 5;
const SPRINT = 1 << 6;
// defaults
const MOVESPEED = 50;
const FRICTION = 0.9;
const LOOKSPEED = 0.005;
const SPRINTMULT = 2;
const KEYMAPPING = {
87: 'FORWARD', /* W */
83: 'BACK', /* S */
65: 'LEFT', /* A */
68: 'RIGHT', /* D */
32: 'UP', /* Spacebar */
67: 'DOWN', /* C */
16: 'SPRINT', /* Shift */
};
export default class SpectatorControls {
constructor(camera, {
lookSpeed = LOOKSPEED,
moveSpeed = MOVESPEED,
friction = FRICTION,
keyMapping = KEYMAPPING,
sprintMultiplier = SPRINTMULT
} = {}) {
this.camera = camera;
this.lookSpeed = lookSpeed;
this.moveSpeed = moveSpeed;
this.friction = friction;
this.sprintMultiplier = sprintMultiplier;
this.keyMapping = Object.assign({}, KEYMAPPING, keyMapping);
this.enabled = false;
this._mouseState = { x: 0, y: 0 };
this._keyState = { press: 0, prevPress: 0 };
this._moveState = { velocity: new THREE.Vector3(0, 0, 0) };
this._processMouseMoveEvent = this._processMouseMoveEvent.bind(this);
this._processKeyEvent = this._processKeyEvent.bind(this);
}
_processMouseMoveEvent(event) {
this._processMouseMove(
event.movementX || event.mozMovementX || event.webkitMovementX,
event.movementY || event.mozMovementY || event.webkitMovementY
);
}
_processMouseMove(x = 0, y = 0) {
this._mouseState = { x, y };
}
_processKeyEvent(event) {
this._processKey(event.keyCode, event.type === "keydown");
}
_processKey(key, isPressed) {
const { press } = this._keyState;
let newPress = press;
switch (this.keyMapping[key]) {
case 'FORWARD':
isPressed ? newPress |= FORWARD : newPress &= ~FORWARD;
break;
case 'BACK':
isPressed ? newPress |= BACK : newPress &= ~BACK;
break;
case 'LEFT':
isPressed ? newPress |= LEFT : newPress &= ~LEFT;
break;
case 'RIGHT':
isPressed ? newPress |= RIGHT : newPress &= ~RIGHT;
break;
case 'UP':
isPressed ? newPress |= UP : newPress &= ~UP;
break;
case 'DOWN':
isPressed ? newPress |= DOWN : newPress &= ~DOWN;
break;
case 'SPRINT':
isPressed ? newPress |= SPRINT : newPress &= ~SPRINT;
break;
default:
break;
}
this._keyState.press = newPress;
}
enable() {
document.addEventListener('mousemove', this._processMouseMoveEvent);
document.addEventListener('keydown', this._processKeyEvent);
document.addEventListener('keyup', this._processKeyEvent);
this.enabled = true;
this.camera.rotation.reorder("YXZ");
}
disable() {
document.removeEventListener('mousemove', this._processMouseMoveEvent);
document.removeEventListener('keydown', this._processKeyEvent);
document.removeEventListener('keyup', this._processKeyEvent);
this.enabled = false;
this._keyState.press = 0;
this._keyState.prevPress = 0;
this._mouseState = { x: 0, y: 0 };
this.camera.rotation.reorder("XYZ");
}
isEnabled() {
return this.enabled;
}
dispose() {
this.disable();
}
update(delta = 1) {
if (!this.enabled) {
// finish move transition
if (this._moveState.velocity.length() > 0) {
this._moveCamera(this._moveState.velocity);
}
return;
}
// view angles
const actualLookSpeed = delta * this.lookSpeed;
const lon = ((20 * this._mouseState.x) * actualLookSpeed);
const lat = ((20 * this._mouseState.y) * actualLookSpeed);
this.camera.rotation.x = Math.max(Math.min(this.camera.rotation.x - lat, Math.PI / 2), - Math.PI / 2);
this.camera.rotation.y -= lon;
this._mouseState = { x: 0, y: 0 };
// movements
let actualMoveSpeed = delta * this.moveSpeed;
const velocity = this._moveState.velocity.clone();
const { press } = this._keyState;
if (press & SPRINT) actualMoveSpeed *= this.sprintMultiplier;
if (press & FORWARD) velocity.z = -actualMoveSpeed;
if (press & BACK) velocity.z = actualMoveSpeed;
if (press & LEFT) velocity.x = -actualMoveSpeed;
if (press & RIGHT) velocity.x = actualMoveSpeed;
if (press & UP) velocity.y = actualMoveSpeed;
if (press & DOWN) velocity.y = -actualMoveSpeed;
this._moveCamera(velocity);
this._moveState.velocity = velocity;
this._keyState.prevPress = press;
}
_moveCamera(velocity) {
velocity.multiplyScalar(this.friction);
velocity.clampLength(0, this.moveSpeed);
this.camera.translateZ(velocity.z);
this.camera.translateX(velocity.x);
this.camera.translateY(velocity.y);
}
mapKey(key, action) {
this.keyMapping = Object.assign({}, this.keyMapping, { [key]: action });
}
}