-
Notifications
You must be signed in to change notification settings - Fork 156
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Gamepad support #53
Gamepad support #53
Conversation
This abstraction will enable us to add logic to opportunistically supply a `Controller` based on a [Gamepad](https://developer.mozilla.org/en-US/docs/Web/API/Gamepad_API/Using_the_Gamepad_API) instead of a keyboard, if available.
This addresses bfirsh#19 and is based off of a prototype I made over four years ago: bolinfest/jsnes@91daafe. On startup, the webapp checks to see if there are any connected Gamepads. If so, it elects to use a `GamepadController` instead of a `KeyboardController`. If the Gamepads get disconnected over the lifetime of the webapp, the current implementation waits for the Gamepads to be reconnected, though it would be more elegant if it fell back on the `KeyboardController` in this case. Note the mapping of the buttons is currently hardcoded. Ideally, there would be a GUI with a picture of the controller, highlighting one button at a time, giving the user the chance to press the corresponding button on their controller to establish the mapping. As a stopgap, we could also let users do this programmatically by stuffing values in `localStorage` because it will probably be a bit of work to create the GUI.
This is super cool, thank you! I will test this properly when I get some time. I intent for this codebase (and jsnes) to conform to prettier, so feel free to run your code and any of mine over it. :) |
// TODO: Listen for the "gamepaddisconnected" event. | ||
console.info('Using GamepadController.'); | ||
return gamepadController; | ||
} else { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No need to wrap this in an else-statement as you have a return here.
const gamepads = findConnectedGamepads(); | ||
if (gamepads.length > 0) { | ||
return new GamepadController(gamepads, options); | ||
} else { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same here. It's more clear to just use the early return pattern.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This mostly looks good, thank you!
One general comment: why is the keyboard controller disabled if there is a gamepad? I see no reason why both could run in tandem. It would be quite confusing for the keyboard to stop working if you happen to have a gamepad connected.
This would also remove a bunch of complexity around creating the controller - you wouldn't need ControllerFactory and so on.
this.checkGamepad(i); | ||
} | ||
|
||
window.requestAnimationFrame(this.poll); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This needs to be removed with window.cancelAnimationFrame()
.
Yet Another Gamepad Implementation in #164. This version had conflicts, so I just merged the other one for the sake of getting it in. Thanks for the contribution anyway @bolinfest! The version I merged needs testing and refactoring if you're still looking for ways to help. :) |
I'm not sure if I followed the existing style of the codebase exactly, but I'd recommend using https://prettier.io/ so no one has to think about it.