new fps calculation, objects move with pixels per second speed

master
Josha von Gizycki 7 years ago
parent 7eb713ed39
commit c370d37157

@ -8,10 +8,12 @@
(def gamestate { (def gamestate {
:canvas (.getElementById js/document "gamecanvas") :canvas (.getElementById js/document "gamecanvas")
:2d (.getContext (.getElementById js/document "gamecanvas") "2d") :2d (.getContext (.getElementById js/document "gamecanvas") "2d")
:target-fps 30
:timing { :timing {
:prev 0 :prev 0
:now 0 :now 0
:fps 0 :fps 0
:elapsed 0
} }
:dimensions { :dimensions {
:w 600 :w 600
@ -31,17 +33,23 @@
(aset (:2d gamestate) "font" "10px monospace") (aset (:2d gamestate) "font" "10px monospace")
(defn set-timing [state timingkey] (defn set-timing [state timingkey]
(update-in state (assoc-in state
[:timing timingkey] [:timing timingkey]
#(.now js/performance))) (.now js/performance)))
(defn set-fps [state] (defn set-fps [state]
(let [newstate (set-timing state :now) (let [elapsed (get-in state [:timing :elapsed])
now (get-in newstate [:timing :now]) fps (/ 1 elapsed)]
prev (get-in newstate [:timing :prev]) (assoc-in state [:timing :fps] fps)))
duration (- now prev)
fps (/ 1000 duration)] (defn set-elapsed-seconds [gamestate]
(update-in newstate [:timing :fps] (fn [] fps)))) (assoc-in gamestate
[:timing :elapsed]
(/
(-
(get-in gamestate [:timing :now])
(get-in gamestate [:timing :prev]))
1000)))
(defn update-scene [gamestate] (defn update-scene [gamestate]
(let [scenekey (:scene gamestate) (let [scenekey (:scene gamestate)
@ -52,9 +60,11 @@
(defn update-step [gamestate] (defn update-step [gamestate]
(-> gamestate (-> gamestate
(set-timing :now)
(set-elapsed-seconds)
(set-fps) (set-fps)
(set-timing :prev) (update-scene)
(update-scene))) (set-timing :prev)))
(defn draw-step [gamestate] (defn draw-step [gamestate]
(.clearRect (:2d gamestate) (.clearRect (:2d gamestate)
@ -77,7 +87,7 @@
(fn [] (fn []
(.requestAnimationFrame js/window (.requestAnimationFrame js/window
#(mainloop newstate))) #(mainloop newstate)))
(/ 1000 30)))) (/ 1000 (:target-fps gamestate)))))
(defn init-scenes [] (defn init-scenes []
(assoc (assoc

@ -13,7 +13,6 @@
:y 50 :y 50
:w 10 :w 10
:h 10 :h 10
:v 0
} }
:box :box
{ {
@ -21,7 +20,7 @@
:y 5 :y 5
:w 10 :w 10
:h 10 :h 10
:v 5 :pps 100
:d :? :d :?
} }
})) }))
@ -30,9 +29,11 @@
(let [box (get-in scenedata [:data :box]) (let [box (get-in scenedata [:data :box])
dir (input/dirinput) dir (input/dirinput)
box (assoc box :d dir)] box (assoc box :d dir)]
(update-in scenedata (assoc-in scenedata
[:data :box] [:data :box]
#(objects/move-inside-gamestate gamestate box)))) (objects/move-inside-gamestate
gamestate
box))))
(defn draw [gamestate scenedata] (defn draw [gamestate scenedata]
(let [{{:keys [bumper box]} :data} scenedata (let [{{:keys [bumper box]} :data} scenedata

@ -9,21 +9,21 @@
(< (+ x w) (+ cx cw)) (< (+ x w) (+ cx cw))
(< (+ y h) (+ cy ch))))) (< (+ y h) (+ cy ch)))))
(defn moved-object [obj] (defn moved-object [obj pxs]
(let [{:keys [x y v d]} obj] (let [{:keys [x y d]} obj]
(cond (cond
(= d :w) (= d :w)
(assoc obj (assoc obj
:x (- x v)) :x (- x pxs))
(= d :e) (= d :e)
(assoc obj (assoc obj
:x (+ x v)) :x (+ x pxs))
(= d :n) (= d :n)
(assoc obj (assoc obj
:y (- y v)) :y (- y pxs))
(= d :s) (= d :s)
(assoc obj (assoc obj
:y (+ y v)) :y (+ y pxs))
:else obj))) :else obj)))
(defn bump-in-wall [obj container] (defn bump-in-wall [obj container]
@ -36,14 +36,23 @@
:s (update obj :y #(- (+ cy ch) h 1)) :s (update obj :y #(- (+ cy ch) h 1))
:? obj))) :? obj)))
(defn move-inside [obj container] (defn pps->px [gamestate obj]
(let [moved (moved-object obj)] (let [prev (get-in gamestate [:timing :prev])
now (get-in gamestate [:timing :now])
secs (/ (- now prev) 1000)
pps (:pps obj)]
(* pps secs)))
(defn move-inside [obj container pxs]
(let [moved (moved-object obj pxs)]
(if (in? moved container) (if (in? moved container)
moved moved
(bump-in-wall obj container)))) (bump-in-wall obj container))))
(defn move-inside-gamestate [gamestate obj] (defn move-inside-gamestate [gamestate obj]
(let [container (assoc (:dimensions gamestate) (let [pxs (pps->px gamestate obj)
container (assoc
(:dimensions gamestate)
:x 0 :x 0
:y 0)] :y 0)]
(move-inside obj container))) (move-inside obj container pxs)))

Loading…
Cancel
Save