コード例 #1
0
    private void OnMouseDown()
    {
        if (hasGameEnded)
        {
            return;
        }

        foreach (ChessSet set in mySets)
        {
            if (set.AmIPromotingAPawn())
            {
                return;
            }
        }

        foreach (ChessPlayer player in myPlayers)
        {
            if (!player.HasGameStarted() && player.GetMyState() == "Active")
            {
                player.GameHasStarted();
            }
        }

        Vector2 currentPosition = GetBoardPosition();
        float   currentX        = currentPosition.x;
        float   currentY        = currentPosition.y;

        foreach (Square square in myBoard.board)
        {
            float squareX = square.transform.position.x;
            float squareY = square.transform.position.y;

            if (!activePiece && square.GetContainedPiece() && squareX == currentX && squareY == currentY)
            {
                activePiece = square.GetContainedPiece();
                activePiece.ResetPromotionState();
                if (activePiece.GetCastling("Either"))
                {
                    activePiece.ResetCastling();
                }

                if (!activePiece.GetMyChessSet().IsMyPlayersTurn())
                {
                    activePiece = null;
                    break;
                }

                activePiece.GetMyChessSet().GetMyEnemyChessSet().RemoveHighlightOfSquares();

                if (!square.IsHighlighted())
                {
                    square.HighlightSquare(isCastling: false);
                }

                if (activePiece.HaveIMoved())
                {
                    activePiece.ResetMovementActivity();
                }

                activePiece.SetShadowVisibility(isCastling: false);

                if (!activePiece.AmIDefendingMyKing() && !activePiece.AmIBlocked() && !activePiece.IsMyKingUnderAttack())
                {
                    activePiece.RecomputePotentialMoves();
                }

                activePiece.ShowPotentialMoves(isCastling: false);
                break;
            }
        }
    }