From f2ed5e513d4cef5dd08023eaba9b34dce88fa4ab Mon Sep 17 00:00:00 2001 From: Abhinav Sarkar Date: Sat, 12 Oct 2013 21:50:48 +0530 Subject: [PATCH] Removed nested defns and moved top level let bindings to defs --- src/cljs/frpong/core.cljs | 464 +++++++++++++++++++------------------- 1 file changed, 231 insertions(+), 233 deletions(-) diff --git a/src/cljs/frpong/core.cljs b/src/cljs/frpong/core.cljs index 08b8cd3..04ba78f 100644 --- a/src/cljs/frpong/core.cljs +++ b/src/cljs/frpong/core.cljs @@ -29,8 +29,6 @@ ;; | renderer | ;; +----------+ -(defn rand [] (.random js/Math)) - (defn abs [x] (.abs js/Math x)) (def PI 3.141592653589793) @@ -53,235 +51,235 @@ (defn next-pos [[x y] [vel-x vel-y] tick] [(+ x (* vel-x tick)) (+ y (* vel-y tick))]) +(def width (- (.-scrollWidth (.-body js/document)) 20)) +(def height (- (.-scrollHeight (.-body js/document)) 125)) +(def padding 5) +(def paddle-size 100) +(def paddle-width 10) +(def ball-radius 8) +(def ball-speed 0.33) +(def init-pos [(/ width 2) (/ height 2)]) +(def paddle-step 20) +(def max-paddle-y (- height paddle-size)) +(def ef-paddle-width (+ paddle-width padding)) +(def init-paddle-pos (/ (- height paddle-size) 2)) + +(defn layout-game + "Lays out the game screen." + [] + (doto (dom/by-id "canvas") + (dom/set-style! "width" (str width "px")) + (dom/set-style! "height" (str height "px"))) + (doto (dom/by-id "ball") + (dom/set-attr! "r" ball-radius) + (dom/set-attr! "cx" (first init-pos)) + (dom/set-attr! "cy" (second init-pos))) + (doseq [id ["lpaddle" "rpaddle"]] + (doto (dom/by-id id) + (dom/set-attr! "width" paddle-width) + (dom/set-attr! "height" paddle-size) + (dom/set-attr! "y" (/ (- height paddle-size) 2)))) + (dom/set-attr! (dom/by-id "lpaddle") "x" 0) + (dom/set-attr! (dom/by-id "rpaddle") "x" (- width paddle-width))) + +(defn start-game + "Sets up the game by creating the signals and setting up the components and starts the game." + [] + (let [frames (frame-chan) ;; frames signal + pos (chan 1) ;; ball position signal + vel (chan 1) ;; ball velocity signal + pl-pos (chan 1) ;; paddle left position signal + pr-pos (chan 1) ;; paddle right position signal + game-state (chan 1) ;; game state signal, the state of the game and the current score + init-vel (let [sgn #(if (< % 0.5) -1 1) + deg (+ 35 (* 20 (rand))) + rad (deg->rad deg)] + (map #(* ball-speed %) + [(* (sgn (rand)) (sin rad)) (* (sgn (rand)) (cos rad))]))] + (setup-components frames game-state pos vel pl-pos pr-pos) + + ;; start the game by setting the initial values of the signals + (put! pos init-pos) + (put! vel init-vel) + (put! pl-pos init-paddle-pos) + (put! pr-pos init-paddle-pos) + (put! game-state [:moving 0]))) + +(defn setup-components + "Multiplexes the signals and sets up the components by connecting them using the signals + tapped from the multiplexers. + The signals are taken as parameters." + [frames game-state pos vel pl-pos pr-pos] + (let [ticks (chan) ;; ticks signal + ticks-m (mult ticks) ;; multiplexers for all signals + pos-m (mult pos) + vel-m (mult vel) + pl-pos-m (mult pl-pos) + pr-pos-m (mult pr-pos) + game-state-m (mult game-state) + + ;; paddle position signals are not at the same rate as the rest so they need to be + ;; sustained at the ticks rate + pl-pos-sust (sustain (tap pl-pos-m) (tap ticks-m (chan (sliding-buffer 1000)))) + pr-pos-sust (sustain (tap pr-pos-m) (tap ticks-m (chan (sliding-buffer 1000))))] + ;; set up the components + (ticker frames (tap game-state-m) ticks) + + (ball-positioner (tap ticks-m) (tap vel-m) (tap pos-m) pos) + (paddle-positioner {83 :down 87 :up} (tap pl-pos-m) pl-pos) + (paddle-positioner {38 :up 40 :down} (tap pr-pos-m) pr-pos) + + (collision-detector (tap ticks-m) (tap pos-m) (tap vel-m) + pl-pos-sust pr-pos-sust (tap game-state-m) game-state vel) + + (renderer (tap ticks-m) (tap game-state-m) (tap pos-m) (tap pl-pos-m) (tap pr-pos-m)))) + +(defn ticker + "Ticker component. + Converts `frames` signal to ticks and outputs them to the `ticks` signal + as long as the `game-state` signal is not :gameover. Once the `game-state` signal is + :gameover, stops the `frames` signal hence stopping the entire game. + Each tick is the number of milliseconds since the last tick was generated." + [[frames stop-frames] game-state ticks] + (let [ticks-in (tick-chan (diff-chan frames))] + (go (loop [] + (let [[state _] (! ticks (! pos-out pos-next)))) + +(defn paddle-positioner + "Paddle Positioner component. + Captures the keydown signal for the provides keycodes and calculates the next paddle + position using the current paddle position (from the `pos-in` signal) and keydown signal + and outputs it to the `pos-out` signal." + [keycodes pos-in pos-out] + (let [keys (key-chan keycodes)] + (go-loop + (let [pos (! pos-out + (condp = ( y (- paddle-y padding)) (< y (+ paddle-y paddle-size padding)))) + +(defn detect-x-collision [x y lpaddle-y rpaddle-y] + (cond + (< x ef-paddle-width) + (if (in-y-range? y lpaddle-y) :collision-left :gameover) + (> x (- width ef-paddle-width)) + (if (in-y-range? y rpaddle-y) :collision-right :gameover) + :else :moving)) + +(defn detect-y-collision [y] + (cond + (< y padding) :collision-left + (> y (- height padding)) :collision-right + :else :moving)) + +(defn collision-state? [state] + (or (= state :collision-left) (= state :collision-right))) + +(defn adjust-vel [state v] + (condp = state + :collision-left (abs v) + :collision-right (- (abs v)) + :moving v + :gameover 0)) + +(defn perturb [v] (* v (+ 1 (/ (- (rand) 0.5) 25)))) + +(defn collision-detector [ticks pos vel-in pl-pos pr-pos game-state-in game-state vel-out] + "Collision Detector component. + Detects the collision of the ball with the walls and the paddles and accordingly calculates + and outputs the next ball velocity and next game state to the `vel-out` and `game-state` + signals respectively. + Reads the current tick, ball position, ball velocity, left and right paddle positions and game state + from the `ticks`, `pos`, `vel-in`, `pl-pos`, `pr-pos` and `game-state` signals respectively." + + (go-loop + (let [;; get all current values + tick (! vel-out [vel-xn vel-yn]) + (>! game-state [state-n score-n])))) + +(defn renderer + "Renderer component. + Renders the ball and paddle positions on the browser. Also shows the game state and stats. + Reads the current values from the signals supplied as parameters." + [ticks game-state pos pl-pos pr-pos] + (let [ball-el (dom/by-id "ball") + state-el (dom/by-id "state") + score-el (dom/by-id "score") + lpaddle-el (dom/by-id "lpaddle") + rpaddle-el (dom/by-id "rpaddle") + fps-el (dom/by-id "fps")] + (go (loop [fps-p nil state-p nil score-p nil] + (let [fps (int (/ 1000 ( to restart") + (ev/listen-once! :keypress #(when (= (:keyCode %) 32) (start-game))))) + (doto ball-el + (dom/set-attr! "cx" x) + (dom/set-attr! "cy" y)) + (recur fps state-text score)))) + (go-loop + (dom/set-attr! lpaddle-el "y" (! (defn ^:export frpong [] - (let [body-el (.-body js/document) - width (- (.-scrollWidth body-el) 20) - height (- (.-scrollHeight body-el) 125) - padding 5 - paddle-size 100 - paddle-width 10 - ball-radius 8 - ball-speed 0.33 - init-pos [(/ width 2) (/ height 2)] - paddle-step 20 - max-paddle-y (- height paddle-size) - ef-paddle-width (+ paddle-width padding) - init-paddle-pos (/ (- height paddle-size) 2)] - - (defn layout-game - "Lays out the game screen." - [] - (doto (dom/by-id "canvas") - (dom/set-style! "width" (str width "px")) - (dom/set-style! "height" (str height "px"))) - (doto (dom/by-id "ball") - (dom/set-attr! "r" ball-radius) - (dom/set-attr! "cx" (first init-pos)) - (dom/set-attr! "cy" (second init-pos))) - (doseq [id ["lpaddle" "rpaddle"]] - (doto (dom/by-id id) - (dom/set-attr! "width" paddle-width) - (dom/set-attr! "height" paddle-size) - (dom/set-attr! "y" (/ (- height paddle-size) 2)))) - (dom/set-attr! (dom/by-id "lpaddle") "x" 0) - (dom/set-attr! (dom/by-id "rpaddle") "x" (- width paddle-width))) - - (defn start-game - "Sets up the game by creating the signals and setting up the components and starts the game." - [] - (let [frames (frame-chan) ;; frames signal - pos (chan 1) ;; ball position signal - vel (chan 1) ;; ball velocity signal - pl-pos (chan 1) ;; paddle left position signal - pr-pos (chan 1) ;; paddle right position signal - game-state (chan 1) ;; game state signal, the state of the game and the current score - init-vel (let [sgn #(if (< % 0.5) -1 1) - deg (+ 35 (* 20 (rand))) - rad (deg->rad deg)] - (map #(* ball-speed %) - [(* (sgn (rand)) (sin rad)) (* (sgn (rand)) (cos rad))]))] - (setup-components frames game-state pos vel pl-pos pr-pos) - - ;; start the game by setting the initial values of the signals - (put! pos init-pos) - (put! vel init-vel) - (put! pl-pos init-paddle-pos) - (put! pr-pos init-paddle-pos) - (put! game-state [:moving 0]))) - - (defn setup-components - "Multiplexes the signals and sets up the components by connecting them using the signals - tapped from the multiplexers. - The signals are taken as parameters." - [frames game-state pos vel pl-pos pr-pos] - (let [ticks (chan) ;; ticks signal - ticks-m (mult ticks) ;; multiplexers for all signals - pos-m (mult pos) - vel-m (mult vel) - pl-pos-m (mult pl-pos) - pr-pos-m (mult pr-pos) - game-state-m (mult game-state) - - ;; paddle position signals are not at the same rate as the rest so they need to be - ;; sustained at the ticks rate - pl-pos-sust (sustain (tap pl-pos-m) (tap ticks-m (chan (sliding-buffer 1000)))) - pr-pos-sust (sustain (tap pr-pos-m) (tap ticks-m (chan (sliding-buffer 1000))))] - ;; set up the components - (ticker frames (tap game-state-m) ticks) - - (ball-positioner (tap ticks-m) (tap vel-m) (tap pos-m) pos) - (paddle-positioner {83 :down 87 :up} (tap pl-pos-m) pl-pos) - (paddle-positioner {38 :up 40 :down} (tap pr-pos-m) pr-pos) - - (collision-detector (tap ticks-m) (tap pos-m) (tap vel-m) - pl-pos-sust pr-pos-sust (tap game-state-m) game-state vel) - - (renderer (tap ticks-m) (tap game-state-m) (tap pos-m) (tap pl-pos-m) (tap pr-pos-m)))) - - (defn ticker - "Ticker component. - Converts `frames` signal to ticks and outputs them to the `ticks` signal - as long as the `game-state` signal is not :gameover. Once the `game-state` signal is - :gameover, stops the `frames` signal hence stopping the entire game. - Each tick is the number of milliseconds since the last tick was generated." - [[frames stop-frames] game-state ticks] - (let [ticks-in (tick-chan (diff-chan frames))] - (go (loop [] - (let [[state _] (! ticks (! pos-out pos-next)))) - - (defn paddle-positioner - "Paddle Positioner component. - Captures the keydown signal for the provides keycodes and calculates the next paddle - position using the current paddle position (from the `pos-in` signal) and keydown signal - and outputs it to the `pos-out` signal." - [keycodes pos-in pos-out] - (let [keys (key-chan keycodes)] - (go-loop - (let [pos (! pos-out - (condp = ( y (- paddle-y padding)) (< y (+ paddle-y paddle-size padding)))) - - (defn detect-x-collision [x y lpaddle-y rpaddle-y] - (cond - (< x ef-paddle-width) - (if (in-y-range? y lpaddle-y) :collision-left :gameover) - (> x (- width ef-paddle-width)) - (if (in-y-range? y rpaddle-y) :collision-right :gameover) - :else :moving)) - - (defn detect-y-collision [y] - (cond - (< y padding) :collision-left - (> y (- height padding)) :collision-right - :else :moving)) - - (defn collision-state? [state] - (or (= state :collision-left) (= state :collision-right))) - - (defn adjust-vel [state v] - (condp = state - :collision-left (abs v) - :collision-right (- (abs v)) - :moving v - :gameover 0)) - - (defn perturb [v] (* v (+ 1 (/ (- (rand) 0.5) 25)))) - - (go-loop - (let [;; get all current values - tick (! vel-out [vel-xn vel-yn]) - (>! game-state [state-n score-n])))) - - (defn renderer - "Renderer component. - Renders the ball and paddle positions on the browser. Also shows the game state and stats. - Reads the current values from the signals supplied as parameters." - [ticks game-state pos pl-pos pr-pos] - (let [ball-el (dom/by-id "ball") - state-el (dom/by-id "state") - score-el (dom/by-id "score") - lpaddle-el (dom/by-id "lpaddle") - rpaddle-el (dom/by-id "rpaddle") - fps-el (dom/by-id "fps")] - (go (loop [fps-p nil state-p nil score-p nil] - (let [fps (int (/ 1000 ( to restart") - (ev/listen-once! :keypress start-game))) - (doto ball-el - (dom/set-attr! "cx" x) - (dom/set-attr! "cy" y)) - (recur fps state-text score)))) - (go-loop - (dom/set-attr! lpaddle-el "y" (