code style, sprites calculate speed in cycles per second

shit's working now, i promise
master
Josha von Gizycki 6 years ago
parent 71160351d1
commit 71f8759bd5

@ -31,10 +31,9 @@
(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 (assoc-in state
state [:timing timingkey]
[:timing timingkey] (.now js/performance)))
(.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"
@ -46,12 +45,12 @@
(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 (update gamestate
gamestate :timing
[:timing :elapsed] #(let [{:keys [now prev]} %]
(/ (-(get-in gamestate [:timing :now]) (assoc % :elapsed
(get-in gamestate [:timing :prev])) (/ (- now prev)
1000))) 1000)))))
(defn update-scene (defn update-scene
"updates the current scene using its udpate function" "updates the current scene using its udpate function"
@ -59,9 +58,9 @@
(if-not (:continue? gamestate) (if-not (:continue? gamestate)
gamestate gamestate
(let [scenekey (:scene gamestate) (let [scenekey (:scene gamestate)
scenestate (get-in gamestate [:scenes scenekey]) {updatefunc :update
updatefunc (:update scenestate) :as scene} (get-in gamestate [:scenes scenekey])
newstate (updatefunc gamestate scenestate)] newstate (updatefunc gamestate scene)]
(assoc-in gamestate [:scenes scenekey] newstate)))) (assoc-in gamestate [:scenes scenekey] newstate))))
(defn continue-running? (defn continue-running?
@ -74,14 +73,11 @@
(cond (cond
(and continue? (and continue?
(input/keydown? :Digit2) (input/keydown? :Digit2)
(input/keydown? :ControlLeft)) (input/keydown? :ControlLeft)) false
false
(and (not continue?) (and (not continue?)
(input/keydown? :Digit3) (input/keydown? :Digit3)
(input/keydown? :ControlLeft)) (input/keydown? :ControlLeft)) true
true :else continue?))))
:else
continue?))))
(defn update-step (defn update-step
"updates timing information and the current scene" "updates timing information and the current scene"
@ -118,11 +114,22 @@
(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 (get-in gamestate [:scenes scenekey]) {:keys [draw] :as scene} (get-in gamestate [:scenes scenekey])]
drawfunc (:draw scene)] (draw gamestate scene))
(drawfunc 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]
(if (:continue? gamestate)
(/ (- 1000 (- (.now js/performance)
(get-in gamestate [:timing :now])))
(: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.
@ -130,35 +137,21 @@
[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 (.setTimeout js/window
;; substract that from the wait time to reach target-fps (fn []
;; more accurately (.requestAnimationFrame
(let [now (get-in newstate [:timing :now]) js/window
duration (- (.now js/performance) now) #(mainloop newstate)))
timeout (if (:continue? newstate) (timeout newstate))))
(/
(- 1000 duration)
(:target-fps newstate))
5000)]
(.setTimeout js/window
(fn []
(.requestAnimationFrame
js/window
#(mainloop newstate)))
timeout))))
(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 (update gamestate
gamestate :scenes
:scenes #(reduce
(reduce (fn [carr [key scene]]
(fn [scenes [scenekey scenestate]] (assoc carr key ((:init scene) gamestate scene)))
(let [initfunc (:init scenestate) {} %)))
newstate (initfunc gamestate scenestate)]
(assoc scenes scenekey newstate)))
{}
(:scenes gamestate))))
(mainloop (init-scenes)) (mainloop (init-scenes))

@ -21,15 +21,15 @@
:from 1 :from 1
:count 8 :count 8
:last-cycle 0 :last-cycle 0
;; seconds per cycle ;; cycles per second
:spc 0.08}}} :cps 10}}}
:viewport {:image (.getElementById js/document "demo-background") :viewport {:image (.getElementById js/document "demo-background")
:keep-in {:x 0 :y 0 :keep-in {:x 0 :y 0
:w 2239 :h 2235} :w 2239 :h 2235}
:x 1 :y 1 :x 1 :y 1
:d :? :d :?
;; pixels per second ;; pixels per second
:pps 350 :pps 150
:w (get-in gamestate [:dimensions :w]) :w (get-in gamestate [:dimensions :w])
:h (get-in gamestate [:dimensions :h])}})) :h (get-in gamestate [:dimensions :h])}}))

@ -3,38 +3,30 @@
(defn- reset-cycle [obj] (defn- reset-cycle [obj]
(let [cycle (get-in obj [:sprite :cycle]) (let [cycle (get-in obj [:sprite :cycle])
{:keys [pos from] maxpos :count} cycle {:keys [pos from] maxpos :count} cycle
reset-position? (> (inc pos) maxpos)] reset? (> (inc pos) maxpos)
new-pos (if reset? from (inc pos))]
(-> obj (-> obj
;; set position ;; set position
(assoc-in (assoc-in [:sprite :cycle :pos] new-pos)
[:sprite :cycle :pos]
(if reset-position?
from
(inc pos)))
;; timestamp of last cycle is 0 ;; timestamp of last cycle is 0
(assoc-in (assoc-in [:sprite :cycle :last-cycle] 0))))
[:sprite :cycle :last-cycle]
0))))
(defn proc [gamestate obj] (defn proc [gamestate obj]
(let [sprite (:sprite obj) (let [cycle (get-in obj [:sprite :cycle])
sprite-cycle (:cycle sprite) {:keys [cps last-cycle]} cycle
{:keys [spc last-cycle]} sprite-cycle
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) (/ 1 cps))
(reset-cycle obj) (reset-cycle obj)
;; no new sprite, increase last-cycle ;; no new sprite, increase last-cycle
(update-in (update-in obj
obj [:sprite :cycle :last-cycle]
[:sprite :cycle :last-cycle] + elapsed))))
#(+ % elapsed)))))
(defn reset [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
@ -43,13 +35,11 @@
{:y (* row size) :x (* pos size)})) {:y (* row size) :x (* pos size)}))
(defn draw [ctx obj] (defn draw [ctx obj]
(let [{:keys [x y w h d sprite]} obj (let [{:keys [x y w h d]
image (:image sprite) {:keys [image size cycle] :as sprite} :sprite} obj
sprite-size (:size sprite)
sprite-cycle (:cycle sprite)
pos (pos-in-sprite sprite d)] pos (pos-in-sprite sprite d)]
(.drawImage (.drawImage
ctx ctx
image image
(:x pos) (:y pos) sprite-size sprite-size (:x pos) (:y pos) size size
x y sprite-size sprite-size))) x y size size)))

Loading…
Cancel
Save