Clear() public method

public Clear ( ) : void
return void
Esempio n. 1
0
    protected virtual void ConstructCurve(SplineInterpolator interp, SplineNode[] nInfo)
    {
        if (Speed <= 0 && nInfo.Length < 3) return;
        float totalLength = 0;
        float[] curveLengths = new float[nInfo.Length];
        float[] nodeArrivalTimes = new float[nInfo.Length];
        float currTime = 0;
        uint c = 1;
        bool pathEnded = false;

        //Cache the tag so we can reset it after we have calculated how fast the object should move.
        string actualTag = gameObject.tag;

        //Preserve the original position so that the object doesn't get deleted due to the simulation.
        Vector3 originalPosition = gameObject.transform.position;

        gameObject.tag = "Simulation";
        //Swap splines just in case we use mSplineInterp elsewhere.
        interp.StartInterpolation(

        //On Path End.
        () => {
            //Debug.Log("On Path Ended");
            pathEnded = true;

        },
        //On Node Arrival.
        (int idxArrival, SplineNode nodeArrival) => {
            curveLengths[c] = mTotalSegmentSpeed;
            //Debug.Log("SplineController: mTotalSegmentSpeed is " + mTotalSegmentSpeed + "from node " + nInfo[idxArrival - 1].Point + " to " + nodeArrival.Point);
            totalLength += mTotalSegmentSpeed;
            mTotalSegmentSpeed = 0;
            ++c;
        },
        //On Node Callback
        (int idxLeavingSpline, SplineNode OnNodeArrivalCallback) => {
            //Debug.Log("On Node callback: " + idxLeavingSpline);

        }, false, eWrapMode.ONCE);

        //Starts the Simulation.
        float deltaTime = 0.00005f;
        float currentTime = 0f;
        while (!pathEnded) {
            interp.Update(currentTime);
            mTotalSegmentSpeed += mSplineInterp.DistanceTraveled;
            currentTime += deltaTime;
        }
        //Debug.Log("SplineController: totalLength is " + totalLength);
        interp.Clear();

        gameObject.transform.position = originalPosition;
        //From that, evaluate how much distance between each node makes up the curve and scale that time to be the break time.
        float totalTime = GetDuration(nInfo);
        //Debug.Log("SplineController: totalTime is " + totalTime);

        float averageSpeed = totalLength / totalTime;

        float timeToEnd = 0;
        float speedMultiplier = 0;
        for (int i = 0; i < curveLengths.Length; i++)
        {
            float hermiteLengthToEvaluate = curveLengths[i];
            //Debug.Log("SplineController: hermiteLengthToEvaluate is " + hermiteLengthToEvaluate + " and the node is " + nInfo[i].Name);
            if (hermiteLengthToEvaluate > 0) {
                speedMultiplier = (hermiteLengthToEvaluate / totalLength) * (1 / Speed);
                timeToEnd = totalTime * speedMultiplier;
                //Debug.Log("SplineController: timeToEnd is " + timeToEnd);
            }

            interp.AddPoint(nInfo[i].Name, nInfo[i].Point,
                            nInfo[i].Rot,
                            timeToEnd + currTime, 0,
                            new Vector2(0, 1));
            currTime += timeToEnd;
        }
        gameObject.tag = actualTag;
    }
Esempio n. 2
0
    protected virtual void SetupSplineInterpolator(SplineInterpolator interp, SplineNode[] ninfo)
    {
        interp.Clear();

        float currTime = 0;

        List<SplineNode> nInfoAsList = new List<SplineNode>(ninfo);

        for (uint c = 0; c < ninfo.Length; c++)
        {
            if (OrientationMode == eOrientationMode.NODE)
            {
                interp.AddPoint(ninfo[c].Name, ninfo[c].Point,
                                ninfo[c].Rot,
                                currTime, ninfo[c].BreakTime,
                                new Vector2(0, 1));
            }
            else if (OrientationMode == eOrientationMode.TANGENT)
            {
                Quaternion rot;
                Vector3 up = ninfo[c].Rot * Vector3.up;

                if (c != ninfo.Length - 1)		// is c the last point?
                    rot = Quaternion.LookRotation(ninfo[c+1].Point - ninfo[c].Point, up);	// no, we can use c+1
                else if (AutoClose)
                    rot = Quaternion.LookRotation(ninfo[0].Point - ninfo[c].Point, up);
                else
                    rot = ninfo[c].Rot;

                interp.AddPoint(ninfo[c].Name, ninfo[c].Point, rot,
                                currTime, ninfo[c].BreakTime,
                                new Vector2(0, 1));
            }

            // when ninfo[i].StopHereForSecs == 0, then each node of the spline is reached at
            // Time.time == timePerNode * c (so that last node is reached when Time.time == TimeBetweenAdjacentNodes).
            // However, when ninfo[i].StopHereForSecs > 0, then the arrival time of node (i+1)-th needs
            // to account for the stop time of node i-th
            currTime += ninfo[c].BreakTime;

            if ((Speed <= 0 || Application.isEditor) && !Application.isPlaying) {
                currTime += TimeBetweenAdjacentNodes;
            }
        }

        //Debug.Log("SplineController, there are " + eOrientationMode.NODE + " nodes currently");

        //Normalizes the speed by adjusting which times our spline interpolator
        //needs to arrive at a particular node before they are added to the actual
        //interpolator.
        if (Speed > 0 && Application.isPlaying) {
            ConstructCurve(interp, ninfo);
        }

        if (AutoClose)
            interp.SetAutoCloseMode(currTime);
    }