| 1 | using System; |
| 2 | using System.Collections; |
| 3 | using System.Collections.Generic; |
| 4 | |
| 5 | /** |
| 6 | * Easing |
| 7 | * Created by: Tim Falken |
| 8 | * |
| 9 | * Contains default eases for use with the Tweening system. |
| 10 | * Can be used separately, for example: |
| 11 | * Easing.EaseInQuad(0.3f); |
| 12 | * |
| 13 | * Functies aangepast van een javascript versie van http://robertpenner.com/easing/ |
| 14 | **/ |
| 15 | |
| 16 | public static class Easing |
| 17 | { |
| 18 | /** |
| 19 | *Delegate function used for making eases. |
| 20 | *Created as, for example: |
| 21 | * (t) => |
| 22 | * { |
| 23 | * return t; |
| 24 | * }; |
| 25 | **/ |
| 26 | public delegate float Ease(float t); |
| 27 | |
| 28 | /** |
| 29 | * Experimental |
| 30 | * Chains eases to play one after another |
| 31 | **/ |
| 32 | public static Ease Combine(Ease[] eases) |
| 33 | { |
| 34 | return (t) => |
| 35 | { |
| 36 | if(t == 0) |
| 37 | { |
| 38 | return 0; |
| 39 | } |
| 40 | |
| 41 | int index = (int)Math.Round(t * (eases.Length - 1)); |
| 42 | |
| 43 | float stepSize = 1.0f / (float)eases.Length; |
| 44 | |
| 45 | float t2 = t % stepSize; |
| 46 | |
| 47 | float step = eases[index](t2 / stepSize) / (float)eases.Length; |
| 48 | |
| 49 | return ((float)index / (float)eases.Length) + step; |
| 50 | }; |
| 51 | } |
| 52 | |
| 53 | public static Ease Merge(Ease ease1, Ease ease2) |
| 54 | { |
| 55 | return (t) => |
| 56 | { |
| 57 | if(t == 0) |
| 58 | { |
| 59 | return 0; |
| 60 | } |
| 61 | |
| 62 | float r1 = ease1(t); |
| 63 | float r2 = ease2(t); |
| 64 | |
| 65 | return r1 + ((r2 - r1) * t); |
| 66 | }; |
| 67 | } |
| 68 | |
| 69 | public static Ease Linear |
| 70 | { |
| 71 | get |
| 72 | { |
| 73 | return (t) => |
| 74 | { |
| 75 | return t; |
| 76 | }; |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | public static Ease EaseInQuad |
| 81 | { |
| 82 | get |
| 83 | { |
| 84 | return (t) => |
| 85 | { |
| 86 | return t * t; |
| 87 | }; |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | public static Ease EaseOutQuad |
| 92 | { |
| 93 | get |
| 94 | { |
| 95 | return (t) => |
| 96 | { |
| 97 | return t * (2 - t); |
| 98 | }; |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | public static Ease EaseInOutQuad |
| 103 | { |
| 104 | get |
| 105 | { |
| 106 | return (t) => |
| 107 | { |
| 108 | if ((t /= 0.5f) < 1) |
| 109 | { |
| 110 | return 0.5f * t * t; |
| 111 | } |
| 112 | |
| 113 | return -0.5f * ((--t) * (t - 2) - 1); |
| 114 | }; |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | public static Ease EaseInCubic |
| 119 | { |
| 120 | get |
| 121 | { |
| 122 | return (t) => |
| 123 | { |
| 124 | return t * t * t; |
| 125 | }; |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | public static Ease EaseOutCubic |
| 130 | { |
| 131 | get |
| 132 | { |
| 133 | return (t) => |
| 134 | { |
| 135 | return (--t) * t * t + 1; |
| 136 | }; |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | public static Ease EaseInOutCubic |
| 141 | { |
| 142 | get |
| 143 | { |
| 144 | return (t) => |
| 145 | { |
| 146 | return t < 0.5f ? 4 * t * t * t : (t - 1) * (2 * t - 2) * (2 * t - 2) + 1; |
| 147 | }; |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | public static Ease EaseInElastic |
| 152 | { |
| 153 | get |
| 154 | { |
| 155 | return (t) => |
| 156 | { |
| 157 | if (t == 0) |
| 158 | { |
| 159 | return 0; |
| 160 | } |
| 161 | |
| 162 | return (t == 0? 0 : (0.04f - 0.04f / t)) * (float)Math.Sin (25 * t) + 1; |
| 163 | }; |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | public static Ease EaseOutElastic |
| 168 | { |
| 169 | get |
| 170 | { |
| 171 | return (t) => |
| 172 | { |
| 173 | if (t == 1) |
| 174 | { |
| 175 | return 1; |
| 176 | } |
| 177 | |
| 178 | return 0.04f * t / (--t) * (float)Math.Sin (25 * t); |
| 179 | }; |
| 180 | } |
| 181 | } |
| 182 | |
| 183 | public static Ease EaseInOutElastic |
| 184 | { |
| 185 | get |
| 186 | { |
| 187 | return (t) => |
| 188 | { |
| 189 | if(t == 0) |
| 190 | { |
| 191 | return 0; |
| 192 | } |
| 193 | |
| 194 | return (t -= 0.5f) < 0 ? (0.02f + 0.01f / t) * (float)Math.Sin (50 * t) : (0.02f - 0.01f / t) * (float)Math.Sin (50 * t) + 1; |
| 195 | }; |
| 196 | } |
| 197 | } |
| 198 | |
| 199 | public static Ease EaseInBack |
| 200 | { |
| 201 | get |
| 202 | { |
| 203 | return (t) => |
| 204 | { |
| 205 | float s = 1.70158f; |
| 206 | return t * t * ((s + 1) * t - s); |
| 207 | }; |
| 208 | } |
| 209 | } |
| 210 | |
| 211 | public static Ease EaseOutBack |
| 212 | { |
| 213 | get |
| 214 | { |
| 215 | return (t) => |
| 216 | { |
| 217 | float s = 1.70158f; |
| 218 | return ((t = t - 1) * t * ((s + 1) * t + s) + 1); |
| 219 | }; |
| 220 | } |
| 221 | } |
| 222 | |
| 223 | public static Ease EaseInOutBack |
| 224 | { |
| 225 | get |
| 226 | { |
| 227 | return (t) => |
| 228 | { |
| 229 | float s = 1.70158f; |
| 230 | |
| 231 | if ((t /= 0.5f) < 1) |
| 232 | { |
| 233 | return 0.5f * (t * t * (((s *= 1.525f) + 1) * t - s)); |
| 234 | } |
| 235 | |
| 236 | return 0.5f * ((t -= 2.0f) * t * (((s *= 1.525f) + 1.0f) * t + s) + 2.0f); |
| 237 | }; |
| 238 | } |
| 239 | } |
| 240 | |
| 241 | public static Ease EaseInBounce |
| 242 | { |
| 243 | get |
| 244 | { |
| 245 | return (t) => |
| 246 | { |
| 247 | return 1 - EaseOutBounce(1 - t); |
| 248 | }; |
| 249 | } |
| 250 | } |
| 251 | |
| 252 | public static Ease EaseOutBounce |
| 253 | { |
| 254 | get |
| 255 | { |
| 256 | return (t) => |
| 257 | { |
| 258 | if (t < (1 / 2.75f)) |
| 259 | { |
| 260 | return (7.5625f * t * t); |
| 261 | } |
| 262 | else if (t < (2 / 2.75f)) |
| 263 | { |
| 264 | return (7.5625f * (t -= (1.5f / 2.75f)) * t + 0.75f); |
| 265 | } |
| 266 | else if (t < (2.5f / 2.75f)) |
| 267 | { |
| 268 | return (7.5625f * (t -= (2.25f / 2.75f)) * t + 0.9375f); |
| 269 | } |
| 270 | else |
| 271 | { |
| 272 | return (7.5625f * (t -= (2.625f / 2.75f)) * t + 0.984375f); |
| 273 | } |
| 274 | }; |
| 275 | } |
| 276 | } |
| 277 | |
| 278 | public static Ease EaseInOutBounce |
| 279 | { |
| 280 | get |
| 281 | { |
| 282 | return (t) => |
| 283 | { |
| 284 | if(t < 0.5f) |
| 285 | { |
| 286 | return EaseInBounce(t * 2) * 0.5f; |
| 287 | } |
| 288 | |
| 289 | return EaseOutBounce(t * 2 - 1) * 0.5f + 0.5f; |
| 290 | }; |
| 291 | } |
| 292 | } |
| 293 | } |