Create macro for setting fields and add joint functions

This commit is contained in:
oakes
2014-01-18 18:21:54 -05:00
parent 2db20471e9
commit 97df99acbf
2 changed files with 53 additions and 25 deletions

View File

@@ -2,8 +2,8 @@
(:require [play-clj.math :as m] (:require [play-clj.math :as m]
[play-clj.utils :as u]) [play-clj.utils :as u])
(:import [com.badlogic.gdx.physics.box2d Body BodyDef ChainShape CircleShape (:import [com.badlogic.gdx.physics.box2d Body BodyDef ChainShape CircleShape
Contact ContactListener EdgeShape Fixture FixtureDef PolygonShape Contact ContactListener EdgeShape Fixture FixtureDef JointDef
Transform World])) PolygonShape Transform World]))
; world ; world
@@ -32,14 +32,11 @@
`~(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")))
(defn body (defmacro body
[& {:keys [] :as options}] [k & options]
(let [body-def (BodyDef.)] `(let [^BodyDef object# (BodyDef.)]
(doseq [[k v] options] (set! (. object# type) (body-type ~k))
(case k (u/fields! object# ~@options)))
:type (set! (. body-def type) v)
(u/throw-key-not-found k)))
body-def))
(defmacro body! (defmacro body!
[entity k & options] [entity k & options]
@@ -51,9 +48,9 @@
(box-2d! screen :create-body body-def)) (box-2d! screen :create-body body-def))
(defmacro create-body! (defmacro create-body!
[screen type-name & options] [screen body-def & options]
`(let [object# (create-body!* ~screen (body :type (body-type ~type-name)))] `(let [^Body object# (create-body!* ~screen ~body-def)]
(u/calls! ^Body object# ~@options))) (u/calls! object# ~@options)))
(defn body-x (defn body-x
[entity] [entity]
@@ -84,20 +81,40 @@
[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
(defmacro joint-init
[k]
`(~(symbol (str u/main-package ".physics.box2d.joints."
(u/key->pascal k) "JointDef."))))
(defmacro joint
[k & options]
`(let [object# (joint-init ~k)]
(u/fields! object# ~@options)))
(defn create-joint!*
[screen joint-def]
(box-2d! screen :create-joint joint-def))
(defmacro create-joint!
[screen joint-def & options]
`(let [object# (create-joint!* ~screen ~joint-def)]
(u/calls! object# ~@options)))
; fixtures ; fixtures
(defn fixture (defn fixture*
[& {:keys [] :as options}] []
(let [fixture-def (FixtureDef.)] (FixtureDef.))
(doseq [[k v] options]
(case k (defmacro fixture
:density (set! (. fixture-def density) v) [& options]
:friction (set! (. fixture-def friction) v) `(let [^FixtureDef object# (fixture*)]
:is-sensor? (set! (. fixture-def isSensor) v) (u/fields! object# ~@options)
:restitution (set! (. fixture-def restitution) v) object#))
:shape (set! (. fixture-def shape) v)
(u/throw-key-not-found k))) ; shapes
fixture-def))
(defn chain* (defn chain*
[] []

View File

@@ -85,6 +85,17 @@
[obj & args] [obj & args]
`(doto ~obj ~@(create-method-calls [] args))) `(doto ~obj ~@(create-method-calls [] args)))
(defn create-field-setters
[obj {:keys [] :as args}]
(map (fn [[k v]]
`(set! (. ~obj ~(symbol (key->camel k))) (eval ~v)))
args))
(defmacro fields!
[obj & args]
`(do ~@(create-field-setters obj args)
~obj))
; data structures ; data structures
(defn gdx-array* (defn gdx-array*