Advertisement
Guest User

Untitled

a guest
Dec 20th, 2014
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.65 KB | None | 0 0
  1. class Slactuate
  2. {
  3. /* how to use: build your own abstraction on m() and p() to ease the setting of properties. call u() each frame you update, then call c() to cleanup. */
  4. public var td /*tween datas*/ = new Array<SlacTween>();
  5. public function new(){}
  6. public function u/*update to the given time*/(t : Float) { for (n in td) n.u(t); }
  7. public function c/*clear tweens updated to past their end time, and return the removed ones*/() {
  8. var i = td.length - 1; var r = new Array<SlacTween>(); if (td.length < 1) return r;
  9. while (i >= 0) { var n = td[i]; if (n.ci > 1.) {r.push(td.splice(i, 1)[0]); } i -= 1; }
  10. return r; }
  11. public function m/*make generic tween*/(tt : Float->Void, eg : Easing, st : Float, gt : Float, lo : Float, ho : Float, id : Int) {
  12. var r = new SlacTween(tt, eg, st, gt, lo, ho, id); td.push(r); return r; }
  13. public function p/*property-setting tween*/(tt : Dynamic, py : String, eg : Easing, st : Float, gt : Float, lo : Float, ho : Float, id : Int) {
  14. return m(function(v) { Reflect.setProperty(tt, py, v); }, eg, st, gt, lo, ho, id); }
  15. private static inline function clamp(pct) { return Math.min(1., Math.max(0.,pct)); }
  16. private static inline function lerp/*unclamped*/(pct : Float, lo : Float, ho : Float) : Float { return (ho - lo) * pct + lo; }
  17. public static function linear(pct : Float, lo : Float, ho : Float) : Float { return lerp(clamp(pct), lo, ho); }
  18. public static function smoothstep(pct : Float, lo : Float, ho : Float) : Float { var x = clamp(pct); return lerp(x * x * x * (x * (x * 6 - 15) + 10), lo, ho); }
  19. public static function lutnearest(a : Array<Float>) : Easing { return function(pct : Float, lo : Float, ho : Float):Float {
  20. return lerp(a[Math.round(clamp(pct) * (a.length - 1))], lo, ho); }}
  21. public static function lutlinear(a : Array<Float>) : Easing { return function(pct : Float, lo : Float, ho : Float):Float {
  22. var k = clamp(pct) * (a.length - 1); var i = Std.int(k); (i>=a.length-1) ? return ho : return lerp(lerp(k - i, a[i], a[i + 1]), lo, ho); }}
  23. }
  24. typedef Easing = Float->Float->Float->Float;
  25. class SlacTween
  26. {
  27. public var tt /*target*/ : Float->Void;
  28. public var eg /*easing*/ : Easing;
  29. public var st /*start time*/ : Float;
  30. public var gt /*goal time*/ : Float;
  31. public var lo /*low output*/ : Float;
  32. public var ho /*high output*/ : Float;
  33. public var ci /*current input*/ : Float = 0.;
  34. public var co /*current output*/ : Float = 0.;
  35. public var id /*user id number*/ : Int;
  36. public function new(tt, eg, st, gt, lo, ho, id) {
  37. this.tt = tt; this.eg = eg; this.st = st; this.gt = gt;
  38. this.lo = lo; this.ho = ho; this.id = id; }
  39. public function u/*update*/(t : Float) { ci = (t - st) / (gt - st); co = eg(ci, lo, ho); tt(co); }
  40. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement