Break entity code into separate namespace

This commit is contained in:
oakes
2014-03-21 20:06:08 -04:00
parent bc0844310d
commit 0920379e28
7 changed files with 68 additions and 67 deletions

View File

@@ -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")

View File

@@ -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)

57
src/play_clj/entities.clj Normal file
View 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)))

View File

@@ -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)

View File

@@ -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

View File

@@ -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)

View File

@@ -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)))