Add docstrings to g2d-physics

This commit is contained in:
oakes
2014-01-20 00:28:19 -05:00
parent 24869fce4e
commit 9cc75ece37
3 changed files with 75 additions and 18 deletions

View File

@@ -11,9 +11,8 @@
(defmacro tiled-map (defmacro tiled-map
"Returns a [TiledMap](http://libgdx.badlogicgames.com/nightlies/docs/api/com/badlogic/gdx/maps/tiled/TiledMap.html) "Returns a [TiledMap](http://libgdx.badlogicgames.com/nightlies/docs/api/com/badlogic/gdx/maps/tiled/TiledMap.html)
(normally, you don't need to use this directly, because the *-tiled-map
(tiled-map \"level1.tmx\") macros that create the renderers will call this themselves)"
"
[s & options] [s & options]
`(u/calls! ^TiledMap (tiled-map* ~s) ~@options)) `(u/calls! ^TiledMap (tiled-map* ~s) ~@options))

View File

@@ -3,6 +3,7 @@
; global ; global
(defn ^:private input-processor (defn ^:private input-processor
"Internal use only"
[{:keys [on-key-down on-key-typed on-key-up on-mouse-moved [{:keys [on-key-down on-key-typed on-key-up on-mouse-moved
on-scrolled on-touch-down on-touch-dragged on-touch-up]} on-scrolled on-touch-down on-touch-dragged on-touch-up]}
execute-fn!] execute-fn!]
@@ -63,10 +64,12 @@
false))) false)))
(defn ^:private gesture-detector (defn ^:private gesture-detector
"Internal use only"
[options execute-fn!] [options execute-fn!]
(proxy [GestureDetector] [(gesture-listener options execute-fn!)])) (proxy [GestureDetector] [(gesture-listener options execute-fn!)]))
(defn ^:private global-listeners (defn ^:private global-listeners
"Internal use only"
[options execute-fn!] [options execute-fn!]
[(input-processor options execute-fn!) [(input-processor options execute-fn!)
(gesture-detector options execute-fn!)]) (gesture-detector options execute-fn!)])
@@ -74,6 +77,7 @@
; ui ; ui
(defn ^:private actor-gesture-listener (defn ^:private actor-gesture-listener
"Internal use only"
[{:keys [on-ui-fling on-ui-long-press on-ui-pan on-ui-pinch [{:keys [on-ui-fling on-ui-long-press on-ui-pan on-ui-pinch
on-ui-tap on-ui-touch-down on-ui-touch-up on-ui-zoom]} on-ui-tap on-ui-touch-down on-ui-touch-up on-ui-zoom]}
execute-fn!] execute-fn!]
@@ -100,12 +104,14 @@
(execute-fn! on-ui-zoom :event e :initial-distance id :distance d)))) (execute-fn! on-ui-zoom :event e :initial-distance id :distance d))))
(defn ^:private change-listener (defn ^:private change-listener
"Internal use only"
[{:keys [on-ui-changed]} execute-fn!] [{:keys [on-ui-changed]} execute-fn!]
(proxy [ChangeListener] [] (proxy [ChangeListener] []
(changed [e a] (changed [e a]
(execute-fn! on-ui-changed :event e :actor a)))) (execute-fn! on-ui-changed :event e :actor a))))
(defn ^:private click-listener (defn ^:private click-listener
"Internal use only"
[{:keys [on-ui-clicked on-ui-enter on-ui-exit [{:keys [on-ui-clicked on-ui-enter on-ui-exit
on-ui-touch-down on-ui-touch-dragged on-ui-touch-up]} on-ui-touch-down on-ui-touch-dragged on-ui-touch-up]}
execute-fn!] execute-fn!]
@@ -125,6 +131,7 @@
(execute-fn! on-ui-touch-up :event e :x x :y y :pointer p :button b)))) (execute-fn! on-ui-touch-up :event e :x x :y y :pointer p :button b))))
(defn ^:private drag-listener (defn ^:private drag-listener
"Internal use only"
[{:keys [on-ui-drag on-ui-drag-start on-ui-drag-stop [{:keys [on-ui-drag on-ui-drag-start on-ui-drag-stop
on-ui-touch-down on-ui-touch-dragged on-ui-touch-up]} on-ui-touch-down on-ui-touch-dragged on-ui-touch-up]}
execute-fn!] execute-fn!]
@@ -144,6 +151,7 @@
(execute-fn! on-ui-drag-stop :event e :x x :y y :pointer p)))) (execute-fn! on-ui-drag-stop :event e :x x :y y :pointer p))))
(defn ^:private focus-listener (defn ^:private focus-listener
"Internal use only"
[{:keys [on-ui-keyboard-focus-changed on-ui-scroll-focus-changed]} [{:keys [on-ui-keyboard-focus-changed on-ui-scroll-focus-changed]}
execute-fn!] execute-fn!]
(proxy [FocusListener] [] (proxy [FocusListener] []
@@ -153,6 +161,7 @@
(execute-fn! on-ui-scroll-focus-changed :event e :actor a :focused? f)))) (execute-fn! on-ui-scroll-focus-changed :event e :actor a :focused? f))))
(defn ^:private ui-listeners (defn ^:private ui-listeners
"Internal use only"
[options execute-fn!] [options execute-fn!]
[(actor-gesture-listener options execute-fn!) [(actor-gesture-listener options execute-fn!)
(change-listener options execute-fn!) (change-listener options execute-fn!)
@@ -163,6 +172,7 @@
; g2d-physics ; g2d-physics
(defn ^:private contact-listener (defn ^:private contact-listener
"Internal use only"
[{:keys [on-begin-contact on-end-contact on-post-solve on-pre-solve]} execute-fn!] [{:keys [on-begin-contact on-end-contact on-post-solve on-pre-solve]} execute-fn!]
(reify ContactListener (reify ContactListener
(beginContact [this c] (beginContact [this c]
@@ -177,6 +187,7 @@
; update functions ; update functions
(defn ^:private update-stage! (defn ^:private update-stage!
"Internal use only"
[{:keys [^Stage renderer ui-listeners]} entities] [{:keys [^Stage renderer ui-listeners]} entities]
(doseq [^Actor a (.getActors renderer)] (doseq [^Actor a (.getActors renderer)]
(.remove a)) (.remove a))
@@ -189,6 +200,7 @@
(add-input! renderer)) (add-input! renderer))
(defn ^:private update-box-2d! (defn ^:private update-box-2d!
"Internal use only"
[{:keys [^World world]} entities] [{:keys [^World world]} entities]
(when-not (.isLocked world) (when-not (.isLocked world)
(let [arr (u/gdx-array [])] (let [arr (u/gdx-array [])]
@@ -205,6 +217,7 @@
(.destroyJoint world joint)))))) (.destroyJoint world joint))))))
(defn ^:private update-screen! (defn ^:private update-screen!
"Internal use only"
([{:keys [world g2dp-listener]}] ([{:keys [world g2dp-listener]}]
(when (isa? (type world) World) (when (isa? (type world) World)
(.setContactListener ^World world g2dp-listener))) (.setContactListener ^World world g2dp-listener)))

View File

@@ -8,6 +8,7 @@
; world ; world
(defn box-2d* (defn box-2d*
"The function version of `box-2d`"
([] ([]
(box-2d* 0 0 true)) (box-2d* 0 0 true))
([gravity-x gravity-y] ([gravity-x gravity-y]
@@ -16,11 +17,16 @@
(World. (m/vector-2 gravity-x gravity-y) sleep?))) (World. (m/vector-2 gravity-x gravity-y) sleep?)))
(defmacro box-2d (defmacro box-2d
"Returns a [World](http://libgdx.badlogicgames.com/nightlies/docs/api/com/badlogic/gdx/physics/box2d/World.html)
(box-2d 0 0)
"
[gravity-x gravity-y & options] [gravity-x gravity-y & options]
`(let [^World object# (box-2d* ~gravity-x ~gravity-y)] `(let [^World object# (box-2d* ~gravity-x ~gravity-y)]
(u/calls! object# ~@options))) (u/calls! object# ~@options)))
(defmacro box-2d! (defmacro box-2d!
"Calls a single method on a `box-2d`"
[screen k & options] [screen k & options]
`(let [^World object# (u/get-obj ~screen :world)] `(let [^World object# (u/get-obj ~screen :world)]
(u/call! object# ~k ~@options))) (u/call! object# ~k ~@options)))
@@ -28,80 +34,103 @@
; bodies ; bodies
(defmacro ^:private body-type (defmacro ^:private body-type
"Internal use only"
[k] [k]
`(symbol (str u/main-package ".physics.box2d.BodyDef$BodyType/" `(symbol (str u/main-package ".physics.box2d.BodyDef$BodyType/"
(u/key->pascal ~k) "Body"))) (u/key->pascal ~k) "Body")))
(defmacro body-def (defmacro body-def
"Returns a [BodyDef](http://libgdx.badlogicgames.com/nightlies/docs/api/com/badlogic/gdx/physics/box2d/BodyDef.html)
(body-def :dynamic)
"
[k & options] [k & options]
`(let [^BodyDef object# (BodyDef.)] `(let [^BodyDef object# (BodyDef.)]
(set! (. object# type) ~(body-type k)) (set! (. object# type) ~(body-type k))
(u/fields! object# ~@options))) (u/fields! object# ~@options)))
(defmacro body! (defmacro body!
"Calls a single method on a body"
[entity k & options] [entity k & options]
`(let [^Body object# (u/get-obj ~entity :body)] `(let [^Body object# (u/get-obj ~entity :body)]
(u/call! object# ~k ~@options))) (u/call! object# ~k ~@options)))
(defn create-body!* (defn create-body!*
"The function version of `create-body!`"
[screen b-def] [screen b-def]
(box-2d! screen :create-body b-def)) (box-2d! screen :create-body b-def))
(defmacro create-body! (defmacro create-body!
"Returns a [Body](http://libgdx.badlogicgames.com/nightlies/docs/api/com/badlogic/gdx/physics/box2d/Body.html)"
[screen b-def & options] [screen b-def & options]
`(let [^Body object# (create-body!* ~screen ~b-def)] `(let [^Body object# (create-body!* ~screen ~b-def)]
(u/calls! object# ~@options))) (u/calls! object# ~@options)))
(defn body-x (defn body-x
"Returns the x position of the body in `entity`"
[entity] [entity]
(. (body! entity :get-position) x)) (. (body! entity :get-position) x))
(defn body-y (defn body-y
"Returns the y position of the body in `entity`"
[entity] [entity]
(. (body! entity :get-position) y)) (. (body! entity :get-position) y))
(defn body-angle (defn body-angle
"Returns the angle of the body in `entity`"
[entity] [entity]
(.getRotation ^Transform (body! entity :get-transform))) (.getRotation ^Transform (body! entity :get-transform)))
(defn body-transform! (defn body-transform!
"Changes the `x`, `y`, and `angle` of the body in `entity`"
[entity x y angle] [entity x y angle]
(body! entity :set-transform x y angle) (body! entity :set-transform x y angle)
entity) entity)
(defn body-x! (defn body-x!
"Changes the `x` of the body in `entity`"
[entity x] [entity x]
(body-transform! entity x (body-y entity) (body-angle entity))) (body-transform! entity x (body-y entity) (body-angle entity)))
(defn body-y! (defn body-y!
"Changes the `y` of the body in `entity`"
[entity y] [entity y]
(body-transform! entity (body-x entity) y (body-angle entity))) (body-transform! entity (body-x entity) y (body-angle entity)))
(defn body-angle! (defn body-angle!
"Changes the `angle` of the body in `entity`"
[entity angle] [entity angle]
(body-transform! entity (body-x entity) (body-y entity) angle)) (body-transform! entity (body-x entity) (body-y entity) angle))
; joints ; joints
(defmacro ^:private joint-init (defmacro ^:private joint-init
"Internal use only"
[k] [k]
`(symbol (str u/main-package ".physics.box2d.joints." `(symbol (str u/main-package ".physics.box2d.joints."
(u/key->pascal ~k) "JointDef."))) (u/key->pascal ~k) "JointDef.")))
(defmacro joint-def (defmacro joint-def
"Returns a [JointDef](http://libgdx.badlogicgames.com/nightlies/docs/api/com/badlogic/gdx/physics/box2d/JointDef.html)
(joint-def :rope)
"
[k & options] [k & options]
`(let [object# (~(joint-init k))] `(let [object# (~(joint-init k))]
(u/fields! object# ~@options))) (u/fields! object# ~@options)))
(defmacro joint! (defmacro joint!
"Calls a single method on a joint"
[object k & options] [object k & options]
`(u/call! ^Joint ~object ~k ~@options)) `(u/call! ^Joint ~object ~k ~@options))
(defn create-joint!* (defn create-joint!*
"The function version of `create-joint`"
[screen j-def] [screen j-def]
(box-2d! screen :create-joint j-def)) (box-2d! screen :create-joint j-def))
(defmacro create-joint! (defmacro create-joint!
"Returns a [Joint](http://libgdx.badlogicgames.com/nightlies/docs/api/com/badlogic/gdx/physics/box2d/Joint.html)"
[screen j-def & options] [screen j-def & options]
`(let [object# (create-joint!* ~screen ~j-def)] `(let [object# (create-joint!* ~screen ~j-def)]
(u/calls! object# ~@options))) (u/calls! object# ~@options)))
@@ -109,30 +138,36 @@
; fixtures ; fixtures
(defmacro fixture-def (defmacro fixture-def
"Returns a [FixtureDef](http://libgdx.badlogicgames.com/nightlies/docs/api/com/badlogic/gdx/physics/box2d/FixtureDef.html)"
[& options] [& options]
`(let [^FixtureDef object# (FixtureDef.)] `(let [^FixtureDef object# (FixtureDef.)]
(u/fields! object# ~@options) (u/fields! object# ~@options)
object#)) object#))
(defmacro fixture! (defmacro fixture!
"Calls a single method on a fixture"
[object k & options] [object k & options]
`(u/call! ^Fixture ~object ~k ~@options)) `(u/call! ^Fixture ~object ~k ~@options))
; shapes ; shapes
(defn chain* (defn chain*
"The function version of `chain`"
[] []
(ChainShape.)) (ChainShape.))
(defmacro chain (defmacro chain
"Returns a [ChainShape](http://libgdx.badlogicgames.com/nightlies/docs/api/com/badlogic/gdx/physics/box2d/ChainShape.html)"
[& options] [& options]
`(u/calls! ^ChainShape (chain*) ~@options)) `(u/calls! ^ChainShape (chain*) ~@options))
(defmacro chain!-shape (defmacro chain-shape!
"Calls a single method on a `chain`"
[object k & options] [object k & options]
`(u/call! ^ChainShape ~object ~k ~@options)) `(u/call! ^ChainShape ~object ~k ~@options))
(defn circle* (defn circle*
"The function version of `circle`"
([] ([]
(CircleShape.)) (CircleShape.))
([radius] ([radius]
@@ -141,64 +176,74 @@
(.setPosition (m/vector-2 radius radius))))) (.setPosition (m/vector-2 radius radius)))))
(defmacro circle (defmacro circle
"Returns a [CircleShape](http://libgdx.badlogicgames.com/nightlies/docs/api/com/badlogic/gdx/physics/box2d/CircleShape.html)"
[radius & options] [radius & options]
`(u/calls! ^CircleShape (circle* ~radius) ~@options)) `(u/calls! ^CircleShape (circle* ~radius) ~@options))
(defmacro circle! (defmacro circle!
"Calls a single method on a `circle`"
[object k & options] [object k & options]
`(u/call! ^CircleShape ~object ~k ~@options)) `(u/call! ^CircleShape ~object ~k ~@options))
(defn edge* (defn edge*
"The function version of `edge`"
[] []
(EdgeShape.)) (EdgeShape.))
(defmacro edge (defmacro edge
"Returns an [EdgeShape](http://libgdx.badlogicgames.com/nightlies/docs/api/com/badlogic/gdx/physics/box2d/EdgeShape.html)"
[& options] [& options]
`(u/calls! ^EdgeShape (edge*) ~@options)) `(u/calls! ^EdgeShape (edge*) ~@options))
(defmacro edge! (defmacro edge!
"Calls a single method on a `edge`"
[object k & options] [object k & options]
`(u/call! ^EdgeShape ~object ~k ~@options)) `(u/call! ^EdgeShape ~object ~k ~@options))
(defn polygon* (defn polygon*
"The function version of `polygon`"
[] []
(PolygonShape.)) (PolygonShape.))
(defmacro polygon (defmacro polygon
"Returns a [PolygonShape](http://libgdx.badlogicgames.com/nightlies/docs/api/com/badlogic/gdx/physics/box2d/PolygonShape.html)"
[& options] [& options]
`(u/calls! ^PolygonShape (polygon*) ~@options)) `(u/calls! ^PolygonShape (polygon*) ~@options))
(defmacro polygon! (defmacro polygon!
"Calls a single method on a `polygon`"
[object k & options] [object k & options]
`(u/call! ^PolygonShape ~object ~k ~@options)) `(u/call! ^PolygonShape ~object ~k ~@options))
; misc ; misc
(defmacro contact! (defmacro contact!
"Calls a single method on a [Contact](http://libgdx.badlogicgames.com/nightlies/docs/api/com/badlogic/gdx/physics/box2d/Contact.html)"
[object k & options] [object k & options]
`(u/call! ^Contact ~object ~k ~@options)) `(u/call! ^Contact ~object ~k ~@options))
(defn find-body (defn find-body
"Returns the first entity in `entities` whose body matches `body`"
[body entities] [body entities]
(some #(if (= body (:body %)) %) entities)) (some #(if (= body (:body %)) %) entities))
(defn first-contact (defn first-body
([screen] "Returns the first body in a contact"
(let [^Contact contact (u/get-obj screen :contact)] [screen]
(assert contact) (let [^Contact contact (u/get-obj screen :contact)]
(-> contact .getFixtureA .getBody))) (assert contact)
([screen entities] (-> contact .getFixtureA .getBody)))
(find-body (first-contact screen) entities)))
(defn second-contact (defn second-body
([screen] "Returns the second body in a contact"
(let [^Contact contact (u/get-obj screen :contact)] [screen]
(assert contact) (let [^Contact contact (u/get-obj screen :contact)]
(-> contact .getFixtureB .getBody))) (assert contact)
([screen entities] (-> contact .getFixtureB .getBody)))
(find-body (second-contact screen) entities)))
(defn step! (defn step!
"Runs the physics simulations for a single frame and optionally returns the
`entities` with their positions updated."
([{:keys [world time-step velocity-iterations position-iterations] ([{:keys [world time-step velocity-iterations position-iterations]
:or {time-step (/ 1 60) velocity-iterations 10 position-iterations 10}}] :or {time-step (/ 1 60) velocity-iterations 10 position-iterations 10}}]
(assert world) (assert world)