From 7342f6dbc846bb7814600c2cdf7f56d39445837e Mon Sep 17 00:00:00 2001 From: Dane Johnson Date: Tue, 21 Nov 2023 17:02:09 -0600 Subject: [PATCH] Subscribe to game loop, UI now does (yoda I am) --- src/age_of_sail/core.clj | 28 +++++++++++++++++++++------- 1 file changed, 21 insertions(+), 7 deletions(-) diff --git a/src/age_of_sail/core.clj b/src/age_of_sail/core.clj index 87b308e..79896ac 100644 --- a/src/age_of_sail/core.clj +++ b/src/age_of_sail/core.clj @@ -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)) +