From 34b5b5ffb1d66ecc66d6940364c457b29de4aa2a Mon Sep 17 00:00:00 2001 From: Abhinav Sarkar Date: Thu, 26 Sep 2013 02:45:13 +0530 Subject: [PATCH] Added paddle positioner --- resources/public/index.html | 4 +- src/cljs/frpong/core.cljs | 76 ++++++++++++++++++++++++++---------- src/cljs/frpong/helpers.cljs | 29 ++++++-------- 3 files changed, 70 insertions(+), 39 deletions(-) diff --git a/resources/public/index.html b/resources/public/index.html index 50ec202..9e2badb 100644 --- a/resources/public/index.html +++ b/resources/public/index.html @@ -13,7 +13,9 @@
Position:
Velocity:
- + + + diff --git a/src/cljs/frpong/core.cljs b/src/cljs/frpong/core.cljs index 4849ba5..673af84 100644 --- a/src/cljs/frpong/core.cljs +++ b/src/cljs/frpong/core.cljs @@ -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 ( 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 (! 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 (! 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 (! 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 []