Ejemplo n.º 1
0
    void Update()
    {
        //Goes through each touch
        foreach (Touch t in Input.touches)
        {
            //If any are over UI, then return
            if (EventSystem.current.IsPointerOverGameObject(t.fingerId))
            {
                return;
            }
        }

        //Returns out if a slider is being dragged
        if (UIManager.Instance.sliderIsBeingDragged)
        {
            return;
        }

        if (inputPhase == InputPhase.Placing)
        {
            //If the player is selecting
            if (inputType.IsHovering())
            {
                //Stores the data from the raycast
                TrackableHit trackableHit;

                //Shoots a ray that can only hit within the bounds of a tracked plane, and if it hits:
                if (Frame.Raycast(inputType.GetCursorPosition().x, inputType.GetCursorPosition().y, TrackableHitFlags.PlaneWithinBounds, out trackableHit))
                {
                    //Create an anchor at the hitPoint
                    Anchor anchor = Session.CreateAnchor(trackableHit.Pose);
                    //Instantiates the chessboard at the hitpoint, as a prefab of the anchor
                    chessBoard = Instantiate(chessBoardPrefab, trackableHit.Pose.position, Quaternion.identity, anchor.transform).transform;

                    //Face chessboard to player
                    chessBoard.LookAt(transform);
                    chessBoard.rotation = Quaternion.Euler(new Vector3(0, chessBoard.rotation.y, 0));

                    //Changes the input phase after the board is placed
                    inputPhase = InputPhase.Playing;

                    //Hides the tracked planes
                    showTrackedPlanes = false;

                    //Sets the move board button to interactable
                    UIManager.Instance.moveBoardButton.interactable = true;
                }
            }

            return;
        }

        else if (inputPhase == InputPhase.Playing)
        {
            //Cast a ray from its mouse position
            Ray ray = cam.ScreenPointToRay(inputType.GetCursorPosition());
            //Temp variable
            RaycastHit hit;
            //If the ray hits
            if (Physics.Raycast(ray, out hit))
            {
                //If it hits a square
                if (hit.collider.tag == "Square")
                {
                    Square selectedSquare = hit.transform.GetComponent <Square>();

                    if (selectedObject != null && inputType.IsSelecting())
                    {
                        //Get the piece script
                        Piece selectedPiece = selectedObject.GetComponent <Piece>();

                        //If the piece is on the team whose turn it is, and it can move to the spot
                        if (selectedPiece.team == TeamManager.Instance.currentPlayingTeam && selectedPiece.Move(selectedSquare.boardPosition))
                        {
                            //Resets the selected piece
                            selectedPiece.UnHighlight();
                            selectedObject = null;
                            hoveredObject  = null;
                            return;
                        }
                    }

                    //If the square has a piece
                    if (selectedSquare.currentPiece != null)
                    {
                        Piece selectedPiece = selectedSquare.currentPiece;
                        //If the player is selecting
                        if (inputType.IsSelecting())
                        {
                            //If the piece belongs to the team whose turn it is
                            if (selectedPiece.team == TeamManager.Instance.currentPlayingTeam)
                            {
                                //If the selected piece is a new piece
                                if (selectedPiece.gameObject != selectedObject && selectedObject != null)
                                {
                                    //Unhighlights the old object
                                    selectedObject.GetComponent <Piece>().UnHighlight();
                                }

                                //Set as selected object
                                selectedObject = selectedPiece.gameObject;
                                //Highlight the squares it can move to
                                selectedPiece.HighlightAllPossibleMoves();
                                //Selects the piece graphically
                                selectedPiece.Select();
                            }
                        }
                        //If the player is hovering and the piece isn't already selected
                        else if (inputType.IsHovering() && !selectedPiece.selected)
                        {
                            if (selectedPiece != (Object)hoveredObject)
                            {
                                if (hoveredObject != null && !hoveredObject.selected)
                                {
                                    hoveredObject.UnHighlight();
                                }
                            }

                            hoveredObject = selectedPiece;
                            hoveredObject.Highlight(Piece.hoverMagnitude);
                        }
                    }
                    else
                    {
                        if (inputType.IsHovering() && !selectedSquare.selected)
                        {
                            if (selectedSquare != (Object)hoveredObject)
                            {
                                if (hoveredObject != null && !hoveredObject.selected)
                                {
                                    hoveredObject.UnHighlight();
                                }
                            }

                            hoveredObject = selectedSquare;
                            hoveredObject.Highlight(Square.hoverHighlightMagnitude);
                        }
                    }
                }
            }
        }


        else if (inputPhase == InputPhase.Moving)
        {
            //If there are 2 fingers on the screen
            if (Input.touchCount > 1)
            {
                //Gets both of the touches
                Touch touch1 = Input.GetTouch(0);
                Touch touch2 = Input.GetTouch(1);

                //Gets the positions of the touches last frame
                Vector2 previousPos1 = touch1.position - touch1.deltaPosition;
                Vector2 previousPos2 = touch2.position - touch2.deltaPosition;

                //Gets the distance between both fingers last frame
                float previousDistance = (previousPos1 - previousPos2).magnitude;
                //Gets the distance between both fingers this frame
                float currentDistance = (touch1.position - touch2.position).magnitude;

                //Gets the change in distance between the 2 fingers in this frame
                float distanceMagnitude = (currentDistance - previousDistance);
                //Multiplies it with scaleSpeed
                float changeAmount = distanceMagnitude * scaleSpeed;

                //What the new scale of the board will be
                Vector3 newScale = chessBoard.localScale + new Vector3(changeAmount, changeAmount, changeAmount);


                //If the new scale is bigger than the max scale, set the scale to the max scale
                if (newScale.x > maxBoardSize)
                {
                    chessBoard.transform.localScale = new Vector3(maxBoardSize, maxBoardSize, maxBoardSize);
                }

                //If the new scale is smaller than the min scale, set the scale to the min scale
                else if (newScale.x < minimumBoardSize)
                {
                    chessBoard.transform.localScale = new Vector3(minimumBoardSize, minimumBoardSize, minimumBoardSize);
                }

                //If the new scale is within the set bounds, set it to the new scale
                else
                {
                    chessBoard.transform.localScale = newScale;
                }
            }

            //If the player is selecting
            else if (inputType.IsHovering())
            {
                //Stores the data from the raycast
                TrackableHit trackableHit;

                //Shoots a ray that can only hit within the bounds of a tracked plane, and if it hits:
                if (Frame.Raycast(inputType.GetCursorPosition().x, inputType.GetCursorPosition().y, TrackableHitFlags.PlaneWithinBounds, out trackableHit))
                {
                    //Moves the chessboard to the selected point
                    chessBoard.position = trackableHit.Pose.position;
                }
            }
        }

        debugText.text = UIManager.Instance.sliderIsBeingDragged.ToString();
    }