// Place(): place a base on a cell void Place(BasePiece piece, Cell targetCell) { // no need to clear when placing piece on board for the first time if (piece.GetCurrentCell()) { piece.GetCurrentCell().ClearCurrentPiece(); } piece.ClearCurrentCell(); piece.transform.position = targetCell.transform.position; piece.SetCurrentCell(targetCell); targetCell.SetCurrentPiece(piece); }
/////////////////////////////////////// // #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); }
// Kill(): void Kill(BasePiece piece) { Debug.Log("PieceManager::Kill() " + piece); // clean up cell reference if (piece.GetCurrentCell()) { piece.GetCurrentCell().ClearCurrentPiece(); } // clean up base piece reference piece.ClearCurrentCell(); // Don't destory them, because we can reuse them for the next game -> deactivate instead // Destroy(piece.gameObject); piece.gameObject.SetActive(false); // game is over when king is killed if (piece is King) { GameManager.Instance.GameOver(); } }
// GetAllMovePositions(): given a BasePiece and a Cell, return list of all available move positions List <Vector2Int> GetAllMovePositions(BasePiece piece) { List <Vector2Int> result = new List <Vector2Int>(); Cell currentCell = piece.GetCurrentCell(); for (int i = 0; i < piece.GetMoveNum(); ++i) { List <Vector2Int> moveCellPositions = GetMovePositions(piece.GetMoveTypes()[i], piece.GetMoveSteps()[i], currentCell, piece.GetFrontDirection()); moveCellPositions.ForEach(position => result.Add(position)); } return(result); }
// DoEndDrag(): Move cells to target cells or return to original position! public void DoEndDrag(BasePiece piece, PointerEventData eventData) { Debug.Log("PieceManager::DoEndDrag()"); // Unhighlight previous available moves mBoard.UnHighlightCells(); if (targetCell) { Move(piece, targetCell); } else { // Return to original cell Place(piece, piece.GetCurrentCell()); } }
// 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(); } }