Last active
August 29, 2015 14:14
-
-
Save timsgardner/7bf4a604427797dae821 to your computer and use it in GitHub Desktop.
def-compget
This file contains hidden or 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
| (defmacro condcast-> [expr xsym & clauses] | |
| (let [exprsym (gensym "exprsym_") | |
| [clauses default] (if (even? (count clauses)) | |
| [clauses nil] | |
| [(butlast clauses) | |
| [:else | |
| `(let [~xsym ~exprsym] | |
| ~(last clauses))]]) | |
| cs (->> clauses | |
| (partition 2) | |
| (mapcat | |
| (fn [[t then]] | |
| `[(instance? ~t ~exprsym) | |
| (let [~(with-meta xsym {:tag t}) ~exprsym] | |
| ~then)])))] | |
| `(let [~exprsym ~expr] | |
| ~(cons 'cond | |
| (concat cs default))))) | |
| ;; working on it | |
| (defmacro gc [gob t] | |
| (let [gobsym (with-meta gob {:tag GameObject})] ;; yeh | |
| `(let [~gobsym ~gob | |
| ^UnityEngine.Component c# (.GetComponent ~gobsym (~'type-args ~t))] | |
| (when-not (destroyed? c#) c#)))) | |
| (defmacro def-compget [name typesym] | |
| (let [objsym (gensym "obj_")] | |
| `(defn ~name ~(with-meta [objsym] {:tag typesym}) | |
| (condcast-> ~objsym ~objsym | |
| ~typesym ~objsym | |
| UnityEngine.GameObject (gc ~objsym ~typesym) | |
| UnityEngine.Component (let [^UnityEngine.GameObject obj2# (.gameObject ~objsym)] | |
| (gc obj2# ~typesym)))))) | |
| ;; (def-compget transform-getit UnityEngine.Transform) | |
| ;; (transform-getit (objects-typed UnityEngine.GameObject)) | |
| ;; => #<Transform Game Object (UnityEngine.Transform)> |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Mrm, not as redundant as it should be. I should incorporate similar code into
get-component. Here's the disassembly of(def-compget transform UnityEngine.Transform):