some codestyle

master
Josha von Gizycki 5 years ago
parent 2539099cb8
commit 32cf4937f8

@ -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

@ -81,11 +81,13 @@
(update-in [:player :y] - (* zoom (/ th 2))) (update-in [:player :y] - (* zoom (/ th 2)))
(assoc-in [:player :animation :tileset-def] (assoc-in [:player :animation :tileset-def]
(tileset/map-def->tileset map-def (tileset/map-def->tileset map-def
(get-in scenestate (-> scenestate
[:player :animation :tileset])))))) :player
:animation
:tileset))))))
(defn init-map-def [scenestate] (defn init-map-def [scenestate]
(if (and (not (get-in scenestate [:map-def :def])) (if (and (not (-> scenestate :map-def :def))
(some? @map-def)) (some? @map-def))
(let [loaded-def (walk/keywordize-keys @map-def) (let [loaded-def (walk/keywordize-keys @map-def)
zoom (get-in scenestate [:map-def :zoom]) zoom (get-in scenestate [:map-def :zoom])
@ -104,7 +106,7 @@
(let [new-scenestate (init-map-def scenestate) (let [new-scenestate (init-map-def scenestate)
player (:player new-scenestate) player (:player new-scenestate)
viewport (:viewport new-scenestate) viewport (:viewport new-scenestate)
dir (get-in gamestate [:input :dir])] dir (-> gamestate :input :dir)]
(assoc new-scenestate (assoc new-scenestate
:player (update-player gamestate player dir) :player (update-player gamestate player dir)
:viewport (update-viewport gamestate viewport dir)))) :viewport (update-viewport gamestate viewport dir))))
@ -113,12 +115,12 @@
(let [viewport (:viewport scenestate) (let [viewport (:viewport scenestate)
{:keys [x y w h background]} viewport {:keys [x y w h background]} viewport
ctx (:ctx gamestate)] ctx (:ctx gamestate)]
(when (get-in scenestate [:map-def :def]) (when (-> scenestate :map-def :def)
(tileset/draw-viewport (:map-def scenestate) (tileset/draw-viewport (:map-def scenestate)
ctx ctx
viewport) viewport)
(let [{:keys [tileset-def curr-id image]} (let [{:keys [tileset-def curr-id image]}
(get-in scenestate [:player :animation])] (-> scenestate :player :animation)]
(tileset/draw-tile ctx (tileset/draw-tile ctx
tileset-def tileset-def
image image

Loading…
Cancel
Save