Simple test "game" with data

This commit is contained in:
Jeremy Penner 2013-03-12 00:19:46 -05:00
parent d7e0453578
commit 29409585e9
3 changed files with 33 additions and 13 deletions

BIN
res/man.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 238 B

View file

@ -1,11 +1,28 @@
(ns hottub.core
(:require [hottub.slick :as slick]))
(:require [hottub.slick :as slick]
[hottub.gs :as gs]
[hottub.resource :as res]))
(defn -main [& args]
(slick/start-game "test-game" :game))
(res/start-resource-expiry-thread)
(slick/start-game "Test game"
{:state :game
:gs (gs/with-gs gs/gs-empty
(gs/add-index :type)
(gs/set-entity (gs/gen-id) {:type :sprite :x 50 :y 50 :image "res/man.png"}))}))
(defmethod slick/update-game :game [state delta])
(defmethod slick/render-game :game [state graphics]
(gs/with-gs (:gs state)
(doseq [sprite (for [spriteid (gs/q :type :sprite)] (gs/entity spriteid))]
(if-let [image (res/get-image (:image sprite))]
(.draw image (:x sprite) (:y sprite))))))
(def ^:dynamic g nil)
(def ^:dynamic s nil)
(defmethod slick/eval-with-bindings :game [state game f]
(binding [g game s state] (f)))
(def ^:dynamic _container nil)
(defmethod slick/eval-with-bindings :game [state game container f]
(binding [g game s state _container container]
(let [gsnew (gs/with-gs (:gs state) (f) (gs/get-gs))]
(slick/game-setstate game (assoc state :gs gsnew)))))

View file

@ -1,11 +1,12 @@
(ns hottub.slick
(:import [org.newdawn.slick AppGameContainer])
(:use ns-tracker.core
[hottub.repl :as repl]))
[hottub.repl :as repl]
[hottub.resource :as res]))
(defmulti update-game (fn [state delta] (:state state)))
(defmulti render-game (fn [state graphics] (:state state)))
(defmulti eval-with-bindings (fn [state game f] (:state state)))
(defmulti eval-with-bindings (fn [state game container f] (:state state)))
(def modified-namespaces (ns-tracker ["src" "test"]))
@ -13,13 +14,13 @@
:name hottub.slick.Game
:prefix game-
:init create
:constructors {[String clojure.lang.Keyword clojure.lang.PersistentArrayMap] [String]}
:constructors {[String clojure.lang.PersistentArrayMap] [String]}
:state state
:extends org.newdawn.slick.BasicGame)
(defn game-create [title startstate value]
(defn game-create [title state]
[[title]
{:state (ref (assoc (or value {}) :state startstate))
{:state (ref state)
:repl (repl/repl-create 9999)}])
(defn game-state-ref [this] (:state (.state this)))
@ -27,7 +28,7 @@
(defn game-init [this container]
(repl/repl-start
(:repl (.state this))
(fn [f] (eval-with-bindings @(game-state-ref this) this f))))
(fn [f] (eval-with-bindings @(game-state-ref this) this container f))))
(defn game-update [this container delta]
(doseq [ns-sym (modified-namespaces)]
@ -43,6 +44,7 @@
(defn game-render [this container graphics]
(try
(res/gc-expired-resources)
(render-game @(game-state-ref this) graphics)
(catch Exception e (prn "Couldn't render" (:state @(game-state-ref this)) (.getMessage e)))))
@ -51,10 +53,11 @@
(defmethod update-game :default [state delta])
(defmethod render-game :default [state graphics])
(defmethod eval-with-bindings :default [state game f] (f))
(defmethod eval-with-bindings :default [state game container f] (f))
(defn start-game [title startstate & [value]]
(let [game (hottub.slick.Game. title startstate value)
(defn start-game [title startstate]
(let [game (hottub.slick.Game. title startstate)
container (AppGameContainer. game 640 480 false)]
(.setAlwaysRender container true)
(.start container)
game))