touchscreen input no longer buffers

Nathan DECHER 5 years ago
parent 86cd935f03
commit 352c3aa16d
  1. 12
      src/js/main.js
  2. 6
      src/js/snek.js

@ -90,11 +90,10 @@
}
};
currentGame=snek;
//XXX
window.snek=snek;
});
window.addEventListener('keydown', async e => {
e.preventDefault();
if(e.key=='f') {
if(document.fullscreenElement) await document.exitFullscreen();
else await main.requestFullscreen();
@ -108,7 +107,8 @@
else if(e.key=='ArrowRight') inputs.right=true;
});
window.addEventListener('touchstart', e => {
const handleTouch=e => {
e.preventDefault();
let x=e.touches[0].clientX/window.innerWidth-.5;
let y=e.touches[0].clientY/window.innerHeight-.5;
const angle=((Math.atan2(x, y)+2*Math.PI)%(2*Math.PI))/Math.PI;
@ -118,6 +118,10 @@
else if(angle>.75 && angle<1.25) inputs.up=true;
else if(angle>1.25 && angle<1.75) inputs.left=true;
else inputs.down=true;
});
inputs.clearBuffer=true;
};
window.addEventListener('touchstart', handleTouch);
window.addEventListener('touchmove', handleTouch);
})();

@ -322,6 +322,12 @@ class SnekGame {
else if(inputs.right && trySet([ 1, 0])) return delete inputs.right;
else if(inputs.up && trySet([ 0,-1])) return delete inputs.up;
else if(inputs.down && trySet([ 0, 1])) return delete inputs.down;
if(inputs.clearBuffer) {
Object
.keys(inputs)
.forEach(k => delete inputs[k]);
}
}
start() {

Loading…
Cancel
Save