Last active
June 1, 2022 02:36
-
-
Save selfsame/4d9d5a0c0698219e37c987fa428747f2 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
(var _ts {}) | |
(fn lerp [a b r] (+ a (* (- b a) r))) | |
(fn vlerp [a b r] (v.vadd a (v.vmul (v.vsub b a) r))) | |
(fn powf [n] (fn [v] (^ v n))) | |
(fn tween [o p v d _] | |
(let [_ (or _ {}) | |
t {:o o :p p :v v :d d :s d | |
:f _.f | |
:l (or _.l lerp) | |
:ei (or _.ei _.e) | |
:eo (or _.eo _.e) | |
:iv (or (and p (. o p)) o)}] | |
(table.insert _ts t) t)) | |
(fn wait [d f] (tween 0 nil 0 d {:f f})) | |
(fn update-tweens [] | |
(local ts {}) | |
(each [_ t (ipairs _ts)] | |
(when t.o | |
(set t.d (- t.d 1)) ;this could be cycles or delta time or whatever your duration is a measure of | |
(var r (- 1 (/ t.d t.s))) | |
(if (and t.ei t.eo) | |
(set r (or (and (< r 0.5) (/ (t.ei (* r 2)) 2)) | |
(- 1 (/ (t.eo (* (- 1 r) 2)) 2)))) | |
t.ei | |
(set r (t.ei r)) | |
t.eo | |
(set r (- 1 (t.eo (- 1 r))))) | |
(var z (t.l t.iv t.v r)) | |
(if t.p (tset t.o t.p z) | |
(set t.o z)) | |
(if (> t.d 0) (table.insert ts t) | |
(if t.f (t.f t.o))))) | |
(set _ts ts)) | |
; (tween object prop_name target_value duration options_table) | |
; options are | |
; :e dual in+out easing fn | |
; :ei,:eo in,out easing fn | |
; :f tween callback (takes 1 argument - the object) | |
; :l lerping function |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment