Esempio n. 1
0
    void Update()
    {
        if (!active)           // input is active if one or more players have been assigned to it (or game is over)
        {
            return;
        }

        Vector2 mousePosition = (Vector2)viewCamera.ScreenToWorldPoint(Input.mousePosition);

        // Pick up piece
        if (Input.GetKeyDown(KeyCode.Mouse0) && !holdingPiece)
        {
            bool isPlayerMove = false;
            for (int i = 0; i < players.Count; i++)
            {
                if (players[i].isMyMove)
                {
                    isPlayerMove = true;
                    break;
                }
            }

            holdingPiece = TryGetPieceUIAtPoint(mousePosition, out pieceHeld);
            if (holdingPiece && isPlayerMove)
            {
                // highlight legal moves for held piece
                Heap legalMoveHeap = HumanPlayer.legalMoves;
                for (int i = 0; i < legalMoveHeap.Count; i++)
                {
                    HighlightSquare(legalMoveHeap.GetMove(i), pieceHeld.algebraicCoordinate);
                }
            }
        }
        // Let go of piece
        else if (Input.GetKeyUp(KeyCode.Mouse0) && holdingPiece)
        {
            PieceUI dropSquare;
            ChessUI.instance.ResetHighlights();
            if (TryGetPieceUIAtPoint(mousePosition, out dropSquare))
            {
                string algebraicMove = pieceHeld.algebraicCoordinate + dropSquare.algebraicCoordinate;
                for (int i = 0; i < players.Count; i++)
                {
                    players[i].TryMakeMove(algebraicMove);
                }
            }


            pieceHeld.Release();
            holdingPiece = false;
        }
        // Drag piece
        else if (Input.GetKey(KeyCode.Mouse0) && holdingPiece)
        {
            pieceHeld.Move(mousePosition);
        }
    }
Esempio n. 2
0
    void Update()
    {
        if (!active)
        { // input is active if one or more players have been assigned to it (or game is over)
            return;
        }

        Vector3 endPointerPosition = GvrPointerInputModule.CurrentRaycastResult.worldPosition;

        // Vector2 mousePosition = (Vector2)viewCamera.ScreenToWorldPoint (Input.mousePosition);

        // Pick up piece
        // If not holding any Piece and clicked anywhere
        if (!holdingPiece && Input.GetMouseButtonDown(0))
        // if (/*Input.GetKeyDown(KeyCode.Mouse0)*/ Input.GetMouseButtonDown(0) && !holdingPiece)
        {
            ChessUI.instance.ResetHighlights();
            // isPlayerMove just checks if any HumanPlayer has his chance
            // If so then we will highlight the regions
            bool isPlayerMove = false;
            for (int i = 0; i < players.Count; i++)
            {
                if (players[i].isMyMove)
                {
                    isPlayerMove = true;
                    break;
                }
            }

            holdingPiece = TryGetPieceUIAtPoint(endPointerPosition, out pieceHeld);
            // This line improves efficiency
            if (!isThereAPiece)
            {
                holdingPiece = false;
            }
            // Debug.Log("holding piece: " + holdingPiece + " isPlayerMove: " + isP);
            if (holdingPiece && isPlayerMove)
            {
                // highlight legal moves for held piece
                Heap legalMoveHeap = HumanPlayer.legalMoves;
                for (int i = 0; i < legalMoveHeap.Count; i++)
                {
                    HighlightSquare(legalMoveHeap.GetMove(i), pieceHeld.algebraicCoordinate);
                }
            }
        }
        // Let go of piece
        // If holding anything and clicked somewhere
        else if (Input.GetMouseButtonDown(0) && holdingPiece)
        // else if (/*Input.GetKeyUp(KeyCode.Mouse0)*/Input.GetMouseButtonUp(0) && holdingPiece)
        {
            PieceUI dropSquare;
            ChessUI.instance.ResetHighlights();
            if (TryGetPieceUIAtPoint(endPointerPosition, out dropSquare))
            {
                string algebraicMove = pieceHeld.algebraicCoordinate + dropSquare.algebraicCoordinate;
                for (int i = 0; i < players.Count; i++)
                {
                    players[i].TryMakeMove(algebraicMove);
                }
            }
            pieceHeld.Release();
            holdingPiece = false;
        }
        // Drag piece
        // else if (/*Input.GetKey(KeyCode.Mouse0)*/ Input.GetMouseButton(0) && holdingPiece)
        // {
        //    pieceHeld.Move(endPointerPosition);
        // }
    }