Even more cleanup

master
Abhinav Sarkar 2013-09-26 19:30:50 +05:30
parent 104005713a
commit 44dc4493e4
1 changed files with 27 additions and 20 deletions

View File

@ -52,19 +52,19 @@
(defn collision-detector (defn collision-detector
[{:keys [width height padding]} tick-chan pos-chan vel-chan-in vel-chan-out] [{:keys [width height padding]} tick-chan pos-chan vel-chan-in vel-chan-out]
(go-loop (let [adjust-v (fn [p v size]
(let [adjust-v (fn [p v size]
(cond (cond
(< p padding) (abs v) (< p padding) (abs v)
(> p (- size padding)) (- (abs v)) (> p (- size padding)) (- (abs v))
:else v)) :else v))]
tick (<! tick-chan) (go-loop
[vel-x vel-y] (<! vel-chan-in) (let [tick (<! tick-chan)
[x y] (<! pos-chan) [vel-x vel-y] (<! vel-chan-in)
[xn yn] [(+ x (* vel-x tick)) (+ y (* vel-y tick))] [x y] (<! pos-chan)
vel-xn (adjust-v xn vel-x width) [xn yn] [(+ x (* vel-x tick)) (+ y (* vel-y tick))]
vel-yn (adjust-v yn vel-y height)] vel-xn (adjust-v xn vel-x width)
(>! vel-chan-out [vel-xn vel-yn])))) vel-yn (adjust-v yn vel-y height)]
(>! vel-chan-out [vel-xn vel-yn])))))
(defn paddle-positioner [keycodes max-y movement pos-chan-in pos-chan-out] (defn paddle-positioner [keycodes max-y movement pos-chan-in pos-chan-out]
(let [key-chan (h/key-chan keycodes)] (let [key-chan (h/key-chan keycodes)]
@ -76,18 +76,22 @@
:down (>! pos-chan-out (min (+ pos movement) max-y))))))) :down (>! pos-chan-out (min (+ pos movement) max-y)))))))
(defn renderer [pos-chan vel-chan pl-pos-chan pr-pos-chan] (defn renderer [pos-chan vel-chan pl-pos-chan pr-pos-chan]
(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 (go-loop
(let [[x y] (map int (<! pos-chan)) (let [[x y] (<! pos-chan)]
vel (<! vel-chan)] (dom/set-text! pos-el (map int [x y]))
(dom/set-text! (dom/by-id "pos") [x y]) (dom/set-text! vel-el (<! vel-chan))
(dom/set-text! (dom/by-id "vel") vel) (doto ball-el
(doto (dom/by-id "ball")
(dom/set-attr! "cx" x) (dom/set-attr! "cx" x)
(dom/set-attr! "cy" y)))) (dom/set-attr! "cy" y))))
(go-loop (go-loop
(dom/set-attr! (dom/by-id "lpaddle") "y" (<! pl-pos-chan))) (dom/set-attr! lpaddle-el "y" (<! pl-pos-chan)))
(go-loop (go-loop
(dom/set-attr! (dom/by-id "rpaddle") "y" (<! pr-pos-chan)))) (dom/set-attr! rpaddle-el "y" (<! pr-pos-chan)))))
(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] frame-chan pos-chan vel-chan pl-pos-chan pr-pos-chan]
@ -131,12 +135,15 @@
:paddle-size 80} :paddle-size 80}
init-vals {:init-pos [5 100] init-vals {:init-pos [5 100]
:init-vel [0.2 0.22] :init-vel [0.2 0.22]
:paddle-movement 10}] :paddle-movement 10}
fps-el (dom/by-id "fps")
frame-el (dom/by-id "frame")]
(doto (dom/by-id "canvas") (doto (dom/by-id "canvas")
(dom/set-style! "width" (str (:width layout) "px")) (dom/set-style! "width" (str (:width layout) "px"))
(dom/set-style! "height" (str (:height layout) "px"))) (dom/set-style! "height" (str (:height layout) "px")))
(go-loop (go-loop
(dom/set-text! (dom/by-id "fps") (<! fps-chan)) (dom/set-text! fps-el (<! fps-chan))
(dom/set-text! (dom/by-id "frame") (<! frame-count-chan))) (dom/set-text! frame-el (<! frame-count-chan)))
(game-init layout init-vals frame-chan-game))) (game-init layout init-vals frame-chan-game)))