Fixed the issue with switching tab causing the ball to go out of boundaries
parent
25ab05ede0
commit
a5aa5964cd
|
@ -8,13 +8,14 @@
|
||||||
<![endif]-->
|
<![endif]-->
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
<div>FPS: <span id="fps"></span></div>
|
<div>Frame: <span id="frame"></span></div>
|
||||||
<div>Position: <span id="pos"></span></div>
|
<div>FPS: <span id="fps"></span></div>
|
||||||
<div>Velocity: <span id="vel"></span></div>
|
<div>Position: <span id="pos"></span></div>
|
||||||
<svg style="width: 300px; height: 200px; border: 1px black solid" id="canvas">
|
<div>Velocity: <span id="vel"></span></div>
|
||||||
<circle id="ball" r="5" cx="5" cy="100"/>
|
<svg style="width: 600px; height: 400px; border: 1px black solid" id="canvas">
|
||||||
</svg>
|
<circle id="ball" r="5" cx="5" cy="100"/>
|
||||||
|
</svg>
|
||||||
|
|
||||||
<!-- pointing to cljsbuild generated js file -->
|
<!-- pointing to cljsbuild generated js file -->
|
||||||
<script src="js/frpong.js"></script>
|
<script src="js/frpong.js"></script>
|
||||||
<script type="text/javascript">frpong.core.init()</script>
|
<script type="text/javascript">frpong.core.init()</script>
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
(ns frpong.core
|
(ns frpong.core
|
||||||
(:require [frpong.helpers :as h]
|
(:require [frpong.helpers :as h :refer [log]]
|
||||||
[cljs.core.async :as async
|
[cljs.core.async :as async
|
||||||
:refer [<! >! chan put! close! sliding-buffer dropping-buffer timeout]]
|
:refer [<! >! chan put! close! sliding-buffer dropping-buffer timeout]]
|
||||||
[domina :as dom :refer [log]]
|
[domina :as dom :refer [log]]
|
||||||
|
@ -32,6 +32,16 @@
|
||||||
|
|
||||||
(defn abs [x] (.abs js/Math x))
|
(defn abs [x] (.abs js/Math x))
|
||||||
|
|
||||||
|
(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))
|
||||||
|
|
||||||
(defn positioner [tick-chan vel-chan pos-chan-in pos-chan-out]
|
(defn positioner [tick-chan vel-chan pos-chan-in pos-chan-out]
|
||||||
(go-loop
|
(go-loop
|
||||||
(let [tick (<! tick-chan)
|
(let [tick (<! tick-chan)
|
||||||
|
@ -65,7 +75,7 @@
|
||||||
(dom/set-attr! (dom/by-id "ball") "cy" y))))
|
(dom/set-attr! (dom/by-id "ball") "cy" y))))
|
||||||
|
|
||||||
(defn game-setup [width height padding frame-chan pos-chan vel-chan]
|
(defn game-setup [width height padding frame-chan pos-chan vel-chan]
|
||||||
(let [[tick-chan-pos tick-chan-collsion] (h/dup-chan (h/diff-chan frame-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)
|
[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)]
|
[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)
|
(positioner tick-chan-pos vel-chan-pos pos-chan-pos pos-chan)
|
||||||
|
@ -85,14 +95,17 @@
|
||||||
|
|
||||||
(defn ^:export init []
|
(defn ^:export init []
|
||||||
(let [frame-chan (h/frame-chan)
|
(let [frame-chan (h/frame-chan)
|
||||||
[frame-chan-fps frame-chan-game] (h/dup-chan frame-chan)
|
[frame-chan-fps frame-chan-game frame-chan-count] (h/multiplex frame-chan 3)
|
||||||
|
|
||||||
fps-chan (h/map-chan #(/ 1000 %) (h/diff-chan frame-chan-fps))
|
fps-chan (h/map-chan #(/ 1000 %) (h/diff-chan frame-chan-fps))
|
||||||
|
frame-count-chan (h/counting-chan frame-chan-count)
|
||||||
|
|
||||||
width 300
|
width 600
|
||||||
height 200
|
height 400
|
||||||
padding 5
|
padding 5
|
||||||
init-pos [5 100]
|
init-pos [5 100]
|
||||||
init-vel [0.2 0.22]]
|
init-vel [0.2 0.22]]
|
||||||
(go-loop (dom/set-text! (dom/by-id "fps") (<! fps-chan)))
|
(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 width height padding init-pos init-vel frame-chan-game)))
|
||||||
|
|
|
@ -9,6 +9,8 @@
|
||||||
(defn now []
|
(defn now []
|
||||||
(.valueOf (js/Date.)))
|
(.valueOf (js/Date.)))
|
||||||
|
|
||||||
|
(defn log [s] (.log js/console s))
|
||||||
|
|
||||||
(defn put-all! [cs x]
|
(defn put-all! [cs x]
|
||||||
(doseq [c cs]
|
(doseq [c cs]
|
||||||
(put! c x)))
|
(put! c x)))
|
||||||
|
@ -178,3 +180,13 @@
|
||||||
(>! c (- ts start))
|
(>! c (- ts start))
|
||||||
(recur ts)))))
|
(recur ts)))))
|
||||||
c))
|
c))
|
||||||
|
|
||||||
|
(defn dropping-chan [source n]
|
||||||
|
(let [c (chan)]
|
||||||
|
(go
|
||||||
|
(loop [count 0]
|
||||||
|
(if (= count 0)
|
||||||
|
(>! c (<! source))
|
||||||
|
(<! source))
|
||||||
|
(recur (rem (inc count) n))))
|
||||||
|
c))
|
||||||
|
|
Loading…
Reference in New Issue