|
|
|
@ -1,50 +1,40 @@
|
|
|
|
|
(ns topdown2d.core
|
|
|
|
|
(:require
|
|
|
|
|
[topdown2d.demoscene :as demoscene]
|
|
|
|
|
[topdown2d.input :as input]))
|
|
|
|
|
[topdown2d.demoscene :as demoscene]
|
|
|
|
|
[topdown2d.input :as input]))
|
|
|
|
|
|
|
|
|
|
(enable-console-print!)
|
|
|
|
|
|
|
|
|
|
(def gamestate {
|
|
|
|
|
:canvas (.getElementById js/document "gamecanvas")
|
|
|
|
|
:ctx (.getContext (.getElementById js/document "gamecanvas") "2d")
|
|
|
|
|
:target-fps 40
|
|
|
|
|
:timing {
|
|
|
|
|
; msecs of previous frame
|
|
|
|
|
:prev 0
|
|
|
|
|
; msecs of current frame
|
|
|
|
|
:now 0
|
|
|
|
|
; fps resulting of prev and now
|
|
|
|
|
:fps 0
|
|
|
|
|
; difference between prev and now in seconds
|
|
|
|
|
:elapsed 0
|
|
|
|
|
}
|
|
|
|
|
; width and height of the canvas
|
|
|
|
|
:dimensions {
|
|
|
|
|
:w 600
|
|
|
|
|
:h 400
|
|
|
|
|
}
|
|
|
|
|
:input {
|
|
|
|
|
:dir :?
|
|
|
|
|
}
|
|
|
|
|
; currently active scene
|
|
|
|
|
:scene :demo
|
|
|
|
|
:scenes {
|
|
|
|
|
:demo {
|
|
|
|
|
:update demoscene/update-scene
|
|
|
|
|
:draw demoscene/draw-scene
|
|
|
|
|
:init demoscene/init
|
|
|
|
|
:data {}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
(def gamestate
|
|
|
|
|
{:canvas (.getElementById js/document "gamecanvas")
|
|
|
|
|
:ctx (.getContext (.getElementById js/document "gamecanvas") "2d")
|
|
|
|
|
:target-fps 40
|
|
|
|
|
:timing {;; msecs of previous frame
|
|
|
|
|
:prev 0
|
|
|
|
|
;; msecs of current frame
|
|
|
|
|
:now 0
|
|
|
|
|
;; fps resulting of prev and now
|
|
|
|
|
:fps 0
|
|
|
|
|
;; difference between prev and now in seconds
|
|
|
|
|
:elapsed 0}
|
|
|
|
|
;; width and height of the canvas
|
|
|
|
|
:dimensions {:w 600
|
|
|
|
|
:h 400}
|
|
|
|
|
:input {:dir :?}
|
|
|
|
|
;; currently active scene
|
|
|
|
|
:scene :demo
|
|
|
|
|
:scenes {:demo {:update demoscene/update-scene
|
|
|
|
|
:draw demoscene/draw-scene
|
|
|
|
|
:init demoscene/init
|
|
|
|
|
:data {}}}})
|
|
|
|
|
|
|
|
|
|
(defn set-timing
|
|
|
|
|
"sets the current time at the given key"
|
|
|
|
|
[state timingkey]
|
|
|
|
|
(assoc-in state
|
|
|
|
|
[:timing timingkey]
|
|
|
|
|
(.now js/performance)))
|
|
|
|
|
(assoc-in
|
|
|
|
|
state
|
|
|
|
|
[:timing timingkey]
|
|
|
|
|
(.now js/performance)))
|
|
|
|
|
|
|
|
|
|
(defn set-fps
|
|
|
|
|
"calculates the current fps using the elapsed time"
|
|
|
|
@ -56,11 +46,10 @@
|
|
|
|
|
(defn set-elapsed-seconds
|
|
|
|
|
"calculates and writes the elapsed seconds since the last frame"
|
|
|
|
|
[gamestate]
|
|
|
|
|
(assoc-in gamestate
|
|
|
|
|
[:timing :elapsed]
|
|
|
|
|
(/
|
|
|
|
|
(-
|
|
|
|
|
(get-in gamestate [:timing :now])
|
|
|
|
|
(assoc-in
|
|
|
|
|
gamestate
|
|
|
|
|
[:timing :elapsed]
|
|
|
|
|
(/ (-(get-in gamestate [:timing :now])
|
|
|
|
|
(get-in gamestate [:timing :prev]))
|
|
|
|
|
1000)))
|
|
|
|
|
|
|
|
|
@ -77,12 +66,12 @@
|
|
|
|
|
"updates timing information and the current scene"
|
|
|
|
|
[gamestate]
|
|
|
|
|
(-> gamestate
|
|
|
|
|
(assoc-in [:input :dir] (input/dir))
|
|
|
|
|
(set-timing :now)
|
|
|
|
|
(set-elapsed-seconds)
|
|
|
|
|
(set-fps)
|
|
|
|
|
(update-scene)
|
|
|
|
|
(set-timing :prev)))
|
|
|
|
|
(assoc-in [:input :dir] (input/dir))
|
|
|
|
|
(set-timing :now)
|
|
|
|
|
(set-elapsed-seconds)
|
|
|
|
|
(set-fps)
|
|
|
|
|
(update-scene)
|
|
|
|
|
(set-timing :prev)))
|
|
|
|
|
|
|
|
|
|
(defn draw-fps
|
|
|
|
|
"draws the current fps"
|
|
|
|
@ -90,22 +79,22 @@
|
|
|
|
|
(let [ctx (:ctx gamestate)]
|
|
|
|
|
(aset ctx "fillStyle" "white")
|
|
|
|
|
(.fillRect
|
|
|
|
|
ctx
|
|
|
|
|
0 0 13 13)
|
|
|
|
|
ctx
|
|
|
|
|
0 0 13 13)
|
|
|
|
|
(aset ctx "fillStyle" "black")
|
|
|
|
|
(aset ctx "font" "10px monospace")
|
|
|
|
|
(.fillText
|
|
|
|
|
(:ctx gamestate)
|
|
|
|
|
(int (get-in gamestate [:timing :fps]))
|
|
|
|
|
0 10)))
|
|
|
|
|
(:ctx gamestate)
|
|
|
|
|
(int (get-in gamestate [:timing :fps]))
|
|
|
|
|
0 10)))
|
|
|
|
|
|
|
|
|
|
(defn draw-step
|
|
|
|
|
"clears the canvas, draws fps and invokes the scene draw function"
|
|
|
|
|
[gamestate]
|
|
|
|
|
(.clearRect (:ctx gamestate)
|
|
|
|
|
0 0
|
|
|
|
|
(get-in gamestate [:dimensions :w])
|
|
|
|
|
(get-in gamestate [:dimensions :h]))
|
|
|
|
|
0 0
|
|
|
|
|
(get-in gamestate [:dimensions :w])
|
|
|
|
|
(get-in gamestate [:dimensions :h]))
|
|
|
|
|
(let [scenekey (:scene gamestate)
|
|
|
|
|
scene (scenekey (:scenes gamestate))
|
|
|
|
|
drawfunc (:draw scene)]
|
|
|
|
@ -119,32 +108,34 @@
|
|
|
|
|
[gamestate]
|
|
|
|
|
(let [newstate (update-step gamestate)]
|
|
|
|
|
(draw-step newstate)
|
|
|
|
|
; calculate the duration of update-step and draw-step
|
|
|
|
|
; substract that from the wait time to reach target-fps
|
|
|
|
|
; more accurately
|
|
|
|
|
;; calculate the duration of update-step and draw-step
|
|
|
|
|
;; substract that from the wait time to reach target-fps
|
|
|
|
|
;; more accurately
|
|
|
|
|
(let [now (get-in newstate [:timing :now])
|
|
|
|
|
duration (- (.now js/performance) now)]
|
|
|
|
|
(.setTimeout js/window
|
|
|
|
|
(fn []
|
|
|
|
|
(.requestAnimationFrame js/window
|
|
|
|
|
#(mainloop newstate)))
|
|
|
|
|
(/
|
|
|
|
|
(- 1000 duration)
|
|
|
|
|
(:target-fps gamestate))))))
|
|
|
|
|
(fn []
|
|
|
|
|
(.requestAnimationFrame
|
|
|
|
|
js/window
|
|
|
|
|
#(mainloop newstate)))
|
|
|
|
|
(/
|
|
|
|
|
(- 1000 duration)
|
|
|
|
|
(:target-fps gamestate))))))
|
|
|
|
|
|
|
|
|
|
(defn init-scenes
|
|
|
|
|
"initiates the scene data maps using their respective init functions"
|
|
|
|
|
[]
|
|
|
|
|
(assoc
|
|
|
|
|
gamestate
|
|
|
|
|
:scenes
|
|
|
|
|
(reduce
|
|
|
|
|
(fn [scenes [scenekey scenedata]]
|
|
|
|
|
(let [initfunc (:init scenedata)
|
|
|
|
|
newdata (initfunc gamestate scenedata)]
|
|
|
|
|
(assoc scenes
|
|
|
|
|
scenekey newdata)))
|
|
|
|
|
{}
|
|
|
|
|
(:scenes gamestate))))
|
|
|
|
|
gamestate
|
|
|
|
|
:scenes
|
|
|
|
|
(reduce
|
|
|
|
|
(fn [scenes [scenekey scenedata]]
|
|
|
|
|
(let [initfunc (:init scenedata)
|
|
|
|
|
newdata (initfunc gamestate scenedata)]
|
|
|
|
|
(assoc
|
|
|
|
|
scenes
|
|
|
|
|
scenekey newdata)))
|
|
|
|
|
{}
|
|
|
|
|
(:scenes gamestate))))
|
|
|
|
|
|
|
|
|
|
(mainloop (init-scenes))
|
|
|
|
|