diff --git a/development/code/logic/PieceLogic.ts b/development/code/logic/PieceLogic.ts index b9a7307a..1e2dd7a1 100644 --- a/development/code/logic/PieceLogic.ts +++ b/development/code/logic/PieceLogic.ts @@ -223,7 +223,31 @@ function move( function failToKillPiece(draggedPiece: Piece, targetPiece: Piece) { destroyItemOnPiece(targetPiece); - revertPieceMoveOnBoard(draggedPiece); + + // Takes the difference of the dragged and target positions in both axis, + // if the dragged position is higher - it would be positive, if lower - negative + // then I use that to determine the direction to move away from the target position + // and divide it by itself cause I wanna move by 1 in any direction + let directionX = 0; + let directionY = 0; + const targetXPosition = targetPiece.position.coordinates[0]; + const targetYPosition = targetPiece.position.coordinates[1]; + const deltaX = draggedPiece.position.coordinates[0] - targetXPosition; + const deltaY = draggedPiece.position.coordinates[1] - targetYPosition; + if (deltaX !== 0) { + directionX = deltaX / Math.abs(deltaX); + } + if (deltaY !== 0) { + directionY = deltaY / Math.abs(deltaY); + } + + const newPosition: Position = { + coordinates: [targetXPosition + directionX, targetYPosition + directionY], + boardId: draggedPiece.position.boardId, + }; + movePieceOnBoard(draggedPiece, newPosition); + draggedPiece.position = newPosition; + game.endMove(); } diff --git a/development/code/logic/pieces/Queen.test.ts b/development/code/logic/pieces/Queen.test.ts index 470d2881..666b7a72 100644 --- a/development/code/logic/pieces/Queen.test.ts +++ b/development/code/logic/pieces/Queen.test.ts @@ -90,7 +90,7 @@ describe('Piece killing', () => { game.setPieces([killerQueen, firstVictimPiece]); onPlayerAction(killerQueen, firstVictimPiece); - let firstVictimPieceBoardId = firstVictimPiece.position.boardId; + const firstVictimPieceBoardId = firstVictimPiece.position.boardId; expect(firstVictimPieceBoardId).toEqual(HEAVEN_BOARD_ID); let killerNewCoordinates = killerQueen.position.coordinates; @@ -109,7 +109,7 @@ describe('Piece killing', () => { killerNewCoordinates = killerQueen.position.coordinates; expect(killerNewCoordinates).toEqual(diagonalVictimPosition.coordinates); - let playerXP = killerQueen.player.xp; + const playerXP = killerQueen.player.xp; expect(playerXP).toBeGreaterThan(0); }); }); diff --git a/development/code/logic/rules/BountyRule.ts b/development/code/logic/rules/BountyRule.ts index 88ddf2be..b669b0e3 100644 --- a/development/code/logic/rules/BountyRule.ts +++ b/development/code/logic/rules/BountyRule.ts @@ -25,7 +25,9 @@ export class BountyRule extends BaseRule { position: { coordinates: pieceCoordinates }, } = piece; new RuleLog( - `There is an open bounty on a ${playerColor} ${pieceName} [${pieceCoordinates.join(',')}].`, + `There is an open bounty on a ${playerColor} ${pieceName} [${pieceCoordinates.join( + ',', + )}].`, ).addToQueue(); } });