Simple test "game" with data
This commit is contained in:
parent
d7e0453578
commit
29409585e9
BIN
res/man.png
Normal file
BIN
res/man.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 238 B |
|
@ -1,11 +1,28 @@
|
||||||
(ns hottub.core
|
(ns hottub.core
|
||||||
(:require [hottub.slick :as slick]))
|
(:require [hottub.slick :as slick]
|
||||||
|
[hottub.gs :as gs]
|
||||||
|
[hottub.resource :as res]))
|
||||||
|
|
||||||
(defn -main [& args]
|
(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/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 g nil)
|
||||||
(def ^:dynamic s nil)
|
(def ^:dynamic s nil)
|
||||||
(defmethod slick/eval-with-bindings :game [state game f]
|
(def ^:dynamic _container nil)
|
||||||
(binding [g game s state] (f)))
|
(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)))))
|
||||||
|
|
||||||
|
|
|
@ -1,11 +1,12 @@
|
||||||
(ns hottub.slick
|
(ns hottub.slick
|
||||||
(:import [org.newdawn.slick AppGameContainer])
|
(:import [org.newdawn.slick AppGameContainer])
|
||||||
(:use ns-tracker.core
|
(: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 update-game (fn [state delta] (:state state)))
|
||||||
(defmulti render-game (fn [state graphics] (: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"]))
|
(def modified-namespaces (ns-tracker ["src" "test"]))
|
||||||
|
|
||||||
|
@ -13,13 +14,13 @@
|
||||||
:name hottub.slick.Game
|
:name hottub.slick.Game
|
||||||
:prefix game-
|
:prefix game-
|
||||||
:init create
|
:init create
|
||||||
:constructors {[String clojure.lang.Keyword clojure.lang.PersistentArrayMap] [String]}
|
:constructors {[String clojure.lang.PersistentArrayMap] [String]}
|
||||||
:state state
|
:state state
|
||||||
:extends org.newdawn.slick.BasicGame)
|
:extends org.newdawn.slick.BasicGame)
|
||||||
|
|
||||||
(defn game-create [title startstate value]
|
(defn game-create [title state]
|
||||||
[[title]
|
[[title]
|
||||||
{:state (ref (assoc (or value {}) :state startstate))
|
{:state (ref state)
|
||||||
:repl (repl/repl-create 9999)}])
|
:repl (repl/repl-create 9999)}])
|
||||||
|
|
||||||
(defn game-state-ref [this] (:state (.state this)))
|
(defn game-state-ref [this] (:state (.state this)))
|
||||||
|
@ -27,7 +28,7 @@
|
||||||
(defn game-init [this container]
|
(defn game-init [this container]
|
||||||
(repl/repl-start
|
(repl/repl-start
|
||||||
(:repl (.state this))
|
(: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]
|
(defn game-update [this container delta]
|
||||||
(doseq [ns-sym (modified-namespaces)]
|
(doseq [ns-sym (modified-namespaces)]
|
||||||
|
@ -43,6 +44,7 @@
|
||||||
|
|
||||||
(defn game-render [this container graphics]
|
(defn game-render [this container graphics]
|
||||||
(try
|
(try
|
||||||
|
(res/gc-expired-resources)
|
||||||
(render-game @(game-state-ref this) graphics)
|
(render-game @(game-state-ref this) graphics)
|
||||||
(catch Exception e (prn "Couldn't render" (:state @(game-state-ref this)) (.getMessage e)))))
|
(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 update-game :default [state delta])
|
||||||
(defmethod render-game :default [state graphics])
|
(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]]
|
(defn start-game [title startstate]
|
||||||
(let [game (hottub.slick.Game. title startstate value)
|
(let [game (hottub.slick.Game. title startstate)
|
||||||
container (AppGameContainer. game 640 480 false)]
|
container (AppGameContainer. game 640 480 false)]
|
||||||
|
(.setAlwaysRender container true)
|
||||||
(.start container)
|
(.start container)
|
||||||
game))
|
game))
|
||||||
|
|
Loading…
Reference in a new issue