diff --git a/src/play_clj/core.clj b/src/play_clj/core.clj index 8a9e52c..7d91290 100644 --- a/src/play_clj/core.clj +++ b/src/play_clj/core.clj @@ -1,5 +1,6 @@ (ns play-clj.core (:require [clojure.set] + [play-clj.entities :as e] [play-clj.utils :as u]) (:import [com.badlogic.gdx Application Audio Files Game Gdx Graphics Input InputMultiplexer InputProcessor Net Screen] @@ -25,7 +26,7 @@ [com.badlogic.gdx.scenes.scene2d.utils ActorGestureListener Align ChangeListener ClickListener DragListener FocusListener] [com.badlogic.gdx.utils Timer$Task] - [play_clj.utils Entity])) + [play_clj.entities Entity])) (load "core_basics") (load "core_cameras") diff --git a/src/play_clj/core_graphics.clj b/src/play_clj/core_graphics.clj index 82a5f15..8b45c88 100644 --- a/src/play_clj/core_graphics.clj +++ b/src/play_clj/core_graphics.clj @@ -247,7 +247,7 @@ with the tiled map file at `path` and `unit` scale (let [^SpriteBatch batch (.getSpriteBatch renderer)] (.begin batch) (doseq [entity entities] - (u/draw-entity! entity batch)) + (e/draw-entity! entity batch)) (.end batch)) entities) @@ -256,7 +256,7 @@ with the tiled map file at `path` and `unit` scale (let [^SpriteBatch batch (.getSpriteBatch renderer)] (.begin batch) (doseq [entity entities] - (u/draw-entity! entity batch)) + (e/draw-entity! entity batch)) (.end batch)) entities) @@ -264,7 +264,7 @@ with the tiled map file at `path` and `unit` scale [{:keys [^ModelBatch renderer ^Camera camera] :as screen} entities] (.begin renderer camera) (doseq [entity entities] - (u/draw-entity! entity screen)) + (e/draw-entity! entity screen)) (.end renderer) entities) @@ -377,6 +377,6 @@ specify which layers to render with or without (sort-by :y #(compare %2 %1)))] (if-let [layer (:layer entity)] (.renderTileLayer renderer layer) - (u/draw-entity! entity batch))) + (e/draw-entity! entity batch))) (.end batch)) entities) diff --git a/src/play_clj/entities.clj b/src/play_clj/entities.clj new file mode 100644 index 0000000..b7553f2 --- /dev/null +++ b/src/play_clj/entities.clj @@ -0,0 +1,57 @@ +(ns play-clj.entities + (:import [com.badlogic.gdx Gdx Graphics] + [com.badlogic.gdx.graphics.g2d NinePatch ParticleEffect SpriteBatch + TextureRegion] + [com.badlogic.gdx.graphics.g3d Environment ModelBatch ModelInstance] + [com.badlogic.gdx.scenes.scene2d Actor])) + +(defprotocol Entity + "Internal use only" + (draw-entity! [this batch] "Draws the entity")) + +(extend-protocol Entity + clojure.lang.PersistentArrayMap + (draw-entity! [this batch]) + clojure.lang.PersistentHashMap + (draw-entity! [this batch])) + +(defrecord TextureEntity [object] Entity + (draw-entity! [{:keys [^TextureRegion object x y width height]} batch] + (let [x (float (or x 0)) + y (float (or y 0)) + width (float (or width (.getRegionWidth object))) + height (float (or height (.getRegionHeight object)))] + (.draw ^SpriteBatch batch object x y width height)))) + +(defrecord NinePatchEntity [object] Entity + (draw-entity! [{:keys [^NinePatch object x y width height]} batch] + (let [x (float (or x 0)) + y (float (or y 0)) + width (float (or width (.getTotalWidth object))) + height (float (or height (.getTotalHeight object)))] + (.draw object ^SpriteBatch batch x y width height)))) + +(defrecord ParticleEffectEntity [object] Entity + (draw-entity! [{:keys [^ParticleEffect object x y delta-time]} batch] + (let [x (float (or x 0)) + y (float (or y 0)) + ^Graphics g (Gdx/graphics) + delta-time (float (or delta-time (.getDeltaTime g)))] + (.setPosition object x y) + (.draw object ^SpriteBatch batch delta-time)))) + +(defrecord ActorEntity [object] Entity + (draw-entity! [{:keys [^Actor object] :as entity} batch] + (doseq [[k v] entity] + (case k + :x (.setX object v) + :y (.setY object v) + :width (.setWidth object v) + :height (.setHeight object v) + nil)) + (.draw object ^SpriteBatch batch 1))) + +(defrecord ModelEntity [object] Entity + (draw-entity! [{:keys [^ModelInstance object]} + {:keys [^ModelBatch renderer ^Environment attributes]}] + (.render renderer object attributes))) diff --git a/src/play_clj/g2d.clj b/src/play_clj/g2d.clj index 5a53af1..71262fe 100644 --- a/src/play_clj/g2d.clj +++ b/src/play_clj/g2d.clj @@ -3,7 +3,8 @@ (:import [com.badlogic.gdx.graphics Texture] [com.badlogic.gdx.graphics.g2d Animation BitmapFont NinePatch ParticleEffect TextureAtlas TextureRegion] - [play_clj.utils TextureEntity NinePatchEntity ParticleEffectEntity])) + [play_clj.entities TextureEntity NinePatchEntity + ParticleEffectEntity])) (defmacro bitmap-font "Returns a [BitmapFont](http://libgdx.badlogicgames.com/nightlies/docs/api/com/badlogic/gdx/graphics/g2d/BitmapFont.html) diff --git a/src/play_clj/g3d.clj b/src/play_clj/g3d.clj index 43f2ee7..ec0a962 100644 --- a/src/play_clj/g3d.clj +++ b/src/play_clj/g3d.clj @@ -8,7 +8,7 @@ [com.badlogic.gdx.graphics.g3d.model.data ModelData] [com.badlogic.gdx.graphics.g3d.utils AnimationController ModelBuilder] - [play_clj.utils ModelEntity])) + [play_clj.entities ModelEntity])) ; animation-controller diff --git a/src/play_clj/ui.clj b/src/play_clj/ui.clj index c156e01..f8cc11d 100644 --- a/src/play_clj/ui.clj +++ b/src/play_clj/ui.clj @@ -12,7 +12,7 @@ [com.badlogic.gdx.scenes.scene2d.utils NinePatchDrawable SpriteDrawable TextureRegionDrawable TiledDrawable] [com.esotericsoftware.tablelayout Cell] - [play_clj.utils ActorEntity])) + [play_clj.entities ActorEntity])) (defmacro drawable "Returns a subclass of [BaseDrawable](http://libgdx.badlogicgames.com/nightlies/docs/api/com/badlogic/gdx/scenes/scene2d/utils/BaseDrawable.html) diff --git a/src/play_clj/utils.clj b/src/play_clj/utils.clj index b6c71b2..bfb5c8e 100644 --- a/src/play_clj/utils.clj +++ b/src/play_clj/utils.clj @@ -1,11 +1,6 @@ (ns play-clj.utils (:require [clojure.string :as s]) - (:import [com.badlogic.gdx Gdx Graphics] - [com.badlogic.gdx.graphics.g2d NinePatch ParticleEffect SpriteBatch - TextureRegion] - [com.badlogic.gdx.graphics.g3d Environment ModelBatch ModelInstance] - [com.badlogic.gdx.scenes.scene2d Actor] - [com.badlogic.gdx.utils Array ArrayMap])) + (:import [com.badlogic.gdx.utils Array ArrayMap])) ; misc @@ -172,56 +167,3 @@ new object to be created each time a field is set) "Calls a single method on a `gdx-array-map`" [object k & options] `(call! ^ArrayMap ~object ~k ~@options)) - -; entity protocol/records - -(defprotocol Entity - "Internal use only" - (draw-entity! [this batch] "Draws the entity")) - -(extend-protocol Entity - clojure.lang.PersistentArrayMap - (draw-entity! [this batch]) - clojure.lang.PersistentHashMap - (draw-entity! [this batch])) - -(defrecord TextureEntity [object] Entity - (draw-entity! [{:keys [^TextureRegion object x y width height]} batch] - (let [x (float (or x 0)) - y (float (or y 0)) - width (float (or width (.getRegionWidth object))) - height (float (or height (.getRegionHeight object)))] - (.draw ^SpriteBatch batch object x y width height)))) - -(defrecord NinePatchEntity [object] Entity - (draw-entity! [{:keys [^NinePatch object x y width height]} batch] - (let [x (float (or x 0)) - y (float (or y 0)) - width (float (or width (.getTotalWidth object))) - height (float (or height (.getTotalHeight object)))] - (.draw object ^SpriteBatch batch x y width height)))) - -(defrecord ParticleEffectEntity [object] Entity - (draw-entity! [{:keys [^ParticleEffect object x y delta-time]} batch] - (let [x (float (or x 0)) - y (float (or y 0)) - ^Graphics g (Gdx/graphics) - delta-time (float (or delta-time (.getDeltaTime g)))] - (.setPosition object x y) - (.draw object ^SpriteBatch batch delta-time)))) - -(defrecord ActorEntity [object] Entity - (draw-entity! [{:keys [^Actor object] :as entity} batch] - (doseq [[k v] entity] - (case k - :x (.setX object v) - :y (.setY object v) - :width (.setWidth object v) - :height (.setHeight object v) - nil)) - (.draw object ^SpriteBatch batch 1))) - -(defrecord ModelEntity [object] Entity - (draw-entity! [{:keys [^ModelInstance object]} - {:keys [^ModelBatch renderer ^Environment attributes]}] - (.render renderer object attributes)))