Esempio n. 1
0
    public static void AttackPiece(Piece attacker, Piece attacked)
    {
        ChessBoard board = ChessTracker.board;

        if (attacked.isWhite)
        {
            board.WhitePieces.Remove(board.WhitePieces.Find(x => x.Controller.name == attacked.Controller.name));
        }
        else
        {
            board.BlackPieces.Remove(board.BlackPieces.Find(x => x.Controller.name == attacked.Controller.name));
        }

        Destroy(ChessHelper.GetPiece(attacked.position).Controller.gameObject);
        attacker.position = attacked.position;
        attacker.Controller.transform.position = ChessHelper.CalcPosition(attacked.position, attacked);

        ChessHistory.selectedSquare.GetComponent <MeshRenderer>().material = ChessHistory.selectedSquareMat;
        ChessHelper.ResetOldSquares();
        ChessHelper.ResetHistory();
        ChessHelper.ResetOldAttackSquares();
        ChessTracker.isWhiteTurn = !ChessTracker.isWhiteTurn;

        ChessHelper.HandleKingsInCheck();
    }
Esempio n. 2
0
    private void OnMouseDown()
    {
        if (ChessHistory.selectedSquare != null && ChessHistory.selectedPiece.PossibleMoves.Contains(position))
        {
            // Move the pawn to chosen square
            ChessHistory.selectedPiece.position = position;
            ChessHistory.selectedPiece.Controller.transform.position = ChessHelper.CalcPosition(position, ChessHistory.selectedPiece);

            if (ChessHistory.selectedPiece is Pawn)
            {
                Pawn pawn = (Pawn)ChessHistory.selectedPiece;
                pawn.isFirstMove = false;
            }

            ChessTracker.isWhiteTurn = !ChessTracker.isWhiteTurn;
            ChessHistory.selectedSquare.GetComponent <MeshRenderer>().material = ChessHistory.selectedSquareMat;
            ChessHelper.ResetOldSquares();
            ChessHelper.ResetOldAttackSquares();
            ChessHelper.ResetHistory();

            // Calculate if any king is in check or checkmate
            ChessHelper.HandleKingsInCheck();
        }
    }
Esempio n. 3
0
    private void OnMouseDown()
    {
        bool setMatOnCurrentSquare  = true;
        bool displayMoveableSquares = true;

        if (piece.isWhite && !ChessTracker.isWhiteTurn)
        {
            if (ChessHistory.selectedPiece != null && ChessHistory.selectedPiece.PossibleAttacks.Contains(piece.position))
            {
                ChessHelper.AttackPiece(ChessHistory.selectedPiece, piece);
                piece.attacks = new List <Vector2>();
            }

            return;
        }
        else if (!piece.isWhite && ChessTracker.isWhiteTurn)
        {
            if (ChessHistory.selectedPiece != null && ChessHistory.selectedPiece.PossibleAttacks.Contains(piece.position))
            {
                ChessHelper.AttackPiece(ChessHistory.selectedPiece, piece);
                piece.attacks = new List <Vector2>();
            }

            return;
        }


        GameObject square = ChessHelper.GetSquare(piece.position);

        if (ChessHistory.selectedSquare != null)
        {
            // If clicks on same piece
            if (square.GetInstanceID() == ChessHistory.selectedSquare.GetInstanceID())
            {
                square.GetComponent <MeshRenderer>().material = ChessHistory.selectedSquareMat;
                setMatOnCurrentSquare = false;
                ChessHelper.ResetHistory();
                displayMoveableSquares = false;
            }
            else
            {
                ChessHistory.selectedSquare.GetComponent <MeshRenderer>().material = ChessHistory.selectedSquareMat;
            }
        }
        // Removing old moveable squares
        ChessHelper.ResetOldSquares();
        ChessHelper.ResetOldAttackSquares();
        piece.attacks = new List <Vector2>();

        Debug.Log(displayMoveableSquares);
        // Displaying moveable squares
        if (displayMoveableSquares)
        {
            foreach (Vector2 possibleMove in piece.PossibleMoves)
            {
                GameObject go = ChessHelper.PlaceSquare(possibleMove, piece.moveableSquare);

                ChessHistory.moveableSquares.Add(go);
            }
        }

        if (setMatOnCurrentSquare)
        {
            ChessHistory.selectedSquareMat = square.GetComponent <MeshRenderer>().material;
            square.GetComponent <MeshRenderer>().material = piece.selectedMat;
            ChessHistory.selectedSquare = square;
            ChessHistory.selectedPiece  = piece;
        }


        // Display possible attacks
        if (displayMoveableSquares)
        {
            foreach (Vector2 possibleAttack in piece.PossibleAttacks)
            {
                if (ChessHelper.HasPiece(possibleAttack) && ChessHelper.GetPiece(possibleAttack).isWhite != piece.isWhite)
                {
                    GameObject go = ChessHelper.PlaceSquare(possibleAttack, piece.attackSquare);
                    ChessHistory.attackSquares.Add(go);
                }
            }
        }
    }