forked from c-frame/aframe-extras
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkeyboard-controls.js
More file actions
153 lines (127 loc) · 4.46 KB
/
keyboard-controls.js
File metadata and controls
153 lines (127 loc) · 4.46 KB
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
require('../../lib/keyboard.polyfill');
const MAX_DELTA = 0.2,
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)
};
this.attachEventListeners();
},
/*******************************************************************
* Movement
*/
isVelocityActive: function () {
return this.data.enabled && !!Object.keys(this.getKeys()).length;
},
getVelocityDelta: function () {
const data = this.data,
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; }
}
return this.dVelocity.clone();
},
/*******************************************************************
* Events
*/
play: function () {
this.attachEventListeners();
},
pause: function () {
this.removeEventListeners();
},
remove: function () {
this.pause();
},
attachEventListeners: function () {
window.addEventListener('keydown', this.listeners.keydown, false);
window.addEventListener('keyup', this.listeners.keyup, false);
window.addEventListener('blur', this.listeners.blur, false);
},
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 (let 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();
}
});