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:
parent
1e2de1dd99
commit
cc62dfb054
|
@ -16,14 +16,13 @@
|
||||||
|
|
||||||
(defmethod slick/update-game :game [state inputtln delta]
|
(defmethod slick/update-game :game [state inputtln delta]
|
||||||
(assoc state :gs (gs/update-gs (:gs state)
|
(assoc state :gs (gs/update-gs (:gs state)
|
||||||
(let [idman (first (gs/q :name :man))
|
(if-let [input (tln/timeline-last-value inputtln)]
|
||||||
man (gs/entity idman)]
|
(let [man (first (gs/q :name :man))]
|
||||||
(if-let [input (tln/timeline-last-value inputtln)]
|
(gs/update-entity (assoc man :x (:mousex input) :y (:mousey input))))))))
|
||||||
(gs/set-entity idman (assoc man :x (:mousex input) :y (:mousey input))))))))
|
|
||||||
|
|
||||||
(defmethod slick/render-game :game [state graphics]
|
(defmethod slick/render-game :game [state graphics]
|
||||||
(gs/with-gs (:gs state)
|
(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))]
|
(if-let [image (res/get-image (:image sprite))]
|
||||||
(.draw image (:x sprite) (:y sprite))))))
|
(.draw image (:x sprite) (:y sprite))))))
|
||||||
|
|
||||||
|
@ -32,6 +31,6 @@
|
||||||
|
|
||||||
(defmethod slick/eval-with-bindings :default [state game f]
|
(defmethod slick/eval-with-bindings :default [state game f]
|
||||||
(binding [g game s state]
|
(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)))))
|
(slick/game-setstate! game (assoc state :gs gsnew)))))
|
||||||
|
|
||||||
|
|
|
@ -63,16 +63,30 @@
|
||||||
(add-id-to-index id (get entitynew key))))
|
(add-id-to-index id (get entitynew key))))
|
||||||
|
|
||||||
(defn set-entity [id value]
|
(defn set-entity [id value]
|
||||||
(update-indices id (entity id) value)
|
(let [value (if value (assoc value ::id id) nil)]
|
||||||
(let [gsnew
|
(update-indices id (entity id) value)
|
||||||
(if (= value nil)
|
(let [gsnew
|
||||||
(assoc @*gamestate* :entities (dissoc (get @*gamestate* :entities) id))
|
(if (= value nil)
|
||||||
(assoc-in @*gamestate* [:entities id] value))]
|
(assoc @*gamestate* :entities (dissoc (get @*gamestate* :entities) id))
|
||||||
(ref-set *gamestate* gsnew)))
|
(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))
|
(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]
|
(defn q [indextype query]
|
||||||
(q-in @*gamestate* indextype query))
|
(q-in @*gamestate* indextype query))
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue