Skip to content

Commit

Permalink
[Pieces] Add piece on top of other pieces validation
Browse files Browse the repository at this point in the history
  • Loading branch information
Tryferos committed Dec 11, 2024
1 parent e85e1cc commit 6741b15
Showing 1 changed file with 27 additions and 1 deletion.
28 changes: 27 additions & 1 deletion resources/js/Game/Board/Piece.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ export const Piece = ({code: pieceCode = 0, origin_position: position, rotation,
}

const onMoveFinish = (moveType: MoveType) => {
const isMoveValid = isPositionValid || isPieceOnBoard();
const isMoveValid = isPositionValid || (isPieceOnBoard() && !isPieceInteractingWithOtherPieces());
if(isMoveValid){
onMoveAccept(moveType);
}else{
Expand Down Expand Up @@ -354,6 +354,32 @@ export const Piece = ({code: pieceCode = 0, origin_position: position, rotation,
return false;
}

const isPieceInteractingWithOtherPieces = () => {
const piece = ref.current;
if(piece){
const pieces = boardState?.boardPieces.filter(item => item.uuid !== piece.uuid) ?? []
//* Check if there are no other pieces
if(pieces.length === 0) {
return false;
}else{
//* Get all the pieces positions that are on board;
const piecesPosition = pieces.map((p) => p.children.map(_ => new THREE.Vector3())).flat();
pieces.forEach((p, i) => p.children.forEach((b, j) => b.getWorldPosition(piecesPosition[i * piece.children.length + j])));
//* Traverse through all blocks to find if any pieces are on top of each other
for(const block of piece.children){
const blockPosition = new THREE.Vector3();
block.getWorldPosition(blockPosition);
const minDistance = Math.min(...piecesPosition.map(p => blockPosition.distanceTo(p)));
const allowedDistance = blockSize * 0.5;
if((allowedDistance >= minDistance) ){
return true;
}
}
}
}
return false;
}


return (
<DragControls axisLock="y" onDragEnd={onDragEnd} onDragStart={onDragStart} dragConfig={{enabled: !lockRotation}}>
Expand Down

0 comments on commit 6741b15

Please sign in to comment.