diff --git a/common/src/play_clj/core.clj b/common/src/play_clj/core.clj index 5c8cda3..b224778 100644 --- a/common/src/play_clj/core.clj +++ b/common/src/play_clj/core.clj @@ -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)) diff --git a/common/src/play_clj/render.clj b/common/src/play_clj/render.clj index 976e858..c3af4c8 100644 --- a/common/src/play_clj/render.clj +++ b/common/src/play_clj/render.clj @@ -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)))