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