master
Abhinav Sarkar 2013-10-15 16:57:09 +05:30
parent 028514ac0e
commit c84788aa43
1 changed files with 24 additions and 17 deletions

View File

@ -50,6 +50,7 @@
(close! c)))) (close! c))))
c)) c))
;; Global settings
(def *width* (- (.-scrollWidth (.-body js/document)) 20)) (def *width* (- (.-scrollWidth (.-body js/document)) 20))
(def *height* (- (.-scrollHeight (.-body js/document)) 125)) (def *height* (- (.-scrollHeight (.-body js/document)) 125))
(def *center* [(/ *width* 2 ) (/ *height* 2)]) (def *center* [(/ *width* 2 ) (/ *height* 2)])
@ -62,7 +63,7 @@
(def *perturb-factor* 0.02) (def *perturb-factor* 0.02)
(def *paddle-width* 10) (def *paddle-width* 10)
(def *paddle-step* 5) (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))
@ -87,7 +88,10 @@
(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
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)))
@ -98,14 +102,14 @@
(defn start-game (defn start-game
"Sets up the game by creating the signals and setting up the components and starts the game." "Sets up the game by creating the signals and setting up the components and starts the game."
[] []
(let [frames (frame-chan) ;; frames signal (let [frames (frame-chan) ;; frames signal
keydowns (event-chan :keydown) keydowns (event-chan :keydown) ;; keydowns signal
keyups (event-chan :keyup) keyups (event-chan :keyup) ;; keyups signal
pos (chan 1) ;; ball position signal pos (chan 1) ;; ball position signal
vel (chan 1) ;; ball velocity signal vel (chan 1) ;; ball velocity signal
acc (chan 1) acc (chan 1) ;; ball acceleration signal
pd-pos (chan 1) ;; paddles position signal pd-pos (chan 1) ;; paddles position signal
game-state (chan 1) ;; game state signal, the state of the game and the current score game-state (chan 1) ;; game state signal, the state of the game and the current score
init-vel (initial-velocity)] init-vel (initial-velocity)]
(setup-components frames keydowns keyups game-state pos vel acc pd-pos) (setup-components frames keydowns keyups game-state pos vel acc pd-pos)
@ -121,7 +125,7 @@
(defn setup-components (defn setup-components
"Creates mult(iple)s of the signals and sets up the components by connecting them using "Creates mult(iple)s of the signals and sets up the components by connecting them using
the signals tapped from the mults. the signals tapped from the mults.
The signals are taken as parameters." The signals and their stop functions are taken as parameters."
[[frames stop-frames] [keydowns stop-keydowns] [keyups stop-keyups] [[frames stop-frames] [keydowns stop-keydowns] [keyups stop-keyups]
game-state pos vel acc pd-pos] game-state pos vel acc pd-pos]
(let [ticks (chan) ;; ticks signal (let [ticks (chan) ;; ticks signal
@ -165,7 +169,10 @@
(recur) (recur)
(stop-game)))))))) (stop-game))))))))
(defn gravity-acc [[x y]] (defn gravity-acc
"Calculates acceleration due to gravitation for the ball caused by a unit mass placed at the
center of the board."
[[x y]]
(let [[cx cy] *center* (let [[cx cy] *center*
x-dist (- cx x) x-dist (- cx x)
y-dist (- cy y) y-dist (- cy y)
@ -199,8 +206,8 @@
(defn paddle-positioner (defn paddle-positioner
"Paddle Positioner component. "Paddle Positioner component.
Captures the keydown signal for the provides keycodes and calculates the next paddle Captures the keys signal for the provides keycodes and calculates the next paddle
position using the current paddle position (from the `pos-in` signal) and keydown signal positions using the current paddle positions (from the `pos-in` signal) and keys signal
and outputs it to the `pos-out` signal." and outputs it to the `pos-out` signal."
[keys pos-in pos-out] [keys pos-in pos-out]
(go-loop (go-loop
@ -248,8 +255,8 @@
and outputs the next ball velocity and next game state to the `vel-out` and `game-state` and outputs the next ball velocity and next game state to the `vel-out` and `game-state`
signals respectively. signals respectively.
Reads the current tick, ball position, ball velocity, ball acceleration, left and right paddle Reads the current tick, ball position, ball velocity, ball acceleration, left and right paddle
positions and game state from the `ticks`, `pos`, `vel-in`, `acc`, `pd-pos` positions and game state from the `ticks`, `pos`, `vel-in`, `acc`, `pd-pos` and `game-state`
and `game-state` signals respectively." signals respectively."
(go-loop (go-loop
(let [;; get all current values (let [;; get all current values
@ -293,7 +300,7 @@
score-el (dom/by-id "score") score-el (dom/by-id "score")
lpaddle-el (dom/by-id "lpaddle") lpaddle-el (dom/by-id "lpaddle")
rpaddle-el (dom/by-id "rpaddle") rpaddle-el (dom/by-id "rpaddle")
fps-el (dom/by-id "fps")] fps-el (dom/by-id "fps")]
(go (loop [fps-p nil state-p nil score-p nil] (go (loop [fps-p nil state-p nil score-p nil]
(let [fps (int (/ 1000 (<! ticks))) (let [fps (int (/ 1000 (<! ticks)))
[x y] (<! pos) [x y] (<! pos)