private void Update()
    {
        if (aiMove)
        {
            board.AIMove(false);
            aiMove = false;
        }

        if (Input.GetMouseButtonDown(0))
        {
            Ray          ray = mainCamera.ScreenPointToRay(Input.mousePosition);
            RaycastHit2D hit = Physics2D.Raycast(ray.origin, new Vector3(0, 0, 1), Mathf.Infinity);
            if (hit.collider != null)
            {
                Vector3    hitPos          = hit.transform.position;
                Vector2Int pos2D           = new Vector2Int((int)hitPos.x, (int)hitPos.y);
                Vector3    illegalPosition = new Vector3(-1, -1, -1);

                if (chosenFigurePosition != illegalPosition && !board.IsCellOccupiedWithFiguresOfPlayerColor(pos2D) && board.IsLegalMove(pos2D))
                {
                    board.MoveFigure(chosenFigurePosition, pos2D);
                    chosenFigurePosition = illegalPosition;
                    aiMove = true;
                    return;
                }

                if (hit.collider.CompareTag("Figure") && board.IsCellOccupiedWithFiguresOfPlayerColor(pos2D))
                {
                    chosenFigurePosition = hitPos;
                    board.HighlightPossibleMoves(pos2D);
                }
            }
        }
    }
Beispiel #2
0
    private void Update()
    {
        if (Input.GetMouseButtonDown(0))
        {
            Ray          ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            RaycastHit2D hit = Physics2D.Raycast(ray.origin, new Vector3(0, 0, 1), Mathf.Infinity);
            if (hit.collider != null)
            {
                Vector2Int pos = new Vector2Int((int)hit.transform.position.x, (int)hit.transform.position.y);

                if (chosenFigure != null && !board.IsCellOccupiedWithFiguresOfPlayerColor(pos) && board.IsLegalMove(pos))
                {
                    board.MoveFigure(chosenFigure.position, pos);
                    chosenFigure = null;
                    playersTurn  = false;
                    return;
                }

                // Maybe can get figure type from the beggining and check if it is not "Empty"
                if (hit.collider.CompareTag("Figure") && board.IsCellOccupiedWithFiguresOfPlayerColor(pos))
                {
                    chosenFigure = hit.collider.transform;
                    board.HighlightPossibleMoves(pos);
                }
            }
        }

        //TODO: Temp
        if (!playersTurn)
        {
            board.AIMove();
            playersTurn = true;
        }
    }