Removed earmuffs from the global variables

master
Abhinav Sarkar 2013-10-16 22:23:45 +05:30
parent c4f1ae4d39
commit 786280d310
1 changed files with 55 additions and 55 deletions

View File

@ -54,77 +54,77 @@
(defn sin [x] (.sin js/Math x)) (defn sin [x] (.sin js/Math x))
;; Global settings ;; Global settings
(def *width* (- (.-scrollWidth (.-body js/document)) 20)) (def width (- (.-scrollWidth (.-body js/document)) 20))
(def *height* (- (.-scrollHeight (.-body js/document)) 130)) (def height (- (.-scrollHeight (.-body js/document)) 130))
(def *center* [(/ *width* 2 ) (/ *height* 2)]) (def center [(/ width 2 ) (/ height 2)])
(def *padding* 5) (def padding 5)
(def *paddle-size* 100) (def paddle-size 100)
(def *ball-radius* 8) (def ball-radius 8)
(def *ball-speed* 0.6) (def ball-speed 0.6)
(def *init-vel-deg-lim* [35 55]) (def init-vel-deg-lim [35 55])
(def *perturb-factor* 0.05) (def perturb-factor 0.05)
(def *init-mass-radius* 0) (def init-mass-radius 0)
(def *paddle-width* 10) (def paddle-width 10)
(def *paddle-step* 8) (def paddle-step 8)
(def *max-paddle-y* (- *height* *paddle-size*)) (def max-paddle-y (- height paddle-size))
(def *ef-paddle-width* (+ *paddle-width* *padding*)) (def ef-paddle-width (+ paddle-width padding))
(def *init-paddle-pos* (/ (- *height* *paddle-size*) 2)) (def init-paddle-pos (/ (- height paddle-size) 2))
(def *gravity* (atom 0.005)) (def gravity (atom 0.005))
(def *gravity-step* 0.005) (def gravity-step 0.005)
(defn mass-radius [] (defn mass-radius []
(+ *init-mass-radius* (* (deref *gravity*) 1000))) (+ init-mass-radius (* (deref gravity) 1000)))
(defn setup-gravity-controls (defn setup-gravity-controls
"Sets up keyboard controls for changing gravity." "Sets up keyboard controls for changing gravity."
[] []
(let [keydowns (first (dom-events :keydown)) (let [keydowns (first (dom-events :keydown))
actions { 37 #(- % *gravity-step*) 39 #(+ % *gravity-step*) } actions { 37 #(- % gravity-step) 39 #(+ % gravity-step) }
mass-el (dom/by-id "mass")] mass-el (dom/by-id "mass")]
(go-loop (go-loop
(let [k (:keyCode (rd keydowns))] (let [k (:keyCode (rd keydowns))]
(when (contains? actions k) (when (contains? actions k)
(do (swap! *gravity* #(max 0 (min 0.1 ((actions k) %)))) (do (swap! gravity #(max 0 (min 0.1 ((actions k) %))))
(dom/set-attr! mass-el "r" (mass-radius)))))))) (dom/set-attr! mass-el "r" (mass-radius))))))))
(defn layout-game (defn layout-game
"Lays out the game screen." "Lays out the game screen."
[] []
(doto (dom/by-id "canvas") (doto (dom/by-id "canvas")
(dom/set-style! "width" (str *width* "px")) (dom/set-style! "width" (str width "px"))
(dom/set-style! "height" (str *height* "px"))) (dom/set-style! "height" (str height "px")))
(doto (dom/by-id "ball") (doto (dom/by-id "ball")
(dom/set-attr! "r" *ball-radius*) (dom/set-attr! "r" ball-radius)
(dom/set-attr! "cx" (first *center*)) (dom/set-attr! "cx" (first center))
(dom/set-attr! "cy" (second *center*))) (dom/set-attr! "cy" (second center)))
(doto (dom/by-id "mass") (doto (dom/by-id "mass")
(dom/set-attr! "r" (mass-radius)) (dom/set-attr! "r" (mass-radius))
(dom/set-attr! "cx" (first *center*)) (dom/set-attr! "cx" (first center))
(dom/set-attr! "cy" (second *center*))) (dom/set-attr! "cy" (second center)))
(doto (dom/by-id "score") (doto (dom/by-id "score")
(dom/set-attr! "x" (first *center*)) (dom/set-attr! "x" (first center))
(dom/set-attr! "y" (- *height* 50))) (dom/set-attr! "y" (- height 50)))
(doseq [id ["lpaddle" "rpaddle"]] (doseq [id ["lpaddle" "rpaddle"]]
(doto (dom/by-id id) (doto (dom/by-id id)
(dom/set-attr! "width" *paddle-width*) (dom/set-attr! "width" paddle-width)
(dom/set-attr! "height" *paddle-size*) (dom/set-attr! "height" paddle-size)
(dom/set-attr! "y" (/ (- *height* *paddle-size*) 2)))) (dom/set-attr! "y" (/ (- height paddle-size) 2))))
(dom/set-attr! (dom/by-id "lpaddle") "x" 0) (dom/set-attr! (dom/by-id "lpaddle") "x" 0)
(dom/set-attr! (dom/by-id "rpaddle") "x" (- *width* *paddle-width*))) (dom/set-attr! (dom/by-id "rpaddle") "x" (- width paddle-width)))
(defn initial-velocity (defn initial-velocity
"Calculates a random initial ball velocity, randomly in any four quadrants, between "Calculates a random initial ball velocity, randomly in any four quadrants, between
the limits of degrees specified by *init-vel-deg-lim*." the limits of degrees specified by init-vel-deg-lim."
[] []
(let [[l h] *init-vel-deg-lim* (let [[l h] init-vel-deg-lim
sgn #(if (< % 0.5) -1 1) sgn #(if (< % 0.5) -1 1)
deg (+ l (* (- h l) (rand))) deg (+ l (* (- h l) (rand)))
rad (deg->rad deg)] rad (deg->rad deg)]
(map #(* *ball-speed* %) (map #(* ball-speed %)
[(* (sgn (rand)) (sin rad)) (* (sgn (rand)) (cos rad))]))) [(* (sgn (rand)) (sin rad)) (* (sgn (rand)) (cos rad))])))
(defn start-game (defn start-game
@ -141,9 +141,9 @@
;; start the game by setting the initial values of the signals ;; start the game by setting the initial values of the signals
(go (go
(wt pos *center*) (wt pos center)
(wt vel init-vel) (wt vel init-vel)
(wt pd-pos [*init-paddle-pos* *init-paddle-pos*]) (wt pd-pos [init-paddle-pos init-paddle-pos])
(wt game-state [:moving 0])))) (wt game-state [:moving 0]))))
(defn start-on-space [] (defn start-on-space []
@ -197,8 +197,8 @@
"Calculates acceleration due to gravitation for the ball caused by the mass placed at the "Calculates acceleration due to gravitation for the ball caused by the mass placed at the
center of the board." center of the board."
[[x y]] [[x y]]
(let [grav (deref *gravity*) (let [grav (deref gravity)
[cx cy] *center* [cx cy] center
x-dist (- cx x) x-dist (- cx x)
y-dist (- cy y) y-dist (- cy y)
distance (sqrt (+ (sq x-dist) (sq y-dist))) distance (sqrt (+ (sq x-dist) (sq y-dist)))
@ -240,26 +240,26 @@
ks (rd keys) ks (rd keys)
move (fn [pos up down] move (fn [pos up down]
(cond (cond
(contains? ks up) (max (- pos *paddle-step*) 0) (contains? ks up) (max (- pos paddle-step) 0)
(contains? ks down) (min (+ pos *paddle-step*) *max-paddle-y*) (contains? ks down) (min (+ pos paddle-step) max-paddle-y)
:else pos))] :else pos))]
(wt pos-out [(move lpos :w :s) (move rpos :up :down)])))) (wt pos-out [(move lpos :w :s) (move rpos :up :down)]))))
(defn in-y-range? [y paddle-y] (defn in-y-range? [y paddle-y]
(and (> y (- paddle-y *padding*)) (< y (+ paddle-y *paddle-size* *padding*)))) (and (> y (- paddle-y padding)) (< y (+ paddle-y paddle-size padding))))
(defn detect-x-collision [x y lpaddle-y rpaddle-y] (defn detect-x-collision [x y lpaddle-y rpaddle-y]
(cond (cond
(< x *ef-paddle-width*) (< x ef-paddle-width)
(if (in-y-range? y lpaddle-y) :collision-left :gameover) (if (in-y-range? y lpaddle-y) :collision-left :gameover)
(> x (- *width* *ef-paddle-width*)) (> x (- width ef-paddle-width))
(if (in-y-range? y rpaddle-y) :collision-right :gameover) (if (in-y-range? y rpaddle-y) :collision-right :gameover)
:else :moving)) :else :moving))
(defn detect-y-collision [y] (defn detect-y-collision [y]
(cond (cond
(< y *padding*) :collision-left (< y padding) :collision-left
(> y (- *height* *padding*)) :collision-right (> y (- height padding)) :collision-right
:else :moving)) :else :moving))
(defn collision? [state] (defn collision? [state]
@ -272,7 +272,7 @@
:moving vel :moving vel
:gameover 0)) :gameover 0))
(defn perturb [v] (* v (+ 1 (* (rand) *perturb-factor*)))) (defn perturb [v] (* v (+ 1 (* (rand) perturb-factor))))
(defn collision-detector [ticks pos vel-in acc pd-pos game-state-in game-state vel-out] (defn collision-detector [ticks pos vel-in acc pd-pos game-state-in game-state vel-out]
"Collision Detector component. "Collision Detector component.
@ -300,8 +300,8 @@
y-collision (collision? y-state) y-collision (collision? y-state)
;; calculate next velocity and game state ;; calculate next velocity and game state
vel-xn (min *ball-speed* (+ (adjust-vel x-state vel-x) (* gx tick))) vel-xn (min ball-speed (+ (adjust-vel x-state vel-x) (* gx tick)))
vel-yn (min *ball-speed* (+ (adjust-vel y-state vel-y) (* gy tick))) vel-yn (min ball-speed (+ (adjust-vel y-state vel-y) (* gy tick)))
state-n (cond state-n (cond
(= x-state :gameover) :gameover (= x-state :gameover) :gameover
(or x-collision y-collision) :collision (or x-collision y-collision) :collision