Compare commits

..

No commits in common. "572726df7cc99c2d3dfd1a32a33ee6a86884de72" and "781c06a9879a8e3245beb2bb599323524ee54335" have entirely different histories.

3 changed files with 39 additions and 58 deletions

View File

@ -4,8 +4,7 @@
:license {:name "EPL-2.0 OR GPL-2.0-or-later WITH Classpath-exception-2.0" :license {:name "EPL-2.0 OR GPL-2.0-or-later WITH Classpath-exception-2.0"
:url "https://www.eclipse.org/legal/epl-2.0/"} :url "https://www.eclipse.org/legal/epl-2.0/"}
:dependencies [[org.clojure/clojure "1.11.1"] :dependencies [[org.clojure/clojure "1.11.1"]
[seesaw "1.5.0"] [seesaw "1.5.0"]]
[com.formdev/flatlaf "3.2.5"]]
:main ^:skip-aot age-of-sail.core :main ^:skip-aot age-of-sail.core
:target-path "target/%s" :target-path "target/%s"
:profiles {:uberjar {:aot :all :profiles {:uberjar {:aot :all

View File

@ -1,10 +1,10 @@
(ns age-of-sail.core (ns age-of-sail.core
(:require [age-of-sail.simulation :refer :all] (:require [age-of-sail.vec2 :refer :all]
[age-of-sail.vec2 :refer :all]
[seesaw.core :refer :all] [seesaw.core :refer :all]
[seesaw.bind :as b]) [seesaw.bind :as b]
(:import com.formdev.flatlaf.FlatDarculaLaf)) [clojure.math :as math]))
(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 []))
@ -49,22 +49,37 @@
(doseq [ship ships] (doseq [ship ships]
(-> ship (physics-step hardcoded-wind)))) (-> 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))
(Thread/sleep (quot 1000 tickrate)))
(when-not (compare-and-set! program :killed :stopped)
(throw "Error: tried to stop a program that wasn't killed!")))
(defn pause-program
[]
(compare-and-set! program :running :paused))
(defn kill-program
[]
(compare-and-set! program :running :paused)
(compare-and-set! program :paused :killed))
(defn start-program
[]
(when (= (first (reset-vals! program :running)) :stopped)
(.start (Thread. game-loop))))
;; UI ;; UI
(FlatDarculaLaf/setup)
(defn ignore-args (defn ignore-args
[f] [f]
(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
[] []
@ -91,14 +106,16 @@
(flow-panel :items ["Ship Name" name]))) (flow-panel :items ["Ship Name" name])))
(defn format-position (defn format-position
[[x y]] [ship]
(format "X: %.2f Y: %.2f" x y)) (apply format "X: %.2f Y: %.2f" (:position @ship)))
(defn ship-info (defn ship-info
[] []
(let [panel (vertical-panel :visible? false) (let [panel (vertical-panel :visible? false)
position (label)] position (label)]
(b/bind ship-pos (b/transform format-position) (b/property position :text)) (b/bind tracked-ship (b/some identity)
(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))
@ -117,7 +134,6 @@
show!))) show!)))
(defn -main (defn -main
[] [args]
(start-program) (start-program)
(show-ui)) (show-ui))

View File

@ -1,34 +0,0 @@
(ns age-of-sail.simulation)
(def tickrate 100)
(def hooks (atom '()))
(defn subscribe!
"Adds a hook to the hooks"
[f]
(swap! hooks conj f))
(defonce program (atom :stopped))
(defn game-loop
[]
(while (#{:running :paused} @program)
(when (= :running @program)
(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!")))
(defn pause-program
[]
(compare-and-set! program :running :paused))
(defn kill-program
[]
(compare-and-set! program :running :paused)
(compare-and-set! program :paused :killed))
(defn start-program
[]
(when (= (first (reset-vals! program :running)) :stopped)
(.start (Thread. game-loop))))