예제 #1
0
    private IEnumerator InputUpdate()
    {
        while (true)
        {
            if (Input.GetKeyDown(jumpInput))
            {
                rawTapInputEvent.Dispatch();
            }

            if (Input.GetKeyDown(aimInput) && !InputHelper.CheckUICollision(Input.mousePosition))
            {
                touchState = TouchStates.Tapped;

                startDownTime = Time.time;
            }

            if (touchState != TouchStates.None)
            {
                if (!Input.GetKeyUp(aimInput))
                {
                    if (Time.time - startDownTime > TimebeforeTappedExpired)
                    {
                        if (touchState == TouchStates.Tapped)
                        {
                            rawTappedExpiredInputEvent.Dispatch();
                        }

                        if (Vector2.Distance(Camera.main.ScreenToWorldPoint(Input.mousePosition), transform.position) > minDistFromPlayer)
                        {
                            touchState = TouchStates.Dragging;
                            Vector2 direction = ((Vector2)Camera.main.ScreenToWorldPoint(Input.mousePosition) - (Vector2)transform.position).normalized;
                            rawDraggingInputEvent.Dispatch(direction);
                        }
                        else if (touchState != TouchStates.Holding)
                        {
                            if (touchState == TouchStates.Dragging)
                            {
                                rawCancelDragInputEvent.Dispatch();
                            }

                            touchState = TouchStates.Holding;
                            rawHoldingInputEvent.Dispatch();
                        }
                    }
                }
                else
                {
                    rawReleaseInputEvent.Dispatch();

                    if (touchState != TouchStates.Holding)
                    {
                        Vector2 direction = ((Vector2)Camera.main.ScreenToWorldPoint(Input.mousePosition) - (Vector2)transform.position).normalized;
                        rawReleaseInDirectionInputEvent.Dispatch(direction);
                    }

                    touchState = TouchStates.None;
                }
            }
            yield return(null);
        }
    }
예제 #2
0
    private IEnumerator InputUpdate()
    {
        while (true)
        {
            bool startedTouching = Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began;
            if (startedTouching && !InputHelper.CheckUICollision(Input.GetTouch(0).position))
            {
                TouchState         = TouchStates.Tapped;
                startTouchPosition = Input.GetTouch(0).position;
                touchDownTime      = Time.time;
            }

            if (TouchState != TouchStates.None)
            {
                if (Input.GetTouch(0).phase != TouchPhase.Ended)
                {
                    float timePassedSinceTouchDown = Time.time - touchDownTime;
                    if (timePassedSinceTouchDown > TimebeforeTappedExpired)
                    {
                        if (TouchState == TouchStates.Tapped)
                        {
                            rawTappedExpiredInputEvent.Dispatch();
                        }

                        Vector2 worldCurrentTouchPosition = Camera.main.ScreenToWorldPoint(Input.GetTouch(0).position);
                        Vector2 worldStartTouchPosition   = Camera.main.ScreenToWorldPoint(startTouchPosition);
                        float   dragDistance = Vector2.Distance(worldStartTouchPosition, worldCurrentTouchPosition);

                        if (dragDistance > minimalDistanceToDrag)
                        {
                            joyStick.SetActive(true);
                            joyStick.transform.position = new Vector3(worldStartTouchPosition.x, worldStartTouchPosition.y, -4);

                            Vector2 direction = (Input.GetTouch(0).position - startTouchPosition).normalized;
                            dragDirIndicator.SetDragDir(direction);

                            TouchState = TouchStates.Dragging;
                            rawDraggingInputEvent.Dispatch(direction);
                        }
                        else if (timePassedSinceTouchDown > TimebeforeTappedExpired && TouchState != TouchStates.Holding)
                        {
                            if (TouchState == TouchStates.Dragging)
                            {
                                rawCancelDragInputEvent.Dispatch();
                            }

                            joyStick.SetActive(false);
                            TouchState = TouchStates.Holding;
                            rawHoldingInputEvent.Dispatch();
                        }
                    }
                }
                else
                {
                    rawReleaseInputEvent.Dispatch();

                    if (TouchState == TouchStates.Dragging)
                    {
                        joyStick.SetActive(false);
                        Vector2 direction = (Input.GetTouch(0).position - startTouchPosition).normalized;
                        rawReleaseInDirectionInputEvent.Dispatch(direction);
                    }
                    else if (TouchState == TouchStates.Tapped)
                    {
                        rawJumpInputEvent.Dispatch();
                    }

                    TouchState = TouchStates.None;
                }
            }

            yield return(null);
        }
    }