Skip to content

Commit

Permalink
[#135] Fixed item interaction logic
Browse files Browse the repository at this point in the history
  • Loading branch information
Ido-Barnea committed Jan 13, 2024
1 parent 1d17563 commit ada2b83
Show file tree
Hide file tree
Showing 14 changed files with 62 additions and 51 deletions.
65 changes: 38 additions & 27 deletions development/code/logic/PieceLogic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { Trap } from './items/Trap';
import { King } from './pieces/King';
import { Pawn } from './pieces/Pawn';
import { Piece } from './pieces/Pieces';
import { Position, Square, getItemByPosition } from './pieces/PiecesUtilities';
import { Position, Square } from './pieces/PiecesUtilities';

function validatePlayerAction(
draggedPiece: Piece,
Expand All @@ -23,27 +23,31 @@ function validatePlayerAction(
if (draggedPiece === target) return false;
if (draggedPiece.position.boardId !== target.position.boardId) return false;

const validMoves = draggedPiece.getValidMoves();
return validMoves.some(position => comparePositions(position, target.position));
const legalMoves = draggedPiece.getLegalMoves();
return legalMoves.some(position => comparePositions(position, target.position));
}

function onActionNonAttackMove(
draggedPiece: Piece,
target: Piece | Square | Item,
targetSquare: Square,
) {
if (target instanceof Item) {
onActionPieceToItem(draggedPiece, target.position);
}
function simulatePath(piece: Piece, endPosition: Position) {
const legalMoves = piece.getLegalMoves();

onActionPieceToSquare(draggedPiece, targetSquare);
const isTargetSquare = (position: Position) => comparePositions(position, endPosition);
const targetIndex = legalMoves.findIndex(isTargetSquare);
if (targetIndex === -1) return;

const positionsBeforeTarget = legalMoves.slice(0, targetIndex);
positionsBeforeTarget.forEach(position => {
game.getItems().forEach(item => {
if (comparePositions(item.position, position)) onActionPieceToItem(piece, item);
});
});
}

export function onPlayerAction(
draggedPiece: Piece,
target: Piece | Square | Item,
) {
if (!validatePlayerAction(draggedPiece, target)) return;
simulatePath(draggedPiece, target.position);

if (target instanceof Piece) {
onActionAttackMove(draggedPiece, target);
Expand All @@ -53,6 +57,18 @@ export function onPlayerAction(
}
}

function onActionNonAttackMove(
draggedPiece: Piece,
target: Piece | Square | Item,
targetSquare: Square,
) {
if (target instanceof Item) {
onActionPieceToItem(draggedPiece, target);
}

onActionPieceToSquare(draggedPiece, targetSquare);
}

export function onPieceFellOffTheBoard(draggedPiece: Piece) {
permanentlyKillPiece(draggedPiece, draggedPiece);
game.setFellOffTheBoardPiece(draggedPiece);
Expand Down Expand Up @@ -132,8 +148,6 @@ function move(
shouldEndTurn = true,
) {
Logger.logMovement(draggedPiece, targetSquare);

onActionPieceToItem(draggedPiece, targetSquare.position);
movePieceOnBoard(draggedPiece, targetSquare);

draggedPiece.position = {
Expand Down Expand Up @@ -205,7 +219,7 @@ function handlePieceSpawning(targetPiece: Piece) {
});

game.getItems().forEach((item) => {
onActionPieceToItem(targetPiece, item.position);
onActionPieceToItem(targetPiece, item);
});

spawnPieceOnBoard(targetPiece);
Expand All @@ -232,18 +246,15 @@ export function permanentlyKillPiece(targetPiece: Piece, draggedPiece: Piece) {
commonKillPieceActions(targetPiece);
}

function onActionPieceToItem(piece: Piece, position: Position) {
const item = getItemByPosition(position);
if (item) {
switch (item.name) {
case ('gold coin'): {
pieceMovedOnCoin(piece, item);
break;
}
case ('trap'): {
pieceMovedOnTrap(piece, item);
break;
}
function onActionPieceToItem(piece: Piece, item: Item) {
switch (item.name) {
case ('gold coin'): {
pieceMovedOnCoin(piece, item);
break;
}
case ('trap'): {
pieceMovedOnTrap(piece, item);
break;
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions development/code/logic/pieces/Bishop.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@ describe('Piece movements', () => {
coordinates: [2, 3],
boardId: OVERWORLD_BOARD_ID,
};
let validMoves = bishop.getValidMoves();
let validMoves = bishop.getLegalMoves();
expect(validMoves).toContainEqual(newPosition);

const invalidPosition: Position = {
coordinates: [0, 0],
boardId: OVERWORLD_BOARD_ID,
};
validMoves = bishop.getValidMoves();
validMoves = bishop.getLegalMoves();
expect(validMoves).not.toContainEqual(invalidPosition);
});
});
2 changes: 1 addition & 1 deletion development/code/logic/pieces/Bishop.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export class Bishop extends Piece {
super(position, player, bishopResource, 'Bishop', logo);
}

getValidMoves(): Array<Position> {
getLegalMoves(): Array<Position> {
const validMoves: Array<Position> = [];
const currentCoordinates = this.position.coordinates;

Expand Down
6 changes: 3 additions & 3 deletions development/code/logic/pieces/King.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,21 +22,21 @@ describe('Piece movements', () => {
coordinates: [4, 6],
boardId: OVERWORLD_BOARD_ID,
};
let validMoves = king.getValidMoves();
let validMoves = king.getLegalMoves();
expect(validMoves).toContainEqual(newStraightPosition);

const newDiagonalPosition: Position = {
coordinates: [5, 6],
boardId: OVERWORLD_BOARD_ID,
};
validMoves = king.getValidMoves();
validMoves = king.getLegalMoves();
expect(validMoves).toContainEqual(newDiagonalPosition);

const invalidPosition: Position = {
coordinates: [0, 0],
boardId: OVERWORLD_BOARD_ID,
};
validMoves = king.getValidMoves();
validMoves = king.getLegalMoves();
expect(validMoves).not.toContainEqual(invalidPosition);
});
});
2 changes: 1 addition & 1 deletion development/code/logic/pieces/King.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ export class King extends Piece {
return true;
}

getValidMoves(): Array<Position> {
getLegalMoves(): Array<Position> {
const validMoves: Array<Position> = [];
const currentCoordinates = this.position.coordinates;

Expand Down
4 changes: 2 additions & 2 deletions development/code/logic/pieces/Knight.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@ describe('Piece movements', () => {
coordinates: [2, 5],
boardId: OVERWORLD_BOARD_ID,
};
let validMoves = knight.getValidMoves();
let validMoves = knight.getLegalMoves();
expect(validMoves).toContainEqual(newPosition);

const invalidPosition: Position = {
coordinates: [1, 5],
boardId: OVERWORLD_BOARD_ID,
};
validMoves = knight.getValidMoves();
validMoves = knight.getLegalMoves();
expect(validMoves).not.toContainEqual(invalidPosition);
});
});
2 changes: 1 addition & 1 deletion development/code/logic/pieces/Knight.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export class Knight extends Piece {
super(position, player, knightResource, 'Knight', logo);
}

getValidMoves(): Array<Position> {
getLegalMoves(): Array<Position> {
const validMoves: Array<Position> = [];
const currentCoordinates = this.position.coordinates;

Expand Down
10 changes: 5 additions & 5 deletions development/code/logic/pieces/Pawn.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ describe('Piece movements', () => {
coordinates: [0, 5],
boardId: OVERWORLD_BOARD_ID,
};
let validMoves = pawn.getValidMoves();
let validMoves = pawn.getLegalMoves();
expect(validMoves).toContainEqual(singleStepMove);

const blackPawnPosition: Position = {
Expand Down Expand Up @@ -52,28 +52,28 @@ describe('Piece movements', () => {
coordinates: [1, 5],
boardId: OVERWORLD_BOARD_ID,
};
validMoves = pawn.getValidMoves();
validMoves = pawn.getLegalMoves();
expect(validMoves).toContainEqual(enPassantMove);

blackPawn.position = {
coordinates: [1, 5],
boardId: OVERWORLD_BOARD_ID,
};
validMoves = pawn.getValidMoves();
validMoves = pawn.getLegalMoves();
expect(validMoves).toContainEqual(blackPawn.position);

const twoStepsInitialMove: Position = {
coordinates: [0, 4],
boardId: OVERWORLD_BOARD_ID,
};
validMoves = pawn.getValidMoves();
validMoves = pawn.getLegalMoves();
expect(validMoves).toContainEqual(twoStepsInitialMove);

const invalidPosition: Position = {
coordinates: [0, 3],
boardId: OVERWORLD_BOARD_ID,
};
validMoves = pawn.getValidMoves();
validMoves = pawn.getLegalMoves();
expect(validMoves).not.toContainEqual(invalidPosition);
});
});
2 changes: 1 addition & 1 deletion development/code/logic/pieces/Pawn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export class Pawn extends Piece {
return enPassantPawns[0];
}

getValidMoves(): Array<Position> {
getLegalMoves(): Array<Position> {
const validMoves: Array<Position> = [];
const currentCoordinates = this.position.coordinates;
const currentPlayer = game.getCurrentPlayer();
Expand Down
2 changes: 1 addition & 1 deletion development/code/logic/pieces/Pieces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export class Piece implements PieceType {
this.pieceLogo = pieceLogo;
}

getValidMoves(): Array<Position> {
getLegalMoves(): Array<Position> {
return [];
}

Expand Down
6 changes: 3 additions & 3 deletions development/code/logic/pieces/Queen.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,21 +22,21 @@ describe('Piece movements', () => {
coordinates: [2, 2],
boardId: OVERWORLD_BOARD_ID,
};
let validMoves = queen.getValidMoves();
let validMoves = queen.getLegalMoves();
expect(validMoves).toContainEqual(newStraightPosition);

const newDiagonalPosition: Position = {
coordinates: [5, 5],
boardId: OVERWORLD_BOARD_ID,
};
validMoves = queen.getValidMoves();
validMoves = queen.getLegalMoves();
expect(validMoves).toContainEqual(newDiagonalPosition);

const invalidPosition: Position = {
coordinates: [0, 0],
boardId: OVERWORLD_BOARD_ID,
};
validMoves = queen.getValidMoves();
validMoves = queen.getLegalMoves();
expect(validMoves).not.toContainEqual(invalidPosition);
});
});
2 changes: 1 addition & 1 deletion development/code/logic/pieces/Queen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export class Queen extends Piece {
super(position, player, queenResource, 'Queen', logo);
}

getValidMoves(): Array<Position> {
getLegalMoves(): Array<Position> {
const validMoves: Array<Position> = [];
const currentCoordinates = this.position.coordinates;

Expand Down
4 changes: 2 additions & 2 deletions development/code/logic/pieces/Rook.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@ describe('Piece movements', () => {
coordinates: [0, 2],
boardId: OVERWORLD_BOARD_ID,
};
let validMoves = rook.getValidMoves();
let validMoves = rook.getLegalMoves();
expect(validMoves).toContainEqual(newPosition);

const invalidPosition: Position = {
coordinates: [7, 7],
boardId: OVERWORLD_BOARD_ID,
};
validMoves = rook.getValidMoves();
validMoves = rook.getLegalMoves();
expect(validMoves).not.toContainEqual(invalidPosition);
});
});
2 changes: 1 addition & 1 deletion development/code/logic/pieces/Rook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export class Rook extends Piece {
super(position, player, rookResource, 'Rook', logo);
}

getValidMoves(): Array<Position> {
getLegalMoves(): Array<Position> {
const validMoves: Array<Position> = [];
const currentCoordinates = this.position.coordinates;

Expand Down

0 comments on commit ada2b83

Please sign in to comment.