Added paddle positioner

master
Abhinav Sarkar 9 years ago
parent a5aa5964cd
commit 34b5b5ffb1
  1. 4
      resources/public/index.html
  2. 76
      src/cljs/frpong/core.cljs
  3. 29
      src/cljs/frpong/helpers.cljs

@ -13,7 +13,9 @@
<div>Position: <span id="pos"></span></div>
<div>Velocity: <span id="vel"></span></div>
<svg style="width: 600px; height: 400px; border: 1px black solid" id="canvas">
<circle id="ball" r="5" cx="5" cy="100"/>
<circle id="ball" r="5" cx="5" cy="100" />
<rect id="lpaddle" x="0" y="160" width="10" height="80" />
<rect id="rpaddle" x="590" y="160" width="10" height="80" />
</svg>
<!-- pointing to cljsbuild generated js file -->

@ -1,5 +1,5 @@
(ns frpong.core
(:require [frpong.helpers :as h :refer [log]]
(:require [frpong.helpers :as h]
[cljs.core.async :as async
:refer [<! >! chan put! close! sliding-buffer dropping-buffer timeout]]
[domina :as dom :refer [log]]
@ -42,7 +42,7 @@
(recur t))))
c))
(defn positioner [tick-chan vel-chan pos-chan-in pos-chan-out]
(defn ball-positioner [tick-chan vel-chan pos-chan-in pos-chan-out]
(go-loop
(let [tick (<! tick-chan)
[vel-x vel-y] (<! vel-chan)
@ -52,7 +52,7 @@
(defn collision-detector [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
(< p padding) (abs v)
(> p (- size padding)) (- (abs v))
@ -65,33 +65,65 @@
vel-yn (adjust-v yn vel-y height)]
(>! vel-chan-out [vel-xn vel-yn]))))
(defn render-loop [pos-chan vel-chan]
(defn key-chan [keycodes]
(let [source (h/event-chan :keydown)
c (chan)]
(go-loop
(let [kc (:keyCode (<! source))]
(when (contains? keycodes kc)
(>! c (keycodes kc)))))
c))
(defn paddle-positioner [keycodes max-y movement pos-chan-in pos-chan-out]
(let [key-chan (key-chan keycodes)]
(go-loop
(let [dir (<! key-chan)
pos (<! pos-chan-in)]
(condp = dir
:up (>! pos-chan-out (max (- pos movement) 0))
:down (>! pos-chan-out (min (+ pos movement) max-y)))))))
(defn render-loop [[pos-chan vel-chan pl-pos-chan pr-pos-chan]]
(go-loop
(let [[x y] (map int (<! pos-chan))
vel (<! vel-chan)]
(dom/set-text! (dom/by-id "pos") [x y])
(dom/set-text! (dom/by-id "vel") vel)
(dom/set-attr! (dom/by-id "ball") "cx" x)
(dom/set-attr! (dom/by-id "ball") "cy" y))))
(dom/set-attr! (dom/by-id "ball") "cy" y)))
(go-loop
(dom/set-attr! (dom/by-id "lpaddle") "y" (<! pl-pos-chan)))
(go-loop
(dom/set-attr! (dom/by-id "rpaddle") "y" (<! pr-pos-chan))))
(defn game-setup [width height padding frame-chan pos-chan vel-chan]
(let [[tick-chan-pos tick-chan-collsion] (h/dup-chan (tick-chan (h/diff-chan frame-chan)))
[pos-chan-pos pos-chan-render pos-chan-collision] (h/multiplex pos-chan 3)
[vel-chan-pos vel-chan-collision vel-chan-render] (h/multiplex vel-chan 3)]
(positioner tick-chan-pos vel-chan-pos pos-chan-pos pos-chan)
(defn game-setup [{:keys [width height padding paddle-size]} paddle-movement
frame-chan pos-chan vel-chan pl-pos-chan pr-pos-chan]
(let [max-y (- height paddle-size)
[tick-chan-pos tick-chan-collsion] (h/dup-chan (tick-chan (h/diff-chan frame-chan)))
[pos-chan-pos pos-chan-collision pos-chan-render] (h/multiplex pos-chan 3)
[vel-chan-pos vel-chan-collision vel-chan-render] (h/multiplex vel-chan 3)
[pl-pos-chan-pos pl-pos-chan-render] (h/multiplex pl-pos-chan 2)
[pr-pos-chan-pos pr-pos-chan-render] (h/multiplex pr-pos-chan 2)]
(ball-positioner tick-chan-pos vel-chan-pos pos-chan-pos pos-chan)
(collision-detector width height padding
tick-chan-collsion pos-chan-collision vel-chan-collision vel-chan)
[pos-chan-render vel-chan-render]))
(paddle-positioner {83 :down 87 :up} max-y paddle-movement pl-pos-chan-pos pl-pos-chan)
(paddle-positioner {38 :up 40 :down} max-y paddle-movement pr-pos-chan-pos pr-pos-chan)
[pos-chan-render vel-chan-render pl-pos-chan-render pr-pos-chan-render]))
(defn game-init [width height padding init-pos init-vel frame-chan]
(defn game-init [layout {:keys [init-pos init-vel paddle-movement]} frame-chan]
(let [pos-chan (chan 1)
vel-chan (chan 1)
[pos-chan-render vel-chan-render]
(game-setup width height padding frame-chan pos-chan vel-chan)]
pl-pos-chan (chan 1)
pr-pos-chan (chan 1)]
(put! pos-chan init-pos)
(put! vel-chan init-vel)
(put! pl-pos-chan 160)
(put! pr-pos-chan 160)
(render-loop pos-chan-render vel-chan-render)))
(render-loop
(game-setup layout paddle-movement
frame-chan pos-chan vel-chan pl-pos-chan pr-pos-chan))))
(defn ^:export init []
(let [frame-chan (h/frame-chan)
@ -100,12 +132,14 @@
fps-chan (h/map-chan #(/ 1000 %) (h/diff-chan frame-chan-fps))
frame-count-chan (h/counting-chan frame-chan-count)
width 600
height 400
padding 5
init-pos [5 100]
init-vel [0.2 0.22]]
layout {:width 600
:height 400
:padding 5
:paddle-size 80}
init-vals {:init-pos [5 100]
:init-vel [0.2 0.22]
:paddle-movement 10}]
(go-loop
(dom/set-text! (dom/by-id "fps") (<! fps-chan))
(dom/set-text! (dom/by-id "frame") (<! frame-count-chan)))
(game-init width height padding init-pos init-vel frame-chan-game)))
(game-init layout init-vals frame-chan-game)))

@ -9,8 +9,6 @@
(defn now []
(.valueOf (js/Date.)))
(defn log [s] (.log js/console s))
(defn put-all! [cs x]
(doseq [c cs]
(put! c x)))
@ -81,20 +79,17 @@
(throttle (chan) source control))
([c source control]
(go
(loop [state ::init last nil cs [source]]
(let [[_ sync] cs]
(let [[v sc] (alts! cs)]
(condp = sc
source (condp = state
::init (do (>! c v)
(recur ::throttling last
(conj cs control)))
::throttling (recur state v cs))
sync (if last
(do (>! c last)
(recur state nil
(conj (pop cs) control)))
(recur ::init last (pop cs))))))))
(loop [state ::init last nil]
(let [[v sc] (alts! [source control])]
(condp = sc
source (condp = state
::init (do (>! c v)
(recur ::throttling last))
::throttling (recur state v))
control (if last
(do (>! c last)
(recur state nil))
(recur ::init last))))))
c))
(defn debounce
@ -153,7 +148,7 @@
(defn event-chan [event-type]
(let [c (chan)]
(ev/listen! js/document event-type #(put! c %))
(ev/listen! event-type #(put! c %))
c))
(defn frame-chan []

Loading…
Cancel
Save