Standardize all entities as hash maps

This commit is contained in:
oakes
2014-01-06 13:23:19 -05:00
parent 16e52427ef
commit 8f97e02a1b
2 changed files with 32 additions and 15 deletions

View File

@@ -17,6 +17,20 @@
[com.badlogic.gdx.scenes.scene2d Actor Stage] [com.badlogic.gdx.scenes.scene2d Actor Stage]
[com.badlogic.gdx.scenes.scene2d.ui Label Label$LabelStyle])) [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_2d")
(load "core_global") (load "core_global")
(load "core_render") (load "core_render")

View File

@@ -15,22 +15,23 @@
(.getSpriteBatch renderer)) (.getSpriteBatch renderer))
(defn draw-actor! (defn draw-actor!
[^SpriteBatch batch {:keys [^Actor actor] :as entity}] [^SpriteBatch batch {:keys [^Actor object] :as entity}]
(.draw ^Actor actor batch 1)) (.draw ^Actor object batch 1))
(defn draw-image! (defn draw-image!
[^SpriteBatch batch {:keys [^TextureRegion image x y width height]}] [^SpriteBatch batch {:keys [^TextureRegion object x y width height]}]
(.draw batch image (float x) (float y) (float width) (float height))) (.draw batch object (float x) (float y) (float width) (float height)))
(defn draw-entity! (defn draw-entity!
[^SpriteBatch batch entity] [^SpriteBatch batch entity]
(cond (if (not (map? entity))
(isa? (type entity) Actor) (draw-entity! batch (create-entity entity))
(draw-actor! batch {:actor entity}) (case (:type entity)
(:actor entity) :actor
(draw-actor! batch entity) (draw-actor! batch entity)
(:image entity) :image
(draw-image! batch entity))) (draw-image! batch entity)
nil)))
(defn draw! [{:keys [renderer] :as screen} entities] (defn draw! [{:keys [renderer] :as screen} entities]
(assert renderer) (assert renderer)
@@ -48,21 +49,23 @@
(cond (cond
(string? img) (string? img)
(-> ^String img Texture. TextureRegion.) (-> ^String img Texture. TextureRegion.)
(map? img)
(TextureRegion. ^TextureRegion (:object img))
:else :else
(TextureRegion. ^TextureRegion img))) img))
(defmacro image (defmacro image
[img & options] [img & options]
`(utils/calls! ^TextureRegion (image* ~img) ~@options)) `(create-entity (utils/calls! ^TextureRegion (image* ~img) ~@options)))
(defmacro image! (defmacro image!
[img k & options] [img k & options]
`(utils/call! ^TextureRegion ~img ~k ~@options)) `(utils/call! ^TextureRegion (:object ~img) ~k ~@options))
(defmacro animation (defmacro animation
[duration images & args] [duration images & args]
`(Animation. ~duration `(Animation. ~duration
(utils/gdx-into-array ~images) (utils/gdx-into-array (map :object ~images))
(utils/gdx-static-field :graphics :g2d :Animation (utils/gdx-static-field :graphics :g2d :Animation
~(or (first args) :normal)))) ~(or (first args) :normal))))