/
wasd-controls-mode2.js
293 lines (252 loc) · 8.32 KB
/
wasd-controls-mode2.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
/*
module.exports = {
AFRAME_INJECTED: 'aframe-injected',
DEFAULT_CAMERA_HEIGHT: 1.6,
DEFAULT_HANDEDNESS: 'right',
keyboardevent: require('./keyboardevent')
};
*/
//module.exports = {
// Tiny KeyboardEvent.code polyfill.
const KEYCODE_TO_CODE = {
'38': 'ArrowUp',
'37': 'ArrowLeft',
'40': 'ArrowDown',
'39': 'ArrowRight',
'87': 'KeyW',
'65': 'KeyA',
'83': 'KeyS',
'68': 'KeyD',
}
//};
/*
let KEYCODE_TO_CODE = require('../constants').keyboardevent.KEYCODE_TO_CODE;
let registerComponent = require('../core/component').registerComponent;
let THREE = require('../lib/three');
let utils = require('../utils/');
*/
//let bind = utils.bind;
function bind (fn, ctx/* , arg1, arg2 */) {
return (function (prependedArgs) {
return function bound () {
// Concat the bound function arguments with those passed to original bind
let args = prependedArgs.concat(Array.prototype.slice.call(arguments, 0));
return fn.apply(ctx, args);
};
})(Array.prototype.slice.call(arguments, 2));
};
//let shouldCaptureKeyEvent = utils.shouldCaptureKeyEvent;
const shouldCaptureKeyEvent = function (event) {
if (event.metaKey) { return false; }
return document.activeElement === document.body;
};
let CLAMP_VELOCITY = 0.00001;
let MAX_DELTA = 0.2;
let KEYS = [
'KeyW', 'KeyA', 'KeyS', 'KeyD',
'ArrowUp', 'ArrowLeft', 'ArrowRight', 'ArrowDown',
];
/**
* WASD component to control entities using WASD keys.
*/
AFRAME.registerComponent("wasd-controls-mode2", {
schema: {
acceleration: { default: 65 },
adAxis: {default: 'x', oneOf: ['x', 'y', 'z']},
adEnabled: {default: true},
adInverted: {default: false},
enabled: {default: true},
fly: { default: false },
wsAxis: {default: 'z', oneOf: ['x', 'y', 'z']},
wsEnabled: {default: true},
wsInverted: {default: false},
yAxis: {default: 'y', oneOf: ['x', 'y', 'z']},
yEnabled: { default: true },
yInverted: { default: false },
//role: { default: 'z', oneOf: ['x', 'y', 'z'] },
roleEnabled: { default: true },
roleInverted: { default: false },
},
init: function () {
// To keep track of the pressed keys.
this.keys = {};
this.easing = 1.1;
this.velocity = new THREE.Vector3();
this.rotationx = 0;
// Bind methods and add event listeners.
this.onBlur = bind(this.onBlur, this);
this.onContextMenu = bind(this.onContextMenu, this);
this.onFocus = bind(this.onFocus, this);
this.onKeyDown = bind(this.onKeyDown, this);
this.onKeyUp = bind(this.onKeyUp, this);
this.onVisibilityChange = bind(this.onVisibilityChange, this);
this.attachVisibilityEventListeners();
},
tick: function (time, delta) {
let data = this.data;
let el = this.el;
let velocity = this.velocity;
const nochg = !velocity[data.adAxis] && !velocity[data.wsAxis] && !velocity[data.yAxis] && !this.rotationx;
if (nochg && isEmptyObject(this.keys)) {
return;
}
// Update velocity.
delta = delta / 1000;
this.updateVelocity(delta);
if (nochg) {
return;
}
el.object3D.rotation.y -= this.rotationx / 200;
// Get movement vector and translate position.
el.object3D.position.add(this.getMovementVector(delta));
//console.log(el.object3D.rotation, el.object3D)
},
remove: function () {
this.removeKeyEventListeners();
this.removeVisibilityEventListeners();
},
play: function () {
this.attachKeyEventListeners();
},
pause: function () {
this.keys = {};
this.removeKeyEventListeners();
},
updateVelocity: function (delta) {
let acceleration;
let adAxis;
let adSign;
let data = this.data;
let keys = this.keys;
let velocity = this.velocity;
let wsAxis;
let wsSign;
adAxis = data.adAxis;
wsAxis = data.wsAxis;
const yAxis = data.yAxis;
const role = data.role;
// If FPS too low, reset velocity.
if (delta > MAX_DELTA) {
velocity[adAxis] = 0;
velocity[wsAxis] = 0;
velocity[yAxis] = 0;
return;
}
// https://gamedev.stackexchange.com/questions/151383/frame-rate-independant-movement-with-acceleration
let scaledEasing = Math.pow(1 / this.easing, delta * 60);
// Velocity Easing.
if (velocity[adAxis] !== 0) {
velocity[adAxis] = velocity[adAxis] * scaledEasing;
}
if (velocity[wsAxis] !== 0) {
velocity[wsAxis] = velocity[wsAxis] * scaledEasing;
}
if (velocity[yAxis] !== 0) {
velocity[yAxis] = velocity[yAxis] * scaledEasing;
}
// Clamp velocity easing.
if (Math.abs(velocity[adAxis]) < CLAMP_VELOCITY) { velocity[adAxis] = 0; }
if (Math.abs(velocity[wsAxis]) < CLAMP_VELOCITY) { velocity[wsAxis] = 0; }
if (Math.abs(velocity[yAxis]) < CLAMP_VELOCITY) { velocity[yAxis] = 0; }
if (!data.enabled) { return; }
// Update velocity using keys pressed.
acceleration = data.acceleration;
if (data.adEnabled) {
adSign = data.adInverted ? -1 : 1;
//if (keys.KeyA || keys.ArrowLeft) { velocity[adAxis] -= adSign * acceleration * delta; }
//if (keys.KeyD || keys.ArrowRight) { velocity[adAxis] += adSign * acceleration * delta; }
if (keys.ArrowLeft || keys.KeyA) { velocity[adAxis] -= adSign * acceleration * delta; }
if (keys.ArrowRight || keys.KeyD) { velocity[adAxis] += adSign * acceleration * delta; }
}
if (this.shiftKey) {
if (data.yEnabled) {
const ySign = data.yInverted ? -1 : 1;
if (keys.ArrowDown || keys.KeyS) { velocity[yAxis] -= ySign * acceleration * delta; }
if (keys.ArrowUp || keys.KeyW) { velocity[yAxis] += ySign * acceleration * delta; }
}
} else {
if (data.wsEnabled) {
wsSign = data.wsInverted ? -1 : 1;
if (keys.ArrowUp || keys.KeyW) { velocity[wsAxis] -= wsSign * acceleration * delta; }
if (keys.ArrowDown || keys.KeyS) { velocity[wsAxis] += wsSign * acceleration * delta; }
}
}
},
getMovementVector: (function () {
let directionVector = new THREE.Vector3(0, 0, 0);
return function (delta) {
let rotation = this.el.getAttribute('rotation');
//rotation.x = this.rotationx;
let velocity = this.velocity;
directionVector.copy(velocity);
directionVector.multiplyScalar(delta);
// Absolute.
if (!rotation) { return directionVector; }
const xRotation = this.data.fly ? rotation.x : 0;
//console.log(xRotation, rotation.x)
// Transform direction relative to heading.
const rotationEuler = new THREE.Euler(0, 0, 0, 'YXZ');
rotationEuler.set(THREE.Math.degToRad(xRotation), THREE.Math.degToRad(rotation.y), 0);
directionVector.applyEuler(rotationEuler);
return directionVector;
};
})(),
attachVisibilityEventListeners: function () {
window.oncontextmenu = this.onContextMenu;
window.addEventListener('blur', this.onBlur);
window.addEventListener('focus', this.onFocus);
document.addEventListener('visibilitychange', this.onVisibilityChange);
},
removeVisibilityEventListeners: function () {
window.removeEventListener('blur', this.onBlur);
window.removeEventListener('focus', this.onFocus);
document.removeEventListener('visibilitychange', this.onVisibilityChange);
},
attachKeyEventListeners: function () {
window.addEventListener('keydown', this.onKeyDown);
window.addEventListener('keyup', this.onKeyUp);
},
removeKeyEventListeners: function () {
window.removeEventListener('keydown', this.onKeyDown);
window.removeEventListener('keyup', this.onKeyUp);
},
onContextMenu: function () {
let keys = Object.keys(this.keys);
for (let i = 0; i < keys.length; i++) {
delete this.keys[keys[i]];
}
},
onBlur: function () {
this.pause();
},
onFocus: function () {
this.play();
},
onVisibilityChange: function () {
if (document.hidden) {
this.onBlur();
} else {
this.onFocus();
}
},
onKeyDown: function (event) {
if (!shouldCaptureKeyEvent(event)) {
return;
}
this.shiftKey = event.shiftKey;
const code = event.code || KEYCODE_TO_CODE[event.keyCode];
if (KEYS.indexOf(code) !== -1) {
this.keys[code] = true;
}
},
onKeyUp: function (event) {
const code = event.code || KEYCODE_TO_CODE[event.keyCode];
delete this.keys[code];
}
});
function isEmptyObject (keys) {
for (const key in keys) {
return false;
}
return true;
}