Gamestate API improvements

- always include the id as an attribute
- implement query operators that return the whole entity, rather than just the ids
- implement update / delete functions which extract the id from the given entity
This commit is contained in:
Jeremy Penner 2013-03-18 22:33:26 -04:00
parent 1e2de1dd99
commit cc62dfb054
2 changed files with 27 additions and 14 deletions

View file

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

View file

@ -63,16 +63,30 @@
(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)))
(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))