Skip to content

Instantly share code, notes, and snippets.

@selfsame
Created March 11, 2015 20:35

Revisions

  1. selfsame created this gist Mar 11, 2015.
    103 changes: 103 additions & 0 deletions gistfile1.clj
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,103 @@




    (defmacro deftween [sym props getter valuer & setter]
    (let [[start duration value target relative ratio uid] props
    dot-duration (symbol (str "." duration))
    dot-start (symbol (str "." start))
    dot-value (symbol (str "." value))
    [_ [this] get-more] getter
    [_ [this-v] value-more] valuer
    set-more (if (first setter) (first (drop 2 (first setter))) (list 'set! get-more value-more))]
    `(~'arcadia.core/defcomponent ~sym ~props
    (~'Awake [~this]
    (~'set! (~dot-duration ~this) (float 5.0))
    (~'set! (~dot-start ~this) (~'UnityEngine.Time/time))
    (~'set! (~dot-value ~this) ~get-more))
    (~'Update [~this-v]
    ;(set! ~get-more ~value-more)
    (~'set! (~'.ratio ~this-v) (float (~'/ (~'- (~'UnityEngine.Time/time) ~start) ~duration)) )
    (~'if (~'> (~'- (~'UnityEngine.Time/time) ~start) ~duration)
    (~'do (~'finish (~'.uid ~this-v) (~'.gameObject ~this-v))
    )
    ~set-more)
    ))))


    ;(~'. ~'UnityEngine.Component (~'Destroy ~this-v))

    (deftween t-position [^float start ^float duration ^Vector3 value ^Vector3 target ^boolean relative ^float ratio ^int uid]
    (-get [this] (.position (.transform this)))
    (-value [this] (Vector3/Lerp value target
    (/ (- (UnityEngine.Time/time) start) duration))))

    (deftween t-scale [^float start ^float duration ^Vector3 value ^Vector3 target ^boolean _ ^float ratio ^int uid]
    (-get [this] (.localScale (.transform this)))
    (-value [this] (Vector3/Lerp value target
    (/ (- (UnityEngine.Time/time) start) duration))))

    (deftween t-material-color [^float start ^float duration ^Color value ^Color target ^boolean _ ^float ratio ^int uid]
    (-get [this] (.color (.material (.GetComponent this UnityEngine.Renderer))))
    (-value [this] (Color/Lerp value target
    (/ (- (UnityEngine.Time/time) start) duration))))

    (deftween t-material-texture-offset [^float start ^float duration ^Vector2 value ^Vector2 target ^boolean _^float ratio ^int uid]
    (-get [this] (.GetTextureOffset (.material (.GetComponent this UnityEngine.Renderer) "_MainTex")))
    (-value [this] )
    (-set [this] (.SetTextureOffset (.material (.GetComponent this UnityEngine.Renderer))
    "_MainTex"
    (Vector2/Lerp value target
    (/ (- (UnityEngine.Time/time) start) duration)))))


    (def UID (atom 0))
    (def REGISTRY (atom {}))

    (defn finish [uid c]
    (when-let [f (get @REGISTRY uid)]
    (swap! REGISTRY dissoc uid)
    (f c)))


    (defn make
    ([tcomp go duration target]
    (let [pre-c (.GetComponent go tcomp)
    c (or pre-c (add-component go tcomp))]
    (when pre-c (set! (.value c) (.target c)))
    (set! (.uid c) (int (swap! UID inc)))
    (set! (.start c) (float (UnityEngine.Time/time)))
    (set! (.duration c) duration)
    (set! (.duration c) duration)
    (set! (.target c) target) c))
    ([tcomp go duration target callback]
    (let [pre-c (.GetComponent go tcomp)
    c (or pre-c (add-component go tcomp))
    uid (int (swap! UID inc))]
    (when pre-c (set! (.value c) (.target c)))
    (set! (.uid c) uid)
    (set! (.start c) (float (UnityEngine.Time/time)))
    (set! (.duration c) duration)
    (set! (.target c) target)
    (swap! REGISTRY conj {uid callback}) c)))

    (defn pus [go] (make t-position go (float (+ 0.3 (rand 0.5))) (V+ (->v3 go) (Vector3. (rand) (rand) (rand))) pus))
    (defn culur [go] (make t-material-color go (float (rand 7)) (color (rand 1) (rand 1) (rand 1)) culur))
    (defn scule [go] (make t-scale go (float (rand 2)) (V* (Vector3. 1 1 1) (+ 1 (rand 3))) scule))


    (defn test-tweens [dim]
    (clear-cloned)
    (destroy! (find-name "trash"))
    (def trash (create-primitive :sphere))
    (set! (.name trash) "trash")
    (add-component trash t-position)
    (add-component trash t-scale)
    (add-component trash t-material-color)

    (doall (for [x (range dim) y (range dim)]
    (let [n (clone! trash (v* [x 0 y] 2))]
    (pus n)
    (scule n)
    (culur n)
    ))))