Ejemplo n.º 1
0
    void Update()
    {
        if (!lockScreen)
        {
            // Zoom on mobile with 2 fingers
            if (Input.touchCount == 2)
            {
                isMultiTouching = true;

                if (Input.GetTouch(0).phase == TouchPhase.Began || Input.GetTouch(1).phase == TouchPhase.Began)
                {
                    touchStart = true;
                }

                // Calculate difference between previous distance between 2 fingers and current distance between 2 fingers
                Touch touchZero = Input.GetTouch(0);
                Touch touchOne  = Input.GetTouch(1);

                Vector2 touchZeroPrevPos = touchZero.position - touchZero.deltaPosition;
                Vector2 touchOnePrevPos  = touchOne.position - touchOne.deltaPosition;

                float prevMagnitude    = (touchZeroPrevPos - touchOnePrevPos).magnitude;
                float currentMagnitude = (touchZero.position - touchOne.position).magnitude;

                float difference = currentMagnitude - prevMagnitude;

                // Apply that difference to zoom function
                Zoom(difference * mobilePinchSensitivity);
            }

            // Zoom on PC with scrollwheel
            Zoom(Input.GetAxis("Mouse ScrollWheel") * PCScrollSensitivity);
        }


        // Initialise some values on initial mouse press
        if (Input.GetMouseButtonDown(0))
        {
            if (EventSystem.current.IsPointerOverGameObject())
            {
                interactingWithGUI = true;
            }
            panning       = false;
            touchStartPos = cam.ScreenToWorldPoint(MousePosition());
        }

        if (Input.GetMouseButton(0))
        {
            if (!interactingWithGUI)
            {
                // Find difference between pointer position and camera position
                direction = touchStartPos - Camera.main.ScreenToWorldPoint(MousePosition());

                // If the player lifts the finger that touched the screen first before lifting the other finger, the game would snap to the new first finger position. That is why we need to set the direction to zero briefly.
                if ((isMultiTouching && Input.touchCount < 2) || touchStart)
                {
                    direction       = Vector3.zero;
                    touchStartPos   = cam.ScreenToWorldPoint(MousePosition());
                    isMultiTouching = false;
                    touchStart      = false;
                }
                else
                {
                    // Move the camera
                    if (!lockScreen)
                    {
                        cam.transform.position += direction;
                    }
                }

                if (!lockScreen)
                {
                    // Clamp camera pos to bounded area
                    cam.transform.position = new Vector3(
                        Mathf.Clamp(cam.transform.position.x, -cameraBounds.x, cameraBounds.x),
                        Mathf.Clamp(cam.transform.position.y, -cameraBounds.y, cameraBounds.y),
                        -10f);
                }

                // Check if player is panning or clicking
                if (!panning)
                {
                    panning = direction.magnitude > 0.1f;
                }
            }
        }

        // Select a tile if player wasn't panning and start color animation
        if (Input.GetMouseButtonUp(0))
        {
            if (!panning && !interactingWithGUI)
            {
                Vector2      mousePos = cam.ScreenToWorldPoint(MousePosition());
                RaycastHit2D hit      = Physics2D.Raycast(mousePos, Vector2.zero);
                if (hit)
                {
                    uiController.ActivateBuildMenu(hit);
                }
                else
                {
                    Deselect();

                    if (GameManager.currentEvent != null)
                    {
                        uiController.eventPanel.SetActive(true);
                    }
                }
            }

            interactingWithGUI = false;
        }
    }