diff --git a/src/hottub/core.clj b/src/hottub/core.clj index 7198979..4b51004 100644 --- a/src/hottub/core.clj +++ b/src/hottub/core.clj @@ -16,14 +16,13 @@ (defmethod slick/update-game :game [state inputtln delta] (assoc state :gs (gs/update-gs (:gs state) - (let [idman (first (gs/q :name :man)) - man (gs/entity idman)] - (if-let [input (tln/timeline-last-value inputtln)] - (gs/set-entity idman (assoc man :x (:mousex input) :y (:mousey input)))))))) + (if-let [input (tln/timeline-last-value inputtln)] + (let [man (first (gs/q :name :man))] + (gs/update-entity (assoc man :x (:mousex input) :y (:mousey input)))))))) (defmethod slick/render-game :game [state graphics] (gs/with-gs (:gs state) - (doseq [sprite (for [spriteid (gs/q :type :sprite)] (gs/entity spriteid))] + (doseq [sprite (gs/q :type :sprite)] (if-let [image (res/get-image (:image sprite))] (.draw image (:x sprite) (:y sprite)))))) @@ -32,6 +31,6 @@ (defmethod slick/eval-with-bindings :default [state game f] (binding [g game s state] - (let [gsnew (gs/with-gs (:gs state) (f) (gs/get-gs))] + (let [gsnew (gs/update-gs (:gs state) (f))] (slick/game-setstate! game (assoc state :gs gsnew))))) diff --git a/src/hottub/gs.clj b/src/hottub/gs.clj index 2fe9690..29ac7ec 100644 --- a/src/hottub/gs.clj +++ b/src/hottub/gs.clj @@ -62,17 +62,31 @@ (remove-id-from-index id (get entityold key)) (add-id-to-index id (get entitynew key)))) -(defn set-entity [id value] - (update-indices id (entity id) value) - (let [gsnew - (if (= value nil) - (assoc @*gamestate* :entities (dissoc (get @*gamestate* :entities) id)) - (assoc-in @*gamestate* [:entities id] value))] - (ref-set *gamestate* gsnew))) +(defn set-entity [id value] + (let [value (if value (assoc value ::id id) nil)] + (update-indices id (entity id) value) + (let [gsnew + (if (= value nil) + (assoc @*gamestate* :entities (dissoc (get @*gamestate* :entities) id)) + (assoc-in @*gamestate* [:entities id] value))] + (ref-set *gamestate* gsnew) + value))) -(defn q-in [gamestate indextype query] +(defn update-entity [value] + (set-entity (::id value) value)) + +(defn remove-entity [value] + (set-entity (::id value) nil)) + +(defn q-id-in [gamestate indextype query] (query-index indextype (get-in gamestate [:indices indextype]) query)) +(defn q-id [indextype query] + (q-id-in @*gamestate* indextype query)) + +(defn q-in [gs indextype query] + (for [id (q-id-in gs indextype query)] (entity id))) + (defn q [indextype query] (q-in @*gamestate* indextype query))