Skip to content

Commit

Permalink
ClojureScript support
Browse files Browse the repository at this point in the history
  • Loading branch information
mfikes committed Dec 6, 2018
1 parent b526361 commit 2304103
Show file tree
Hide file tree
Showing 40 changed files with 121 additions and 53 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,6 @@ pom.xml.asc
.hg/
*.iml
.idea
/figwheel_server.log
/resources/public/js

28 changes: 25 additions & 3 deletions project.clj
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,37 @@
:license {:name "Eclipse Public License"
:url "http://www.eclipse.org/legal/epl-v10.html"}
:dependencies [[org.clojure/clojure "1.9.0"]
[org.clojure/clojurescript "1.10.439"]
[figwheel-sidecar "0.5.15"]
[org.clojure/test.check "0.10.0-alpha3"]
[quil "2.7.1"]
[org.clojure/math.combinatorics "0.1.4"]]
:plugins [[lein-cljsbuild "1.1.7"]
[lein-figwheel "0.5.15"]]
:profiles {:dev {:dependencies [[midje "1.9.2"]]}
;; You can add dependencies that apply to `lein midje` below.
;; An example would be changing the logging destination for test runs.
;; Note that Midje itself is in the `dev` profile to support
;; running autotest in the repl.
:midje {}}
:main spacewar.core)


:main spacewar.core
:clean-targets ^{:protect false} [:target-path "resources/public/js"]
:cljsbuild
{:builds [; development build with figwheel hot swap
{:id "development"
:source-paths ["src"]
:figwheel true
:compiler
{:main "spacewar.core"
:output-to "resources/public/js/main.js"
:output-dir "resources/public/js/development"
:asset-path "js/development"}}
; minified and bundled build for deployment
{:id "optimized"
:source-paths ["src"]
:compiler
{:main "spacewar.core"
:output-to "resources/public/js/main.js"
:output-dir "resources/public/js/optimized"
:asset-path "js/optimized"
:optimizations :advanced}}]})
11 changes: 11 additions & 0 deletions resources/public/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<!DOCTYPE html>
<html>
<head>
<title>Space War</title>
</head>
<body>
<canvas id="space-war"></canvas>
<script src="js/main.js"></script>
<script>spacewar.core._main()</script>
</body>
</html>
26 changes: 18 additions & 8 deletions src/spacewar/core.clj → src/spacewar/core.cljc
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
(ns spacewar.core
(:require [quil.core :as q]
(:require [quil.core :as q #?@(:cljs [:include-macros true])]
[quil.middleware :as m]
[spacewar.ui.complex :as main-viewer]
[spacewar.ui.view-frame :as view-frame]
Expand All @@ -16,7 +16,8 @@
[spacewar.game-logic.romulans :as romulans]
[spacewar.util :as util]
[clojure.spec.alpha :as s]
[clojure.java.io :as io]))
[clojure.tools.reader.edn :as edn]
#?(:clj [clojure.java.io :as io])))

(s/def ::update-time number?)
(s/def ::transport-check-time number?)
Expand Down Expand Up @@ -64,13 +65,16 @@
:game-over false}))

(defn game-saved? []
(.exists (io/file "spacewar.world")))
#?(:clj (.exists (io/file "spacewar.world"))
:cljs (and (exists? js/localStorage)
(.getItem js/localStorage "spacewar.world"))))

(defn setup []
(let [vmargin 30
hmargin 5
world (if (game-saved?)
(read-string (slurp "spacewar.world"))
#?(:clj (read-string (slurp "spacewar.world"))
:cljs (edn/read-string (.getItem js/localStorage "spacewar.world")))
(make-initial-world))]
(q/frame-rate glc/frame-rate)
(q/color-mode :rgb)
Expand Down Expand Up @@ -171,7 +175,9 @@
(conj messages {:text "Game Over!" :duration 10000000})
messages)]
(when game-ending?
(.delete (io/file "spacewar.world")))
#?(:clj (.delete (io/file "spacewar.world"))
:cljs (when (exists? js/localStorage)
(.removeItem js/localStorage "spacewar.world"))))
(assoc world :game-over game-over
:explosions explosions
:messages messages)
Expand Down Expand Up @@ -266,7 +272,9 @@
(update-world-per-second world)
world)]
(when (and new-minute? (not (:game-over world)))
(spit "spacewar.world" world))
#?(:clj (spit "spacewar.world" world)
:cljs (when (exists? js/localStorage)
(.setItem js/localStorage "spacewar.world" world))))
(assoc context
:state complex
:world world)))
Expand All @@ -279,11 +287,13 @@
(p/draw state))

(declare space-war)
(defn -main [& args]
(defn ^:export -main [& args]

(q/defsketch space-war
:title "Space War"
:size [(- (q/screen-width) 10) (- (q/screen-height) 40)]
:size #?(:clj [(- (q/screen-width) 10) (- (q/screen-height) 40)]
:cljs [(max (- (.-scrollWidth (.-body js/document)) 20) 900)
(max (- (.-innerHeight js/window) 25) 700)])
:setup setup
:update update-state
:draw draw-state
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
[clojure.set :as set]
[spacewar.game-logic.explosions :as explosions]
[clojure.spec.alpha :as s]
[quil.core :as q]))
[quil.core :as q #?@(:cljs [:include-macros true])]))


(s/def ::x number?)
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
29 changes: 15 additions & 14 deletions src/spacewar/ui/complex.clj → src/spacewar/ui/complex.cljc
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
(ns spacewar.ui.complex
(:require [quil.core :as q]
(spacewar.ui [protocols :as p]
[view-frame :as f]
[config :as uic])
(spacewar.ui.control-panels [scan-panel :refer [->scan-panel]]
[engine-panel :refer [->engine-panel]]
[weapons-panel :refer [->weapons-panel]]
[damage-panel :refer [->damage-panel]]
[status-panel :refer [->status-panel]]
[deploy-panel :refer [->deploy-panel]])
(spacewar.ui.widgets [lights :refer [->indicator-light
rectangle-light
round-light]])))
(:require [quil.core :as q #?@(:cljs [:include-macros true])]
[spacewar.ui.protocols :as p]
[spacewar.ui.view-frame :as f]
[spacewar.ui.config :as uic]
[spacewar.ui.control-panels.scan-panel :refer [->scan-panel]]
[spacewar.ui.control-panels.engine-panel :refer [->engine-panel]]
[spacewar.ui.control-panels.weapons-panel :refer [->weapons-panel]]
[spacewar.ui.control-panels.damage-panel :refer [->damage-panel]]
[spacewar.ui.control-panels.status-panel :refer [->status-panel]]
[spacewar.ui.control-panels.deploy-panel :refer [->deploy-panel]]
[spacewar.ui.widgets.lights :refer [->indicator-light
rectangle-light
round-light]]))

(defn draw-light-panel [state]
(let [{:keys [x y w h indicators background]} state]
Expand Down Expand Up @@ -109,7 +109,8 @@
(q/text-font (:lcars (q/state :fonts)) 18)
(let [{:keys [x y fps]} state
fps (or fps 0)
fps (format "FPS: %6.2f" (float fps))]
fps #?(:clj (format "FPS: %6.2f" (float fps))
:cljs (str "FPS: " (.toFixed fps 2)))]
(q/text fps x y))
)

Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
(ns spacewar.ui.control-panels.damage-panel
(:require (spacewar.ui [protocols :as p]
[config :as uic])
(:require [spacewar.ui.protocols :as p]
[spacewar.ui.config :as uic]
[spacewar.ui.widgets.lcars :as lcars]
[spacewar.ui.widgets.lights :as lights]
[spacewar.ui.widgets.named-indicator :refer [->named-indicator]]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
(ns spacewar.ui.control-panels.deploy-panel
(:require (spacewar.ui [protocols :as p]
[config :as uic])
(:require [spacewar.ui.protocols :as p]
[spacewar.ui.config :as uic]
[spacewar.ui.widgets.lcars :as lcars]
[spacewar.ui.widgets.button :refer [->button]]
[spacewar.game-logic.ship :as ship]))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
[spacewar.geometry :as geo]
[spacewar.vector :as vector]
[spacewar.game-logic.ship :as ship]
[quil.core :as q]))
[quil.core :as q #?@(:cljs [:include-macros true])]))

(deftype engine-panel [state]
p/Drawable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
[spacewar.ui.widgets.direction-selector :refer [->direction-selector]]
[spacewar.ui.widgets.slider :refer [->slider]]
[spacewar.ui.widgets.engage :refer [->engage]]
[quil.core :as q]))
[quil.core :as q #?@(:cljs [:include-macros true])]))
(defn- button-color [selected button]
(if (= selected button)
uic/weapons-panel-selection-color
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
(ns spacewar.ui.front-view
(:require [quil.core :as q]
(:require [quil.core :as q #?@(:cljs [:include-macros true])]
[spacewar.geometry :as geo]
[spacewar.ui.strategic-scan :refer [->strategic-scan]]
[spacewar.ui.protocols :as p]
Expand Down
4 changes: 2 additions & 2 deletions src/spacewar/ui/icons.clj → src/spacewar/ui/icons.cljc
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
(ns spacewar.ui.icons
(:require [quil.core :as q]
(:require [quil.core :as q #?@(:cljs [:include-macros true])]
[spacewar.ui.config :as uic]
[spacewar.game-logic.config :as glc]
[spacewar.geometry :as geo]
Expand All @@ -25,7 +25,7 @@
(q/fill 0 0 0 150)
(q/no-stroke)
(q/ellipse-mode :center)
(q/arc 0 0 30 30 0 (age-angle age) :pie))
(q/arc 0 0 30 30 0 (age-angle age) #?(:clj :pie)))

(defn- draw-base-contents [antimatter dilithium]
(let [antimatter-angle (* 2 Math/PI (/ antimatter glc/base-antimatter-maximum))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
(ns spacewar.ui.protocols
#?(:cljs (:refer-clojure :exclude [clone]))
(:require [clojure.spec.alpha :as s]))

;update-state returns [new-drawable [events]]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
(ns spacewar.ui.strategic-scan
(:require [quil.core :as q]
(:require [quil.core :as q #?@(:cljs [:include-macros true])]
[spacewar.geometry :as geo]
[spacewar.ui.config :as uic]
[spacewar.ui.icons :as icons]
Expand Down Expand Up @@ -125,7 +125,7 @@
(let [{:keys [x y w h]} state]
(q/with-translation
[(+ x (/ w 2)) (+ y (/ h 2))]
(draw-background state)
#?(:clj (draw-background state))
(draw-stars state)
(draw-klingons state)
(when (not (-> state :game-over))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
(ns spacewar.ui.tactical-scan
(:require [quil.core :as q]
(:require [quil.core :as q #?@(:cljs [:include-macros true])]
[spacewar.util :as util]
[spacewar.ui.config :as uic]
[spacewar.ui.icons :as icons]
Expand Down Expand Up @@ -130,7 +130,7 @@
(q/no-stroke)
(q/fill 255 255 255 50)
(q/ellipse-mode :center)
(q/arc 0 0 tgt-radius tgt-radius start stop :pie))
(q/arc 0 0 tgt-radius tgt-radius start stop #?(:clj :pie)))
(icons/draw-ship-icon [vx vy] radians)
)))

Expand Down
16 changes: 13 additions & 3 deletions src/spacewar/ui/view_frame.clj → src/spacewar/ui/view_frame.cljc
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
(ns spacewar.ui.view-frame
(:require [quil.core :as q]
(:require [quil.core :as q #?@(:cljs [:include-macros true])]
[spacewar.ui.config :as uic]
[spacewar.ui.strategic-scan :refer [->strategic-scan]]
[spacewar.ui.tactical-scan :refer [->tactical-scan]]
Expand All @@ -25,6 +25,13 @@
[message-x (+ y h)]
(q/text text 0 0)))))

(defn fill-outside-rect [x y w h screen-w screen-h rgb]
(apply q/fill rgb)
(q/no-stroke)
(q/rect 0 0 x screen-h)
(q/rect x 0 w y)
(q/rect (+ x w) 0 (- screen-w w x) screen-h)
(q/rect x (+ y h) w (- screen-h h y)))

(deftype view-frame [state]
p/Drawable
Expand All @@ -36,11 +43,14 @@
(q/rect (- x 5) (- y 5) (+ w 10) (+ h 10))
(apply q/fill uic/black)
(q/rect x y w h 5)
(q/clip x y w h)
#?(:clj (q/clip x y w h))
(when (not (:sensor-loss state))
(p/draw contents))
#?(:cljs (fill-outside-rect (- x 5) (- y 5) (+ w 10) (+ h 10)
(.-width (q/current-graphics)) (.-height (q/current-graphics))
uic/light-grey))
(draw-messages state)
(q/no-clip)))
#?(:clj (q/no-clip))))

(setup [_] (let [{:keys [x y w h]} state
bounds {:x x :y y :h h :w w}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
(ns spacewar.ui.widgets.button
(:require [quil.core :as q]
(:require [quil.core :as q #?@(:cljs [:include-macros true])]
[spacewar.ui.protocols :as p]
[spacewar.geometry :as geo]
[spacewar.ui.config :as uic]))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
(ns spacewar.ui.widgets.direction-selector
(:require [quil.core :as q]
(:require [quil.core :as q #?@(:cljs [:include-macros true])]
[spacewar.ui.protocols :as p]
[spacewar.geometry :as geo]
[spacewar.ui.config :as uic]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
(ns spacewar.ui.widgets.engage
(:require [quil.core :as q]
(:require [quil.core :as q #?@(:cljs [:include-macros true])]
[spacewar.ui.protocols :as p]
[spacewar.geometry :as geo]
[spacewar.ui.config :as uic]))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
(ns spacewar.ui.widgets.horizontal-scale
(:require [quil.core :as q]
(:require [quil.core :as q #?@(:cljs [:include-macros true])]
[spacewar.ui.protocols :as p]
[spacewar.ui.config :as uic]))

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
(ns spacewar.ui.widgets.lcars
(:require [quil.core :as q]
(:require [quil.core :as q #?@(:cljs [:include-macros true])]
[spacewar.ui.config :as uic]))

(defn- lcars-points [state]
Expand Down Expand Up @@ -29,6 +29,15 @@
:c2 [(+ x w (- uic/stringer-width)) (+ y uic/banner-width)]
:label-position [(+ x 10) (+ y 10)]})))

(defn quadratic-vertex [x1 y1 cx cy x3 y3]
(q/bezier-vertex
(+ x1 (* (/ 2 3) (- cx x1)))
(+ y1 (* (/ 2 3) (- cy y1)))
(+ x3 (* (/ 2 3) (- cx x3)))
(+ y3 (* (/ 2 3) (- cy y3)))
x3
y3))

(defn draw-banner [state]
(let [color (:color state)
{:keys [a b c d e f g h c1 c2 label-position]} (lcars-points state)]
Expand All @@ -37,11 +46,13 @@
(q/begin-shape)
(apply q/vertex a)
(apply q/vertex b)
(apply q/quadratic-vertex (concat c1 c))
#?(:clj (apply q/quadratic-vertex (concat c1 c))
:cljs (apply quadratic-vertex (concat b c1 c)))
(apply q/vertex d)
(apply q/vertex e)
(apply q/vertex f)
(apply q/quadratic-vertex (concat c2 g))
#?(:clj (apply q/quadratic-vertex (concat c2 g))
:cljs (apply quadratic-vertex (concat f c2 g)))
(apply q/vertex h)
(apply q/vertex a)
(q/end-shape)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
(ns spacewar.ui.widgets.lights
(:require [quil.core :as q]
(:require [quil.core :as q #?@(:cljs [:include-macros true])]
[spacewar.ui.protocols :as p]
[spacewar.ui.config :as uic]))

Expand Down
Loading

0 comments on commit 2304103

Please sign in to comment.