Restructure, add laf
This commit is contained in:
@@ -1,17 +1,12 @@
|
||||
(ns age-of-sail.core
|
||||
(:require [age-of-sail.vec2 :refer :all]
|
||||
(:require [age-of-sail.simulation :refer :all]
|
||||
[age-of-sail.vec2 :refer :all]
|
||||
[seesaw.core :refer :all]
|
||||
[seesaw.bind :as b]
|
||||
[clojure.math :as math]))
|
||||
[seesaw.bind :as b])
|
||||
(:import com.formdev.flatlaf.FlatDarculaLaf))
|
||||
|
||||
(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"
|
||||
@@ -56,33 +51,8 @@
|
||||
|
||||
(subscribe! #(tick @ships))
|
||||
|
||||
;; Simulation controls
|
||||
(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))))
|
||||
|
||||
;; UI
|
||||
(FlatDarculaLaf/setup)
|
||||
(defn ignore-args
|
||||
[f]
|
||||
(fn [& _] (f)))
|
||||
@@ -100,17 +70,17 @@
|
||||
[]
|
||||
(let [start-button (button :text "Start" :listen [:mouse-clicked (ignore-args start-program)])
|
||||
pause-button (button :text "Pause" :listen [:mouse-clicked (ignore-args pause-program)])
|
||||
kill-button (button :text "Kill" :listen [:mouse-clicked (ignore-args kill-program)])]
|
||||
kill-button (button :text "Kill" :listen [:mouse-clicked (ignore-args kill-program)])]
|
||||
(letfn [(start-enabled? [state] (contains? #{:stopped :paused} state))
|
||||
(pause-enabled? [state] (= :running state))
|
||||
(kill-enabled? [state] (contains? #{:running :paused} state))]
|
||||
(kill-enabled? [state] (contains? #{:running :paused} state))]
|
||||
(b/bind program (b/tee
|
||||
(b/bind (b/transform start-enabled?) (b/property start-button :enabled?))
|
||||
(b/bind (b/transform pause-enabled?) (b/property pause-button :enabled?))
|
||||
(b/bind (b/transform kill-enabled?) (b/property kill-button :enabled?))))
|
||||
(b/bind (b/transform kill-enabled?) (b/property kill-button :enabled?))))
|
||||
(config! start-button :enabled? (start-enabled? @program))
|
||||
(config! pause-button :enabled? (pause-enabled? @program))
|
||||
(config! kill-button :enabled? (kill-enabled? @program))
|
||||
(config! kill-button :enabled? (kill-enabled? @program))
|
||||
(horizontal-panel :items [start-button pause-button kill-button]))))
|
||||
|
||||
(defn ship-chooser
|
||||
|
||||
34
src/age_of_sail/simulation.clj
Normal file
34
src/age_of_sail/simulation.clj
Normal file
@@ -0,0 +1,34 @@
|
||||
(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))))
|
||||
Reference in New Issue
Block a user