2013-09-15 03:19:05 +05:30
|
|
|
(ns frpong.core
|
2013-09-26 02:45:13 +05:30
|
|
|
(:require [frpong.helpers :as h]
|
2013-09-25 15:28:33 +05:30
|
|
|
[cljs.core.async :as async
|
|
|
|
:refer [<! >! chan put! close! sliding-buffer dropping-buffer timeout]]
|
|
|
|
[domina :as dom :refer [log]]
|
2013-09-15 03:19:05 +05:30
|
|
|
[domina.events :as ev])
|
2013-09-25 15:28:33 +05:30
|
|
|
(:require-macros [cljs.core.async.macros :as m :refer [go]]
|
|
|
|
[frpong.core :refer (go-loop)]))
|
2013-09-25 16:37:14 +05:30
|
|
|
;;
|
|
|
|
;; Signal Diagram
|
|
|
|
;;
|
2013-09-25 17:08:06 +05:30
|
|
|
;; +---------------------+
|
|
|
|
;; | +-------------+ |
|
|
|
|
;; | | | |
|
|
|
|
;; v v | |
|
|
|
|
;; +----------+ vel-chan | |
|
|
|
|
;; +-->|c-detector+----------+ |
|
|
|
|
;; | +----------+ | |
|
|
|
|
;; | +-----------------+ |
|
|
|
|
;; | | +-----------------+
|
|
|
|
;; | | | |
|
|
|
|
;; | v v |
|
|
|
|
;; +---------+ frame-chan +------+ tick-chan | +----------+ pos-chan |
|
|
|
|
;; |frame-gen+------------>|ticker+-----------+-->|positioner+--------------+
|
|
|
|
;; +---------+ +------+ +----------+ |
|
|
|
|
;; +---------------------+
|
|
|
|
;; |
|
|
|
|
;; v
|
|
|
|
;; +----------+
|
|
|
|
;; | renderer |
|
|
|
|
;; +----------+
|
2013-09-15 03:19:05 +05:30
|
|
|
|
2013-09-25 22:05:07 +05:30
|
|
|
(defn abs [x] (.abs js/Math x))
|
|
|
|
|
2013-09-25 23:24:43 +05:30
|
|
|
(defn tick-chan [frame-chan]
|
|
|
|
(let [c (chan)]
|
|
|
|
(go
|
|
|
|
(loop [prev (<! frame-chan)]
|
|
|
|
(let [t (<! frame-chan)]
|
|
|
|
(when (< t (* 10 prev))
|
|
|
|
(>! c t))
|
|
|
|
(recur t))))
|
|
|
|
c))
|
|
|
|
|
2013-09-26 02:45:13 +05:30
|
|
|
(defn ball-positioner [tick-chan vel-chan pos-chan-in pos-chan-out]
|
2013-09-25 15:28:33 +05:30
|
|
|
(go-loop
|
|
|
|
(let [tick (<! tick-chan)
|
|
|
|
[vel-x vel-y] (<! vel-chan)
|
|
|
|
[x y] (<! pos-chan-in)
|
|
|
|
pos-next [(+ x (* vel-x tick)) (+ y (* vel-y tick))]]
|
|
|
|
(>! pos-chan-out pos-next))))
|
2013-09-15 03:19:05 +05:30
|
|
|
|
2013-09-26 10:52:21 +05:30
|
|
|
(defn collision-detector
|
|
|
|
[{:keys [width height padding]} tick-chan pos-chan vel-chan-in vel-chan-out]
|
2013-09-26 19:30:50 +05:30
|
|
|
(let [adjust-v (fn [p v size]
|
2013-09-25 22:05:07 +05:30
|
|
|
(cond
|
|
|
|
(< p padding) (abs v)
|
|
|
|
(> p (- size padding)) (- (abs v))
|
2013-09-26 19:30:50 +05:30
|
|
|
:else v))]
|
|
|
|
(go-loop
|
|
|
|
(let [tick (<! tick-chan)
|
|
|
|
[vel-x vel-y] (<! vel-chan-in)
|
|
|
|
[x y] (<! pos-chan)
|
|
|
|
[xn yn] [(+ x (* vel-x tick)) (+ y (* vel-y tick))]
|
|
|
|
vel-xn (adjust-v xn vel-x width)
|
|
|
|
vel-yn (adjust-v yn vel-y height)]
|
|
|
|
(>! vel-chan-out [vel-xn vel-yn])))))
|
2013-09-15 03:19:05 +05:30
|
|
|
|
2013-09-26 02:45:13 +05:30
|
|
|
(defn paddle-positioner [keycodes max-y movement pos-chan-in pos-chan-out]
|
2013-09-26 03:02:16 +05:30
|
|
|
(let [key-chan (h/key-chan keycodes)]
|
2013-09-26 02:45:13 +05:30
|
|
|
(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)))))))
|
|
|
|
|
2013-09-26 10:52:21 +05:30
|
|
|
(defn renderer [pos-chan vel-chan pl-pos-chan pr-pos-chan]
|
2013-09-26 19:30:50 +05:30
|
|
|
(let [pos-el (dom/by-id "pos")
|
|
|
|
vel-el (dom/by-id "vel")
|
|
|
|
ball-el (dom/by-id "ball")
|
|
|
|
lpaddle-el (dom/by-id "lpaddle")
|
|
|
|
rpaddle-el (dom/by-id "rpaddle")]
|
2013-09-25 22:05:07 +05:30
|
|
|
(go-loop
|
2013-09-26 19:30:50 +05:30
|
|
|
(let [[x y] (<! pos-chan)]
|
|
|
|
(dom/set-text! pos-el (map int [x y]))
|
|
|
|
(dom/set-text! vel-el (<! vel-chan))
|
|
|
|
(doto ball-el
|
2013-09-26 10:52:21 +05:30
|
|
|
(dom/set-attr! "cx" x)
|
|
|
|
(dom/set-attr! "cy" y))))
|
2013-09-26 02:45:13 +05:30
|
|
|
(go-loop
|
2013-09-26 19:30:50 +05:30
|
|
|
(dom/set-attr! lpaddle-el "y" (<! pl-pos-chan)))
|
2013-09-26 02:45:13 +05:30
|
|
|
(go-loop
|
2013-09-26 19:30:50 +05:30
|
|
|
(dom/set-attr! rpaddle-el "y" (<! pr-pos-chan)))))
|
2013-09-25 22:05:07 +05:30
|
|
|
|
2013-09-26 03:02:16 +05:30
|
|
|
(defn game-setup [{:keys [width height padding paddle-size] :as layout} paddle-movement
|
2013-09-26 02:45:13 +05:30
|
|
|
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)
|
2013-09-26 03:02:16 +05:30
|
|
|
[pl-pos-chan-pos pl-pos-chan-render] (h/dup-chan pl-pos-chan)
|
|
|
|
[pr-pos-chan-pos pr-pos-chan-render] (h/dup-chan pr-pos-chan)]
|
2013-09-26 02:45:13 +05:30
|
|
|
(ball-positioner tick-chan-pos vel-chan-pos pos-chan-pos pos-chan)
|
2013-09-26 03:02:16 +05:30
|
|
|
(collision-detector layout tick-chan-collsion pos-chan-collision vel-chan-collision vel-chan)
|
2013-09-26 02:45:13 +05:30
|
|
|
(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]))
|
2013-09-25 22:05:07 +05:30
|
|
|
|
2013-09-26 10:52:21 +05:30
|
|
|
(defn game-init [{:keys [height paddle-size] :as layout}
|
|
|
|
{:keys [init-pos init-vel paddle-movement]} frame-chan]
|
|
|
|
(let [paddle-x (/ (- height paddle-size) 2)
|
|
|
|
pos-chan (chan 1)
|
2013-09-25 22:05:07 +05:30
|
|
|
vel-chan (chan 1)
|
2013-09-26 02:45:13 +05:30
|
|
|
pl-pos-chan (chan 1)
|
|
|
|
pr-pos-chan (chan 1)]
|
2013-09-25 22:05:07 +05:30
|
|
|
(put! pos-chan init-pos)
|
|
|
|
(put! vel-chan init-vel)
|
2013-09-26 10:52:21 +05:30
|
|
|
(put! pl-pos-chan paddle-x)
|
|
|
|
(put! pr-pos-chan paddle-x)
|
2013-09-25 22:05:07 +05:30
|
|
|
|
2013-09-26 10:52:21 +05:30
|
|
|
(apply renderer
|
|
|
|
(game-setup layout paddle-movement frame-chan pos-chan vel-chan pl-pos-chan pr-pos-chan))))
|
2013-09-25 22:05:07 +05:30
|
|
|
|
2013-09-15 03:19:05 +05:30
|
|
|
(defn ^:export init []
|
2013-09-25 15:28:33 +05:30
|
|
|
(let [frame-chan (h/frame-chan)
|
2013-09-26 03:02:16 +05:30
|
|
|
[frame-chan-fps frame-chan-count frame-chan-game] (h/multiplex frame-chan 3)
|
2013-09-25 22:05:07 +05:30
|
|
|
|
|
|
|
fps-chan (h/map-chan #(/ 1000 %) (h/diff-chan frame-chan-fps))
|
2013-09-25 23:24:43 +05:30
|
|
|
frame-count-chan (h/counting-chan frame-chan-count)
|
2013-09-25 22:05:07 +05:30
|
|
|
|
2013-09-26 02:45:13 +05:30
|
|
|
layout {:width 600
|
2013-09-26 10:52:21 +05:30
|
|
|
:height 300
|
2013-09-26 02:45:13 +05:30
|
|
|
:padding 5
|
|
|
|
:paddle-size 80}
|
|
|
|
init-vals {:init-pos [5 100]
|
|
|
|
:init-vel [0.2 0.22]
|
2013-09-26 19:30:50 +05:30
|
|
|
:paddle-movement 10}
|
|
|
|
|
|
|
|
fps-el (dom/by-id "fps")
|
|
|
|
frame-el (dom/by-id "frame")]
|
2013-09-26 10:52:21 +05:30
|
|
|
(doto (dom/by-id "canvas")
|
|
|
|
(dom/set-style! "width" (str (:width layout) "px"))
|
|
|
|
(dom/set-style! "height" (str (:height layout) "px")))
|
|
|
|
|
2013-09-25 23:24:43 +05:30
|
|
|
(go-loop
|
2013-09-26 19:30:50 +05:30
|
|
|
(dom/set-text! fps-el (<! fps-chan))
|
|
|
|
(dom/set-text! frame-el (<! frame-count-chan)))
|
2013-09-26 02:45:13 +05:30
|
|
|
(game-init layout init-vals frame-chan-game)))
|