tiles position

Signed-off-by: ale <ale@manalejandro.com>
Este commit está contenido en:
ale
2025-11-12 18:22:30 +01:00
padre 930f4ca5a5
commit 1bdf86f170

Ver fichero

@@ -130,7 +130,7 @@ export function isValidMove(tile: DominoTile, side: 'left' | 'right', boardEnds:
return canPlaceTile(tile, targetEnd.value); return canPlaceTile(tile, targetEnd.value);
} }
// Calculate tile position on board with multi-directional placement // Calculate tile position on board with smart wrapping to keep tiles visible
export function calculateTilePosition( export function calculateTilePosition(
board: PlacedTile[], board: PlacedTile[],
side: 'left' | 'right', side: 'left' | 'right',
@@ -153,89 +153,84 @@ export function calculateTilePosition(
let orientation: 'horizontal' | 'vertical' = 'horizontal'; let orientation: 'horizontal' | 'vertical' = 'horizontal';
let rotation = 0; let rotation = 0;
// Calculate how many tiles on this side to determine direction changes // Count tiles on current side to determine wrapping
const tilesOnSide = side === 'right' ? board.length - 1 : 0; const tilesOnSide = side === 'right' ? board.length - 1 : 0;
const directionIndex = Math.floor(tilesOnSide / 3); // Change direction every 3 tiles for tighter spiral const segmentLength = 5; // Change direction every 5 tiles
const positionInDirection = tilesOnSide % 3; const segmentIndex = Math.floor(tilesOnSide / segmentLength);
if (side === 'right') { if (side === 'right') {
const direction = directionIndex % 4; // 0: right, 1: down, 2: left, 3: up // Alternate between horizontal right and vertical down
const isVerticalSegment = segmentIndex % 2 === 1;
switch (direction) { if (isVerticalSegment && lastTile.orientation === 'horizontal') {
case 0: // Going right (horizontal) // Transition from horizontal to vertical (going down)
orientation = isDouble ? 'vertical' : 'horizontal'; orientation = isDouble ? 'horizontal' : 'vertical';
position = { const offset = lastTile.orientation === 'horizontal' ? tileWidth : tileHeight;
x: lastTile.position.x + (lastTile.orientation === 'horizontal' ? tileWidth : tileHeight) + spacing, position = {
y: lastTile.position.y, x: lastTile.position.x + offset + spacing,
}; y: lastTile.position.y,
break; };
case 1: // Going down (vertical) } else if (!isVerticalSegment && lastTile.orientation === 'vertical') {
orientation = isDouble ? 'horizontal' : 'vertical'; // Transition from vertical to horizontal (going right)
position = { orientation = isDouble ? 'vertical' : 'horizontal';
x: lastTile.position.x, const offset = lastTile.orientation === 'vertical' ? tileHeight : tileWidth;
y: lastTile.position.y + (lastTile.orientation === 'vertical' ? tileHeight : tileWidth) + spacing, position = {
}; x: lastTile.position.x,
break; y: lastTile.position.y + offset + spacing,
case 2: // Going left (horizontal) };
orientation = isDouble ? 'vertical' : 'horizontal'; } else if (isVerticalSegment) {
position = { // Continue vertical (going down)
x: lastTile.position.x - (lastTile.orientation === 'horizontal' ? tileWidth : tileHeight) - spacing, orientation = isDouble ? 'horizontal' : 'vertical';
y: lastTile.position.y, const offset = lastTile.orientation === 'vertical' ? tileHeight : tileWidth;
}; position = {
break; x: lastTile.position.x,
case 3: // Going up (vertical) y: lastTile.position.y + offset + spacing,
orientation = isDouble ? 'horizontal' : 'vertical'; };
position = { } else {
x: lastTile.position.x, // Continue horizontal (going right)
y: lastTile.position.y - (lastTile.orientation === 'vertical' ? tileHeight : tileWidth) - spacing, orientation = isDouble ? 'vertical' : 'horizontal';
}; const offset = lastTile.orientation === 'horizontal' ? tileWidth : tileHeight;
break; position = {
default: x: lastTile.position.x + offset + spacing,
orientation = 'horizontal'; y: lastTile.position.y,
position = { };
x: lastTile.position.x + tileWidth + spacing,
y: lastTile.position.y,
};
} }
} else { } else {
// Left side uses same logic but in reverse // Left side: alternate between horizontal left and vertical up
const direction = directionIndex % 4; // 0: left, 1: up, 2: right, 3: down const isVerticalSegment = segmentIndex % 2 === 1;
switch (direction) { if (isVerticalSegment && lastTile.orientation === 'horizontal') {
case 0: // Going left (horizontal) // Transition from horizontal to vertical (going up)
orientation = isDouble ? 'vertical' : 'horizontal'; orientation = isDouble ? 'horizontal' : 'vertical';
position = { const offset = lastTile.orientation === 'horizontal' ? tileWidth : tileHeight;
x: lastTile.position.x - (lastTile.orientation === 'horizontal' ? tileWidth : tileHeight) - spacing, position = {
y: lastTile.position.y, x: lastTile.position.x - offset - spacing,
}; y: lastTile.position.y,
break; };
case 1: // Going up (vertical) } else if (!isVerticalSegment && lastTile.orientation === 'vertical') {
orientation = isDouble ? 'horizontal' : 'vertical'; // Transition from vertical to horizontal (going left)
position = { orientation = isDouble ? 'vertical' : 'horizontal';
x: lastTile.position.x, const offset = lastTile.orientation === 'vertical' ? tileHeight : tileWidth;
y: lastTile.position.y - (lastTile.orientation === 'vertical' ? tileHeight : tileWidth) - spacing, position = {
}; x: lastTile.position.x,
break; y: lastTile.position.y - offset - spacing,
case 2: // Going right (horizontal) };
orientation = isDouble ? 'vertical' : 'horizontal'; } else if (isVerticalSegment) {
position = { // Continue vertical (going up)
x: lastTile.position.x + (lastTile.orientation === 'horizontal' ? tileWidth : tileHeight) + spacing, orientation = isDouble ? 'horizontal' : 'vertical';
y: lastTile.position.y, const offset = lastTile.orientation === 'vertical' ? tileHeight : tileWidth;
}; position = {
break; x: lastTile.position.x,
case 3: // Going down (vertical) y: lastTile.position.y - offset - spacing,
orientation = isDouble ? 'horizontal' : 'vertical'; };
position = { } else {
x: lastTile.position.x, // Continue horizontal (going left)
y: lastTile.position.y + (lastTile.orientation === 'vertical' ? tileHeight : tileWidth) + spacing, orientation = isDouble ? 'vertical' : 'horizontal';
}; const offset = lastTile.orientation === 'horizontal' ? tileWidth : tileHeight;
break; position = {
default: x: lastTile.position.x - offset - spacing,
orientation = 'horizontal'; y: lastTile.position.y,
position = { };
x: lastTile.position.x - tileWidth - spacing,
y: lastTile.position.y,
};
} }
} }