Exemplo n.º 1
0
    //this is the function that allows piece selection   0 = white  1 = black
    public void onPieceClick(int lastSelectedColor)
    {
        piecemovScript.resetSquares();

        Piece capturedPiece = null;

        //if there wasn't a selected gameobject
        if (piecemovScript.selectedPiece == null)
        {
            piecemovScript.selectedPiece = transform.gameObject;
            groundRend.enabled           = true;
            //stop execution in this case
        }

        // set lastest piece render to false (unselect) only of there is one selected and is not the same as clicked
        else
        {
            piecemovScript.selectedPiece.transform.GetChild(0).GetComponent <Renderer>().enabled = false;
            piecemovScript.selectedPiece = transform.gameObject;
            groundRend.enabled           = true;
        }

        piecemovScript.disableTexts();
        ////////////////////////////////////////////////////
        //the square selection mechanics is implemented here
        ////////////////////////////////////////////////////

        /////////////
        //  PAWN
        ////////////
        if (pieceTp == PieceType.pawn)
        {
            // loop accross the squares
            foreach (GameObject sq in piecemovScript.squares)
            {
                float distToSquare = (transform.position - sq.transform.position).magnitude;
                float angle        = 0;

                //check movement of the pawn in function of the color
                if (color == 0)
                {
                    angle = Vector3.Angle(transform.forward, -(sq.transform.position - piecemovScript.selectedPiece.transform.position));
                }
                else if (color == 1)
                {
                    angle = Vector3.Angle(transform.forward, (sq.transform.position - piecemovScript.selectedPiece.transform.position));
                }

                //movement is posible if can move forward and is not blocked
                // or if there is a opponent piece at the diagonal square

                capturedPiece = GetOccupiedDiffCol(sq);

                if (((Mathf.Abs(distToSquare - distance) < th && angle < 0.5f) && (capturedPiece == null && IsOccupied_sameCol(sq) == false) ||
                     (Mathf.Abs(distToSquare - distance * Mathf.Sqrt(2)) < th && (Mathf.Abs(angle - 45) < 0.5f)) &&
                     capturedPiece != null
                     ) && IsBlocked(sq) == false && captured == false)
                {
                    //the square is activated and can be selected
                    //set the captured piece

                    sq.GetComponent <ChessSquare>().active(capturedPiece);
                }
            }
        }

        /////////////
        //  knight
        ////////////

        else if (pieceTp == PieceType.knight)
        {
            // loop accross the squares
            foreach (GameObject sq in piecemovScript.squares)
            {
                float distToSquare = (transform.position - sq.transform.position).magnitude;

                //Debug.Log(distToSquare);
                capturedPiece = GetOccupiedDiffCol(sq);
                if ((Mathf.Abs(distToSquare - distance) < th) && IsOccupied_sameCol(sq) == false && IsBlocked(sq) == false && captured == false)
                {
                    sq.GetComponent <ChessSquare>().active(capturedPiece);
                }
            }
        }

        /////////////
        //  bishop
        ////////////
        else if (pieceTp == PieceType.bishop)
        {
            foreach (GameObject sq in piecemovScript.squares)
            {
                //check if a piece is on the square


                float angle = Vector3.Angle(transform.forward, -(sq.transform.position - piecemovScript.selectedPiece.transform.position));
                capturedPiece = GetOccupiedDiffCol(sq);

                if ((Mathf.Abs(angle - 45) < 0.5f ||
                     Mathf.Abs(angle - 135) < 0.5f ||
                     Mathf.Abs(angle - 225) < 0.5f ||
                     Mathf.Abs(angle - 315) < 0.5f) && IsOccupied_sameCol(sq) == false && IsBlocked(sq) == false && captured == false)
                {
                    sq.GetComponent <ChessSquare>().active(capturedPiece);
                }
            }
        }
        /////////////
        //  rock
        ////////////
        else if (pieceTp == PieceType.rock)
        {
            foreach (GameObject sq in piecemovScript.squares)
            {
                float angle = Vector3.Angle(transform.forward, (sq.transform.position - piecemovScript.selectedPiece.transform.position));
                capturedPiece = GetOccupiedDiffCol(sq);

                if ((angle < 0.5f || Mathf.Abs(angle - 90) < 0.5f || Mathf.Abs(angle + 90) < 0.5f || Mathf.Abs(angle - 180) < 0.5f) && IsOccupied_sameCol(sq) == false && IsBlocked(sq) == false && captured == false)
                {
                    sq.GetComponent <ChessSquare>().active(capturedPiece);
                }
            }
        }

        ////////////
        // QUEEN
        ///////////
        else if (pieceTp == PieceType.queen)
        {
            foreach (GameObject sq in piecemovScript.squares)
            {
                float angle = Vector3.Angle(transform.forward, (sq.transform.position - piecemovScript.selectedPiece.transform.position));
                capturedPiece = GetOccupiedDiffCol(sq);
                if ((Mathf.Abs(angle - 45) < 0.5f ||
                     Mathf.Abs(angle - 135) < 0.5f ||
                     Mathf.Abs(angle - 225) < 0.5f ||
                     Mathf.Abs(angle - 315) < 0.5f ||
                     angle < 0.5f || Mathf.Abs(angle - 90) < 0.5f || Mathf.Abs(angle + 90) < 0.5f || Mathf.Abs(angle - 180) < 0.5f) && IsOccupied_sameCol(sq) == false && IsBlocked(sq) == false && captured == false)
                {
                    sq.GetComponent <ChessSquare>().active(capturedPiece);
                }
            }
        }

        ////////////
        // king
        ///////////
        else if (pieceTp == PieceType.king)
        {
            foreach (GameObject sq in piecemovScript.squares)
            {
                capturedPiece = GetOccupiedDiffCol(sq);
                float distToSquare = (transform.position - sq.transform.position).magnitude;

                if ((Mathf.Abs(distToSquare - distance) < th || Mathf.Abs(distToSquare - distance * Mathf.Sqrt(2)) < th) && IsOccupied_sameCol(sq) == false &&
                    IsBlocked(sq) == false &&
                    piecemovScript.CheckCheck(color, sq.transform) == false && captured == false)
                {
                    sq.GetComponent <ChessSquare>().active(capturedPiece);
                }
            }
        }
    }