soft-pausing the gameloop with ctrl-2, continue running the gameloop with ctrl-3

master
Josha von Gizycki 7 years ago
parent 25adfdb6fc
commit 3ae3a8299e

@ -9,6 +9,7 @@
{:canvas (.getElementById js/document "gamecanvas") {:canvas (.getElementById js/document "gamecanvas")
:ctx (.getContext (.getElementById js/document "gamecanvas") "2d") :ctx (.getContext (.getElementById js/document "gamecanvas") "2d")
:target-fps 40 :target-fps 40
:running true
:timing {;; msecs of previous frame :timing {;; msecs of previous frame
:prev 0 :prev 0
;; msecs of current frame ;; msecs of current frame
@ -62,6 +63,25 @@
newdata (updatefunc gamestate scenedata)] newdata (updatefunc gamestate scenedata)]
(assoc-in gamestate [:scenes scenekey] newdata))) (assoc-in gamestate [:scenes scenekey] newdata)))
(defn continue-running?
"checks if the gameloop should keep running, based on input"
[gamestate]
(update
gamestate
:running
(fn [running]
(cond
(and running
(input/keydown? :Digit2)
(input/keydown? :ControlLeft))
false
(and (not running)
(input/keydown? :Digit3)
(input/keydown? :ControlLeft))
true
:else
true))))
(defn update-step (defn update-step
"updates timing information and the current scene" "updates timing information and the current scene"
[gamestate] [gamestate]
@ -70,6 +90,7 @@
(set-timing :now) (set-timing :now)
(set-elapsed-seconds) (set-elapsed-seconds)
(set-fps) (set-fps)
(continue-running?)
(update-scene) (update-scene)
(set-timing :prev))) (set-timing :prev)))
@ -112,15 +133,20 @@
;; substract that from the wait time to reach target-fps ;; substract that from the wait time to reach target-fps
;; more accurately ;; more accurately
(let [now (get-in newstate [:timing :now]) (let [now (get-in newstate [:timing :now])
duration (- (.now js/performance) now)] duration (- (.now js/performance) now)
continue? (:running newstate)
tfps (:target-fps gamestate)
timeout (if continue?
(/
(- 1000 duration)
(:target-fps gamestate))
5000)]
(.setTimeout js/window (.setTimeout js/window
(fn [] (fn []
(.requestAnimationFrame (.requestAnimationFrame
js/window js/window
#(mainloop newstate))) #(mainloop newstate)))
(/ timeout))))
(- 1000 duration)
(:target-fps gamestate))))))
(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"

Loading…
Cancel
Save