|
|
@ -8,7 +8,6 @@
|
|
|
|
(def gamestate
|
|
|
|
(def gamestate
|
|
|
|
{:canvas (by-id "gamecanvas")
|
|
|
|
{:canvas (by-id "gamecanvas")
|
|
|
|
:ctx (.getContext (by-id "gamecanvas") "2d")
|
|
|
|
:ctx (.getContext (by-id "gamecanvas") "2d")
|
|
|
|
:target-fps 60
|
|
|
|
|
|
|
|
:continue? true
|
|
|
|
:continue? true
|
|
|
|
:timing {;; msecs of previous frame
|
|
|
|
:timing {;; msecs of previous frame
|
|
|
|
:prev 0
|
|
|
|
:prev 0
|
|
|
@ -40,7 +39,7 @@
|
|
|
|
(defn set-fps
|
|
|
|
(defn set-fps
|
|
|
|
"calculates the current fps using the elapsed time"
|
|
|
|
"calculates the current fps using the elapsed time"
|
|
|
|
[state]
|
|
|
|
[state]
|
|
|
|
(let [elapsed (get-in state [:timing :elapsed])
|
|
|
|
(let [elapsed (-> state :timing :elapsed)
|
|
|
|
fps (/ 1 elapsed)]
|
|
|
|
fps (/ 1 elapsed)]
|
|
|
|
(assoc-in state [:timing :fps] fps)))
|
|
|
|
(assoc-in state [:timing :fps] fps)))
|
|
|
|
|
|
|
|
|
|
|
@ -98,13 +97,11 @@
|
|
|
|
[gamestate]
|
|
|
|
[gamestate]
|
|
|
|
(let [ctx (:ctx gamestate)]
|
|
|
|
(let [ctx (:ctx gamestate)]
|
|
|
|
(aset ctx "fillStyle" "white")
|
|
|
|
(aset ctx "fillStyle" "white")
|
|
|
|
(.fillRect
|
|
|
|
(.fillRect ctx
|
|
|
|
ctx
|
|
|
|
|
|
|
|
0 0 13 13)
|
|
|
|
0 0 13 13)
|
|
|
|
(aset ctx "fillStyle" "black")
|
|
|
|
(aset ctx "fillStyle" "black")
|
|
|
|
(aset ctx "font" "10px monospace")
|
|
|
|
(aset ctx "font" "10px monospace")
|
|
|
|
(.fillText
|
|
|
|
(.fillText (:ctx gamestate)
|
|
|
|
(:ctx gamestate)
|
|
|
|
|
|
|
|
(int (get-in gamestate [:timing :fps]))
|
|
|
|
(int (get-in gamestate [:timing :fps]))
|
|
|
|
0 10)))
|
|
|
|
0 10)))
|
|
|
|
|
|
|
|
|
|
|
@ -116,44 +113,29 @@
|
|
|
|
(get-in gamestate [:dimensions :w])
|
|
|
|
(get-in gamestate [:dimensions :w])
|
|
|
|
(get-in gamestate [:dimensions :h]))
|
|
|
|
(get-in gamestate [:dimensions :h]))
|
|
|
|
(let [scenekey (:scene gamestate)
|
|
|
|
(let [scenekey (:scene gamestate)
|
|
|
|
{:keys [draw] :as scene} (get-in gamestate [:scenes scenekey])]
|
|
|
|
{:keys [draw] :as scene} (-> gamestate :scenes scenekey)]
|
|
|
|
(draw gamestate scene))
|
|
|
|
(draw gamestate scene))
|
|
|
|
(draw-fps gamestate))
|
|
|
|
(draw-fps gamestate))
|
|
|
|
|
|
|
|
|
|
|
|
(defn timeout
|
|
|
|
|
|
|
|
"calculates the duration of update-step and draw-step.
|
|
|
|
|
|
|
|
substracts that from the wait time to reach target-fps
|
|
|
|
|
|
|
|
more accurately.
|
|
|
|
|
|
|
|
if continue? is true, wait for 5 seconds plain"
|
|
|
|
|
|
|
|
[gamestate]
|
|
|
|
|
|
|
|
(let [update-elapsed (- (.now js/performance)
|
|
|
|
|
|
|
|
(get-in gamestate [:timing :now]))]
|
|
|
|
|
|
|
|
(if (:continue? gamestate)
|
|
|
|
|
|
|
|
(/ (- 1000 update-elapsed)
|
|
|
|
|
|
|
|
(:target-fps gamestate))
|
|
|
|
|
|
|
|
5000)))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(defn mainloop
|
|
|
|
(defn mainloop
|
|
|
|
"transforms the given gamestate by invoking a series of update
|
|
|
|
"transforms the given gamestate by invoking a series of update
|
|
|
|
functions and draws it using the 2d context of the gamestate.
|
|
|
|
functions and draws it using the 2d context of the gamestate.
|
|
|
|
then, it calls itself again with a delay according to the target fps"
|
|
|
|
then, it calls itself again using requestAnimationFrame"
|
|
|
|
[gamestate]
|
|
|
|
[gamestate]
|
|
|
|
(let [newstate (update-step gamestate)]
|
|
|
|
(let [newstate (update-step gamestate)]
|
|
|
|
(set! (.-imageSmoothingEnabled (:ctx gamestate)) false)
|
|
|
|
(set! (.-imageSmoothingEnabled (:ctx gamestate)) false)
|
|
|
|
(draw-step newstate)
|
|
|
|
(draw-step newstate)
|
|
|
|
(when-not @reloaded
|
|
|
|
(when-not @reloaded
|
|
|
|
(.setTimeout js/window
|
|
|
|
(.requestAnimationFrame js/window
|
|
|
|
(fn []
|
|
|
|
#(mainloop newstate)))))
|
|
|
|
(.requestAnimationFrame
|
|
|
|
|
|
|
|
js/window
|
|
|
|
|
|
|
|
#(mainloop newstate)))
|
|
|
|
|
|
|
|
(timeout newstate)))))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(defn init-scenes
|
|
|
|
(defn init-scenes
|
|
|
|
"initiates the scene data maps using their respective init functions"
|
|
|
|
"initiates the scene data maps using their respective init functions"
|
|
|
|
[]
|
|
|
|
[]
|
|
|
|
(set! (.-width (:canvas gamestate)) (get-in gamestate [:dimensions :w]))
|
|
|
|
(set! (.-width (:canvas gamestate))
|
|
|
|
(set! (.-height (:canvas gamestate)) (get-in gamestate [:dimensions :h]))
|
|
|
|
(-> gamestate :dimensions :w))
|
|
|
|
|
|
|
|
(set! (.-height (:canvas gamestate))
|
|
|
|
|
|
|
|
(-> gamestate :dimensions :h))
|
|
|
|
(update gamestate
|
|
|
|
(update gamestate
|
|
|
|
:scenes
|
|
|
|
:scenes
|
|
|
|
#(reduce
|
|
|
|
#(reduce
|
|
|
|