Skip to content
Permalink
Browse files

ClojureScript support

  • Loading branch information...
mfikes committed Dec 5, 2018
1 parent b526361 commit 2304103b1d4adfcbaec0fca28fbf189c45ecd2f0
Showing with 121 additions and 53 deletions.
  1. +2 −0 .gitignore
  2. +25 −3 project.clj
  3. +11 −0 resources/public/index.html
  4. +18 −8 src/spacewar/{core.clj → core.cljc}
  5. 0 src/spacewar/game_logic/{bases.clj → bases.cljc}
  6. 0 src/spacewar/game_logic/{clouds.clj → clouds.cljc}
  7. 0 src/spacewar/game_logic/{config.clj → config.cljc}
  8. 0 src/spacewar/game_logic/{explosions.clj → explosions.cljc}
  9. 0 src/spacewar/game_logic/{hit.clj → hit.cljc}
  10. 0 src/spacewar/game_logic/{klingons.clj → klingons.cljc}
  11. 0 src/spacewar/game_logic/{romulans.clj → romulans.cljc}
  12. 0 src/spacewar/game_logic/{ship.clj → ship.cljc}
  13. +1 −1 src/spacewar/game_logic/{shots.clj → shots.cljc}
  14. 0 src/spacewar/game_logic/{stars.clj → stars.cljc}
  15. 0 src/spacewar/game_logic/{world.clj → world.cljc}
  16. 0 src/spacewar/{geometry.clj → geometry.cljc}
  17. +15 −14 src/spacewar/ui/{complex.clj → complex.cljc}
  18. 0 src/spacewar/ui/{config.clj → config.cljc}
  19. +2 −2 src/spacewar/ui/control_panels/{damage_panel.clj → damage_panel.cljc}
  20. +2 −2 src/spacewar/ui/control_panels/{deploy_panel.clj → deploy_panel.cljc}
  21. +1 −1 src/spacewar/ui/control_panels/{engine_panel.clj → engine_panel.cljc}
  22. 0 src/spacewar/ui/control_panels/{scan_panel.clj → scan_panel.cljc}
  23. 0 src/spacewar/ui/control_panels/{status_panel.clj → status_panel.cljc}
  24. +1 −1 src/spacewar/ui/control_panels/{weapons_panel.clj → weapons_panel.cljc}
  25. +1 −1 src/spacewar/ui/{front_view.clj → front_view.cljc}
  26. +2 −2 src/spacewar/ui/{icons.clj → icons.cljc}
  27. +1 −0 src/spacewar/ui/{protocols.clj → protocols.cljc}
  28. +2 −2 src/spacewar/ui/{strategic_scan.clj → strategic_scan.cljc}
  29. +2 −2 src/spacewar/ui/{tactical_scan.clj → tactical_scan.cljc}
  30. +13 −3 src/spacewar/ui/{view_frame.clj → view_frame.cljc}
  31. +1 −1 src/spacewar/ui/widgets/{button.clj → button.cljc}
  32. +1 −1 src/spacewar/ui/widgets/{direction_selector.clj → direction_selector.cljc}
  33. +1 −1 src/spacewar/ui/widgets/{engage.clj → engage.cljc}
  34. +1 −1 src/spacewar/ui/widgets/{horizontal_scale.clj → horizontal_scale.cljc}
  35. +14 −3 src/spacewar/ui/widgets/{lcars.clj → lcars.cljc}
  36. +1 −1 src/spacewar/ui/widgets/{lights.clj → lights.cljc}
  37. +1 −1 src/spacewar/ui/widgets/{named_indicator.clj → named_indicator.cljc}
  38. +1 −1 src/spacewar/ui/widgets/{slider.clj → slider.cljc}
  39. 0 src/spacewar/{util.clj → util.cljc}
  40. +1 −1 src/spacewar/{vector.clj → vector.cljc}
@@ -11,4 +11,6 @@ pom.xml.asc
.hg/
*.iml
.idea
/figwheel_server.log
/resources/public/js

@@ -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}}]})
@@ -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>
@@ -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]
@@ -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?)
@@ -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)
@@ -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)
@@ -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)))
@@ -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
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.
@@ -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?)
File renamed without changes.
File renamed without changes.
File renamed without changes.
@@ -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]
@@ -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))
)

File renamed without changes.
@@ -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]]
@@ -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]))
@@ -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
@@ -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
@@ -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]
@@ -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]
@@ -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))
@@ -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]]
@@ -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]
@@ -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))
@@ -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]
@@ -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)
)))

@@ -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]]
@@ -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
@@ -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}
@@ -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]))
@@ -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]
@@ -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]))
@@ -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]))

@@ -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]
@@ -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)]
@@ -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)
@@ -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]))

0 comments on commit 2304103

Please sign in to comment.
You can’t perform that action at this time.