using System; namespace Otter { /// /// Static class with useful easer functions that can be used by Tweens. /// public static class Ease { const float PI = 3.14159f; const float PI2 = PI / 2; const float B1 = 1 / 2.75f; const float B2 = 2 / 2.75f; const float B3 = 1.5f / 2.75f; const float B4 = 2.5f / 2.75f; const float B5 = 2.25f / 2.75f; const float B6 = 2.625f / 2.75f; /// /// Ease a value to its target and then back. Use this to wrap another easing function. /// public static Func ToAndFro(Func easer) { return t => ToAndFro(easer(t)); } /// /// Ease a value to its target and then back. /// public static float ToAndFro(float t) { return t < 0.5f ? t * 2 : 1 + ((t - 0.5f) / 0.5f) * -1; } /// /// Linear. /// /// Time. /// Eased timescale. public static float Linear(float t) { return t; } /// /// Elastic in. /// /// Time elapsed. /// Eased timescale. public static float ElasticIn(float t) { return (float)(Math.Sin(13 * PI2 * t) * Math.Pow(2, 10 * (t - 1))); } /// /// Elastic out. /// /// Time elapsed. /// Eased timescale. public static float ElasticOut(float t) { if (t == 1) return 1; return (float)(Math.Sin(-13 * PI2 * (t + 1)) * Math.Pow(2, -10 * t) + 1); } /// /// Elastic in and out. /// /// Time elapsed. /// Eased timescale. public static float ElasticInOut(float t) { if (t < 0.5) { return (float)(0.5 * Math.Sin(13 * PI2 * (2 * t)) * Math.Pow(2, 10 * ((2 * t) - 1))); } return (float)(0.5 * (Math.Sin(-13 * PI2 * ((2 * t - 1) + 1)) * Math.Pow(2, -10 * (2 * t - 1)) + 2)); } /// /// Quadratic in. /// /// Time elapsed. /// Eased timescale. public static float QuadIn(float t) { return (float)(t * t); } /// /// Quadratic out. /// /// Time elapsed. /// Eased timescale. public static float QuadOut(float t) { return (float)(-t * (t - 2)); } /// /// Quadratic in and out. /// /// Time elapsed. /// Eased timescale. public static float QuadInOut(float t) { return (float)(t <= .5 ? t * t * 2 : 1 - (--t) * t * 2); } /// /// Cubic in. /// /// Time elapsed. /// Eased timescale. public static float CubeIn(float t) { return (float)(t * t * t); } /// /// Cubic out. /// /// Time elapsed. /// Eased timescale. public static float CubeOut(float t) { return (float)(1 + (--t) * t * t); } /// /// Cubic in and out. /// /// Time elapsed. /// Eased timescale. public static float CubeInOut(float t) { return (float)(t <= .5 ? t * t * t * 4 : 1 + (--t) * t * t * 4); } /// /// Quart in. /// /// Time elapsed. /// Eased timescale. public static float QuartIn(float t) { return (float)(t * t * t * t); } /// /// Quart out. /// /// Time elapsed. /// Eased timescale. public static float QuartOut(float t) { return (float)(1 - (t -= 1) * t * t * t); } /// /// Quart in and out. /// /// Time elapsed. /// Eased timescale. public static float QuartInOut(float t) { return (float)(t <= .5 ? t * t * t * t * 8 : (1 - (t = t * 2 - 2) * t * t * t) / 2 + .5); } /// /// Quint in. /// /// Time elapsed. /// Eased timescale. public static float QuintIn(float t) { return (float)(t * t * t * t * t); } /// /// Quint out. /// /// Time elapsed. /// Eased timescale. public static float QuintOut(float t) { return (float)((t = t - 1) * t * t * t * t + 1); } /// /// Quint in and out. /// /// Time elapsed. /// Eased timescale. public static float QuintInOut(float t) { return (float)(((t *= 2) < 1) ? (t * t * t * t * t) / 2 : ((t -= 2) * t * t * t * t + 2) / 2); } /// /// Sine in. /// /// Time elapsed. /// Eased timescale. public static float SineIn(float t) { if (t == 1) return 1; return (float)(-Math.Cos(PI2 * t) + 1); } /// /// Sine out. /// /// Time elapsed. /// Eased timescale. public static float SineOut(float t) { return (float)(Math.Sin(PI2 * t)); } /// /// Sine in and out /// /// Time elapsed. /// Eased timescale. public static float SineInOut(float t) { return (float)(-Math.Cos(PI * t) / 2 + .5); } /// /// Bounce in. /// /// Time elapsed. /// Eased timescale. public static float BounceIn(float t) { t = 1 - t; if (t < B1) return (float)(1 - 7.5625 * t * t); if (t < B2) return (float)(1 - (7.5625 * (t - B3) * (t - B3) + .75)); if (t < B4) return (float)(1 - (7.5625 * (t - B5) * (t - B5) + .9375)); return (float)(1 - (7.5625 * (t - B6) * (t - B6) + .984375)); } /// /// Bounce out. /// /// Time elapsed. /// Eased timescale. public static float BounceOut(float t) { if (t < B1) return (float)(7.5625 * t * t); if (t < B2) return (float)(7.5625 * (t - B3) * (t - B3) + .75); if (t < B4) return (float)(7.5625 * (t - B5) * (t - B5) + .9375); return (float)(7.5625 * (t - B6) * (t - B6) + .984375); } /// /// Bounce in and out. /// /// Time elapsed. /// Eased timescale. public static float BounceInOut(float t) { if (t < .5) { t = 1 - t * 2; if (t < B1) return (float)((1 - 7.5625 * t * t) / 2); if (t < B2) return (float)((1 - (7.5625 * (t - B3) * (t - B3) + .75)) / 2); if (t < B4) return (float)((1 - (7.5625 * (t - B5) * (t - B5) + .9375)) / 2); return (float)((1 - (7.5625 * (t - B6) * (t - B6) + .984375)) / 2); } t = t * 2 - 1; if (t < B1) return (float)((7.5625 * t * t) / 2 + .5); if (t < B2) return (float)((7.5625 * (t - B3) * (t - B3) + .75) / 2 + .5); if (t < B4) return (float)((7.5625 * (t - B5) * (t - B5) + .9375) / 2 + .5); return (float)((7.5625 * (t - B6) * (t - B6) + .984375) / 2 + .5); } /// /// Circle in. /// /// Time elapsed. /// Eased timescale. public static float CircIn(float t) { return (float)(-(Math.Sqrt(1 - t * t) - 1)); } /// /// Circle out /// /// Time elapsed. /// Eased timescale. public static float CircOut(float t) { return (float)(Math.Sqrt(1 - (t - 1) * (t - 1))); } /// /// Circle in and out. /// /// Time elapsed. /// Eased timescale. public static float CircInOut(float t) { return (float)(t <= .5 ? (Math.Sqrt(1 - t * t * 4) - 1) / -2 : (Math.Sqrt(1 - (t * 2 - 2) * (t * 2 - 2)) + 1) / 2); } /// /// Exponential in. /// /// Time elapsed. /// Eased timescale. public static float ExpoIn(float t) { return (float)(Math.Pow(2, 10 * (t - 1))); } /// /// Exponential out. /// /// Time elapsed. /// Eased timescale. public static float ExpoOut(float t) { if (t == 1) return 1; return (float)(-Math.Pow(2, -10 * t) + 1); } /// /// Exponential in and out. /// /// Time elapsed. /// Eased timescale. public static float ExpoInOut(float t) { if (t == 1) return 1; return (float)(t < .5 ? Math.Pow(2, 10 * (t * 2 - 1)) / 2 : (-Math.Pow(2, -10 * (t * 2 - 1)) + 2) / 2); } /// /// Back in. /// /// Time elapsed. /// Eased timescale. public static float BackIn(float t) { return (float)(t * t * (2.70158 * t - 1.70158)); } /// /// Back out. /// /// Time elapsed. /// Eased timescale. public static float BackOut(float t) { return (float)(1 - (--t) * (t) * (-2.70158 * t - 1.70158)); } /// /// Back in and out. /// /// Time elapsed. /// Eased timescale. public static float BackInOut(float t) { t *= 2; if (t < 1) return (float)(t * t * (2.70158 * t - 1.70158) / 2); t--; return (float)((1 - (--t) * (t) * (-2.70158 * t - 1.70158)) / 2 + .5); } } }