X-Git-Url: https://git.r.bdr.sh/rbdr/sumo/blobdiff_plain/1676911c8a2621274bf75ff7271faa926cf58d6c..43413defcb854c8706a84a6a54a61c7977b52614:/lib/systems/control_mapper.js diff --git a/lib/systems/control_mapper.js b/lib/systems/control_mapper.js index e91ecc6..c39aea3 100644 --- a/lib/systems/control_mapper.js +++ b/lib/systems/control_mapper.js @@ -2,9 +2,11 @@ import { System } from '@serpentity/serpentity'; import ControllableNode from '../nodes/controllable'; -/* global window */ +/* global navigator window */ const internals = { + gamepadState: { + }, keyboardState: { } }; @@ -32,6 +34,7 @@ export default class ControlMapperSystem extends System { this.controllables = null; this._initializeKeyboard(); + this._initializeGamepad(); } /** @@ -71,11 +74,21 @@ export default class ControlMapperSystem extends System { */ 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]); + } + } } } } @@ -95,6 +108,32 @@ export default class ControlMapperSystem extends System { }); } + // 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) {