Permalink
Browse files

housekeeping

  • Loading branch information...
1 parent 6074fde commit 70a3c30ab8a0db2a12a70b4b5ed0faff1c6ce95f unclebob committed Jul 19, 2010
Showing with 42 additions and 12 deletions.
  1. +1 −0 .gitignore
  2. +4 −4 project.clj
  3. +30 −6 src/orbit/world.clj
  4. +7 −2 src/physics/object.clj
View
1 .gitignore
@@ -2,4 +2,5 @@
classes/*
lib/*
.idea/*
+*.jar
View
8 project.clj
@@ -1,10 +1,10 @@
(defproject clojureOrbit "0.1"
:description "simple Orbital Simulator written in Clojure."
:url "http://github.com/unclebob/clojureOrbit"
- :dependencies [[org.clojure/clojure "1.1.0"]
- [org.clojure/clojure-contrib "1.1.0"]]
+ :dependencies [[org.clojure/clojure "1.2.0-master-SNAPSHOT"]
+ [org.clojure/clojure-contrib "1.2.0-SNAPSHOT"]]
:dev-dependencies [[swank-clojure "1.2.0-SNAPSHOT"]
- [leiningen/lein-swank "1.2.0-SNAPSHOT"]]
+ [leiningen/lein-swank "1.2.0-SNAPSHOT"]]
:main orbitmain
-)
+ )
View
36 src/orbit/world.clj
@@ -11,7 +11,7 @@
(def center (position/make 500 500))
-(defstruct controls :magnification :center :trails :clear)
+(defstruct controls :magnification :speed :center :trails :clear)
(defn size-by-mass [{m :mass}]
(+ 3 (Math/sqrt m))
@@ -54,13 +54,16 @@
(draw-object g obj controls)
)
(.clearRect g 0 0 1000 20)
- (.drawString g (str "Objects: " (count world)) 20 20)
+ (.drawString g (format "Objects: %d, Magnification: %4.3g, Speed: %d"
+ (count world)
+ (:magnification controls)
+ (:speed controls)) 20 20)
)
)
-(defn update-world [world]
+(defn update-world [world controls]
(dosync
- (alter world object/update-all))
+ (alter world #(object/update-all (:speed controls) %)))
)
(defn magnify [factor controls world]
@@ -82,6 +85,12 @@
(defn toggle-trail [controls]
(dosync (alter controls #(assoc % :trails (not (:trails @controls))))))
+(defn speed-up [controls]
+ (dosync (alter controls #(assoc % :speed (inc (:speed @controls))))))
+
+(defn slow-down [controls]
+ (dosync (alter controls #(assoc % :speed (max 1 (dec (:speed @controls)))))))
+
(defn- quit-key? [c]
(= \q c)
)
@@ -102,6 +111,14 @@
(= \t c)
)
+(defn- speed-key? [c]
+ (= \> c)
+ )
+
+(defn- slow-key? [c]
+ (= \< c)
+ )
+
(defn world-panel [frame world controls]
(proxy [JPanel ActionListener KeyListener] []
(paintComponent [g]
@@ -111,7 +128,7 @@
(reset-screen-state controls)
)
(actionPerformed [e]
- (update-world world)
+ (update-world world @controls)
(.repaint this)
)
(keyPressed [e]
@@ -124,6 +141,8 @@
(minus-key? c) (magnify 0.9 controls world)
(space-key? c) (magnify 1.0 controls world)
(trail-key? c) (toggle-trail controls)
+ (speed-key? c) (speed-up controls)
+ (slow-key? c) (slow-down controls)
)
(.repaint this)
)
@@ -185,7 +204,12 @@
(defn world-frame []
(let [
- controls (ref (struct controls 1 center false false))
+ controls (ref (struct-map controls
+ :magnification 1.0
+ :speed 1
+ :center center
+ :trails false
+ :clear false))
world (ref (create-world))
frame (JFrame. "Orbit")
panel (world-panel frame world controls)
View
9 src/physics/object.clj
@@ -131,5 +131,10 @@
)
)
-(defn update-all [os]
- (-> os collide-all calculate-forces-on-all accelerate-all reposition-all))
+(defn update-all
+ ([os]
+ (-> os collide-all calculate-forces-on-all accelerate-all reposition-all))
+
+ ([speed os]
+ (-> os collide-all calculate-forces-on-all accelerate-all reposition-all))
+ )

0 comments on commit 70a3c30

Please sign in to comment.