Exemplo n.º 1
0
    private float _startControlRealTime; //����ʱ��ʱ�䣬�����ж��Ƿ�ʱ�䳬���ٽ�ֵ
    private void Update()
    {
        if (CancelControlNextFrame)//������ֹ���ݣ���������һ֡���ڲ���״̬
        {
            _lastTouchState        = TouchState.Off;
            _controlState          = ControlState.NotControl;
            CancelControlNextFrame = false;
        }

        Vector2    curTouchPos;
        TouchState curTouchState;

        #region ��ȡ���ݲ���

        if (Input.GetKey(KeyCode.Mouse0))                                                        //�����갴��
        {
            curTouchPos   = Input.mousePosition;                                                 //����Ϊ�ٿ���
            curTouchState = Input.touchCount <= 0 ? TouchState.Touched : TouchState.MoreThanOne; //�Ƿ����б��Ĵ���
        }
        else //û������
        {
            if (Input.touchCount == 0)        //Ҳû�д���
            {
                curTouchPos   = Vector2.zero; //����Ĭ��ֵ�������õ���
                curTouchState = TouchState.Off;
            }
            else
            {
                curTouchPos   = Input.touches[0].position;
                curTouchState = Input.touchCount == 1 ? TouchState.Touched : TouchState.MoreThanOne;
            }
        }

        #endregion

        #region ������������

        if (curTouchState == TouchState.Touched)
        {
            //todo:������ȷ�Ļ��������¼�����������������ǰ���Ľ������ڵ�
            var ray = ControlCamera.ScreenPointToRay(curTouchPos);
            Debug.DrawRay(ray.origin, ray.direction * ControlCamera.farClipPlane, Color.red);
            RaycastHit hit;
            if (!Physics.Raycast(ray, out hit, ControlCamera.farClipPlane) || hit.collider != GetComponent <Collider>())
            //��������û�����������߰����IJ����ң�����Ϊδ���ţ����Ż�
            {
                curTouchState = TouchState.Off;
            }
        }

        #endregion

        #region ���²���״̬

        if (_controlState == ControlState.NotControl) //��һ֡��ʼ��������һ֡���ܿ���Ч��
        {
            if (curTouchState == TouchState.Touched)
            {
                _controlState         = ControlState.JustStartControl;
                _startControlPos      = curTouchPos;
                _startControlRealTime = Time.realtimeSinceStartup;
            }
        }
        else if (_controlState == ControlState.JustStartControl) //��һ֡���ڸտ�ʼ����״̬
        {
            if (curTouchState == TouchState.Touched)
            {
                ProcessDrag(_lastTouchPos, curTouchPos); //������������

                if (Vector2.Distance(curTouchPos, _startControlPos) > CriticalClickDistanceInMillimeter ||
                    Time.realtimeSinceStartup - _startControlRealTime > CriticalClickTimeInterval)
                //�Ƿ񳬹��ٽ磬����Drag״̬�����ٿ����ǵ�����
                {
                    _controlState = ControlState.Drag;
                }
            }
            else if (curTouchState == TouchState.Off) //�Ǿ��ǵ�����
            {
                ProcessClick(_startControlPos);       //�����¼�����˿/��˿

                _controlState = ControlState.NotControl;
            }
            else // curTouchState == TouchState.MoreThanOne//˫ָģʽ
            {
                _controlState = ControlState.NotControl;
            }
        }
        else if (_controlState == ControlState.Drag)
        {
            if (curTouchState == TouchState.Touched)
            {
                ProcessDrag(_lastTouchPos, curTouchPos); //������������
            }
            else //��ֹ����
            {
                _controlState = ControlState.NotControl;
            }
        }

        if (_controlState != ControlState.NotControl)
        {
            _lastTouchPos = curTouchPos;
        }
        _lastTouchState = curTouchState;

        #endregion

        if (_controlState == ControlState.JustStartControl || _controlState == ControlState.Drag)
        {
            var dL = (curTouchPos - _lastTouchPos).y * ControlCamera.orthographicSize * 2 / Screen.height;
            WormToControl.LengthenSilk(dL);
        }
    }