Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
19 changed files
with
921 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
/out | ||
/target | ||
/lib | ||
/classes | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
# boids | ||
|
||
A Clojure library designed to ... well, that part is up to you. | ||
|
||
## Usage | ||
|
||
FIXME | ||
|
||
## License | ||
|
||
Copyright © 2013 FIXME | ||
|
||
Distributed under the Eclipse Public License, the same as Clojure. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
(defproject boids "0.1.0-SNAPSHOT" | ||
:dependencies [[org.clojure/clojure "1.5.0"] | ||
[ring/ring-jetty-adapter "1.1.1"] | ||
[compojure "1.1.5"]] | ||
:source-paths ["src/clj"] | ||
:main boids.server | ||
:plugins [[lein-cljsbuild "0.3.0"]] | ||
:cljsbuild {:builds [{:source-paths ["src/cljs"] | ||
:compiler {:optimizations :none | ||
:output-to "resources/public/deps.js" | ||
:output-dir "resources/public/build"}}]}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
luke@Macintosh.local.29213 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<html> | ||
<head><title>Hello, World!</title></head> | ||
<body> | ||
<script src="lib/three.min.js"></script> | ||
<script src="build/goog/base.js"></script> | ||
<script src="deps.js"></script> | ||
<script>goog.require('boids.main');</script> | ||
<script>boids.main.main()</script> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<html> | ||
<head><title>A Browser REPL</title></head> | ||
<body> | ||
<script src="build/goog/base.js"></script> | ||
<script src="deps.js"></script> | ||
<script>goog.require('boids.brepl');</script> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<html> | ||
<head><title>Hello, World!</title></head> | ||
<body> | ||
<script src="build/goog/base.js"></script> | ||
<script src="deps.js"></script> | ||
<script>goog.require('boids.hello');</script> | ||
<script>boids.hello.hello_world()</script> | ||
</body> | ||
</html> |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<html> | ||
<head><title>Hello, World!</title></head> | ||
<body> | ||
<script src="build/goog/base.js"></script> | ||
<script src="deps.js"></script> | ||
<script>goog.require('boids.main');</script> | ||
<script>boids.main.main()</script> | ||
</body> | ||
</html> |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<html> | ||
<head><title>Hello, World!</title></head> | ||
<body> | ||
<script src="lib/three.min.js"></script> | ||
<script src="build/goog/base.js"></script> | ||
<script src="deps.js"></script> | ||
<script>goog.require('boids.main');</script> | ||
<script>boids.main.main()</script> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
luke@Macintosh.local.29213 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
(ns boids.server | ||
(:gen-class) | ||
(:require [ring.adapter.jetty :as jetty] | ||
[compojure.route :as route])) | ||
|
||
(defn -main [& args] | ||
(jetty/run-jetty (route/resources "/") | ||
{:port 3000})) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
(ns boids.brepl | ||
(:require [clojure.browser.repl :as repl])) | ||
|
||
(repl/connect "http://localhost:9000/repl") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
(ns boids.hello) | ||
|
||
(defn hello-world | ||
[] | ||
(js/alert "Hello, world!")) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
(ns boids.main | ||
(:require [boids.protocols :as p] | ||
[boids.view :as v])) | ||
|
||
(defn main | ||
"Starts everything running" | ||
[] | ||
(v/create (.-body js/document))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
(ns boids.protocols) | ||
|
||
(defprotocol Math | ||
"Model for operations involving position and heading." | ||
(update-position [this position heading speed] | ||
"Giving a position, a heading and a speed, return a new | ||
position.") | ||
(combine-headings [this heading-1 heading-2] | ||
"Combine two headings into a single heading.")) | ||
|
||
(defprotocol Boid | ||
"A representation of a boid" | ||
(position [this] | ||
"Returns a position vector (in Cartesian coordinates)") | ||
(heading [this] | ||
"Returns a heading vector (in radians)")) | ||
|
||
(defprotocol Behavior | ||
"A model of a boid's behavior" | ||
(delta-heading [this boid flock] | ||
"Given a boid and a flock (a sequence of other boids), return a | ||
delta heading of the boid.")) | ||
|
||
(defprotocol View | ||
"A dynamically updating view of the simulation." | ||
(add [view boid-atom] | ||
"Adds a boid atom to the view.")) | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
(ns boids.view | ||
(:require [boids.protocols :as p])) | ||
|
||
(defn create | ||
"Initializes a view and adds it to the provided HTML element." | ||
[element] | ||
(let [canvas (.createElement js/document "canvas")] | ||
(.setAttribute canvas "width" (.-innerWidth js/window)) | ||
(.setAttribute canvas "height" (.-innerHeight js/window)) | ||
(.appendChild element canvas))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
(defn obj | ||
"Convert a cljs map to a Clojure map" | ||
[m] | ||
(let [out (js-obj)] | ||
(doseq [[k v] m] | ||
(aset out (name k) v)) | ||
out)) | ||
|
||
(defn make-camera | ||
"Creates and returns a three.js camera" | ||
[window-width window-height] | ||
(let [camera | ||
(THREE/PerspectiveCamera. 75 (/ window-width window-height) 1 10000)] | ||
(set! (.-z (.-position camera)) 1000) | ||
camera)) | ||
|
||
(defn make-3d-object | ||
"Creates and returns a three.js mesh object" | ||
[] | ||
(let [geometry (THREE/CylinderGeometry. 0 50 400 8 2 false) | ||
material (THREE/MeshBasicMaterial. (obj {:color 0 :wireframe true}))] | ||
(THREE/Mesh. geometry material))) | ||
|
||
(defn make-renderer | ||
"Creates and returns a three.js renderer" | ||
[width height] | ||
(let [renderer (THREE/WebGLRenderer. )] | ||
(.setSize renderer width height) | ||
renderer)) | ||
|
||
(defn display | ||
"Initializes a full display and adds it to the given DOM | ||
element. Returns a display map." | ||
[element width height] | ||
(let [scene (THREE/Scene.) | ||
objects [(make-3d-object)] | ||
renderer (make-renderer width height) | ||
camera (make-camera width height)] | ||
(doseq [mesh objects] | ||
(.add scene mesh)) | ||
(.appendChild element (.-domElement renderer)) | ||
{:scene scene | ||
:objects objects | ||
:renderer renderer | ||
:camera camera})) | ||
|
||
(def the-display (atom nil)) | ||
|
||
(defn rotate | ||
"Rotates a mesh" | ||
[mesh x y z] | ||
(let [rotation (.-rotation mesh)] | ||
(set! (.-x rotation) (+ x (.-x rotation))) | ||
(set! (.-y rotation) (+ y (.-y rotation))) | ||
(set! (.-z rotation) (+ z (.-z rotation))))) | ||
|
||
(defn animate | ||
"Animates the display contained in the given display atom" | ||
[display] | ||
(let [{:keys [objects camera scene renderer]} @display] | ||
(doseq [mesh objects] | ||
(rotate mesh 0.00 0.01 0.01)) | ||
(.render renderer scene camera)) | ||
(js/requestAnimationFrame #(animate display))) | ||
|
||
(defn main [] | ||
(reset! the-display (display (.-body js/document) | ||
(.-innerWidth js/window) | ||
(.-innerHeight js/window))) | ||
(animate the-display)) |