Exemplo n.º 1
0
    // method for moving the chess piece on another cell
    private void MoveChessman(int x, int y)
    {
        if (allowedMoves[x, y])
        {
            Chessman c = Chessmans [x, y];



            if (c != null && c.isWhite != isWhiteTurn)
            {
                // capture the piece

                // if it is a king
                if (c.GetType() == typeof(King))
                {
                    EndGame();
                    return;
                }

                activeChessman.Remove(c.gameObject);
                Destroy(c.gameObject);
            }

            if (x == EnPassantMove [0] && y == EnPassantMove [1])
            {
                // white turn
                if (isWhiteTurn)
                {
                    c = Chessmans [x, y - 1];
                }
                else
                {
                    c = Chessmans [x, y + 1];
                }
                activeChessman.Remove(c.gameObject);
                Destroy(c.gameObject);
            }

            EnPassantMove [0] = -1;
            EnPassantMove [1] = -1;
            if (selectedChessman.GetType() == typeof(Pawn))
            {
                // time for promotion
                // white
                if (y == 7)
                {
                    activeChessman.Remove(selectedChessman.gameObject);
                    Destroy(selectedChessman.gameObject);
                    SpawnChessman(1, x, y, -90);
                    selectedChessman = Chessmans [x, y];
                }
                else if (y == 0)
                {
                    activeChessman.Remove(selectedChessman.gameObject);
                    Destroy(selectedChessman.gameObject);
                    SpawnChessman(7, x, y, -90);
                    selectedChessman = Chessmans [x, y];
                }

                if (selectedChessman.CurrentY == 1 && y == 3)
                {
                    EnPassantMove [0] = x;
                    EnPassantMove [1] = y - 1;
                }
                else if (selectedChessman.CurrentY == 6 && y == 4)
                {
                    EnPassantMove [0] = x;
                    EnPassantMove [1] = y + 1;
                }
            }

            // only case of casteling
            if (c != null && c.isWhite == isWhiteTurn && selectedChessman.GetType() == typeof(King) && c.GetType() == typeof(Rook))
            {
                // lets move the rook and the king in the respective position after casteling
                int currentKingPos = selectedChessman.CurrentX;
                int rookPos        = c.CurrentX;

                int _x = -1;
                int _y = -1;

                // castling to left side of the board
                if (currentKingPos > rookPos)
                {
                    // king pose - 2;
                    // rook pose + 2
                    _x = currentKingPos - 2;


                    if (isWhiteTurn)
                    {
                        _y = 0;
                        activeChessman.Remove(selectedChessman.gameObject);
                        Destroy(selectedChessman.gameObject);
                        SpawnChessman(0, _x, _y, -90);


                        activeChessman.Remove(c.gameObject);
                        Destroy(c.gameObject);
                        SpawnChessman(2, rookPos + 3, _y, -90);
                    }
                    else
                    {
                        _y = 7;
                        activeChessman.Remove(selectedChessman.gameObject);
                        Destroy(selectedChessman.gameObject);
                        SpawnChessman(6, _x, _y, -90);


                        activeChessman.Remove(c.gameObject);
                        Destroy(c.gameObject);
                        SpawnChessman(8, rookPos + 3, _y, -90);
                    }
                }
                else if (currentKingPos < rookPos)
                {
                    // king pose - 2;
                    // rook pose + 2
                    _x = currentKingPos + 2;


                    if (isWhiteTurn)
                    {
                        _y = 0;
                        activeChessman.Remove(selectedChessman.gameObject);
                        Destroy(selectedChessman.gameObject);
                        SpawnChessman(0, _x, _y, -90);


                        activeChessman.Remove(c.gameObject);
                        Destroy(c.gameObject);
                        SpawnChessman(2, rookPos - 2, _y, -90);
                    }
                    else
                    {
                        _y = 7;
                        activeChessman.Remove(selectedChessman.gameObject);
                        Destroy(selectedChessman.gameObject);
                        SpawnChessman(6, _x, _y, -90);


                        activeChessman.Remove(c.gameObject);
                        Destroy(c.gameObject);
                        SpawnChessman(8, rookPos - 2, _y, -90);
                    }
                }
            }

            Chessmans [selectedChessman.CurrentX, selectedChessman.CurrentY] = null;
            selectedChessman.transform.position = GetTileCenter(x, y);
            selectedChessman.SetPosition(x, y);
            Chessmans [x, y] = selectedChessman;
            isWhiteTurn      = !isWhiteTurn;
        }
        // Check status
        if (selectedChessman.Threatened())
        {
            Debug.Log("Check");
        }
        BoardHighlights.Instance.hideHighlights();
        selectedChessman = null;
    }