(ns frpong.core (:require [frpong.helpers :as h] [cljs.core.async :as async :refer [! chan put! close! sliding-buffer dropping-buffer timeout]] [domina :as dom :refer [log]] [domina.events :as ev]) (:require-macros [cljs.core.async.macros :as m :refer [go]] [frpong.core :refer (go-loop)])) ;; ;; Signal Diagram ;; ;; +---------------------+ ;; | +-------------+ | ;; | | | | ;; v v | | ;; +----------+ vel-chan | | ;; +-->|c-detector+----------+ | ;; | +----------+ | | ;; | +-----------------+ | ;; | | +-----------------+ ;; | | | | ;; | v v | ;; +---------+ frame-chan +------+ tick-chan | +----------+ pos-chan | ;; |frame-gen+------------>|ticker+-----------+-->|positioner+--------------+ ;; +---------+ +------+ +----------+ | ;; +---------------------+ ;; | ;; v ;; +----------+ ;; | renderer | ;; +----------+ (defn abs [x] (.abs js/Math x)) (defn tick-chan [frame-chan] (let [c (chan)] (go (loop [prev (! c t)) (recur t)))) c)) (defn ball-positioner [tick-chan vel-chan pos-chan-in pos-chan-out] (go-loop (let [tick (! pos-chan-out pos-next)))) (defn collision-detector [{:keys [width height padding]} tick-chan pos-chan vel-chan-in vel-chan-out] (go-loop (let [adjust-v (fn [p v size] (cond (< p padding) (abs v) (> p (- size padding)) (- (abs v)) :else v)) tick (! vel-chan-out [vel-xn vel-yn])))) (defn paddle-positioner [keycodes max-y movement pos-chan-in pos-chan-out] (let [key-chan (h/key-chan keycodes)] (go-loop (let [dir (! pos-chan-out (max (- pos movement) 0)) :down (>! pos-chan-out (min (+ pos movement) max-y))))))) (defn renderer [pos-chan vel-chan pl-pos-chan pr-pos-chan] (go-loop (let [[x y] (map int (