Created
March 4, 2015 10:59
-
-
Save joelparsons/d365593bd4f89e316234 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import SpriteKit | |
struct ActionTimingFunction { | |
private static let pi : Float = 3.14159 | |
private static let pi2 : Float = pi / 2.0 | |
static func linear (t : Float) -> Float{ | |
return t | |
} | |
static func easeInQuad(t : Float) -> Float{ | |
return t * t | |
} | |
static func easeOutQuad(t : Float) -> Float{ | |
return t * (2.0 - t) | |
} | |
static func easeInOutQuad(t : Float) -> Float{ | |
if (t < 0.5) { | |
return 2.0 * t * t | |
} else { | |
let f = t - 1.0 | |
return 1.0 - 2.0 * f * f | |
} | |
} | |
static func easeInCubic(t : Float) -> Float{ | |
return t * t * t | |
} | |
static func easeOutCubic(t : Float) -> Float{ | |
let f = t - 1.0 | |
return 1.0 + f * f * f | |
} | |
static func easeInOutCubic(t : Float) -> Float{ | |
if (t < 0.5) { | |
return 4.0 * t * t * t | |
} else { | |
let f = t - 1.0 | |
return 1.0 + 4.0 * f * f * f | |
} | |
} | |
static func easeInQuart(t : Float) -> Float{ | |
return t * t * t * t | |
} | |
static func easeOutQuart(t : Float) -> Float{ | |
let f = t - 1.0 | |
return 1.0 - f * f * f * f | |
} | |
static func easeInOutQuart(t : Float) -> Float{ | |
if (t < 0.5) { | |
return 8.0 * t * t * t * t | |
} else { | |
let f = t - 1.0 | |
return 1.0 - 8.0 * f * f * f * f | |
} | |
} | |
static func easeInQuint(t : Float) -> Float{ | |
return t * t * t * t * t | |
} | |
static func easeOutQuint(t : Float) -> Float{ | |
let f = t - 1.0 | |
return 1.0 + f * f * f * f * f | |
} | |
static func easeInOutQuint(t : Float) -> Float{ | |
if (t < 0.5) { | |
return 16.0 * t * t * t * t * t | |
} else { | |
let f = t - 1.0 | |
return 1.0 + 16.0 * f * f * f * f * f | |
} | |
} | |
static func easeInSine(t : Float) -> Float{ | |
return sinf((t - 1.0) * pi2) + 1.0 | |
} | |
static func easeOutSine(t : Float) -> Float{ | |
return sinf(t * pi2) | |
} | |
static func easeInOutSine(t : Float) -> Float{ | |
return 0.5 * (1.0 - cosf(t * pi)) | |
} | |
static func easeInCirc(t : Float) -> Float{ | |
return 1.0 - sqrtf(1.0 - t * t) | |
} | |
static func easeOutCirc(t : Float) -> Float{ | |
return sqrtf((2.0 - t) * t) | |
} | |
static func easeInOutCirc(t : Float) -> Float{ | |
if (t < 0.5) { | |
return 0.5 * (1.0 - sqrtf(1.0 - 4.0 * t * t)) | |
} else { | |
return 0.5 * sqrt(-4.0 * t * t + 8.0 * t - 3.0) + 0.5 | |
} | |
} | |
static func easeInExpo(t : Float) -> Float{ | |
return (t == 0.0) ? t : powf(2.0, 10.0 * (t - 1.0)) | |
} | |
static func easeOutExpo(t : Float) -> Float{ | |
return (t == 1.0) ? t : 1.0 - powf(2.0, -10.0 * t) | |
} | |
static func easeInOutExpo(t : Float) -> Float{ | |
if (t == 0.0 || t == 1.0) { | |
return t | |
} else if (t < 0.5) { | |
return 0.5 * powf(2.0, 20.0 * t - 10.0) | |
} else { | |
return 1.0 - 0.5 * powf(2.0, -20.0 * t + 10.0) | |
} | |
} | |
static func easeInElastic(t : Float) -> Float{ | |
return sin(13.0 * pi2 * t) * powf(2.0, 10.0 * (t - 1.0)) | |
} | |
static func easeOutElastic(t : Float) -> Float{ | |
return sinf(-13.0 * pi2 * (t + 1.0)) * powf(2.0, -10.0 * t) + 1.0 | |
} | |
static func easeInOutElastic(t : Float) -> Float{ | |
if (t < 0.5) { | |
return 0.5 * sinf(13.0 * pi * t) * powf(2.0, 20.0 * t - 10.0) | |
} else { | |
return 0.5 * sinf(-13.0 * pi * t) * powf(2.0, -20.0 * t + 10.0) + 1.0 | |
} | |
} | |
private static let s : Float = 1.70158 | |
static func easeInBack(t : Float) -> Float{ | |
return ((s + 1.0) * t - s) * t * t | |
} | |
static func easeOutBack(t : Float) -> Float{ | |
let f = 1.0 - t | |
let a = 1.0 - ((s + 1.0) * f - s) * f * f | |
return a | |
} | |
static func easeInOutBack(t : Float) -> Float{ | |
if (t < 0.5) { | |
let f = 2.0 * t | |
return 0.5 * ((s + 1.0) * f - s) * f * f | |
} else { | |
let f = 2.0 * (1.0 - t) | |
return 1.0 - 0.5 * ((s + 1.0) * f - s) * f * f | |
} | |
} | |
static func easeInBackExtreme(t : Float) -> Float{ | |
return (t * t - sinf(t * pi)) * t | |
} | |
static func easeOutBackExtreme(t : Float) -> Float{ | |
let f = 1.0 - t | |
return 1.0 - (f * f - sinf(f * pi)) * f | |
} | |
static func easeInOutBackExtreme(t : Float) -> Float{ | |
if (t < 0.5) { | |
let f = 2.0 * t | |
return 0.5 * (f * f - sinf(f * pi)) * f | |
} else { | |
let f = 2.0 * (1.0 - t) | |
return 1.0 - 0.5 * (f * f - sinf(f * pi)) * f | |
} | |
} | |
static func easeInBounce(t : Float) -> Float{ | |
return 1.0 - easeOutBounce(1.0 - t) | |
} | |
static func easeOutBounce(t : Float) -> Float{ | |
let m : Float = 7.5625 | |
if (t < 1.0 / 2.75) { | |
return m * t * t | |
} else if (t < 2.0 / 2.75) { | |
let t1 = t - 1.5 / 2.75 | |
return m * t1 * t1 + 0.75 | |
} else if (t < 2.5 / 2.75) { | |
let t1 = t - 2.25 / 2.75 | |
return m * t1 * t1 + 0.9375 | |
} else { | |
let t1 = t - 2.625 / 2.75 | |
return m * t1 * t1 + 0.984375 | |
} | |
} | |
static func easeInOutBounce(t : Float) -> Float{ | |
if (t < 0.5) { | |
return 0.5 * easeInBounce(t * 2.0) | |
} else { | |
return 0.5 * easeOutBounce(t * 2.0 - 1.0) + 0.5 | |
} | |
} | |
static func smoothStep(t : Float) -> Float{ | |
return t * t * (3 - 2 * t) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Adapted from
SKTTimingFunctions.m
to work withSKAction.timingFunction
Unfortunately this is broken as actions stop executing as soon as a timing function returns >= 1