From 8f97e02a1b76da29d66e78ca0772669e6d650d69 Mon Sep 17 00:00:00 2001 From: oakes Date: Mon, 6 Jan 2014 13:23:19 -0500 Subject: [PATCH] Standardize all entities as hash maps --- src/play_clj/core.clj | 14 ++++++++++++++ src/play_clj/core_2d.clj | 33 ++++++++++++++++++--------------- 2 files changed, 32 insertions(+), 15 deletions(-) diff --git a/src/play_clj/core.clj b/src/play_clj/core.clj index f18bad1..a752910 100644 --- a/src/play_clj/core.clj +++ b/src/play_clj/core.clj @@ -17,6 +17,20 @@ [com.badlogic.gdx.scenes.scene2d Actor Stage] [com.badlogic.gdx.scenes.scene2d.ui Label Label$LabelStyle])) +(defmulti create-entity class) + +(defmethod create-entity TextureRegion + [obj] + {:type :image :object obj :x 0 :y 0}) + +(defmethod create-entity Actor + [obj] + {:type :actor :object obj :x 0 :y 0}) + +(defn replace-entity + [old-entity new-entity] + (assoc old-entity :object (:object new-entity))) + (load "core_2d") (load "core_global") (load "core_render") diff --git a/src/play_clj/core_2d.clj b/src/play_clj/core_2d.clj index a4673c1..816ed69 100644 --- a/src/play_clj/core_2d.clj +++ b/src/play_clj/core_2d.clj @@ -15,22 +15,23 @@ (.getSpriteBatch renderer)) (defn draw-actor! - [^SpriteBatch batch {:keys [^Actor actor] :as entity}] - (.draw ^Actor actor batch 1)) + [^SpriteBatch batch {:keys [^Actor object] :as entity}] + (.draw ^Actor object batch 1)) (defn draw-image! - [^SpriteBatch batch {:keys [^TextureRegion image x y width height]}] - (.draw batch image (float x) (float y) (float width) (float height))) + [^SpriteBatch batch {:keys [^TextureRegion object x y width height]}] + (.draw batch object (float x) (float y) (float width) (float height))) (defn draw-entity! [^SpriteBatch batch entity] - (cond - (isa? (type entity) Actor) - (draw-actor! batch {:actor entity}) - (:actor entity) - (draw-actor! batch entity) - (:image entity) - (draw-image! batch entity))) + (if (not (map? entity)) + (draw-entity! batch (create-entity entity)) + (case (:type entity) + :actor + (draw-actor! batch entity) + :image + (draw-image! batch entity) + nil))) (defn draw! [{:keys [renderer] :as screen} entities] (assert renderer) @@ -48,21 +49,23 @@ (cond (string? img) (-> ^String img Texture. TextureRegion.) + (map? img) + (TextureRegion. ^TextureRegion (:object img)) :else - (TextureRegion. ^TextureRegion img))) + img)) (defmacro image [img & options] - `(utils/calls! ^TextureRegion (image* ~img) ~@options)) + `(create-entity (utils/calls! ^TextureRegion (image* ~img) ~@options))) (defmacro image! [img k & options] - `(utils/call! ^TextureRegion ~img ~k ~@options)) + `(utils/call! ^TextureRegion (:object ~img) ~k ~@options)) (defmacro animation [duration images & args] `(Animation. ~duration - (utils/gdx-into-array ~images) + (utils/gdx-into-array (map :object ~images)) (utils/gdx-static-field :graphics :g2d :Animation ~(or (first args) :normal))))