From bcc7e28747d2043e7c9fff962de9eaf630d1f72f Mon Sep 17 00:00:00 2001 From: Abhinav Sarkar Date: Wed, 9 Oct 2013 00:33:56 +0530 Subject: [PATCH] Got paddle position sustain working, some refactoring --- src/cljs/frpong/core.cljs | 131 +++++++++++++++++++---------------- src/cljs/frpong/helpers.cljs | 24 ++++++- 2 files changed, 94 insertions(+), 61 deletions(-) diff --git a/src/cljs/frpong/core.cljs b/src/cljs/frpong/core.cljs index 15b3f1c..2ebb12a 100644 --- a/src/cljs/frpong/core.cljs +++ b/src/cljs/frpong/core.cljs @@ -32,102 +32,113 @@ (defn abs [x] (.abs js/Math x)) -(defn tick-chan [frame-chan] +(defn tick-chan [frames] (let [c (chan)] (go - (loop [prev (! c t)) (recur t)))) 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 - (let [tick (! pos-chan-out pos-next)))) + (>! pos-out pos-next)))) (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] (cond - (< p padding) (abs v) - (> p (- size padding)) (- (abs v)) - :else v))] + (< p padding) [(abs v) :collision] + (> p (- size padding)) [(- (abs v)) :collision] + :else [v :moving]))] (go-loop - (let [tick (! vel-chan-out [vel-xn vel-yn]))))) + [vel-xn bs-x] (adjust-v xn vel-x width) + [vel-yn bs-y] (adjust-v yn vel-y height)] + (>! 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] - (let [key-chan (h/key-chan keycodes)] +(defn paddle-positioner [keycodes max-y movement pos-in pos-out] + (let [keys (h/key-chan keycodes)] (go-loop - (let [dir (! pos-chan-out (max (- pos movement) 0)) - :down (>! pos-chan-out (min (+ pos movement) max-y))))))) + (let [dir (! pos-out + (condp = dir + :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") vel-el (dom/by-id "vel") ball-el (dom/by-id "ball") lpaddle-el (dom/by-id "lpaddle") rpaddle-el (dom/by-id "rpaddle")] (go-loop - (let [[x y] (! c2 (! c v))) + c)) + (defn multiplex [in cs-or-n] (let [cs (if (number? cs-or-n) (repeatedly cs-or-n chan) @@ -92,6 +100,18 @@ (recur ::init last)))))) 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 ([source msecs] (debounce (chan) source msecs)) @@ -162,7 +182,9 @@ (defn frame-chan [] (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) c))