Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Slactuate
- {
- /* 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. */
- public var td /*tween datas*/ = new Array<SlacTween>();
- public function new(){}
- public function u/*update to the given time*/(t : Float) { for (n in td) n.u(t); }
- public function c/*clear tweens updated to past their end time, and return the removed ones*/() {
- var i = td.length - 1; var r = new Array<SlacTween>(); if (td.length < 1) return r;
- while (i >= 0) { var n = td[i]; if (n.ci > 1.) {r.push(td.splice(i, 1)[0]); } i -= 1; }
- return r; }
- public function m/*make generic tween*/(tt : Float->Void, eg : Easing, st : Float, gt : Float, lo : Float, ho : Float, id : Int) {
- var r = new SlacTween(tt, eg, st, gt, lo, ho, id); td.push(r); return r; }
- public function p/*property-setting tween*/(tt : Dynamic, py : String, eg : Easing, st : Float, gt : Float, lo : Float, ho : Float, id : Int) {
- return m(function(v) { Reflect.setProperty(tt, py, v); }, eg, st, gt, lo, ho, id); }
- private static inline function clamp(pct) { return Math.min(1., Math.max(0.,pct)); }
- private static inline function lerp/*unclamped*/(pct : Float, lo : Float, ho : Float) : Float { return (ho - lo) * pct + lo; }
- public static function linear(pct : Float, lo : Float, ho : Float) : Float { return lerp(clamp(pct), lo, ho); }
- 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); }
- public static function lutnearest(a : Array<Float>) : Easing { return function(pct : Float, lo : Float, ho : Float):Float {
- return lerp(a[Math.round(clamp(pct) * (a.length - 1))], lo, ho); }}
- public static function lutlinear(a : Array<Float>) : Easing { return function(pct : Float, lo : Float, ho : Float):Float {
- 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); }}
- }
- typedef Easing = Float->Float->Float->Float;
- class SlacTween
- {
- public var tt /*target*/ : Float->Void;
- public var eg /*easing*/ : Easing;
- public var st /*start time*/ : Float;
- public var gt /*goal time*/ : Float;
- public var lo /*low output*/ : Float;
- public var ho /*high output*/ : Float;
- public var ci /*current input*/ : Float = 0.;
- public var co /*current output*/ : Float = 0.;
- public var id /*user id number*/ : Int;
- public function new(tt, eg, st, gt, lo, ho, id) {
- this.tt = tt; this.eg = eg; this.st = st; this.gt = gt;
- this.lo = lo; this.ho = ho; this.id = id; }
- public function u/*update*/(t : Float) { ci = (t - st) / (gt - st); co = eg(ci, lo, ho); tt(co); }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement