Cleaned up code, fixed collision detection
parent
b367dfe6cb
commit
25ab05ede0
|
@ -10,6 +10,7 @@
|
||||||
<body>
|
<body>
|
||||||
<div>FPS: <span id="fps"></span></div>
|
<div>FPS: <span id="fps"></span></div>
|
||||||
<div>Position: <span id="pos"></span></div>
|
<div>Position: <span id="pos"></span></div>
|
||||||
|
<div>Velocity: <span id="vel"></span></div>
|
||||||
<svg style="width: 300px; height: 200px; border: 1px black solid" id="canvas">
|
<svg style="width: 300px; height: 200px; border: 1px black solid" id="canvas">
|
||||||
<circle id="ball" r="5" cx="5" cy="100"/>
|
<circle id="ball" r="5" cx="5" cy="100"/>
|
||||||
</svg>
|
</svg>
|
||||||
|
|
|
@ -30,6 +30,8 @@
|
||||||
;; | renderer |
|
;; | renderer |
|
||||||
;; +----------+
|
;; +----------+
|
||||||
|
|
||||||
|
(defn abs [x] (.abs js/Math x))
|
||||||
|
|
||||||
(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)
|
||||||
|
@ -40,43 +42,57 @@
|
||||||
|
|
||||||
(defn collision-detector [width height padding tick-chan pos-chan vel-chan-in vel-chan-out]
|
(defn collision-detector [width height padding tick-chan pos-chan vel-chan-in vel-chan-out]
|
||||||
(go-loop
|
(go-loop
|
||||||
(let [tick (<! tick-chan)
|
(let [adjust-v (fn [p v size]
|
||||||
|
(cond
|
||||||
|
(< p padding) (abs v)
|
||||||
|
(> p (- size padding)) (- (abs v))
|
||||||
|
:else v))
|
||||||
|
tick (<! tick-chan)
|
||||||
[vel-x vel-y] (<! vel-chan-in)
|
[vel-x vel-y] (<! vel-chan-in)
|
||||||
[x y] (<! pos-chan)
|
[x y] (<! pos-chan)
|
||||||
[xn yn] [(+ x (* vel-x tick)) (+ y (* vel-y tick))]
|
[xn yn] [(+ x (* vel-x tick)) (+ y (* vel-y tick))]
|
||||||
vel-xn (if (and (> xn padding) (< xn (- width padding))) vel-x (- vel-x))
|
vel-xn (adjust-v xn vel-x width)
|
||||||
vel-yn (if (and (> yn padding) (< yn (- height padding))) vel-y (- vel-y))]
|
vel-yn (adjust-v yn vel-y height)]
|
||||||
(>! vel-chan-out [vel-xn vel-yn]))))
|
(>! vel-chan-out [vel-xn vel-yn]))))
|
||||||
|
|
||||||
|
(defn render-loop [pos-chan vel-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))))
|
||||||
|
|
||||||
|
(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))
|
||||||
|
[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)
|
||||||
|
(collision-detector width height padding
|
||||||
|
tick-chan-collsion pos-chan-collision vel-chan-collision vel-chan)
|
||||||
|
[pos-chan-render vel-chan-render]))
|
||||||
|
|
||||||
|
(defn game-init [width height padding init-pos init-vel 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)]
|
||||||
|
(put! pos-chan init-pos)
|
||||||
|
(put! vel-chan init-vel)
|
||||||
|
|
||||||
|
(render-loop pos-chan-render vel-chan-render)))
|
||||||
|
|
||||||
(defn ^:export init []
|
(defn ^:export init []
|
||||||
(let [frame-chan (h/frame-chan)
|
(let [frame-chan (h/frame-chan)
|
||||||
[frame-chan1 frame-chan2] (h/dup-chan frame-chan)
|
[frame-chan-fps frame-chan-game] (h/dup-chan frame-chan)
|
||||||
|
|
||||||
fps-chan (h/map-chan #(/ 1000 %) (h/diff-chan frame-chan2))
|
fps-chan (h/map-chan #(/ 1000 %) (h/diff-chan frame-chan-fps))
|
||||||
|
|
||||||
width 300
|
width 300
|
||||||
height 200
|
height 200
|
||||||
padding 5
|
padding 5
|
||||||
init-pos [5 100]
|
init-pos [5 100]
|
||||||
init-vel [0.1 0.12]
|
init-vel [0.2 0.22]]
|
||||||
|
(go-loop (dom/set-text! (dom/by-id "fps") (<! fps-chan)))
|
||||||
[tick-chan-pos tick-chan-collsion] (h/dup-chan (h/diff-chan frame-chan1))
|
(game-init width height padding init-pos init-vel frame-chan-game)))
|
||||||
|
|
||||||
pos-chan (chan)
|
|
||||||
[pos-chan-pos pos-chan-render pos-chan-collision] (h/multiplex pos-chan 3)
|
|
||||||
|
|
||||||
vel-chan (chan)
|
|
||||||
[vel-chan-pos vel-chan-collision] (h/dup-chan vel-chan)]
|
|
||||||
(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)
|
|
||||||
|
|
||||||
(go (>! pos-chan init-pos))
|
|
||||||
(go (>! vel-chan init-vel))
|
|
||||||
|
|
||||||
(go-loop
|
|
||||||
(let [[x y] (map int (<! pos-chan-render))]
|
|
||||||
(dom/set-text! (dom/by-id "fps") (<! fps-chan))
|
|
||||||
(dom/set-text! (dom/by-id "pos") [x y])
|
|
||||||
(dom/set-attr! (dom/by-id "ball") "cx" x)
|
|
||||||
(dom/set-attr! (dom/by-id "ball") "cy" y)))))
|
|
||||||
|
|
Loading…
Reference in New Issue