From a270a2a806ad98420e04db08a48acfbb16b6c0a1 Mon Sep 17 00:00:00 2001 From: Dane Johnson Date: Mon, 6 Nov 2023 11:49:58 -0600 Subject: [PATCH] Fully pivot to pirates, some UI --- project.clj | 2 +- scratches/bootstrap.clj | 6 ++-- src/{space_sim => age_of_sail}/core.clj | 45 +++++++++++++++---------- src/{space_sim => age_of_sail}/vec2.clj | 2 +- 4 files changed, 33 insertions(+), 22 deletions(-) rename src/{space_sim => age_of_sail}/core.clj (60%) rename src/{space_sim => age_of_sail}/vec2.clj (94%) diff --git a/project.clj b/project.clj index a5da462..1659659 100644 --- a/project.clj +++ b/project.clj @@ -5,7 +5,7 @@ :url "https://www.eclipse.org/legal/epl-2.0/"} :dependencies [[org.clojure/clojure "1.11.1"] [seesaw "1.5.0"]] - :main ^:skip-aot space-sim.core + :main ^:skip-aot age-of-sail.core :target-path "target/%s" :profiles {:uberjar {:aot :all :jvm-opts ["-Dclojure.compiler.direct-linking=true"]}}) diff --git a/scratches/bootstrap.clj b/scratches/bootstrap.clj index 2243f1d..b5587ad 100644 --- a/scratches/bootstrap.clj +++ b/scratches/bootstrap.clj @@ -1,6 +1,7 @@ -;; you should be in space-sim.core> +;; you should be in age-of-sail.core> (def my-ship (ref {:position [0. 0.] + :name "Virgina Woolfe" :heading (normalize [1.0 1.0]) :slots [{:type :downwind-sail :length 2 :furl 1.0}] :velocity [1. 0.]})) @@ -9,8 +10,7 @@ (start-program) @my-ship -(dosync (alter my-ship assoc :heading (normalize [0. 1.]))) (stop-program) - (show-ui) +@tracked-ship diff --git a/src/space_sim/core.clj b/src/age_of_sail/core.clj similarity index 60% rename from src/space_sim/core.clj rename to src/age_of_sail/core.clj index 59480be..01f0081 100644 --- a/src/space_sim/core.clj +++ b/src/age_of_sail/core.clj @@ -1,6 +1,7 @@ -(ns space-sim.core - (:require [space-sim.vec2 :refer :all] +(ns age-of-sail.core + (:require [age-of-sail.vec2 :refer :all] [seesaw.core :refer :all] + [seesaw.bind :as b] [clojure.math :as math])) (def tickrate 100) @@ -8,6 +9,10 @@ (defonce ships (atom [])) (defonce program (atom nil)) +(defn find-ship + [ships name] + (some #(when (= name (:name @%)) %) ships)) + (defn downwind-force "Calculates the force for a single square downwind sail Force = furl × length² × dot(wind, heading)" @@ -18,13 +23,11 @@ [ship wind] (loop [slots (:slots ship) force (zero)] - (if (empty? slots) - force - (let [slot (first slots) - tail (rest slots)] - (if (= :downwind-sail (:type slot)) - (recur tail (add force (downwind-force slot wind (:heading ship)))) - (recur tail force)))))) + (if-let [slot (first slots)] + (if (= :downwind-sail (:type slot)) + (recur (rest slots) (add force (downwind-force slot wind (:heading ship)))) + (recur (rest slots) force)) + force))) (defn physics-step [ship wind] @@ -50,21 +53,29 @@ [] (stop-program) (let [thread-continue (atom true)] - (.start (Thread. (fn [] - (while @thread-continue - (tick @ships) - (Thread/sleep (quot 1000 tickrate)))))) + (.start + (Thread. + (fn [] + (while @thread-continue + (tick @ships) + (Thread/sleep (quot 1000 tickrate)))))) (reset! program thread-continue))) ;; UI +(def tracked-ship (atom nil)) + (defn ship-chooser [] - (let [namebox (text)] - (flow-panel :items ["Ship Name" namebox]) - (listen namebox :key-typed #()))) + (let [name (text :columns 20)] + (b/bind (.getDocument name) (b/transform #(find-ship @ships %)) tracked-ship) + (flow-panel :items ["Ship Name" name]))) + +(defn ship-info + []) + (defn show-ui [] - (-> (frame :title "Space Sim") pack! show!)) + (-> (frame :title "Space Sim" :content (ship-chooser)) pack! show!)) (defn -main [args] diff --git a/src/space_sim/vec2.clj b/src/age_of_sail/vec2.clj similarity index 94% rename from src/space_sim/vec2.clj rename to src/age_of_sail/vec2.clj index 5e8b75f..3f76e3c 100644 --- a/src/space_sim/vec2.clj +++ b/src/age_of_sail/vec2.clj @@ -1,4 +1,4 @@ -(ns space-sim.vec2 +(ns age-of-sail.vec2 (:require [clojure.math :as math])) (defn zero