refactor main loop, made mistakes, fixed weird thing deep down in collisions

master
Josha von Gizycki 5 years ago
parent 7e3431a163
commit 6e5bfecf21

@ -58,6 +58,7 @@
(let [prev (get-in gamestate [:timing :prev]) (let [prev (get-in gamestate [:timing :prev])
now (get-in gamestate [:timing :now]) now (get-in gamestate [:timing :now])
secs (/ (- now prev) 1000) secs (/ (- now prev) 1000)
secs (-> gamestate :timing :elapsed)
pps (:pps obj)] pps (:pps obj)]
(* pps secs))) (* pps secs)))

@ -29,68 +29,56 @@
(def reloaded (atom false)) (def reloaded (atom false))
(defn set-timing (defn curr-fps
"sets the current time at the given key"
[state timingkey]
(assoc-in state
[:timing timingkey]
(.now js/performance)))
(defn set-fps
"calculates the current fps using the elapsed time" "calculates the current fps using the elapsed time"
[state] [elapsed]
(let [elapsed (-> state :timing :elapsed) (/ 1 elapsed))
fps (/ 1 elapsed)]
(assoc-in state [:timing :fps] fps))) (defn elapsed-seconds
"calculates the elapsed seconds since the last frame"
[gamestate now]
(/ (- now (-> gamestate :timing :prev))
1000))
(defn set-elapsed-seconds (defn curr-scene
"calculates and writes the elapsed seconds since the last frame" "returns the current scene"
[gamestate] [gamestate]
(update gamestate (get-in gamestate [:scenes (:scene gamestate)]))
:timing
#(let [{:keys [now prev]} %]
(assoc % :elapsed
(/ (- now prev)
1000)))))
(defn update-scene (defn run-scene-update
"updates the current scene using its udpate function" "updates the current scene using its udpate function"
[gamestate] [gamestate scene]
(if-not (:continue? gamestate) ((:update scene) gamestate scene))
gamestate
(let [scenekey (:scene gamestate)
{updatefunc :update
:as scene} (get-in gamestate [:scenes scenekey])
newstate (updatefunc gamestate scene)]
(assoc-in gamestate [:scenes scenekey] newstate))))
(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] [prev-continue?]
(update
gamestate
:continue?
(fn [continue?]
(cond (cond
(and continue? (and prev-continue?
(input/keydown? :Digit2) (input/keydown? :Digit2)
(input/keydown? :ControlLeft)) false (input/keydown? :ControlLeft)) false
(and (not continue?) (and (not prev-continue?)
(input/keydown? :Digit3) (input/keydown? :Digit3)
(input/keydown? :ControlLeft)) true (input/keydown? :ControlLeft)) true
:else continue?)))) :else prev-continue?))
(defn update-step (defn update-step
"updates timing information and the current scene" "updates timing information and the current scene"
[gamestate] [gamestate]
(-> gamestate (let [now (.now js/performance)
(assoc-in [:input :dir] (input/dir)) secs (elapsed-seconds gamestate now)
(set-timing :now) scene (curr-scene gamestate)
(set-elapsed-seconds) continue? (continue-running? (:continue? gamestate))]
(set-fps) (as-> gamestate $
(continue-running?) (assoc-in $ [:input :dir] (input/dir))
(update-scene) (assoc $ :timing {:now now
(set-timing :prev))) :elapsed secs
:fps (curr-fps secs)})
(assoc-in $ [:scenes (:scene gamestate)]
(if continue?
(run-scene-update $ scene)
scene))
(assoc-in $ [:timing :prev] (.now js/performance)))))
(defn draw-fps (defn draw-fps
"draws the current fps" "draws the current fps"
@ -123,7 +111,6 @@
then, it calls itself again using requestAnimationFrame" then, it calls itself again using requestAnimationFrame"
[gamestate] [gamestate]
(let [newstate (update-step gamestate)] (let [newstate (update-step gamestate)]
(set! (.-imageSmoothingEnabled (:ctx gamestate)) false)
(draw-step newstate) (draw-step newstate)
(when-not @reloaded (when-not @reloaded
(.requestAnimationFrame js/window (.requestAnimationFrame js/window
@ -136,6 +123,8 @@
(-> gamestate :dimensions :w)) (-> gamestate :dimensions :w))
(set! (.-height (:canvas gamestate)) (set! (.-height (:canvas gamestate))
(-> gamestate :dimensions :h)) (-> gamestate :dimensions :h))
(set! (.-imageSmoothingEnabled (:ctx gamestate))
false)
(update gamestate (update gamestate
:scenes :scenes
#(reduce #(reduce

Loading…
Cancel
Save