/// <summary> /// Adjusts the factor based on the curve type from the given key. /// </summary> public static float AdjustFactor(float factor, SpriterKey key) { switch (key.CurveType) { case SpriterCurveType.Instant: factor = 0.0f; break; case SpriterCurveType.Linear: break; case SpriterCurveType.Quadratic: factor = MathHelper.Bezier(0.0f, key.C1, 1.0f, factor); break; case SpriterCurveType.Cubic: factor = MathHelper.Bezier(0.0f, key.C1, key.C2, 1.0f, factor); break; case SpriterCurveType.Quartic: factor = MathHelper.Bezier(0.0f, key.C1, key.C2, key.C3, 1.0f, factor); break; case SpriterCurveType.Quintic: factor = MathHelper.Bezier(0.0f, key.C1, key.C2, key.C3, key.C4, 1.0f, factor); break; case SpriterCurveType.Bezier: factor = MathHelper.Bezier2D(key.C1, key.C2, key.C3, key.C4, factor); break; } return(factor); }
/// <summary> /// Adjusts the animation time taking into account key curve types. /// </summary> public static float AdjustTime(float targetTime, SpriterKey keyA, SpriterKey keyB, float animationLength) { float nextTime = GetNextTime(keyA, keyB, animationLength); float factor = GetFactor(keyA, keyB, animationLength, targetTime); return(MathHelper.Linear(keyA.Time, nextTime, factor)); }
/// <summary> /// Gets next time for the two keys based on relative keys timeline position. /// </summary> private static float GetNextTime(SpriterKey keyA, SpriterKey keyB, float animationLength) { if (keyA.Time < keyB.Time) { return(keyB.Time); } else if (keyB.Time + animationLength == keyA.Time) { return(keyA.Time + animationLength); } else { return(animationLength); } }
/// <summary> /// Gets the interpolation factor for the two keys. Takes into account key curve types. /// </summary> public static float GetFactor(SpriterKey keyA, SpriterKey keyB, float animationLength, float targetTime) { float timeA = keyA.Time; float timeB = keyB.Time; if (timeA > timeB) { timeB += animationLength; if (targetTime < timeA) { targetTime += animationLength; } } float factor = MathHelper.GetFactor(timeA, timeB, targetTime); factor = AdjustFactor(factor, keyA); return(factor); }