|
|
@ -31,10 +31,9 @@
|
|
|
|
(defn set-timing
|
|
|
|
(defn set-timing
|
|
|
|
"sets the current time at the given key"
|
|
|
|
"sets the current time at the given key"
|
|
|
|
[state timingkey]
|
|
|
|
[state timingkey]
|
|
|
|
(assoc-in
|
|
|
|
(assoc-in state
|
|
|
|
state
|
|
|
|
[:timing timingkey]
|
|
|
|
[:timing timingkey]
|
|
|
|
(.now js/performance)))
|
|
|
|
(.now js/performance)))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(defn set-fps
|
|
|
|
(defn set-fps
|
|
|
|
"calculates the current fps using the elapsed time"
|
|
|
|
"calculates the current fps using the elapsed time"
|
|
|
@ -46,12 +45,12 @@
|
|
|
|
(defn set-elapsed-seconds
|
|
|
|
(defn set-elapsed-seconds
|
|
|
|
"calculates and writes the elapsed seconds since the last frame"
|
|
|
|
"calculates and writes the elapsed seconds since the last frame"
|
|
|
|
[gamestate]
|
|
|
|
[gamestate]
|
|
|
|
(assoc-in
|
|
|
|
(update gamestate
|
|
|
|
gamestate
|
|
|
|
:timing
|
|
|
|
[:timing :elapsed]
|
|
|
|
#(let [{:keys [now prev]} %]
|
|
|
|
(/ (-(get-in gamestate [:timing :now])
|
|
|
|
(assoc % :elapsed
|
|
|
|
(get-in gamestate [:timing :prev]))
|
|
|
|
(/ (- now prev)
|
|
|
|
1000)))
|
|
|
|
1000)))))
|
|
|
|
|
|
|
|
|
|
|
|
(defn update-scene
|
|
|
|
(defn update-scene
|
|
|
|
"updates the current scene using its udpate function"
|
|
|
|
"updates the current scene using its udpate function"
|
|
|
@ -59,9 +58,9 @@
|
|
|
|
(if-not (:continue? gamestate)
|
|
|
|
(if-not (:continue? gamestate)
|
|
|
|
gamestate
|
|
|
|
gamestate
|
|
|
|
(let [scenekey (:scene gamestate)
|
|
|
|
(let [scenekey (:scene gamestate)
|
|
|
|
scenestate (get-in gamestate [:scenes scenekey])
|
|
|
|
{updatefunc :update
|
|
|
|
updatefunc (:update scenestate)
|
|
|
|
:as scene} (get-in gamestate [:scenes scenekey])
|
|
|
|
newstate (updatefunc gamestate scenestate)]
|
|
|
|
newstate (updatefunc gamestate scene)]
|
|
|
|
(assoc-in gamestate [:scenes scenekey] newstate))))
|
|
|
|
(assoc-in gamestate [:scenes scenekey] newstate))))
|
|
|
|
|
|
|
|
|
|
|
|
(defn continue-running?
|
|
|
|
(defn continue-running?
|
|
|
@ -74,14 +73,11 @@
|
|
|
|
(cond
|
|
|
|
(cond
|
|
|
|
(and continue?
|
|
|
|
(and continue?
|
|
|
|
(input/keydown? :Digit2)
|
|
|
|
(input/keydown? :Digit2)
|
|
|
|
(input/keydown? :ControlLeft))
|
|
|
|
(input/keydown? :ControlLeft)) false
|
|
|
|
false
|
|
|
|
|
|
|
|
(and (not continue?)
|
|
|
|
(and (not continue?)
|
|
|
|
(input/keydown? :Digit3)
|
|
|
|
(input/keydown? :Digit3)
|
|
|
|
(input/keydown? :ControlLeft))
|
|
|
|
(input/keydown? :ControlLeft)) true
|
|
|
|
true
|
|
|
|
:else continue?))))
|
|
|
|
:else
|
|
|
|
|
|
|
|
continue?))))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(defn update-step
|
|
|
|
(defn update-step
|
|
|
|
"updates timing information and the current scene"
|
|
|
|
"updates timing information and the current scene"
|
|
|
@ -118,11 +114,22 @@
|
|
|
|
(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)
|
|
|
|
scene (get-in gamestate [:scenes scenekey])
|
|
|
|
{:keys [draw] :as scene} (get-in gamestate [:scenes scenekey])]
|
|
|
|
drawfunc (:draw scene)]
|
|
|
|
(draw gamestate scene))
|
|
|
|
(drawfunc 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]
|
|
|
|
|
|
|
|
(if (:continue? gamestate)
|
|
|
|
|
|
|
|
(/ (- 1000 (- (.now js/performance)
|
|
|
|
|
|
|
|
(get-in gamestate [:timing :now])))
|
|
|
|
|
|
|
|
(: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.
|
|
|
@ -130,35 +137,21 @@
|
|
|
|
[gamestate]
|
|
|
|
[gamestate]
|
|
|
|
(let [newstate (update-step gamestate)]
|
|
|
|
(let [newstate (update-step gamestate)]
|
|
|
|
(draw-step newstate)
|
|
|
|
(draw-step newstate)
|
|
|
|
;; calculate the duration of update-step and draw-step
|
|
|
|
(.setTimeout js/window
|
|
|
|
;; substract that from the wait time to reach target-fps
|
|
|
|
(fn []
|
|
|
|
;; more accurately
|
|
|
|
(.requestAnimationFrame
|
|
|
|
(let [now (get-in newstate [:timing :now])
|
|
|
|
js/window
|
|
|
|
duration (- (.now js/performance) now)
|
|
|
|
#(mainloop newstate)))
|
|
|
|
timeout (if (:continue? newstate)
|
|
|
|
(timeout newstate))))
|
|
|
|
(/
|
|
|
|
|
|
|
|
(- 1000 duration)
|
|
|
|
|
|
|
|
(:target-fps newstate))
|
|
|
|
|
|
|
|
5000)]
|
|
|
|
|
|
|
|
(.setTimeout js/window
|
|
|
|
|
|
|
|
(fn []
|
|
|
|
|
|
|
|
(.requestAnimationFrame
|
|
|
|
|
|
|
|
js/window
|
|
|
|
|
|
|
|
#(mainloop newstate)))
|
|
|
|
|
|
|
|
timeout))))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(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"
|
|
|
|
[]
|
|
|
|
[]
|
|
|
|
(assoc
|
|
|
|
(update gamestate
|
|
|
|
gamestate
|
|
|
|
:scenes
|
|
|
|
:scenes
|
|
|
|
#(reduce
|
|
|
|
(reduce
|
|
|
|
(fn [carr [key scene]]
|
|
|
|
(fn [scenes [scenekey scenestate]]
|
|
|
|
(assoc carr key ((:init scene) gamestate scene)))
|
|
|
|
(let [initfunc (:init scenestate)
|
|
|
|
{} %)))
|
|
|
|
newstate (initfunc gamestate scenestate)]
|
|
|
|
|
|
|
|
(assoc scenes scenekey newstate)))
|
|
|
|
|
|
|
|
{}
|
|
|
|
|
|
|
|
(:scenes gamestate))))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(mainloop (init-scenes))
|
|
|
|
(mainloop (init-scenes))
|
|
|
|