diff --git a/public/js/app.js b/public/js/app.js index bb19847..a1f9554 100644 --- a/public/js/app.js +++ b/public/js/app.js @@ -11,9 +11,17 @@ const keyState = { ArrowDown: false, }; +// Track key press timing for tap vs hold detection +let keyPressStartTime = null; +let wasAutoMoved = { + ArrowLeft: false, + ArrowRight: false, + ArrowDown: false, +}; + let dasCounter = 0; -const DAS_DELAY = 100; // ms before auto-repeat starts -const ARR_SPEED = 30; // ms between auto-repeat moves +const DAS_DELAY = 75; // ms before auto-repeat starts (reduced from 100 for faster response) +const ARR_SPEED = 25; // ms between auto-repeat moves (reduced from 30 for faster movement) // Initialize when DOM is ready document.addEventListener('DOMContentLoaded', () => { @@ -154,6 +162,8 @@ function setupKeyboardControls() { // Track held keys for movement if (keyState.hasOwnProperty(e.key)) { keyState[e.key] = true; + keyPressStartTime = performance.now(); // Track when key was pressed + wasAutoMoved[e.key] = false; // Reset auto-move flag for tap detection dasCounter = 0; // Reset DAS counter when key pressed e.preventDefault(); return; @@ -186,6 +196,21 @@ function setupKeyboardControls() { if (keyState.hasOwnProperty(e.key)) { keyState[e.key] = false; dasCounter = 0; + + // Tap detection: if key was held for less than DAS_DELAY and not auto-moved, + // send a single move for tap-based horizontal movement + if (keyPressStartTime) { + const pressDuration = performance.now() - keyPressStartTime; + if (pressDuration < DAS_DELAY && !wasAutoMoved[e.key]) { + // It was a tap - send single move + if (e.key === 'ArrowLeft') { + network.sendMove('left'); + } else if (e.key === 'ArrowRight') { + network.sendMove('right'); + } + } + } + keyPressStartTime = null; } }); @@ -285,14 +310,23 @@ function gameLoop(currentTime) { // Handle DAS (Delayed Auto Shift) for continuous movement if (!network.isSpectator && (keyState.ArrowLeft || keyState.ArrowRight || keyState.ArrowDown)) { dasCounter += deltaTime; - + // Send initial move immediately, then auto-repeat after DAS_DELAY if (dasCounter >= DAS_DELAY) { const autoRepeatCounter = (dasCounter - DAS_DELAY) % ARR_SPEED; if (autoRepeatCounter < (deltaTime || 0)) { - if (keyState.ArrowLeft) network.sendMove('left'); - if (keyState.ArrowRight) network.sendMove('right'); - if (keyState.ArrowDown) network.sendDrop(); + if (keyState.ArrowLeft) { + network.sendMove('left'); + wasAutoMoved.ArrowLeft = true; // Mark as auto-moved so keyup doesn't send another + } + if (keyState.ArrowRight) { + network.sendMove('right'); + wasAutoMoved.ArrowRight = true; + } + if (keyState.ArrowDown) { + network.sendDrop(); + wasAutoMoved.ArrowDown = true; + } } } }