small refactorings, indentation style

master
Josha von Gizycki 7 years ago
parent 9be3f6b848
commit 25adfdb6fc

@ -5,44 +5,34 @@
(enable-console-print!) (enable-console-print!)
(def gamestate { (def gamestate
: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
:timing { :timing {;; msecs of previous frame
; msecs of previous frame
:prev 0 :prev 0
; msecs of current frame ;; msecs of current frame
:now 0 :now 0
; fps resulting of prev and now ;; fps resulting of prev and now
:fps 0 :fps 0
; difference between prev and now in seconds ;; difference between prev and now in seconds
:elapsed 0 :elapsed 0}
} ;; width and height of the canvas
; width and height of the canvas :dimensions {:w 600
:dimensions { :h 400}
:w 600 :input {:dir :?}
:h 400 ;; currently active scene
}
:input {
:dir :?
}
; currently active scene
:scene :demo :scene :demo
:scenes { :scenes {:demo {:update demoscene/update-scene
:demo {
:update demoscene/update-scene
:draw demoscene/draw-scene :draw demoscene/draw-scene
:init demoscene/init :init demoscene/init
:data {} :data {}}}})
}
}
})
(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 state (assoc-in
state
[:timing timingkey] [:timing timingkey]
(.now js/performance))) (.now js/performance)))
@ -56,11 +46,10 @@
(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 gamestate (assoc-in
gamestate
[:timing :elapsed] [:timing :elapsed]
(/ (/ (-(get-in gamestate [:timing :now])
(-
(get-in gamestate [:timing :now])
(get-in gamestate [:timing :prev])) (get-in gamestate [:timing :prev]))
1000))) 1000)))
@ -119,14 +108,15 @@
[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 ;; calculate the duration of update-step and draw-step
; 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)]
(.setTimeout js/window (.setTimeout js/window
(fn [] (fn []
(.requestAnimationFrame js/window (.requestAnimationFrame
js/window
#(mainloop newstate))) #(mainloop newstate)))
(/ (/
(- 1000 duration) (- 1000 duration)
@ -142,7 +132,8 @@
(fn [scenes [scenekey scenedata]] (fn [scenes [scenekey scenedata]]
(let [initfunc (:init scenedata) (let [initfunc (:init scenedata)
newdata (initfunc gamestate scenedata)] newdata (initfunc gamestate scenedata)]
(assoc scenes (assoc
scenes
scenekey newdata))) scenekey newdata)))
{} {}
(:scenes gamestate)))) (:scenes gamestate))))

@ -5,43 +5,34 @@
[topdown2d.sprites :as sprites])) [topdown2d.sprites :as sprites]))
(defn init [gamestate scenedata] (defn init [gamestate scenedata]
(assoc scenedata (assoc
:data { scenedata
:player { :data
:x (- (/ (get-in gamestate [:dimensions :w]) 2) 32) {:player {:x (- (/ (get-in gamestate [:dimensions :w]) 2) 32)
:y (- (/ (get-in gamestate [:dimensions :h]) 2) 32) :y (- (/ (get-in gamestate [:dimensions :h]) 2) 32)
:w 64 :h 64 :w 64 :h 64
:d :s :d :s
:sprite { :sprite
:image (.getElementById js/document "demo-player") {:image (.getElementById js/document "demo-player")
:size 64 :size 64
:rows { :rows {:w 1 :e 3
:w 1 :e 3
:n 0 :s 2 :n 0 :s 2
:? 2 :? 2}
} :cycle {:pos 0
:cycle { :from 1
:pos 0 :count 8
:from 1 :count 8
:last-cycle 0 :last-cycle 0
; seconds per cycle ;; seconds per cycle
:spc 0.08 :spc 0.08}}}
} :viewport {:image (.getElementById js/document "demo-background")
} :keep-in {:x 0 :y 0
} :w 2239 :h 2235}
:viewport {
:image (.getElementById js/document "demo-background")
:keep-in {
:x 0 :y 0
:w 2239 :h 2235
}
:x 1 :y 1 :x 1 :y 1
:d :? :d :?
:pps 350 :pps 350
:w (get-in gamestate [:dimensions :w]) :w (get-in gamestate [:dimensions :w])
:h (get-in gamestate [:dimensions :h]) :h (get-in gamestate [:dimensions :h])
} }}))
}))
(defn update-player [gamestate player dir] (defn update-player [gamestate player dir]
(let [old-dir (:d player) (let [old-dir (:d player)
@ -49,8 +40,8 @@
(as-> player p (as-> player p
(assoc p :d new-dir) (assoc p :d new-dir)
(if (= :? dir) (if (= :? dir)
(sprites/reset-cycle player) (sprites/reset player)
(sprites/proc-cycle gamestate p))))) (sprites/proc gamestate p)))))
(defn update-viewport [gamestate viewport dir] (defn update-viewport [gamestate viewport dir]
(collision/move-inside (collision/move-inside

@ -5,13 +5,15 @@
(defn keydown? [code] (defn keydown? [code]
(get @keysdown (name code) false)) (get @keysdown (name code) false))
(.addEventListener js/document (.addEventListener
js/document
"keydown" "keydown"
(fn [event] (fn [event]
(swap! keysdown #(assoc % (.-code event) true)) (swap! keysdown #(assoc % (.-code event) true))
false)) false))
(.addEventListener js/document (.addEventListener
js/document
"keyup" "keyup"
(fn [event] (fn [event]
(swap! keysdown (swap! keysdown

@ -1,35 +1,42 @@
(ns topdown2d.sprites) (ns topdown2d.sprites)
(defn proc-cycle [gamestate obj] (defn- reset-cycle [obj]
(let [sprite (:sprite obj) (let [cycle (get-in obj [:sprite :cycle])
sprite-cycle (:cycle sprite) {:keys [pos from] maxpos :count} cycle
from (:from sprite-cycle) reset-position? (> (inc pos) maxpos)]
maxpos (:count sprite-cycle)
{:keys [pos spc last-cycle]} sprite-cycle
restart? (> (inc pos) maxpos)
elapsed (get-in gamestate [:timing :elapsed])]
; new sprite frame?
(if (> (+ last-cycle elapsed) spc)
; start cycle from new?
; reset last-cycle
(-> obj (-> obj
;; set position
(assoc-in (assoc-in
[:sprite :cycle :pos] [:sprite :cycle :pos]
(if restart? from (inc pos))) (if reset-position?
from
(inc pos)))
;; timestamp of last cycle is 0
(assoc-in (assoc-in
[:sprite :cycle :last-cycle] [:sprite :cycle :last-cycle]
0)) 0))))
(update-in obj
(defn proc [gamestate obj]
(let [sprite (:sprite obj)
sprite-cycle (:cycle sprite)
{:keys [spc last-cycle]} sprite-cycle
elapsed (get-in gamestate [:timing :elapsed])]
;; new sprite frame?
(if (> (+ last-cycle elapsed) spc)
(reset-cycle obj)
;; no new sprite, increase last-cycle
(update-in
obj
[:sprite :cycle :last-cycle] [:sprite :cycle :last-cycle]
#(+ % elapsed))))) #(+ % elapsed)))))
(defn reset-cycle [obj] (defn reset [obj]
(assoc-in (assoc-in
obj obj
[:sprite :cycle :pos] [:sprite :cycle :pos]
0)) 0))
(defn pos-in-sprite [sprite d] (defn- pos-in-sprite [sprite d]
(let [{:keys [size rows]} sprite (let [{:keys [size rows]} sprite
pos (get-in sprite [:cycle :pos]) pos (get-in sprite [:cycle :pos])
row (d rows)] row (d rows)]
@ -41,7 +48,8 @@
sprite-size (:size sprite) sprite-size (:size sprite)
sprite-cycle (:cycle sprite) sprite-cycle (:cycle sprite)
pos (pos-in-sprite sprite d)] pos (pos-in-sprite sprite d)]
(.drawImage ctx (.drawImage
ctx
image image
(:x pos) (:y pos) sprite-size sprite-size (:x pos) (:y pos) sprite-size sprite-size
x y sprite-size sprite-size))) x y sprite-size sprite-size)))

Loading…
Cancel
Save