impliment piece movement and a check for valid moves.

This commit is contained in:
taylor berukoff 2025-11-11 18:41:34 -10:00
parent f4eb35e141
commit 432ab9e0d6
1 changed files with 48 additions and 14 deletions

View File

@ -1,4 +1,6 @@
document.addEventListener('DOMContentLoaded', () => { document.addEventListener('DOMContentLoaded', () => {
const MAX_SNAP_DISTANCE = 90;
// Generate the board // Generate the board
for (let i=0; i < 5; i++) { for (let i=0; i < 5; i++) {
for (let j = 0; j < 5; j++) { for (let j = 0; j < 5; j++) {
@ -49,40 +51,72 @@ document.addEventListener('DOMContentLoaded', () => {
player_token.style.top = `${e.pageY - offsetY}px`; player_token.style.top = `${e.pageY - offsetY}px`;
}); });
function resetToken() { function resetToken(startingCell) {
const startingCell = document.getElementById(`cell-4-0`); if (startingCell == null) {
startingCell = document.getElementById('cell-4-0');
}
// Reset position inside cell // Reset position inside cell
player_token.style.position = 'relative'; // back to relative so it sits inside the cell player_token.style.position = 'relative'; // back to relative so it sits inside the cell
player_token.style.left = ''; player_token.style.left = '';
player_token.style.top = ''; player_token.style.top = '';
player_token.style.zIndex = ''; player_token.style.zIndex = '';
originalCell.appendChild(token); startingCell.appendChild(player_token);
} }
function getClosestCell(x,y) { function getClosestCellByDistance(x, y) {
const boardRect = board.getBoundingClientRect(); let closestCell = null;
const cellSize = boardRect.width / 5; let minDistance = Infinity;
// Convert page coordinates to board-relative coordinates const cells = document.querySelectorAll('.cell');
const relX = x - boardRect.left; cells.forEach(cell => {
const relY = y - boardRect.top; const rect = cell.getBoundingClientRect();
const cellCenterX = rect.left + rect.width / 2;
const cellCenterY = rect.top + rect.height / 2;
// Find closest column and row const dx = x - cellCenterX;
const col = Math.min(4, Math.max(0, Math.floor(relX / cellSize))); const dy = y - cellCenterY;
const row = Math.min(4, Math.max(0, Math.floor(relY / cellSize))); const distance = Math.sqrt(dx*dx + dy*dy);
return document.getElementById(`cell-${row}-${col}`); if (distance < minDistance) {
minDistance = distance;
closestCell = cell;
}
});
return { cell: closestCell, distance: minDistance };
} }
function checkMoveValid(startingCell, endingCell) {
const startRow = startingCell.dataset.row
const startCol = startingCell.dataset.col
const endRow = endingCell.dataset.row
const endCol = endingCell.dataset.col
const rowDiff = Math.abs(startRow - endRow)
const colDiff = Math.abs(startCol - endCol)
return ( (rowDiff == 1 && colDiff == 2) || (rowDiff == 2 && colDiff == 1) );
}
// stop dragging once they let go // stop dragging once they let go
player_token.addEventListener('pointerup', (e) => { player_token.addEventListener('pointerup', (e) => {
isDragging = false; isDragging = false;
// Find nearest cell // Find nearest cell
const closestCell = getClosestCell(e.pageX, e.pageY); const {cell: closestCell, distance} = getClosestCellByDistance(e.pageX, e.pageY);
if (distance > MAX_SNAP_DISTANCE) {
resetToken(player_token.parentElement);
return;
}
const isValid = checkMoveValid(player_token.parentElement, closestCell);
if (!isValid) {
resetToken(player_token.parentElement)
return;
}
// Reset token style and append to cell // Reset token style and append to cell
player_token.style.position = 'relative'; player_token.style.position = 'relative';