From 7403a429fe13239d2ed64d8c2d5a59450788f8bc Mon Sep 17 00:00:00 2001 From: lindboe Date: Sat, 30 Jun 2018 16:12:45 -0700 Subject: [PATCH 1/3] Full grid drawing function Complete function to draw a square grid. Adds several helper functions for taking an initial element and shifting it a certain amount a certain number of times. Co-authored-by: Preben Ingvaldsen --- src/space_game/core.cljs | 46 ++++++++++++++++++++++++++++++++++++---- 1 file changed, 42 insertions(+), 4 deletions(-) diff --git a/src/space_game/core.cljs b/src/space_game/core.cljs index f420856..2c268c6 100644 --- a/src/space_game/core.cljs +++ b/src/space_game/core.cljs @@ -6,6 +6,39 @@ (defonce game (p/create-game (.-innerWidth js/window) (.-innerHeight js/window))) (defonce state (atom {})) +(defn shift-line-x + "Return a new line ({:x1 x1 :y1 y1 :x2 x2 :y2 y2}) shifted up or down by a given x value." + [line x-shift] + (-> line + (update :x1 + x-shift) + (update :x2 + x-shift))) + +(defn shift-line-y + "Return a new line shifted left or right by a given y value." + [line y-shift] + (-> line + (update :y1 + y-shift) + (update :y2 + y-shift))) + +(defn repeat-lines-x + "Given an initial line, construct a list of that line plus a given number of repeats of that line, shifted x amount" + [init-line num-squares x-shift] + (let [num-reps (+ 1 num-squares)] ;; number of lines to generate = squares + 1 + (for [i (range num-reps)] + (shift-line-x init-line (* i x-shift))))) + +(defn repeat-lines-y + "Given an initial line, construct a list of that line plus a given number of repeats of that line, shifted y amount" + [init-line num-squares y-shift] + (let [num-reps (+ 1 num-squares)] ;; number of lines to generate = squares + 1 + (for [i (range num-reps)] + (shift-line-y init-line (* i y-shift))))) + +(defn coordinates-to-line-element + "A helper function to put a map of coordinates in a line display element." + [coordinates] + [:line coordinates]) + (defn square-grid "Draw a square grid with given number of squares along a side, of the given square width." @@ -19,9 +52,14 @@ top-line-left-x (int (- width-center half-line-width)) top-line-right-x (int (+ width-center half-line-width)) left-line-top-y (int (- height-center half-line-width)) - left-line-bottom-y (int (+ height-center half-line-width))] - [[:line {:x1 top-line-left-x, :y1 left-line-top-y, :x2 top-line-right-x, :y2 left-line-top-y}] - [:line {:x1 top-line-left-x, :y1 left-line-top-y, :x2 top-line-left-x, :y2 left-line-bottom-y}]])) + left-line-bottom-y (int (+ height-center half-line-width)) + top-line {:x1 top-line-left-x :y1 left-line-top-y :x2 top-line-right-x :y2 left-line-top-y} + left-line {:x1 top-line-left-x :y1 left-line-top-y :x2 top-line-left-x :y2 left-line-bottom-y} + grid + (map coordinates-to-line-element + (concat (repeat-lines-x left-line num-squares square-width) + (repeat-lines-y top-line num-squares square-width)))] + grid)) (def main-screen (reify p/Screen @@ -30,7 +68,7 @@ (on-hide [this]) (on-render [this] (p/render game - (square-grid game 5 50))))) + (square-grid game 5 50))))) (events/listen js/window "mousemove" From 1e1eb6d2ef70b2254ac14e4698892e6fbf3fd1e2 Mon Sep 17 00:00:00 2001 From: Preben Ingvaldsen Date: Wed, 4 Jul 2018 15:00:48 -0700 Subject: [PATCH 2/3] Grid testing proof of concept The grid drawing function involves enough code that it should be its own file. We attempted writing clojurescript tests first, but we can make the grid file compile for just clojure if we pass in window height and width to the function, which we should be doing anyways. But, since setting up the clojurescript tests wasn't quite trivial, this commit exists as a proof of concept showing how to do it if the grid namespace was cljs. I'll be removing the cljs versions after this commit since we don't actually want them, but we may want something like this in the future. The "testing" part of the boot invocation is like a "with-profile test" in lein, although I'm a fan of just constructing a custom task to do that, I have no reason to run the test tasks without adding the test/ directory to source paths. I do wonder if there is a way to get the test runner to ignore src/ files, since they won't contain tests, but I assume removing src/ from source-paths will break things. It's not a big deal right now though, just results in extra output. Co-authored by: lindboe --- README.md | 7 ++++ build.boot | 6 +++ src/space_game/core.cljs | 69 ++++------------------------------ src/space_game/grid.cljc | 54 ++++++++++++++++++++++++++ src/space_game/grid.cljs | 57 ++++++++++++++++++++++++++++ test/space_game/grid_test.cljc | 8 ++++ test/space_game/grid_test.cljs | 9 +++++ 7 files changed, 148 insertions(+), 62 deletions(-) create mode 100644 src/space_game/grid.cljc create mode 100644 src/space_game/grid.cljs create mode 100644 test/space_game/grid_test.cljc create mode 100644 test/space_game/grid_test.cljs diff --git a/README.md b/README.md index 55339bd..38c5ab2 100644 --- a/README.md +++ b/README.md @@ -17,3 +17,10 @@ the application in a browser (localhost:3000), and in the repl client you should see a "connected!" message after a moment, and now you should be able to require cljs files and send javascript to the browser. (Note: incognito windows do not seem to work at the moment.) + +### Testing + +To run the clojure tests, run `boot testing test`. (The "testing" part adds the +test/ directory to the source paths.) + +To run clojurescript tests, run `boot testing test-cljs`. diff --git a/build.boot b/build.boot index 3fb429c..17a8cd1 100644 --- a/build.boot +++ b/build.boot @@ -8,6 +8,8 @@ [weasel "0.7.0" :scope "test"] [org.clojure/tools.nrepl "0.2.12" :scope "test"] [adzerk/boot-reload "0.5.2" :scope "test"] + [adzerk/boot-test "1.2.0" :scope "test"] + [crisptrutski/boot-cljs-test "0.3.5-SNAPSHOT" :scope "test"] [pandeiro/boot-http "0.8.3" :scope "test"] [javax.xml.bind/jaxb-api "2.3.0" :scope "test"] ; necessary for Java 9 compatibility ; project deps @@ -20,6 +22,8 @@ '[adzerk.boot-cljs :refer [cljs]] '[adzerk.boot-cljs-repl :refer [cljs-repl start-repl]] '[adzerk.boot-reload :refer [reload]] + '[adzerk.boot-test :refer :all] + '[crisptrutski.boot-cljs-test :refer [test-cljs]] '[pandeiro.boot-http :refer [serve]]) (deftask run [] @@ -48,3 +52,5 @@ (deftask build [] (set-env! :resource-paths #(conj % "prod-resources")) (comp (cljs :optimizations :advanced) (target))) + +(deftask testing [] (merge-env! :source-paths #{"test"}) identity) diff --git a/src/space_game/core.cljs b/src/space_game/core.cljs index 2c268c6..a9b0c68 100644 --- a/src/space_game/core.cljs +++ b/src/space_game/core.cljs @@ -1,66 +1,12 @@ (ns space-game.core (:require [play-cljs.core :as p] - [goog.events :as events]) + [goog.events :as events] + [space-game.grid :as grid]) (:require-macros [space-game.music :refer [build-for-cljs]])) (defonce game (p/create-game (.-innerWidth js/window) (.-innerHeight js/window))) (defonce state (atom {})) -(defn shift-line-x - "Return a new line ({:x1 x1 :y1 y1 :x2 x2 :y2 y2}) shifted up or down by a given x value." - [line x-shift] - (-> line - (update :x1 + x-shift) - (update :x2 + x-shift))) - -(defn shift-line-y - "Return a new line shifted left or right by a given y value." - [line y-shift] - (-> line - (update :y1 + y-shift) - (update :y2 + y-shift))) - -(defn repeat-lines-x - "Given an initial line, construct a list of that line plus a given number of repeats of that line, shifted x amount" - [init-line num-squares x-shift] - (let [num-reps (+ 1 num-squares)] ;; number of lines to generate = squares + 1 - (for [i (range num-reps)] - (shift-line-x init-line (* i x-shift))))) - -(defn repeat-lines-y - "Given an initial line, construct a list of that line plus a given number of repeats of that line, shifted y amount" - [init-line num-squares y-shift] - (let [num-reps (+ 1 num-squares)] ;; number of lines to generate = squares + 1 - (for [i (range num-reps)] - (shift-line-y init-line (* i y-shift))))) - -(defn coordinates-to-line-element - "A helper function to put a map of coordinates in a line display element." - [coordinates] - [:line coordinates]) - -(defn square-grid - "Draw a square grid with given number of squares along a side, of the given - square width." - [game num-squares square-width] - (let [window-width (p/get-width game) - window-height (p/get-height game) - grid-width (* num-squares square-width) - width-center (/ window-width 2) - height-center (/ window-height 2) - half-line-width (/ grid-width 2) - top-line-left-x (int (- width-center half-line-width)) - top-line-right-x (int (+ width-center half-line-width)) - left-line-top-y (int (- height-center half-line-width)) - left-line-bottom-y (int (+ height-center half-line-width)) - top-line {:x1 top-line-left-x :y1 left-line-top-y :x2 top-line-right-x :y2 left-line-top-y} - left-line {:x1 top-line-left-x :y1 left-line-top-y :x2 top-line-left-x :y2 left-line-bottom-y} - grid - (map coordinates-to-line-element - (concat (repeat-lines-x left-line num-squares square-width) - (repeat-lines-y top-line num-squares square-width)))] - grid)) - (def main-screen (reify p/Screen (on-show [this] @@ -68,16 +14,15 @@ (on-hide [this]) (on-render [this] (p/render game - (square-grid game 5 50))))) - + (grid/square-grid game 5 50 js/window.innerWidth js/window.innerHeight))))) (events/listen js/window "mousemove" - (fn [event] - (swap! state assoc :text-x (.-clientX event) :text-y (.-clientY event)))) + (fn [event] + (swap! state assoc :text-x (.-clientX event) :text-y (.-clientY event)))) (events/listen js/window "resize" - (fn [event] - (p/set-size game js/window.innerWidth js/window.innerHeight))) + (fn [event] + (p/set-size game js/window.innerWidth js/window.innerHeight))) (doto game (p/start) diff --git a/src/space_game/grid.cljc b/src/space_game/grid.cljc new file mode 100644 index 0000000..e4c659d --- /dev/null +++ b/src/space_game/grid.cljc @@ -0,0 +1,54 @@ +(ns space-game.grid) + +(defn shift-line-x + "Return a new line ({:x1 x1 :y1 y1 :x2 x2 :y2 y2}) shifted up or down by a given x value." + [line x-shift] + (-> line + (update :x1 + x-shift) + (update :x2 + x-shift))) + +(defn shift-line-y + "Return a new line shifted left or right by a given y value." + [line y-shift] + (-> line + (update :y1 + y-shift) + (update :y2 + y-shift))) + +(defn repeat-lines-x + "Given an initial line, construct a list of that line plus a given number of repeats of that line, shifted x amount" + [init-line num-squares x-shift] + (let [num-reps (+ 1 num-squares)] ;; number of lines to generate = squares + 1 + (for [i (range num-reps)] + (shift-line-x init-line (* i x-shift))))) + +(defn repeat-lines-y + "Given an initial line, construct a list of that line plus a given number of repeats of that line, shifted y amount" + [init-line num-squares y-shift] + (let [num-reps (+ 1 num-squares)] ;; number of lines to generate = squares + 1 + (for [i (range num-reps)] + (shift-line-y init-line (* i y-shift))))) + +(defn coordinates-to-line-element + "A helper function to put a map of coordinates in a line display element." + [coordinates] + [:line coordinates]) + +(defn square-grid + "Draw a square grid with given number of squares along a side, of the given + square width." + [game num-squares square-width window-width window-height] + (let [grid-width (* num-squares square-width) + width-center (/ window-width 2) + height-center (/ window-height 2) + half-line-width (/ grid-width 2) + top-line-left-x (int (- width-center half-line-width)) + top-line-right-x (int (+ width-center half-line-width)) + left-line-top-y (int (- height-center half-line-width)) + left-line-bottom-y (int (+ height-center half-line-width)) + top-line {:x1 top-line-left-x :y1 left-line-top-y :x2 top-line-right-x :y2 left-line-top-y} + left-line {:x1 top-line-left-x :y1 left-line-top-y :x2 top-line-left-x :y2 left-line-bottom-y} + grid + (map coordinates-to-line-element + (concat (repeat-lines-x left-line num-squares square-width) + (repeat-lines-y top-line num-squares square-width)))] + grid)) diff --git a/src/space_game/grid.cljs b/src/space_game/grid.cljs new file mode 100644 index 0000000..b2e0114 --- /dev/null +++ b/src/space_game/grid.cljs @@ -0,0 +1,57 @@ +(ns space-game.grid + (:require [play-cljs.core :as p])) + +(defn shift-line-x + "Return a new line ({:x1 x1 :y1 y1 :x2 x2 :y2 y2}) shifted up or down by a given x value." + [line x-shift] + (-> line + (update :x1 + x-shift) + (update :x2 + x-shift))) + +(defn shift-line-y + "Return a new line shifted left or right by a given y value." + [line y-shift] + (-> line + (update :y1 + y-shift) + (update :y2 + y-shift))) + +(defn repeat-lines-x + "Given an initial line, construct a list of that line plus a given number of repeats of that line, shifted x amount" + [init-line num-squares x-shift] + (let [num-reps (+ 1 num-squares)] ;; number of lines to generate = squares + 1 + (for [i (range num-reps)] + (shift-line-x init-line (* i x-shift))))) + +(defn repeat-lines-y + "Given an initial line, construct a list of that line plus a given number of repeats of that line, shifted y amount" + [init-line num-squares y-shift] + (let [num-reps (+ 1 num-squares)] ;; number of lines to generate = squares + 1 + (for [i (range num-reps)] + (shift-line-y init-line (* i y-shift))))) + +(defn coordinates-to-line-element + "A helper function to put a map of coordinates in a line display element." + [coordinates] + [:line coordinates]) + +(defn square-grid + "Draw a square grid with given number of squares along a side, of the given + square width." + [game num-squares square-width] + (let [window-width (p/get-width game) + window-height (p/get-height game) + grid-width (* num-squares square-width) + width-center (/ window-width 2) + height-center (/ window-height 2) + half-line-width (/ grid-width 2) + top-line-left-x (int (- width-center half-line-width)) + top-line-right-x (int (+ width-center half-line-width)) + left-line-top-y (int (- height-center half-line-width)) + left-line-bottom-y (int (+ height-center half-line-width)) + top-line {:x1 top-line-left-x :y1 left-line-top-y :x2 top-line-right-x :y2 left-line-top-y} + left-line {:x1 top-line-left-x :y1 left-line-top-y :x2 top-line-left-x :y2 left-line-bottom-y} + grid + (map coordinates-to-line-element + (concat (repeat-lines-x left-line num-squares square-width) + (repeat-lines-y top-line num-squares square-width)))] + grid)) \ No newline at end of file diff --git a/test/space_game/grid_test.cljc b/test/space_game/grid_test.cljc new file mode 100644 index 0000000..a396663 --- /dev/null +++ b/test/space_game/grid_test.cljc @@ -0,0 +1,8 @@ +(ns space-game.grid-test + (:require [clojure.test :refer :all] + [space-game.grid :as grid])) + +(deftest shift-line-x-test + (let [base-line {:x1 0 :x2 0 :y1 0 :y2 0} + expected-line {:x1 5 :x2 5 :y1 0 :y2 0}] + (is (= expected-line (grid/shift-line-x base-line 5))))) diff --git a/test/space_game/grid_test.cljs b/test/space_game/grid_test.cljs new file mode 100644 index 0000000..a152dea --- /dev/null +++ b/test/space_game/grid_test.cljs @@ -0,0 +1,9 @@ +(ns space-game.grid-test + (:require-macros [cljs.test :refer [deftest testing is]]) + (:require [cljs.test] + [space-game.grid :as grid])) + +(deftest shift-line-x-test [] + (let [base-line {:x1 0 :x2 0 :y1 0 :y2 0} + expected-line {:x1 5 :x2 5 :y1 0 :y2 0}] + (is (= expected-line (grid/shift-line-x base-line 5))))) From c314a73d8fbffcdd69e245c27732ecf77a0e3119 Mon Sep 17 00:00:00 2001 From: lindboe Date: Fri, 13 Jul 2018 14:55:15 -0700 Subject: [PATCH 3/3] Remove cljs testing examples Removing the cljs testing examples because they're not actually needed code, I just left them in the commit history for future reference. --- README.md | 2 -- build.boot | 2 -- src/space_game/grid.cljs | 57 ---------------------------------- test/space_game/grid_test.cljs | 9 ------ 4 files changed, 70 deletions(-) delete mode 100644 src/space_game/grid.cljs delete mode 100644 test/space_game/grid_test.cljs diff --git a/README.md b/README.md index 38c5ab2..ba7fba9 100644 --- a/README.md +++ b/README.md @@ -22,5 +22,3 @@ do not seem to work at the moment.) To run the clojure tests, run `boot testing test`. (The "testing" part adds the test/ directory to the source paths.) - -To run clojurescript tests, run `boot testing test-cljs`. diff --git a/build.boot b/build.boot index 17a8cd1..06ebae4 100644 --- a/build.boot +++ b/build.boot @@ -9,7 +9,6 @@ [org.clojure/tools.nrepl "0.2.12" :scope "test"] [adzerk/boot-reload "0.5.2" :scope "test"] [adzerk/boot-test "1.2.0" :scope "test"] - [crisptrutski/boot-cljs-test "0.3.5-SNAPSHOT" :scope "test"] [pandeiro/boot-http "0.8.3" :scope "test"] [javax.xml.bind/jaxb-api "2.3.0" :scope "test"] ; necessary for Java 9 compatibility ; project deps @@ -23,7 +22,6 @@ '[adzerk.boot-cljs-repl :refer [cljs-repl start-repl]] '[adzerk.boot-reload :refer [reload]] '[adzerk.boot-test :refer :all] - '[crisptrutski.boot-cljs-test :refer [test-cljs]] '[pandeiro.boot-http :refer [serve]]) (deftask run [] diff --git a/src/space_game/grid.cljs b/src/space_game/grid.cljs deleted file mode 100644 index b2e0114..0000000 --- a/src/space_game/grid.cljs +++ /dev/null @@ -1,57 +0,0 @@ -(ns space-game.grid - (:require [play-cljs.core :as p])) - -(defn shift-line-x - "Return a new line ({:x1 x1 :y1 y1 :x2 x2 :y2 y2}) shifted up or down by a given x value." - [line x-shift] - (-> line - (update :x1 + x-shift) - (update :x2 + x-shift))) - -(defn shift-line-y - "Return a new line shifted left or right by a given y value." - [line y-shift] - (-> line - (update :y1 + y-shift) - (update :y2 + y-shift))) - -(defn repeat-lines-x - "Given an initial line, construct a list of that line plus a given number of repeats of that line, shifted x amount" - [init-line num-squares x-shift] - (let [num-reps (+ 1 num-squares)] ;; number of lines to generate = squares + 1 - (for [i (range num-reps)] - (shift-line-x init-line (* i x-shift))))) - -(defn repeat-lines-y - "Given an initial line, construct a list of that line plus a given number of repeats of that line, shifted y amount" - [init-line num-squares y-shift] - (let [num-reps (+ 1 num-squares)] ;; number of lines to generate = squares + 1 - (for [i (range num-reps)] - (shift-line-y init-line (* i y-shift))))) - -(defn coordinates-to-line-element - "A helper function to put a map of coordinates in a line display element." - [coordinates] - [:line coordinates]) - -(defn square-grid - "Draw a square grid with given number of squares along a side, of the given - square width." - [game num-squares square-width] - (let [window-width (p/get-width game) - window-height (p/get-height game) - grid-width (* num-squares square-width) - width-center (/ window-width 2) - height-center (/ window-height 2) - half-line-width (/ grid-width 2) - top-line-left-x (int (- width-center half-line-width)) - top-line-right-x (int (+ width-center half-line-width)) - left-line-top-y (int (- height-center half-line-width)) - left-line-bottom-y (int (+ height-center half-line-width)) - top-line {:x1 top-line-left-x :y1 left-line-top-y :x2 top-line-right-x :y2 left-line-top-y} - left-line {:x1 top-line-left-x :y1 left-line-top-y :x2 top-line-left-x :y2 left-line-bottom-y} - grid - (map coordinates-to-line-element - (concat (repeat-lines-x left-line num-squares square-width) - (repeat-lines-y top-line num-squares square-width)))] - grid)) \ No newline at end of file diff --git a/test/space_game/grid_test.cljs b/test/space_game/grid_test.cljs deleted file mode 100644 index a152dea..0000000 --- a/test/space_game/grid_test.cljs +++ /dev/null @@ -1,9 +0,0 @@ -(ns space-game.grid-test - (:require-macros [cljs.test :refer [deftest testing is]]) - (:require [cljs.test] - [space-game.grid :as grid])) - -(deftest shift-line-x-test [] - (let [base-line {:x1 0 :x2 0 :y1 0 :y2 0} - expected-line {:x1 5 :x2 5 :y1 0 :y2 0}] - (is (= expected-line (grid/shift-line-x base-line 5)))))