Got paddle position sustain working, some refactoring

master
Abhinav Sarkar 2013-10-09 00:33:56 +05:30
parent 44dc4493e4
commit bcc7e28747
2 changed files with 94 additions and 61 deletions

View File

@ -32,102 +32,113 @@
(defn abs [x] (.abs js/Math x)) (defn abs [x] (.abs js/Math x))
(defn tick-chan [frame-chan] (defn tick-chan [frames]
(let [c (chan)] (let [c (chan)]
(go (go
(loop [prev (<! frame-chan)] (loop [prev (<! frames)]
(let [t (<! frame-chan)] (let [t (<! frames)]
(when (< t (* 10 prev)) (when (< t (* 10 prev))
(>! c t)) (>! c t))
(recur t)))) (recur t))))
c)) c))
(defn ball-positioner [tick-chan vel-chan pos-chan-in pos-chan-out] (defn ball-positioner [ticks vel pos-in pos-out]
(go-loop (go-loop
(let [tick (<! tick-chan) (let [tick (<! ticks)
[vel-x vel-y] (<! vel-chan) [vel-x vel-y] (<! vel)
[x y] (<! pos-chan-in) [x y] (<! pos-in)
pos-next [(+ x (* vel-x tick)) (+ y (* vel-y tick))]] pos-next [(+ x (* vel-x tick)) (+ y (* vel-y tick))]]
(>! pos-chan-out pos-next)))) (>! pos-out pos-next))))
(defn collision-detector (defn collision-detector
[{:keys [width height padding]} tick-chan pos-chan vel-chan-in vel-chan-out] [{:keys [width height padding]}
ticks ball-state pos pl-pos pr-pos vel-in vel-out]
(let [adjust-v (fn [p v size] (let [adjust-v (fn [p v size]
(cond (cond
(< p padding) (abs v) (< p padding) [(abs v) :collision]
(> p (- size padding)) (- (abs v)) (> p (- size padding)) [(- (abs v)) :collision]
:else v))] :else [v :moving]))]
(go-loop (go-loop
(let [tick (<! tick-chan) (let [tick (<! ticks)
[vel-x vel-y] (<! vel-chan-in) [vel-x vel-y] (<! vel-in)
[x y] (<! pos-chan) [x y] (<! pos)
pl-pos (<! pl-pos)
pr-pos (<! pr-pos)
[xn yn] [(+ x (* vel-x tick)) (+ y (* vel-y tick))] [xn yn] [(+ x (* vel-x tick)) (+ y (* vel-y tick))]
vel-xn (adjust-v xn vel-x width) [vel-xn bs-x] (adjust-v xn vel-x width)
vel-yn (adjust-v yn vel-y height)] [vel-yn bs-y] (adjust-v yn vel-y height)]
(>! vel-chan-out [vel-xn vel-yn]))))) (>! vel-out [vel-xn vel-yn])
(>! ball-state
(if (or (= bs-x :collision) (= bs-y :collision)) :collision :moving))))))
(defn paddle-positioner [keycodes max-y movement pos-chan-in pos-chan-out] (defn paddle-positioner [keycodes max-y movement pos-in pos-out]
(let [key-chan (h/key-chan keycodes)] (let [keys (h/key-chan keycodes)]
(go-loop (go-loop
(let [dir (<! key-chan) (let [dir (<! keys)
pos (<! pos-chan-in)] pos (<! pos-in)]
(condp = dir (>! pos-out
:up (>! pos-chan-out (max (- pos movement) 0)) (condp = dir
:down (>! pos-chan-out (min (+ pos movement) max-y))))))) :up (max (- pos movement) 0)
:down (min (+ pos movement) max-y)))))))
(defn renderer [pos-chan vel-chan pl-pos-chan pr-pos-chan] (defn renderer [ball-state pos vel pl-pos pr-pos]
(let [pos-el (dom/by-id "pos") (let [pos-el (dom/by-id "pos")
vel-el (dom/by-id "vel") vel-el (dom/by-id "vel")
ball-el (dom/by-id "ball") ball-el (dom/by-id "ball")
lpaddle-el (dom/by-id "lpaddle") lpaddle-el (dom/by-id "lpaddle")
rpaddle-el (dom/by-id "rpaddle")] rpaddle-el (dom/by-id "rpaddle")]
(go-loop (go-loop
(let [[x y] (<! pos-chan)] (let [[x y] (<! pos)]
(dom/set-text! pos-el (map int [x y])) (dom/set-text! pos-el (map int [x y]))
(dom/set-text! vel-el (<! vel-chan)) (dom/set-text! vel-el (<! vel))
(doto ball-el (doto ball-el
(dom/set-attr! "cx" x) (dom/set-attr! "cx" x)
(dom/set-attr! "cy" y)))) (dom/set-attr! "cy" y)
(dom/set-style! "fill" (if (= :collision (<! ball-state)) "#0f0" "#000")))))
(go-loop (go-loop
(dom/set-attr! lpaddle-el "y" (<! pl-pos-chan))) (dom/set-attr! lpaddle-el "y" (<! pl-pos)))
(go-loop (go-loop
(dom/set-attr! rpaddle-el "y" (<! pr-pos-chan))))) (dom/set-attr! rpaddle-el "y" (<! pr-pos)))))
(defn game-setup [{:keys [width height padding paddle-size] :as layout} paddle-movement (defn game-setup [{:keys [width height padding paddle-size] :as layout} paddle-movement
frame-chan pos-chan vel-chan pl-pos-chan pr-pos-chan] frames ball-state pos vel pl-pos pr-pos]
(let [max-y (- height paddle-size) (let [max-y (- height paddle-size)
[tick-chan-pos tick-chan-collsion] (h/dup-chan (tick-chan (h/diff-chan frame-chan))) [tick-pos tick-collsion tick-pl tick-pr] (h/multiplex (tick-chan (h/diff-chan frames)) 4)
[pos-chan-pos pos-chan-collision pos-chan-render] (h/multiplex pos-chan 3) [pos-pos pos-collision pos-render] (h/multiplex pos 3)
[vel-chan-pos vel-chan-collision vel-chan-render] (h/multiplex vel-chan 3) [vel-pos vel-collision vel-render] (h/multiplex vel 3)
[pl-pos-chan-pos pl-pos-chan-render] (h/dup-chan pl-pos-chan) [pl-pos-pos pl-pos-collision pl-pos-render] (h/multiplex pl-pos 3)
[pr-pos-chan-pos pr-pos-chan-render] (h/dup-chan pr-pos-chan)] [pr-pos-pos pr-pos-collision pr-pos-render] (h/multiplex pr-pos 3)]
(ball-positioner tick-chan-pos vel-chan-pos pos-chan-pos pos-chan) (ball-positioner tick-pos vel-pos pos-pos pos)
(collision-detector layout tick-chan-collsion pos-chan-collision vel-chan-collision vel-chan) (collision-detector layout tick-collsion ball-state pos-collision
(paddle-positioner {83 :down 87 :up} max-y paddle-movement pl-pos-chan-pos pl-pos-chan) (h/sustain pl-pos-collision tick-pl)
(paddle-positioner {38 :up 40 :down} max-y paddle-movement pr-pos-chan-pos pr-pos-chan) (h/sustain pr-pos-collision tick-pr)
[pos-chan-render vel-chan-render pl-pos-chan-render pr-pos-chan-render])) vel-collision vel)
(paddle-positioner {83 :down 87 :up} max-y paddle-movement pl-pos-pos pl-pos)
(paddle-positioner {38 :up 40 :down} max-y paddle-movement pr-pos-pos pr-pos)
[ball-state pos-render vel-render pl-pos-render pr-pos-render]))
(defn game-init [{:keys [height paddle-size] :as layout} (defn game-init [{:keys [height paddle-size] :as layout}
{:keys [init-pos init-vel paddle-movement]} frame-chan] {:keys [init-pos init-vel paddle-movement]} frames]
(let [paddle-x (/ (- height paddle-size) 2) (let [init-paddle-pos (/ (- height paddle-size) 2)
pos-chan (chan 1) pos (chan 1)
vel-chan (chan 1) vel (chan 1)
pl-pos-chan (chan 1) pl-pos (chan 1)
pr-pos-chan (chan 1)] pr-pos (chan 1)
(put! pos-chan init-pos) ball-state (chan 1)]
(put! vel-chan init-vel) (put! pos init-pos)
(put! pl-pos-chan paddle-x) (put! vel init-vel)
(put! pr-pos-chan paddle-x) (put! pl-pos init-paddle-pos)
(put! pr-pos init-paddle-pos)
(apply renderer (apply renderer
(game-setup layout paddle-movement frame-chan pos-chan vel-chan pl-pos-chan pr-pos-chan)))) (game-setup layout paddle-movement frames ball-state pos vel pl-pos pr-pos))))
(defn ^:export init [] (defn ^:export init []
(let [frame-chan (h/frame-chan) (let [frames (h/map-chan first (h/frame-chan))
[frame-chan-fps frame-chan-count frame-chan-game] (h/multiplex frame-chan 3) [frames-fps frames-count frames-game] (h/multiplex frames 3)
fps-chan (h/map-chan #(/ 1000 %) (h/diff-chan frame-chan-fps)) fps (h/map-chan #(/ 1000 %) (h/diff-chan frames-fps))
frame-count-chan (h/counting-chan frame-chan-count) frames-count (h/counting-chan frames-count)
layout {:width 600 layout {:width 600
:height 300 :height 300
@ -144,6 +155,6 @@
(dom/set-style! "height" (str (:height layout) "px"))) (dom/set-style! "height" (str (:height layout) "px")))
(go-loop (go-loop
(dom/set-text! fps-el (<! fps-chan)) (dom/set-text! fps-el (<! fps))
(dom/set-text! frame-el (<! frame-count-chan))) (dom/set-text! frame-el (<! frames-count)))
(game-init layout init-vals frame-chan-game))) (game-init layout init-vals frames-game)))

View File

@ -21,6 +21,14 @@
(>! c2 (<! c1)))) (>! c2 (<! c1))))
c2)) c2))
(defn probe [ch probe-name]
(let [c (chan)]
(go-loop
(let [v (<! ch)]
(log (str (now) " " probe-name ": " v))
(>! c v)))
c))
(defn multiplex [in cs-or-n] (defn multiplex [in cs-or-n]
(let [cs (if (number? cs-or-n) (let [cs (if (number? cs-or-n)
(repeatedly cs-or-n chan) (repeatedly cs-or-n chan)
@ -92,6 +100,18 @@
(recur ::init last)))))) (recur ::init last))))))
c)) c))
(defn sustain
([source control]
(sustain (chan) source control))
([c source control]
(go
(loop [last nil]
(let [[v ch] (alts! [source control] :priority true)]
(condp = ch
source (do (>! c v) (recur v))
control (do (when last (>! c last)) (recur last))))))
c))
(defn debounce (defn debounce
([source msecs] ([source msecs]
(debounce (chan) source msecs)) (debounce (chan) source msecs))
@ -162,7 +182,9 @@
(defn frame-chan [] (defn frame-chan []
(let [c (chan (sliding-buffer 1000)) (let [c (chan (sliding-buffer 1000))
step (fn step [ts] (do (put! c ts) (.requestAnimationFrame js/window step)))] step (fn step [ts]
(let [req-id (.requestAnimationFrame js/window step)]
(put! c [ts req-id])))]
(.requestAnimationFrame js/window step) (.requestAnimationFrame js/window step)
c)) c))