Пример #1
0
        /// <summary> Gets a value between 0.0f and 1.0f that we should multiply against the XPerSecond or YPerSecond value to know how many units we should move in this Update frame. </summary>
        private float GetPerSecondFactor(Easing[] easings, float currentPercent)
        {
            if (easings == null || !easings.Any())
                return 1.0f;

            Easing? closestEasing = null;
            if (easings[0].StartPercent > currentPercent)
            {
                // We're before the first easing we have, so we'll use the first easing's initial speed.
                return easings[0].StartPerSecondPercent;
            }

            foreach (var easing in easings)
            {
                if (easing.StartPercent <= currentPercent)
                {
                    if (easing.EndPercent >= currentPercent)
                    {
                        //return easing.EndPerSecondPercent;

                        // We're somewhere within this Easing, so let's get the specific percent of the PerSecond value we should be using.
                        // (Current - Start) / (End - Start)
                        // Example: 0.0 to 0.5, current at 0.2 -> (0.2 - 0.0) / (0.5 - 0.0) -> 0.2 / 0.5 = 0.4f
                        // Example: 0.5 to 0.75, current at 0.67 -> (0.67 - 0.5) / (0.75 - 0.5) -> 0.17 / 0.25 = 0.68f
                        // Taking this value gives us how much of the PerSecond we should be using for this round. However, the full PerSecond
                        // is also eased between the Start and End time slot, so we'll use the formula to determine how much of the full PerSecond
                        // we should be using.
                        var perSecondPercent = (currentPercent - easing.StartPercent) / (easing.EndPercent - easing.StartPercent);

                        // The below examples work for Acceleration and Deceleration)
                        //  Accel (0.25 to 0.8 at   0% time): 0.0f * (0.8 - 0.25) + 0.25 = 0.0 *  0.55f + 0.25 =  0.0f   + 0.25 = 0.25  = 25.0% of the YPerSecond for this frame.
                        //        (0.25 to 0.8 at  20% time): 0.2f * (0.8 - 0.25) + 0.25 = 0.2 *  0.55f + 0.25 =  0.11f  + 0.25 = 0.36  = 36.0% of the YPerSecond for this frame.
                        //        (0.25 to 0.8 at  50% time): 0.5f * (0.8 - 0.25) + 0.25 = 0.5 *  0.55f + 0.25 =  0.275f + 0.25 = 0.525 = 52.5% of the YPerSecond for this frame.
                        //        (0.25 to 0.8 at  80% time): 0.8f * (0.8 - 0.25) + 0.25 = 0.8 *  0.55f + 0.25 =  0.44f  + 0.25 = 0.69  = 69.0% of the YPerSecond for this frame.
                        //        (0.25 to 0.8 at 100% time): 1.0f * (0.8 - 0.25) + 0.25 = 1.0 *  0.55f + 0.25 =  0.55f  + 0.25 = 0.8   = 80.0% of the YPerSecond for this frame.
                        // Deccel (0.8 to 0.25 at   0% time): 0.0f * (0.25 - 0.8) + 0.8  = 0.0 * -0.55f + 0.8  =  0.0f   + 0.8  = 0.8   = 25.0% of the YPerSecond for this frame.
                        //        (0.8 to 0.25 at  20% time): 0.2f * (0.25 - 0.8) + 0.8  = 0.2 * -0.55f + 0.8  = -0.11f  + 0.8  = 0.69  = 69.0% of the YPerSecond for this frame.
                        //        (0.8 to 0.25 at  50% time): 0.5f * (0.25 - 0.8) + 0.8  = 0.5 * -0.55f + 0.8  = -0.275f + 0.8  = 0.525 = 52.5% of the YPerSecond for this frame.
                        //        (0.8 to 0.25 at  80% time): 0.8f * (0.25 - 0.8) + 0.8  = 0.8 * -0.55f + 0.8  = -0.44f  + 0.8  = 0.36  = 36.0% of the YPerSecond for this frame.
                        //        (0.8 to 0.25 at 100% time): 1.0f * (0.25 - 0.8) + 0.8  = 1.0 * -0.55f + 0.8  = -0.55f  + 0.8  = 0.25  = 25.0% of the YPerSecond for this frame.
                        return (perSecondPercent * (easing.EndPerSecondPercent - easing.StartPerSecondPercent)) + easing.StartPerSecondPercent;
                    }
                    else
                    {
                        // We're beyond the End of the easing, so this will be our new 'Closest' one to where we're at.
                        closestEasing = easing;
                    }
                }
            }

            // This easing is the last one where we passed the End but didn't fall into another bucket, so we'll go at the max of this easing bracket, if any. Otherwise, max speed.
            if (closestEasing.HasValue)
            {
                return closestEasing.Value.EndPerSecondPercent;
            }
            return 1.0f;
        }