diff --git a/example/src/Game.tsx b/example/src/Game.tsx index b4c013e..9f05fe5 100644 --- a/example/src/Game.tsx +++ b/example/src/Game.tsx @@ -8,7 +8,7 @@ import { GameViewWrapper, } from 'meepl'; import assets from './Assets'; -import { isZoneAvailable } from './Logic'; +import { isZoneAvailable } from './gameLogic'; export default function Game(props) { const { G, ctx, plugins, moves } = props; diff --git a/example/src/gameConfig.tsx b/example/src/gameConfig.tsx index 2d9d0e4..6976df6 100644 --- a/example/src/gameConfig.tsx +++ b/example/src/gameConfig.tsx @@ -1,6 +1,6 @@ import { createGameConfig, createGridZones, MOVE_ERROR } from 'meepl'; import ChessPieces, { ChessPieceType } from './ChessPieces'; -import { isZoneAvailable } from './Logic'; +import { isZoneAvailable } from './gameLogic'; const zones = createGridZones({ rows: 8, @@ -44,6 +44,16 @@ const moves = { const piece = G.pieces.find((p) => p.id === currPlayer.activePiece); if (!piece) return MOVE_ERROR.INVALID_MOVE; if (isZoneAvailable(zoneId, currPlayer, { G })) { + const pieceOnZone = G.pieces.find((p) => p.currZoneId === zoneId); + // take piece + if (pieceOnZone) { + G.pieces = G.pieces.filter((p) => p.id !== pieceOnZone.id); + player.set({ + ...currPlayer, + takenPieces: [...currPlayer.takenPieces, pieceOnZone], + }); + } + // move piece piece.currZoneId = zoneId; moveSuccessful = true; } @@ -67,7 +77,9 @@ const ChessGame = createGameConfig({ playerView: (players) => players, playerSetup: (playerID) => ({ name: `Player ${playerID}`, + id: playerID, activePiece: null, + takenPieces: [], }), }); diff --git a/example/src/Logic.ts b/example/src/gameLogic.ts similarity index 83% rename from example/src/Logic.ts rename to example/src/gameLogic.ts index f895aa3..10f98f6 100644 --- a/example/src/Logic.ts +++ b/example/src/gameLogic.ts @@ -13,7 +13,7 @@ export function isZoneAvailable(id: string, player: any, args: any): boolean { switch (activePiece.type) { case ChessPieceType.rook: - return isZoneAvailableForRook(currZone, targetZone, G); + return isZoneAvailableForRook(currZone, targetZone, activePlayer, G); default: return false; } @@ -22,6 +22,7 @@ export function isZoneAvailable(id: string, player: any, args: any): boolean { function isZoneAvailableForRook( currZone: ZoneType, targetZone: ZoneType, + activePlayer: any, G: any ): boolean { const { @@ -55,7 +56,13 @@ function isZoneAvailableForRook( gridX < Math.max(currX, targetX) ); }); - if (!isPieceBetween) return true; + if (!isPieceBetween) { + const pieceOnTarget = pieces.find( + ({ currZoneId }) => currZoneId === targetZone.id + ); + if (!pieceOnTarget || pieceOnTarget.owner !== activePlayer.id) + return true; + } } return false; }