Last active
November 21, 2016 18:49
-
-
Save morningtoast/dda72295efa1979eff73bb14a1555ac0 to your computer and use it in GitHub Desktop.
pico8 web controls
This file contains hidden or 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
| /* | |
| HTML to add to exported | |
| <button id="goleft" onmousedown="btnpress(1)" onmouseup="btnpress()">UP</button> | |
| <p><button id="goleft" onmousedown="btnpress(4)" onmouseup="btnpress()">LEFT</button> | |
| <button id="goleft" onmousedown="btnpress(2)" onmouseup="btnpress()">RIGHT</button></p> | |
| <button id="goleft" onmousedown="btnpress(3)" onmouseup="btnpress()">DOWN</button> | |
| <hr> | |
| <button id="goleft" onmousedown="btnpress(5)" onmouseup="btnpress()">Z</button> | |
| <button id="goleft" onmousedown="btnpress(6)" onmouseup="btnpress()">X</button> | |
| */ | |
| function btnpress(st) { | |
| goup=false; | |
| goright=false; | |
| godown=false; | |
| goleft=false; | |
| btnz=false; | |
| btnx=false; | |
| if(st==1) { goup=true; } | |
| if(st==2) { goright=true; } | |
| if(st==3) { godown=true; } | |
| if(st==4) { goleft=true; } | |
| if(st==5) { btnz=true; } | |
| if(st==6) { btnx=true; } | |
| } | |
| // Array through which we'll communicate with PICO-8. | |
| var pico8_buttons = [0]; | |
| var goup=false; | |
| var goright=false; | |
| var godown=false; | |
| var goleft=false; | |
| var btnz=false; | |
| var btnx=false; | |
| // Start polling gamepads. | |
| requestAnimationFrame(updateGamepads); | |
| // Workhorse function, updates pico8_buttons once per frame. | |
| function updateGamepads() { | |
| var bitmask = 0; | |
| var gamepads = navigator.getGamepads ? navigator.getGamepads() : []; | |
| // Gather input from all known gamepads. | |
| // All gamepads are mapped to player #1, for now. | |
| for (var i = 0; i < gamepads.length; i++) { | |
| var gp = gamepads[i]; | |
| if (!gp || !gp.connected) continue; | |
| // directions (from axes or d-pad "buttons") | |
| bitmask |= (axis(gp,0) < -0.5 || btn(gp,14)) ? 1 : 0; | |
| bitmask |= (axis(gp,0) > 0.5 || btn(gp,15)) ? 2 : 0; | |
| bitmask |= (axis(gp,1) < -0.5 || btn(gp,12)) ? 4 : 0; | |
| bitmask |= (axis(gp,1) > 0.5 || btn(gp,13)) ? 8 : 0; | |
| // buttons (mapped twice for user convenience) | |
| bitmask |= (btn(gp,0) || btn(gp,2)) ? 16 : 0; | |
| bitmask |= (btn(gp,1) || btn(gp,3)) ? 32 : 0; | |
| } | |
| bitmask |= (goleft) ? 1 : 0; | |
| bitmask |= (goright) ? 2 : 0; | |
| bitmask |= (goup) ? 4 : 0; | |
| bitmask |= (godown) ? 8 : 0; | |
| bitmask |= (btnz) ? 16 : 0; | |
| bitmask |= (btnx) ? 32 : 0; | |
| // Update actual array and restart next frame. | |
| pico8_buttons[0] = bitmask; | |
| //goleft=false; | |
| requestAnimationFrame(updateGamepads); | |
| } | |
| // Helpers for accessing gamepad | |
| function axis(gp,n) { return gp.axes[n] || 0.0; } | |
| function btn(gp,b) { return gp.buttons[b] ? gp.buttons[b].pressed : false; } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment