Created
May 20, 2024 05:26
-
-
Save EncodeTheCode/a9915d835dc202af1becde4e35d13aff to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const BUTTONS = { | |
'TRIANGLE': 0x1000, | |
'CIRCLE': 0x2000, | |
'CROSS': 0x4000, | |
'SQUARE': 0x8000 | |
}; | |
function isButtonPressed(state, button) { | |
return (state.buttons & button) === 0; | |
} | |
function readGamepadState(gamepad) { | |
if (!gamepad) return; | |
const state = { | |
buttons: gamepad.buttons, | |
}; | |
console.log("Triangle pressed: ", isButtonPressed(state, BUTTONS['TRIANGLE'])); | |
console.log("Circle pressed: ", isButtonPressed(state, BUTTONS['CIRCLE'])); | |
console.log("Cross pressed: ", isButtonPressed(state, BUTTONS['CROSS'])); | |
console.log("Square pressed: ", isButtonPressed(state, BUTTONS['SQUARE'])); | |
} | |
function updateGamepads() { | |
const gamepads = navigator.getGamepads(); | |
for (const gamepad of gamepads) { | |
if (gamepad) { | |
readGamepadState(gamepad); | |
} | |
} | |
} | |
// Poll for gamepad state every 100ms | |
setInterval(updateGamepads, 100); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment