touchscreen input no longer buffers

This commit is contained in:
Nathan DECHER 2020-03-26 12:04:18 +01:00
parent 86cd935f03
commit 352c3aa16d
2 changed files with 14 additions and 4 deletions

View file

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

View file

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