-
-
Notifications
You must be signed in to change notification settings - Fork 306
/
Copy pathkeyboard-controls.js
162 lines (135 loc) · 4.87 KB
/
keyboard-controls.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
/* global AFRAME, THREE */
/* eslint-disable no-prototype-builtins */
require('../../lib/keyboard.polyfill');
const PROXY_FLAG = '__keyboard-controls-proxy';
const KeyboardEvent = window.KeyboardEvent;
/**
* Keyboard Controls component.
*
* Stripped-down version of: https://github.com/donmccurdy/aframe-keyboard-controls
*
* Bind keyboard events to components, or control your entities with the WASD keys.
*
* Why use KeyboardEvent.code? "This is set to a string representing the key that was pressed to
* generate the KeyboardEvent, without taking the current keyboard layout (e.g., QWERTY vs.
* Dvorak), locale (e.g., English vs. French), or any modifier keys into account. This is useful
* when you care about which physical key was pressed, rather thanwhich character it corresponds
* to. For example, if you’re a writing a game, you might want a certain set of keys to move the
* player in different directions, and that mapping should ideally be independent of keyboard
* layout. See: https://developers.google.com/web/updates/2016/04/keyboardevent-keys-codes
*
* @namespace wasd-controls
* keys the entity moves and if you release it will stop. Easing simulates friction.
* to the entity when pressing the keys.
* @param {bool} [enabled=true] - To completely enable or disable the controls
*/
module.exports = AFRAME.registerComponent('keyboard-controls', {
schema: {
enabled: { default: true },
debug: { default: false }
},
init: function () {
this.dVelocity = new THREE.Vector3();
this.localKeys = {};
this.listeners = {
keydown: this.onKeyDown.bind(this),
keyup: this.onKeyUp.bind(this),
blur: this.onBlur.bind(this),
onContextMenu: this.onContextMenu.bind(this),
};
},
/*******************************************************************
* Movement
*/
isVelocityActive: function () {
return this.data.enabled && !!Object.keys(this.getKeys()).length;
},
getVelocityDelta: function () {
const data = this.data;
const keys = this.getKeys();
this.dVelocity.set(0, 0, 0);
if (data.enabled) {
if (keys.KeyW || keys.ArrowUp) { this.dVelocity.z -= 1; }
if (keys.KeyA || keys.ArrowLeft) { this.dVelocity.x -= 1; }
if (keys.KeyS || keys.ArrowDown) { this.dVelocity.z += 1; }
if (keys.KeyD || keys.ArrowRight) { this.dVelocity.x += 1; }
// Move faster when the shift key is down.
if (keys.ShiftLeft) { this.dVelocity = this.dVelocity.multiplyScalar(2); }
}
return this.dVelocity.clone();
},
/*******************************************************************
* Events
*/
play: function () {
this.attachEventListeners();
},
pause: function () {
this.removeEventListeners();
},
attachEventListeners: function () {
window.addEventListener("contextmenu", this.listeners.onContextMenu, false);
window.addEventListener("keydown", this.listeners.keydown, false);
window.addEventListener("keyup", this.listeners.keyup, false);
window.addEventListener("blur", this.listeners.blur, false);
},
onContextMenu: function () {
for (const code in this.localKeys) {
if (this.localKeys.hasOwnProperty(code)) {
delete this.localKeys[code];
}
}
},
removeEventListeners: function () {
window.removeEventListener('keydown', this.listeners.keydown);
window.removeEventListener('keyup', this.listeners.keyup);
window.removeEventListener('blur', this.listeners.blur);
},
onKeyDown: function (event) {
if (AFRAME.utils.shouldCaptureKeyEvent(event)) {
this.localKeys[event.code] = true;
this.emit(event);
}
},
onKeyUp: function (event) {
if (AFRAME.utils.shouldCaptureKeyEvent(event)) {
delete this.localKeys[event.code];
this.emit(event);
}
},
onBlur: function () {
for (const code in this.localKeys) {
if (this.localKeys.hasOwnProperty(code)) {
delete this.localKeys[code];
}
}
},
emit: function (event) {
// TODO - keydown only initially?
// TODO - where the f is the spacebar
// Emit original event.
if (PROXY_FLAG in event) {
// TODO - Method never triggered.
this.el.emit(event.type, event);
}
// Emit convenience event, identifying key.
this.el.emit(event.type + ':' + event.code, new KeyboardEvent(event.type, event));
if (this.data.debug) console.log(event.type + ':' + event.code);
},
/*******************************************************************
* Accessors
*/
isPressed: function (code) {
return code in this.getKeys();
},
getKeys: function () {
if (this.isProxied()) {
return this.el.sceneEl.components['proxy-controls'].getKeyboard();
}
return this.localKeys;
},
isProxied: function () {
const proxyControls = this.el.sceneEl.components['proxy-controls'];
return proxyControls && proxyControls.isConnected();
}
});