Allow adding/removing entities

This commit is contained in:
oakes
2013-12-29 15:30:15 -05:00
parent 9088f9906b
commit 8f97c3fa57
2 changed files with 29 additions and 9 deletions

View File

@@ -37,7 +37,16 @@
(swap! screen assoc
:renderer (create-renderer renderer)
:camera (create-camera camera)
:total-time 0)
:total-time 0
:entities []
:add-entity (fn [entity]
(->> entity
(conj (:entities @screen))
(swap! screen assoc :entities)))
:del-entity (fn [entity]
(->> (:entities @screen)
(remove #(= % entity))
(swap! screen assoc :entities))))
(on-show @screen))
(render [delta-time]
(swap! screen assoc :total-time (+ (:total-time @screen) delta-time))
@@ -52,6 +61,14 @@
[name & {:keys [] :as options}]
`(def ~name (defscreen* ~options)))
(defn add-entity!
[{:keys [add-entity]} e]
(add-entity e))
(defn remove-entity!
[{:keys [del-entity]} e]
(del-entity e))
(defn set-screen!
[^Game game ^Screen screen]
(.setScreen game screen))

View File

@@ -49,7 +49,7 @@
; draw entities
(defmulti sprite-batch (fn [screen] (class (:renderer screen))) :default nil)
(defmulti sprite-batch #(-> % :renderer class) :default nil)
(defmethod sprite-batch nil [screen]
(SpriteBatch.))
@@ -58,10 +58,13 @@
(.getSpriteBatch (:renderer screen)))
(defn draw-entities!
[screen entities]
(let [batch (sprite-batch screen)]
(.begin batch)
(doseq [{:keys [image x y width height]} entities]
(when (and image x y width height)
(.draw batch image x y width height)))
(.end batch)))
([screen]
(draw-entities! screen (-> screen :entities)))
([screen entities]
(let [batch (sprite-batch screen)]
(.begin batch)
(doseq [{:keys [image x y width height]} entities]
(when (and image x y width height)
(.draw batch image x y width height)))
(.end batch)
batch)))