Beispiel #1
0
    private void Update()
    {
        if (UiController.Paused || _locked)
        {
            return;
        }
        if (_moving)
        {
            if (_rb2D.velocity.magnitude <= Constants.BallResetSpeed)
            {
                StartCoroutine(CoroutineUtils.Timer(1, Respawn));
            }
            return;
        }

        if (Input.GetMouseButton(0))
        {
            if (!_mouseLastClicked)
            {
                var mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);

                if ((mousePos - Constants.MousePosDepth - transform.position).magnitude <
                    Constants.BallTouchSize)
                {
                    _mouseStartPos = mousePos;
                    _sliding       = true;
                }
            }

            if (_sliding)
            {
                var delta = Camera.main.ScreenToWorldPoint(Input.mousePosition) - _mouseStartPos;

                if (delta.magnitude > Constants.BallMaxMagnitude)
                {
                    delta = delta.normalized * Constants.BallMaxMagnitude;
                }

                if (delta.y > 0)
                {
                    delta.y = 0;
                }

                _rb2D.position = _startPos + delta;

                if (_line != null)
                {
                    Destroy(_line);
                }
                _line = LineFactory.CreateDashedLine(DefaultSprite,
                                                     _startPos + delta + Constants.PredictLineDepth,
                                                     _startPos - delta + Constants.PredictLineDepth,
                                                     Constants.PredictLineColor, Constants.PredictLineThickness,
                                                     Constants.PredictLineDashLength);
            }

            _mouseLastClicked = true;
        }
        else
        {
            if (_mouseLastClicked)
            {
                var force = (_startPos - (Vector3)_rb2D.position) * SpeedFactor;

                Destroy(_line);

                if (force.magnitude >= Constants.BallMinSpeed)
                {
                    _rb2D.velocity = force;
                    _moving        = true;
                }
                else
                {
                    _rb2D.position = _startPos;
                }

                _sliding = false;
            }

            _mouseLastClicked = false;
        }
    }