Skip to content

Instantly share code, notes, and snippets.

@selfsame
Created April 3, 2015 20:08
Show Gist options
  • Select an option

  • Save selfsame/ba0a39c8b54cd99d9d6d to your computer and use it in GitHub Desktop.

Select an option

Save selfsame/ba0a39c8b54cd99d9d6d to your computer and use it in GitHub Desktop.
(defmacro deftween [sym props & methods ] ;getter valuer & setter
(let [{:keys [getter valuer setter adder]} (conj default-methods (into {} (map sort-methods methods)))
compsym (symbol (str sym "_tween"))
res-props (concat '[^boolean active ^System.Double delay ^System.Double start ^System.Double duration ^boolean relative ^float ratio ^int uid
getfn addfn easefn easesig] props)
[[this] get-more] getter
[[this-v] value-more] valuer
getterfn (cons 'fn getter)
set-more (or setter (list 'set! get-more value-more))]
`(do
(arcadia.core/defcomponent ~compsym ~res-props
(~'Awake [~this]
(set! (.getfn ~this) ~getterfn)
(set! (.addfn ~this) ~adder)
(set! (.duration ~this) 5.0)
(set! (.start ~this) (UnityEngine.Time/time))
(set! (.value ~this) ~get-more)
(set! (.target ~this) ~get-more))
(~'Update [~this-v]
(if (.active ~this-v)
(do
(if (> (.delay ~this-v) 0.0)
(do (set! (.delay ~this-v) (- (.delay ~this-v) (UnityEngine.Time/deltaTime)))
(when (<= (.delay ~this-v) 0.0)
(set! (.start ~this-v) (- (UnityEngine.Time/time) (.delay ~this-v))))))
(when (<= (.delay ~this-v) 0.0)
(if (> (- (UnityEngine.Time/time) (.start ~this-v)) (.duration ~this-v))
(do (set! (.ratio ~this-v) (float 1.0))
~set-more
(set! (.active ~this-v) false)
(set! (.delay ~this-v)
(- (- (UnityEngine.Time/time) (.start ~this-v))
(.duration ~this-v)))
(tween.core/finish (.uid ~this-v) (.gameObject ~this-v)))
(do
(set! (.ratio ~this-v)
(float ((.easefn ~this)
(/ (- (UnityEngine.Time/time) (.start ~this-v))
(.duration ~this-v)) )))
~set-more)) ))))
(~'OnDestroy [~'this]
(swap! tween.core/REGISTRY dissoc (.uid ~'this))))
(defn ~sym ~'[& more] (apply tween.core/make (cons ~compsym ~'more)))
)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment