Break entity code into separate namespace
This commit is contained in:
@@ -1,5 +1,6 @@
|
|||||||
(ns play-clj.core
|
(ns play-clj.core
|
||||||
(:require [clojure.set]
|
(:require [clojure.set]
|
||||||
|
[play-clj.entities :as e]
|
||||||
[play-clj.utils :as u])
|
[play-clj.utils :as u])
|
||||||
(:import [com.badlogic.gdx Application Audio Files Game Gdx Graphics Input
|
(:import [com.badlogic.gdx Application Audio Files Game Gdx Graphics Input
|
||||||
InputMultiplexer InputProcessor Net Screen]
|
InputMultiplexer InputProcessor Net Screen]
|
||||||
@@ -25,7 +26,7 @@
|
|||||||
[com.badlogic.gdx.scenes.scene2d.utils ActorGestureListener Align
|
[com.badlogic.gdx.scenes.scene2d.utils ActorGestureListener Align
|
||||||
ChangeListener ClickListener DragListener FocusListener]
|
ChangeListener ClickListener DragListener FocusListener]
|
||||||
[com.badlogic.gdx.utils Timer$Task]
|
[com.badlogic.gdx.utils Timer$Task]
|
||||||
[play_clj.utils Entity]))
|
[play_clj.entities Entity]))
|
||||||
|
|
||||||
(load "core_basics")
|
(load "core_basics")
|
||||||
(load "core_cameras")
|
(load "core_cameras")
|
||||||
|
|||||||
@@ -247,7 +247,7 @@ with the tiled map file at `path` and `unit` scale
|
|||||||
(let [^SpriteBatch batch (.getSpriteBatch renderer)]
|
(let [^SpriteBatch batch (.getSpriteBatch renderer)]
|
||||||
(.begin batch)
|
(.begin batch)
|
||||||
(doseq [entity entities]
|
(doseq [entity entities]
|
||||||
(u/draw-entity! entity batch))
|
(e/draw-entity! entity batch))
|
||||||
(.end batch))
|
(.end batch))
|
||||||
entities)
|
entities)
|
||||||
|
|
||||||
@@ -256,7 +256,7 @@ with the tiled map file at `path` and `unit` scale
|
|||||||
(let [^SpriteBatch batch (.getSpriteBatch renderer)]
|
(let [^SpriteBatch batch (.getSpriteBatch renderer)]
|
||||||
(.begin batch)
|
(.begin batch)
|
||||||
(doseq [entity entities]
|
(doseq [entity entities]
|
||||||
(u/draw-entity! entity batch))
|
(e/draw-entity! entity batch))
|
||||||
(.end batch))
|
(.end batch))
|
||||||
entities)
|
entities)
|
||||||
|
|
||||||
@@ -264,7 +264,7 @@ with the tiled map file at `path` and `unit` scale
|
|||||||
[{:keys [^ModelBatch renderer ^Camera camera] :as screen} entities]
|
[{:keys [^ModelBatch renderer ^Camera camera] :as screen} entities]
|
||||||
(.begin renderer camera)
|
(.begin renderer camera)
|
||||||
(doseq [entity entities]
|
(doseq [entity entities]
|
||||||
(u/draw-entity! entity screen))
|
(e/draw-entity! entity screen))
|
||||||
(.end renderer)
|
(.end renderer)
|
||||||
entities)
|
entities)
|
||||||
|
|
||||||
@@ -377,6 +377,6 @@ specify which layers to render with or without
|
|||||||
(sort-by :y #(compare %2 %1)))]
|
(sort-by :y #(compare %2 %1)))]
|
||||||
(if-let [layer (:layer entity)]
|
(if-let [layer (:layer entity)]
|
||||||
(.renderTileLayer renderer layer)
|
(.renderTileLayer renderer layer)
|
||||||
(u/draw-entity! entity batch)))
|
(e/draw-entity! entity batch)))
|
||||||
(.end batch))
|
(.end batch))
|
||||||
entities)
|
entities)
|
||||||
|
|||||||
57
src/play_clj/entities.clj
Normal file
57
src/play_clj/entities.clj
Normal file
@@ -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)))
|
||||||
@@ -3,7 +3,8 @@
|
|||||||
(:import [com.badlogic.gdx.graphics Texture]
|
(:import [com.badlogic.gdx.graphics Texture]
|
||||||
[com.badlogic.gdx.graphics.g2d Animation BitmapFont NinePatch
|
[com.badlogic.gdx.graphics.g2d Animation BitmapFont NinePatch
|
||||||
ParticleEffect TextureAtlas TextureRegion]
|
ParticleEffect TextureAtlas TextureRegion]
|
||||||
[play_clj.utils TextureEntity NinePatchEntity ParticleEffectEntity]))
|
[play_clj.entities TextureEntity NinePatchEntity
|
||||||
|
ParticleEffectEntity]))
|
||||||
|
|
||||||
(defmacro bitmap-font
|
(defmacro bitmap-font
|
||||||
"Returns a [BitmapFont](http://libgdx.badlogicgames.com/nightlies/docs/api/com/badlogic/gdx/graphics/g2d/BitmapFont.html)
|
"Returns a [BitmapFont](http://libgdx.badlogicgames.com/nightlies/docs/api/com/badlogic/gdx/graphics/g2d/BitmapFont.html)
|
||||||
|
|||||||
@@ -8,7 +8,7 @@
|
|||||||
[com.badlogic.gdx.graphics.g3d.model.data ModelData]
|
[com.badlogic.gdx.graphics.g3d.model.data ModelData]
|
||||||
[com.badlogic.gdx.graphics.g3d.utils AnimationController
|
[com.badlogic.gdx.graphics.g3d.utils AnimationController
|
||||||
ModelBuilder]
|
ModelBuilder]
|
||||||
[play_clj.utils ModelEntity]))
|
[play_clj.entities ModelEntity]))
|
||||||
|
|
||||||
; animation-controller
|
; animation-controller
|
||||||
|
|
||||||
|
|||||||
@@ -12,7 +12,7 @@
|
|||||||
[com.badlogic.gdx.scenes.scene2d.utils NinePatchDrawable
|
[com.badlogic.gdx.scenes.scene2d.utils NinePatchDrawable
|
||||||
SpriteDrawable TextureRegionDrawable TiledDrawable]
|
SpriteDrawable TextureRegionDrawable TiledDrawable]
|
||||||
[com.esotericsoftware.tablelayout Cell]
|
[com.esotericsoftware.tablelayout Cell]
|
||||||
[play_clj.utils ActorEntity]))
|
[play_clj.entities ActorEntity]))
|
||||||
|
|
||||||
(defmacro drawable
|
(defmacro drawable
|
||||||
"Returns a subclass of [BaseDrawable](http://libgdx.badlogicgames.com/nightlies/docs/api/com/badlogic/gdx/scenes/scene2d/utils/BaseDrawable.html)
|
"Returns a subclass of [BaseDrawable](http://libgdx.badlogicgames.com/nightlies/docs/api/com/badlogic/gdx/scenes/scene2d/utils/BaseDrawable.html)
|
||||||
|
|||||||
@@ -1,11 +1,6 @@
|
|||||||
(ns play-clj.utils
|
(ns play-clj.utils
|
||||||
(:require [clojure.string :as s])
|
(:require [clojure.string :as s])
|
||||||
(:import [com.badlogic.gdx Gdx Graphics]
|
(:import [com.badlogic.gdx.utils Array ArrayMap]))
|
||||||
[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]))
|
|
||||||
|
|
||||||
; misc
|
; 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`"
|
"Calls a single method on a `gdx-array-map`"
|
||||||
[object k & options]
|
[object k & options]
|
||||||
`(call! ^ArrayMap ~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)))
|
|
||||||
|
|||||||
Reference in New Issue
Block a user