Improve double tap detection for mobile hard drop

This commit is contained in:
cmclark00 2025-03-25 17:54:15 -04:00
parent b0a7e634ef
commit 74565de189

View file

@ -2185,7 +2185,7 @@ let touchStartY = 0;
let touchStartTime = 0; let touchStartTime = 0;
const SWIPE_THRESHOLD = 15; // Reduced threshold for more responsive movement const SWIPE_THRESHOLD = 15; // Reduced threshold for more responsive movement
const TAP_THRESHOLD = 200; // milliseconds const TAP_THRESHOLD = 200; // milliseconds
const DOUBLE_TAP_THRESHOLD = 400; // Increased to prevent accidental double-taps const DOUBLE_TAP_THRESHOLD = 300; // Reduced from 400 to make double-tap easier
const TRIPLE_TAP_THRESHOLD = 600; // Maximum time between first and third tap const TRIPLE_TAP_THRESHOLD = 600; // Maximum time between first and third tap
let lastTapTime = 0; let lastTapTime = 0;
let secondLastTapTime = 0; // Track time of second-to-last tap for triple tap detection let secondLastTapTime = 0; // Track time of second-to-last tap for triple tap detection
@ -2195,7 +2195,7 @@ let lastTapX = 0;
let lastTapY = 0; let lastTapY = 0;
let secondLastTapX = 0; // Track position of second-to-last tap let secondLastTapX = 0; // Track position of second-to-last tap
let secondLastTapY = 0; // Track position of second-to-last tap let secondLastTapY = 0; // Track position of second-to-last tap
const TAP_DISTANCE_THRESHOLD = 20; // Maximum distance to consider as same-position tap const TAP_DISTANCE_THRESHOLD = 40; // Increased from 20 to be more forgiving for double-taps
const MOVE_COOLDOWN = 60; // Cooldown between moves to prevent too rapid movement const MOVE_COOLDOWN = 60; // Cooldown between moves to prevent too rapid movement
// Initialize touch controls // Initialize touch controls
@ -2318,40 +2318,39 @@ function handleTouchEnd(event) {
// Check for double tap (for hard drop) // Check for double tap (for hard drop)
const timeBetweenTaps = touchEndTime - lastTapTime; const timeBetweenTaps = touchEndTime - lastTapTime;
if (timeBetweenTaps < DOUBLE_TAP_THRESHOLD && distanceFromLastTap < TAP_DISTANCE_THRESHOLD) {
// Track second tap for potential triple tap if (lastTapTime > 0 && timeBetweenTaps < DOUBLE_TAP_THRESHOLD && distanceFromLastTap < TAP_DISTANCE_THRESHOLD) {
// This is a double-tap, do hard drop
p.hardDrop();
// Store for potential triple tap
secondLastTapTime = lastTapTime; secondLastTapTime = lastTapTime;
secondLastTapX = lastTapX; secondLastTapX = lastTapX;
secondLastTapY = lastTapY; secondLastTapY = lastTapY;
// Update last tap
lastTapTime = touchEndTime; lastTapTime = touchEndTime;
lastTapX = touchX; lastTapX = touchX;
lastTapY = touchY; lastTapY = touchY;
// If we already have a second tap recorded, this is too far from triple tap time // Debug
if (touchEndTime - secondLastTapTime > DOUBLE_TAP_THRESHOLD) { console.log("Double tap detected - hard drop");
p.hardDrop(); return;
lastTapTime = 0; }
secondLastTapTime = 0;
} // Single tap - rotates piece
} else { p.rotate('right');
// Single tap - rotates piece and resets tap tracking
if (lastTapTime === 0) { // Update tracking for potential double/triple tap
// First tap if (lastTapTime > 0) {
p.rotate('right'); // Store previous tap data
lastTapTime = touchEndTime; secondLastTapTime = lastTapTime;
lastTapX = touchX; secondLastTapX = lastTapX;
lastTapY = touchY; secondLastTapY = lastTapY;
} else {
// Too far from last tap position - treat as new first tap
p.rotate('right');
secondLastTapTime = 0;
lastTapTime = touchEndTime;
lastTapX = touchX;
lastTapY = touchY;
}
} }
// Set current tap as the last tap
lastTapTime = touchEndTime;
lastTapX = touchX;
lastTapY = touchY;
} }
// Reset touch identifier // Reset touch identifier