/////////////////////////////////////// // #region Private Methods // /////////////////////////////////////// // ShowMoves(): given a BasePiece and Position, display available moves void ShowMoves(BasePiece piece) { Debug.Log("PieceManager::ShowMoves()"); // highlight original position mBoard.HighlightCell(piece.GetCurrentCell().GetBoardPosition(), piece.GetTeamColor(), false); // highlight available moves List <Vector2Int> allMovePositions = GetAllMovePositions(piece); mBoard.HighlightCells(allMovePositions, piece.GetTeamColor(), true); }
// VerifyKingInCheck(): highlight enemy king position if piece is checking enemy king void VerifyKingInCheck(BasePiece piece) { if (IsChecking(piece)) { // highlight enemy king Color teamColor = piece.GetTeamColor(); if (teamColor == Color.white) { foreach (BasePiece p in mBlackPieces) { if (p is King) { Debug.Log("highlight enemy king!"); Vector2Int position = p.GetCurrentCell().GetBoardPosition(); mBoard.HighlightCell(position, p.GetTeamColor(), true); return; } } } else { foreach (BasePiece p in mWhitePieces) { if (p is King) { Debug.Log("highlight enemy king!"); Vector2Int position = p.GetCurrentCell().GetBoardPosition(); mBoard.HighlightCell(position, p.GetTeamColor(), true); return; } } } } }
// InquireCellState() public static CellState InquireCellState(BasePiece inquirerPiece, Cell targetCell) { Debug.Log("Cell::InquireCellState: " + targetCell.GetBoardPosition()); if (!targetCell.GetCurrentPiece()) { return(Cell.CellState.Empty); } else { Color inquirerTeamColor = inquirerPiece.GetTeamColor(); Color targetTeamColor = targetCell.GetCurrentPiece().GetTeamColor(); if (inquirerTeamColor == targetTeamColor) { return(Cell.CellState.Friend); } else { return(Cell.CellState.Enemy); } } }
// Move(): move is an attempt! only place if target cell state is empty or enemy void Move(BasePiece piece, Cell targetCell) { Cell.CellState targetCellState = Cell.InquireCellState(piece, targetCell); if (targetCellState == Cell.CellState.Empty) { Debug.Log("target cell state is: " + targetCellState); // Highlight path to target cell Vector2Int origPosition = piece.GetCurrentCell().GetBoardPosition(); Vector2Int targetPosition = targetCell.GetBoardPosition(); List <Vector2Int> pathPositions = GetPathPositions(origPosition, targetPosition); Color teamColor = piece.GetTeamColor(); mBoard.HighlightCells(pathPositions, teamColor, false); // Move to empty cell Place(piece, targetCell); if (piece is Pawn) { Pawn pawnPiece = (Pawn)piece; pawnPiece.SetFirstMove(false); } // Verify if enemy king in check VerifyKingInCheck(piece); GameManager.Instance.NextTurn(); } else if (targetCellState == Cell.CellState.Friend) { Debug.Log("target cell state is: " + targetCellState); // Return to original cell Place(piece, piece.GetCurrentCell()); } else if (targetCellState == Cell.CellState.Enemy) { Debug.Log("target cell state is: " + targetCellState); // Highlight path to target cell Vector2Int origPosition = piece.GetCurrentCell().GetBoardPosition(); Vector2Int targetPosition = targetCell.GetBoardPosition(); List <Vector2Int> pathPositions = GetPathPositions(origPosition, targetPosition); Color teamColor = piece.GetTeamColor(); mBoard.HighlightCells(pathPositions, teamColor, false); // Maybe not a good idea... looks kinda confusing... // // highlight piece to be captured // mBoard.HighlightCell(targetPosition, targetCell.GetCurrentPiece().GetTeamColor()); // Capture Kill(targetCell.GetCurrentPiece()); // Move to empty cell Place(piece, targetCell); if (piece is Pawn) { Pawn pawnPiece = (Pawn)piece; pawnPiece.SetFirstMove(false); } // Verify if enemy king in check VerifyKingInCheck(piece); GameManager.Instance.NextTurn(); } }