import ControllableNode from '../nodes/controllable';
-/* global window */
-
const internals = {
+ gamepadState: {
+ },
keyboardState: {
}
};
this.controllables = null;
this._initializeKeyboard();
+ this._initializeGamepad();
}
/**
*/
update(currentFrameDuration) {
+ this._updateGamepads();
+
for (const controllable of this.controllables) {
for (const map of controllable.controlMap.map) {
if (map.source.type === 'keyboard') {
this._setValue(controllable.entity, map.target, !!internals.keyboardState[map.source.index]);
}
+
+ if (map.source.type === 'gamepad') {
+ const gamepad = internals.gamepadState[map.source.gamepadNumber];
+ if (gamepad) {
+ const source = gamepad[map.source.gamepadInputSource];
+ source && this._setValue(controllable.entity, map.target, source[map.source.index]);
+ }
+ }
}
}
}
});
}
+ // Requests gamepad access and binds to the events
+
+ _initializeGamepad() {
+
+ window.addEventListener('gamepadconnected', (event) => {
+
+ internals.gamepadState[event.gamepad.index] = event.gamepad;
+ window.gamepad = event.gamepad;
+ });
+
+ window.addEventListener('gamepaddisconnected', (event) => {
+
+ delete internals.gamepadState[event.gamepad.index];
+ });
+ }
+
+ // Update Gamepad
+
+ _updateGamepads() {
+
+ const gamepads = navigator.getGamepads();
+ for (const index of Object.keys(internals.gamepadState)) {
+ internals.gamepadState[index] = gamepads[index];
+ }
+ }
+
// Sets the value to a target
_setValue(entity, target, value) {
currentObject[finalProperty] += finalValue;
}
}
-};
+}