コード例 #1
0
    public void Load(SmartPentagram pentagram, float radius, float letterSize)
    {
        Debug.Log("Scroll: Load. Pentagram is null: " + (pentagram == null));
        _pentagram = pentagram;

        int   nLetters         = pentagram.Letters().Length;
        float turningAngle     = 2 * Mathf.PI / nLetters;
        float stepVectorLength = 2 * radius * Mathf.Sin(turningAngle / 2);

        Debug.Log("Radius: " + radius + "\nStep: " + stepVectorLength);

        Vector2 letterPosition = new Vector2(0, radius);
        Vector2 stepVector     = new Vector2(0, -stepVectorLength);

        VectorService.RotateVector(ref stepVector, (turningAngle - Mathf.PI) / 2);

        Debug.Log("Going to place " + nLetters + "letters.");
        _pentaLetters = new PentaLetter[nLetters];
        for (int i = 0; i < nLetters; i++)
        {
            Debug.Log("Placing letter №" + i + ": " + pentagram.Letters()[i]);
            _pentaLetters[i] = _pool.GetLetter();
            _pentaLetters[i].Construct(pentagram.Letters()[i], nLetters);


            RectTransform rt = _pentaLetters[i].GetComponent <RectTransform>();
            rt.SetParent(this.transform);


            rt.SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal, letterSize);
            rt.SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, letterSize);

            rt.anchoredPosition3D = new Vector3(letterPosition.x, letterPosition.y, _letterZ);
            letterPosition        = letterPosition + stepVector;


            _pentaLetters[i].AddDragEndedCallback(TryActivate);
            _pentaLetters[i].AddLetterSelectedCallback(SelectLetter);

            VectorService.RotateVector(ref stepVector, turningAngle);
        }
    }
コード例 #2
0
    private void DrawCurve()
    {
        _lineRenderer.positionCount = 0;

        if (_lettersCount == 1)
        {
            return;
        }

        if (_lettersCount == 2)
        {
            _lineRenderer.positionCount = 2;
            _lineRenderer.SetPosition(0, _lettersPos[0]);
            _lineRenderer.SetPosition(1, _lettersPos[1]);
            return;
        }

        Vector3 prevDir = Vector3.zero;

        for (int segN = 1; segN < _lettersCount; segN++)
        {
            Vector3 curDir = Vector3.zero;

            Vector3 supportingPoint1 = Vector3.zero;
            Vector3 supportingPoint2 = Vector3.zero;

            Vector3 A = _lettersPos[segN - 1];
            Vector3 B = _lettersPos[segN];
            Vector3 C = (segN != _lettersCount - 1) ? _lettersPos[segN + 1] : _lettersPos[segN];


            Vector3 BA           = A - B;
            Vector3 BC           = C - B;
            float   angleBetween = Mathf.Deg2Rad * Vector3.SignedAngle(BC, BA, _rotationAxis);


            float rotationAngle = angleBetween;
            rotationAngle += (rotationAngle >= 0) ? Mathf.PI : -Mathf.PI;
            rotationAngle  = rotationAngle / 2;
            VectorService.RotateVector(ref BC, rotationAngle);

            curDir = BC.normalized;

            float lever1Length = _leversPercentage * BA.magnitude;
            if (lever1Length < _minLeverLength)
            {
                lever1Length = _minLeverLength;
            }

            float lever2Length = _leversPercentage * BC.magnitude;
            if (lever2Length < _minLeverLength)
            {
                lever2Length = _minLeverLength;
            }

            supportingPoint1 = A + ((-1) * lever1Length) * prevDir;
            supportingPoint2 = B + (lever2Length) * curDir;

            prevDir = curDir;

            /*
             * // Drawing supporting zigzag
             * _lineRenderer.positionCount += 3;
             * _lineRenderer.SetPosition (3 * segN, B);
             * _lineRenderer.SetPosition (3 * segN - 1,     supportingPoint2);
             * _lineRenderer.SetPosition (3 * segN - 2, supportingPoint1); Debug.LogWarning(supportingPoint1);
             */


            _lineRenderer.positionCount += _segmentsPerPair;
            for (int i = 0; i < _segmentsPerPair; i++)
            {
                float   t     = (float)i / (float)(_segmentsPerPair - 1);
                Vector3 point = CalculateBezierPoint(t, A, supportingPoint1, supportingPoint2, B);
                _lineRenderer.SetPosition((segN - 1) * _segmentsPerPair + i, point);
            }
        }
    }