fix gameloop resuming after 5 seconds, stop updating the scene while paused

master
Josha von Gizycki 7 years ago
parent 3ae3a8299e
commit 1493b0f1a1

@ -9,7 +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 :continue? true
:timing {;; msecs of previous frame :timing {;; msecs of previous frame
:prev 0 :prev 0
;; msecs of current frame ;; msecs of current frame
@ -57,30 +57,32 @@
(defn update-scene (defn update-scene
"updates the current scene using its udpate function" "updates the current scene using its udpate function"
[gamestate] [gamestate]
(let [scenekey (:scene gamestate) (if-not (:continue? gamestate)
scenedata (get-in gamestate [:scenes scenekey]) gamestate
updatefunc (:update scenedata) (let [scenekey (:scene gamestate)
newdata (updatefunc gamestate scenedata)] scenedata (get-in gamestate [:scenes scenekey])
(assoc-in gamestate [:scenes scenekey] newdata))) updatefunc (:update scenedata)
newdata (updatefunc gamestate scenedata)]
(assoc-in gamestate [:scenes scenekey] newdata))))
(defn continue-running? (defn continue-running?
"checks if the gameloop should keep running, based on input" "checks if the gameloop should keep running, based on input"
[gamestate] [gamestate]
(update (update
gamestate gamestate
:running :continue?
(fn [running] (fn [continue?]
(cond (cond
(and running (and continue?
(input/keydown? :Digit2) (input/keydown? :Digit2)
(input/keydown? :ControlLeft)) (input/keydown? :ControlLeft))
false false
(and (not running) (and (not continue?)
(input/keydown? :Digit3) (input/keydown? :Digit3)
(input/keydown? :ControlLeft)) (input/keydown? :ControlLeft))
true true
:else :else
true)))) continue?))))
(defn update-step (defn update-step
"updates timing information and the current scene" "updates timing information and the current scene"
@ -134,12 +136,10 @@
;; 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) timeout (if (:continue? newstate)
tfps (:target-fps gamestate)
timeout (if continue?
(/ (/
(- 1000 duration) (- 1000 duration)
(:target-fps gamestate)) (:target-fps newstate))
5000)] 5000)]
(.setTimeout js/window (.setTimeout js/window
(fn [] (fn []

Loading…
Cancel
Save