|
|
@ -8,10 +8,12 @@
|
|
|
|
(def gamestate {
|
|
|
|
(def gamestate {
|
|
|
|
:canvas (.getElementById js/document "gamecanvas")
|
|
|
|
:canvas (.getElementById js/document "gamecanvas")
|
|
|
|
:2d (.getContext (.getElementById js/document "gamecanvas") "2d")
|
|
|
|
:2d (.getContext (.getElementById js/document "gamecanvas") "2d")
|
|
|
|
|
|
|
|
:target-fps 30
|
|
|
|
:timing {
|
|
|
|
:timing {
|
|
|
|
:prev 0
|
|
|
|
:prev 0
|
|
|
|
:now 0
|
|
|
|
:now 0
|
|
|
|
:fps 0
|
|
|
|
:fps 0
|
|
|
|
|
|
|
|
:elapsed 0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
:dimensions {
|
|
|
|
:dimensions {
|
|
|
|
:w 600
|
|
|
|
:w 600
|
|
|
@ -31,17 +33,23 @@
|
|
|
|
(aset (:2d gamestate) "font" "10px monospace")
|
|
|
|
(aset (:2d gamestate) "font" "10px monospace")
|
|
|
|
|
|
|
|
|
|
|
|
(defn set-timing [state timingkey]
|
|
|
|
(defn set-timing [state timingkey]
|
|
|
|
(update-in state
|
|
|
|
(assoc-in state
|
|
|
|
[:timing timingkey]
|
|
|
|
[:timing timingkey]
|
|
|
|
#(.now js/performance)))
|
|
|
|
(.now js/performance)))
|
|
|
|
|
|
|
|
|
|
|
|
(defn set-fps [state]
|
|
|
|
(defn set-fps [state]
|
|
|
|
(let [newstate (set-timing state :now)
|
|
|
|
(let [elapsed (get-in state [:timing :elapsed])
|
|
|
|
now (get-in newstate [:timing :now])
|
|
|
|
fps (/ 1 elapsed)]
|
|
|
|
prev (get-in newstate [:timing :prev])
|
|
|
|
(assoc-in state [:timing :fps] fps)))
|
|
|
|
duration (- now prev)
|
|
|
|
|
|
|
|
fps (/ 1000 duration)]
|
|
|
|
(defn set-elapsed-seconds [gamestate]
|
|
|
|
(update-in newstate [:timing :fps] (fn [] fps))))
|
|
|
|
(assoc-in gamestate
|
|
|
|
|
|
|
|
[:timing :elapsed]
|
|
|
|
|
|
|
|
(/
|
|
|
|
|
|
|
|
(-
|
|
|
|
|
|
|
|
(get-in gamestate [:timing :now])
|
|
|
|
|
|
|
|
(get-in gamestate [:timing :prev]))
|
|
|
|
|
|
|
|
1000)))
|
|
|
|
|
|
|
|
|
|
|
|
(defn update-scene [gamestate]
|
|
|
|
(defn update-scene [gamestate]
|
|
|
|
(let [scenekey (:scene gamestate)
|
|
|
|
(let [scenekey (:scene gamestate)
|
|
|
@ -52,9 +60,11 @@
|
|
|
|
|
|
|
|
|
|
|
|
(defn update-step [gamestate]
|
|
|
|
(defn update-step [gamestate]
|
|
|
|
(-> gamestate
|
|
|
|
(-> gamestate
|
|
|
|
|
|
|
|
(set-timing :now)
|
|
|
|
|
|
|
|
(set-elapsed-seconds)
|
|
|
|
(set-fps)
|
|
|
|
(set-fps)
|
|
|
|
(set-timing :prev)
|
|
|
|
(update-scene)
|
|
|
|
(update-scene)))
|
|
|
|
(set-timing :prev)))
|
|
|
|
|
|
|
|
|
|
|
|
(defn draw-step [gamestate]
|
|
|
|
(defn draw-step [gamestate]
|
|
|
|
(.clearRect (:2d gamestate)
|
|
|
|
(.clearRect (:2d gamestate)
|
|
|
@ -77,7 +87,7 @@
|
|
|
|
(fn []
|
|
|
|
(fn []
|
|
|
|
(.requestAnimationFrame js/window
|
|
|
|
(.requestAnimationFrame js/window
|
|
|
|
#(mainloop newstate)))
|
|
|
|
#(mainloop newstate)))
|
|
|
|
(/ 1000 30))))
|
|
|
|
(/ 1000 (:target-fps gamestate)))))
|
|
|
|
|
|
|
|
|
|
|
|
(defn init-scenes []
|
|
|
|
(defn init-scenes []
|
|
|
|
(assoc
|
|
|
|
(assoc
|
|
|
|