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