Subscribe to game loop, UI now does (yoda I am)

This commit is contained in:
Dane Johnson 2023-11-21 17:02:09 -06:00
parent 781c06a987
commit 7342f6dbc8

View File

@ -7,6 +7,11 @@
(def tickrate 100) (def tickrate 100)
(def hardcoded-wind [0.1 3.0]) ;; A strong easternly wind! (def hardcoded-wind [0.1 3.0]) ;; A strong easternly wind!
(defonce ships (atom [])) (defonce ships (atom []))
(def hooks (atom '()))
(defn subscribe!
"Adds a hook to the hooks"
[f]
(swap! hooks conj f))
(defn ship-names (defn ship-names
"Gets the names from a list of ships" "Gets the names from a list of ships"
@ -49,13 +54,16 @@
(doseq [ship ships] (doseq [ship ships]
(-> ship (physics-step hardcoded-wind)))) (-> ship (physics-step hardcoded-wind))))
(subscribe! #(tick @ships))
;; Simulation controls ;; Simulation controls
(defonce program (atom :stopped)) (defonce program (atom :stopped))
(defn game-loop (defn game-loop
[] []
(while (#{:running :paused} @program) (while (#{:running :paused} @program)
(when (= :running @program) (when (= :running @program)
(tick @ships)) (doseq [hook @hooks]
(hook)))
(Thread/sleep (quot 1000 tickrate))) (Thread/sleep (quot 1000 tickrate)))
(when-not (compare-and-set! program :killed :stopped) (when-not (compare-and-set! program :killed :stopped)
(throw "Error: tried to stop a program that wasn't killed!"))) (throw "Error: tried to stop a program that wasn't killed!")))
@ -80,6 +88,13 @@
(fn [& _] (f))) (fn [& _] (f)))
(def tracked-ship (atom nil)) (def tracked-ship (atom nil))
(def ship-pos (b/notify-later))
(defn update-pos
[]
(let [ship @tracked-ship]
(when ship
(b/notify ship-pos (:position @ship)))))
(subscribe! update-pos)
(defn simulation-controls (defn simulation-controls
[] []
@ -106,16 +121,14 @@
(flow-panel :items ["Ship Name" name]))) (flow-panel :items ["Ship Name" name])))
(defn format-position (defn format-position
[ship] [[x y]]
(apply format "X: %.2f Y: %.2f" (:position @ship))) (format "X: %.2f Y: %.2f" x y))
(defn ship-info (defn ship-info
[] []
(let [panel (vertical-panel :visible? false) (let [panel (vertical-panel :visible? false)
position (label)] position (label)]
(b/bind tracked-ship (b/some identity) (b/bind ship-pos (b/transform format-position) (b/property position :text))
(b/tee
(b/bind (b/transform format-position) (b/value position))))
(add! panel (flow-panel :items ["Position" position])) (add! panel (flow-panel :items ["Position" position]))
panel)) panel))
@ -134,6 +147,7 @@
show!))) show!)))
(defn -main (defn -main
[args] []
(start-program) (start-program)
(show-ui)) (show-ui))