Fix square piece rotation bug and duplicate UI elements

This commit is contained in:
cmclark00 2025-03-25 18:27:53 -04:00
parent 7d45fc36fb
commit 4cb5250465

141
script.js
View file

@ -579,6 +579,11 @@ class Piece {
// Direction can be 'right' or 'left' // Direction can be 'right' or 'left'
if (gameOver || paused) return; if (gameOver || paused) return;
// Square piece (O) doesn't rotate
if (this.tetrominoType === 'O') {
return;
}
// Clear previous position // Clear previous position
this.undraw(); this.undraw();
clearPreviousPiecePosition(); clearPreviousPiecePosition();
@ -692,6 +697,11 @@ class Piece {
// 3D horizontal rotation effect (around Y axis) // 3D horizontal rotation effect (around Y axis)
rotate3DY() { rotate3DY() {
// Square pieces (O) shouldn't rotate
if (this.tetrominoType === 'O') {
return;
}
// Clear previous position // Clear previous position
this.undraw(); this.undraw();
clearPreviousPiecePosition(); clearPreviousPiecePosition();
@ -748,6 +758,11 @@ class Piece {
// 3D vertical rotation effect (around X axis) // 3D vertical rotation effect (around X axis)
rotate3DX() { rotate3DX() {
// Square pieces (O) shouldn't rotate
if (this.tetrominoType === 'O') {
return;
}
// Clear previous position // Clear previous position
this.undraw(); this.undraw();
clearPreviousPiecePosition(); clearPreviousPiecePosition();
@ -2410,6 +2425,11 @@ function handleTouchEnd(event) {
// Create touch instructions overlay // Create touch instructions overlay
function createTouchControlButtons() { function createTouchControlButtons() {
// Check if elements already exist
if (document.querySelector('.rotate-buttons')) {
return;
}
// Create 3D rotation buttons // Create 3D rotation buttons
const rotateButtons = document.createElement('div'); const rotateButtons = document.createElement('div');
rotateButtons.className = 'rotate-buttons'; rotateButtons.className = 'rotate-buttons';
@ -2434,78 +2454,83 @@ function createTouchControlButtons() {
} }
}); });
// Create touch instructions overlay // Create touch instructions overlay if it doesn't exist
const touchInstructions = document.createElement('div'); if (!document.querySelector('.touch-instructions')) {
touchInstructions.className = 'touch-instructions'; const touchInstructions = document.createElement('div');
touchInstructions.innerHTML = ` touchInstructions.className = 'touch-instructions';
<h3>Touch Controls</h3> touchInstructions.innerHTML = `
<p><b>Swipe left/right:</b> Move piece</p> <h3>Touch Controls</h3>
<p><b>Swipe down:</b> Soft drop</p> <p><b>Swipe left/right:</b> Move piece</p>
<p><b>Tap anywhere:</b> Rotate right</p> <p><b>Swipe down:</b> Soft drop</p>
<p><b>Double-tap:</b> Hard drop</p> <p><b>Tap anywhere:</b> Rotate right</p>
<p><b>3D Flip Buttons:</b> Rotate in 3D</p> <p><b>Double-tap:</b> Hard drop</p>
`; <p><b>3D Flip Buttons:</b> Rotate in 3D</p>
document.body.appendChild(touchInstructions); `;
document.body.appendChild(touchInstructions);
// Show instructions briefly, then fade out
setTimeout(() => {
touchInstructions.classList.add('fade-out');
setTimeout(() => {
// Keep element in DOM but hidden, so it can be shown again later
touchInstructions.style.opacity = '0';
touchInstructions.style.display = 'none';
}, 1000);
}, 5000);
// Add instruction toggle button to score container
const instructionsBtn = document.createElement('button');
instructionsBtn.className = 'game-btn instructions-btn';
instructionsBtn.innerHTML = 'Controls';
instructionsBtn.addEventListener('click', function() {
// Show instructions again
touchInstructions.style.display = 'block';
touchInstructions.style.opacity = '1';
touchInstructions.classList.remove('fade-out');
// Fade out after 5 seconds // Show instructions briefly, then fade out
setTimeout(() => { setTimeout(() => {
touchInstructions.classList.add('fade-out'); touchInstructions.classList.add('fade-out');
setTimeout(() => { setTimeout(() => {
// Keep element in DOM but hidden, so it can be shown again later
touchInstructions.style.opacity = '0';
touchInstructions.style.display = 'none'; touchInstructions.style.display = 'none';
}, 1000); }, 1000);
}, 5000); }, 5000);
}); }
// Add button to score container // Add instruction toggle button to score container if it doesn't exist
const scoreContainer = document.querySelector('.score-container'); const scoreContainer = document.querySelector('.score-container');
if (scoreContainer) { if (scoreContainer && !document.querySelector('.instructions-btn')) {
const instructionsBtn = document.createElement('button');
instructionsBtn.className = 'game-btn instructions-btn';
instructionsBtn.innerHTML = 'Controls';
instructionsBtn.addEventListener('click', function() {
// Show instructions again
const touchInstructions = document.querySelector('.touch-instructions');
if (touchInstructions) {
touchInstructions.style.display = 'block';
touchInstructions.style.opacity = '1';
touchInstructions.classList.remove('fade-out');
// Fade out after 5 seconds
setTimeout(() => {
touchInstructions.classList.add('fade-out');
setTimeout(() => {
touchInstructions.style.display = 'none';
}, 1000);
}, 5000);
}
});
// Add button to score container
scoreContainer.appendChild(instructionsBtn); scoreContainer.appendChild(instructionsBtn);
} }
// Add performance mode toggle // Add performance mode toggle if it doesn't exist
const perfToggle = document.createElement('input'); if (scoreContainer && !document.querySelector('.perf-toggle')) {
perfToggle.type = 'checkbox'; const perfToggle = document.createElement('input');
perfToggle.id = 'toggle-mobile-performance'; perfToggle.type = 'checkbox';
perfToggle.checked = mobilePerformanceMode; perfToggle.id = 'toggle-mobile-performance';
perfToggle.checked = mobilePerformanceMode;
const perfLabel = document.createElement('label');
perfLabel.htmlFor = 'toggle-mobile-performance'; const perfLabel = document.createElement('label');
perfLabel.textContent = 'Perf Mode'; perfLabel.htmlFor = 'toggle-mobile-performance';
perfLabel.className = 'perf-label'; perfLabel.textContent = 'Perf Mode';
perfLabel.className = 'perf-label';
const perfContainer = document.createElement('div');
perfContainer.className = 'perf-toggle'; const perfContainer = document.createElement('div');
perfContainer.appendChild(perfToggle); perfContainer.className = 'perf-toggle';
perfContainer.appendChild(perfLabel); perfContainer.appendChild(perfToggle);
perfContainer.appendChild(perfLabel);
if (scoreContainer) {
scoreContainer.appendChild(perfContainer); scoreContainer.appendChild(perfContainer);
// Add event listener
perfToggle.addEventListener('change', function() {
mobilePerformanceMode = this.checked;
});
} }
// Add event listener
perfToggle.addEventListener('change', function() {
mobilePerformanceMode = this.checked;
});
} }
// Set active piece to next piece and create new next piece // Set active piece to next piece and create new next piece