Skip to content

Commit

Permalink
cleanup hit area
Browse files Browse the repository at this point in the history
  • Loading branch information
cjgammon committed Aug 31, 2024
1 parent 025f3c6 commit b76904f
Showing 1 changed file with 53 additions and 3 deletions.
56 changes: 53 additions & 3 deletions vite-project/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ function replaceUsedPiece(usedPieceElement) {

// Function to initialize drag-and-drop and touch events
function initDragAndDrop() {
const gridCells = document.querySelectorAll('.grid-cell');
//const gridCells = document.querySelectorAll('.grid-cell');
const tetrisPieces = document.querySelectorAll('.tetris-piece');

tetrisPieces.forEach(piece => {
Expand All @@ -239,13 +239,18 @@ function initDragAndDrop() {
document.addEventListener('mousemove', drag);
document.addEventListener('touchmove', drag, { passive: false });

document.addEventListener('mouseup', dragEnd);
document.addEventListener('touchend', dragEnd);
//document.addEventListener('mouseup', dragEnd);
//document.addEventListener('touchend', dragEnd);

document.addEventListener('mouseup', drop);
document.addEventListener('touchend', drop);

/*
gridCells.forEach(cell => {
cell.addEventListener('mouseup', drop);
cell.addEventListener('touchend', drop);
});
*/
}

// Unified drag start event handler
Expand Down Expand Up @@ -331,6 +336,7 @@ function drag(e) {
}
}

/*
// Unified drop event handler
function drop(e) {
e.preventDefault();
Expand Down Expand Up @@ -381,6 +387,50 @@ function drop(e) {
clearDraggedPiece();
clearPreview();
}
*/

function drop(e) {
e.preventDefault();
console.log('drop')
if (!draggedPiece) return;

const clientX = e.clientX || e.touches[0].clientX;
const clientY = e.clientY || e.touches[0].clientY;
const cellSize = document.querySelector('.grid-cell').offsetWidth;
const pieceId = draggedPiece.dataset.originalId;
const pieceShape = JSON.parse(draggedPiece.dataset.shape);

const gridSize = 5;
const gridContainer = document.getElementById('game-grid-container');
const gridRect = gridContainer.getBoundingClientRect();

// Check if the drop occurred within the grid container boundaries
if (clientX < gridRect.left || clientX > gridRect.right ||
clientY < gridRect.top || clientY > gridRect.bottom) {
console.log('out of bounds')
returnPieceToBank(pieceId);
clearDraggedPiece();
clearPreview();
return;
}

const pieceWidth = draggedPiece.offsetWidth;
const pieceHeight = draggedPiece.offsetHeight;
const targetCol = Math.floor((clientX - gridRect.left - 5 - pieceWidth / 2) / cellSize);
const targetRow = Math.floor((clientY - gridRect.top - 5 - pieceHeight / 2) / cellSize);
const targetIndex = targetRow * gridSize + targetCol;

if (canPlacePiece(pieceShape, targetIndex, gridSize)) {
const color = draggedPiece.dataset.color;
placePiece(pieceShape, targetIndex, gridSize, color);
replaceUsedPiece(document.getElementById(pieceId));
} else {
returnPieceToBank(pieceId);
}

clearDraggedPiece();
clearPreview();
}

// Function to update the dragged piece position
function updateDraggedPiecePosition(clientX, clientY, activeRow, activeCol, cellSize) {
Expand Down

0 comments on commit b76904f

Please sign in to comment.