small refactorings, indentation style

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

@ -1,50 +1,40 @@
(ns topdown2d.core (ns topdown2d.core
(:require (:require
[topdown2d.demoscene :as demoscene] [topdown2d.demoscene :as demoscene]
[topdown2d.input :as input])) [topdown2d.input :as input]))
(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
} :dimensions {:w 600
; width and height of the canvas :h 400}
:dimensions { :input {:dir :?}
:w 600 ;; currently active scene
:h 400 :scene :demo
} :scenes {:demo {:update demoscene/update-scene
:input { :draw demoscene/draw-scene
:dir :? :init demoscene/init
} :data {}}}})
; currently active scene
:scene :demo
:scenes {
:demo {
:update demoscene/update-scene
:draw demoscene/draw-scene
:init demoscene/init
: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
[:timing timingkey] state
(.now js/performance))) [:timing timingkey]
(.now js/performance)))
(defn set-fps (defn set-fps
"calculates the current fps using the elapsed time" "calculates the current fps using the elapsed time"
@ -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
[:timing :elapsed] gamestate
(/ [:timing :elapsed]
(- (/ (-(get-in gamestate [:timing :now])
(get-in gamestate [:timing :now])
(get-in gamestate [:timing :prev])) (get-in gamestate [:timing :prev]))
1000))) 1000)))
@ -77,12 +66,12 @@
"updates timing information and the current scene" "updates timing information and the current scene"
[gamestate] [gamestate]
(-> gamestate (-> gamestate
(assoc-in [:input :dir] (input/dir)) (assoc-in [:input :dir] (input/dir))
(set-timing :now) (set-timing :now)
(set-elapsed-seconds) (set-elapsed-seconds)
(set-fps) (set-fps)
(update-scene) (update-scene)
(set-timing :prev))) (set-timing :prev)))
(defn draw-fps (defn draw-fps
"draws the current fps" "draws the current fps"
@ -90,22 +79,22 @@
(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)))
(defn draw-step (defn draw-step
"clears the canvas, draws fps and invokes the scene draw function" "clears the canvas, draws fps and invokes the scene draw function"
[gamestate] [gamestate]
(.clearRect (:ctx gamestate) (.clearRect (:ctx gamestate)
0 0 0 0
(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)
scene (scenekey (:scenes gamestate)) scene (scenekey (:scenes gamestate))
drawfunc (:draw scene)] drawfunc (:draw scene)]
@ -119,32 +108,34 @@
[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
#(mainloop newstate))) js/window
(/ #(mainloop newstate)))
(- 1000 duration) (/
(:target-fps gamestate)))))) (- 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"
[] []
(assoc (assoc
gamestate gamestate
:scenes :scenes
(reduce (reduce
(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
scenekey newdata))) scenes
{} scenekey newdata)))
(:scenes gamestate)))) {}
(:scenes gamestate))))
(mainloop (init-scenes)) (mainloop (init-scenes))

@ -1,47 +1,38 @@
(ns topdown2d.demoscene (ns topdown2d.demoscene
(:require (:require
[topdown2d.collision :as collision] [topdown2d.collision :as collision]
[topdown2d.input :as input] [topdown2d.input :as input]
[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
} :from 1
:cycle { :count 8
:pos 0 :last-cycle 0
:from 1 :count 8 ;; seconds per cycle
:last-cycle 0 :spc 0.08}}}
; seconds per cycle :viewport {:image (.getElementById js/document "demo-background")
:spc 0.08 :keep-in {:x 0 :y 0
} :w 2239 :h 2235}
} :x 1 :y 1
} :d :?
:viewport { :pps 350
:image (.getElementById js/document "demo-background") :w (get-in gamestate [:dimensions :w])
:keep-in { :h (get-in gamestate [:dimensions :h])
:x 0 :y 0 }}))
:w 2239 :h 2235
}
:x 1 :y 1
:d :?
:pps 350
:w (get-in gamestate [:dimensions :w])
: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,33 +40,33 @@
(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
(assoc viewport :d dir) (assoc viewport :d dir)
(:keep-in viewport) (:keep-in viewport)
(collision/pps->px gamestate viewport))) (collision/pps->px gamestate viewport)))
(defn update-scene [gamestate scenedata] (defn update-scene [gamestate scenedata]
(let [player (get-in scenedata [:data :player]) (let [player (get-in scenedata [:data :player])
viewport (get-in scenedata [:data :viewport]) viewport (get-in scenedata [:data :viewport])
dir (get-in gamestate [:input :dir])] dir (get-in gamestate [:input :dir])]
(-> scenedata (-> scenedata
(assoc-in [:data :player] (assoc-in [:data :player]
(update-player gamestate player dir)) (update-player gamestate player dir))
(assoc-in [:data :viewport] (assoc-in [:data :viewport]
(update-viewport gamestate viewport dir))))) (update-viewport gamestate viewport dir)))))
(defn draw-scene [gamestate scenedata] (defn draw-scene [gamestate scenedata]
(let [viewport (get-in scenedata [:data :viewport]) (let [viewport (get-in scenedata [:data :viewport])
{:keys [x y w h background]} viewport] {:keys [x y w h background]} viewport]
(.drawImage (.drawImage
(:ctx gamestate) (:ctx gamestate)
(:image viewport) (:image viewport)
x y w h x y w h
0 0 w h)) 0 0 w h))
(sprites/draw (sprites/draw
(:ctx gamestate) (:ctx gamestate)
(get-in scenedata [:data :player]))) (get-in scenedata [:data :player])))

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

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

Loading…
Cancel
Save