Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draw a grid and test it #4

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,8 @@ 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.)
4 changes: 4 additions & 0 deletions build.boot
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
[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"]
[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
Expand All @@ -20,6 +21,7 @@
'[adzerk.boot-cljs :refer [cljs]]
'[adzerk.boot-cljs-repl :refer [cljs-repl start-repl]]
'[adzerk.boot-reload :refer [reload]]
'[adzerk.boot-test :refer :all]
'[pandeiro.boot-http :refer [serve]])

(deftask run []
Expand Down Expand Up @@ -48,3 +50,5 @@
(deftask build []
(set-env! :resource-paths #(conj % "prod-resources"))
(comp (cljs :optimizations :advanced) (target)))

(deftask testing [] (merge-env! :source-paths #{"test"}) identity)
31 changes: 7 additions & 24 deletions src/space_game/core.cljs
Original file line number Diff line number Diff line change
@@ -1,45 +1,28 @@
(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 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))]
[[: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}]]))

(def main-screen
(reify p/Screen
(on-show [this]
(reset! state {:text-x 20 :text-y 30}))
(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)
Expand Down
54 changes: 54 additions & 0 deletions src/space_game/grid.cljc
Original file line number Diff line number Diff line change
@@ -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))
8 changes: 8 additions & 0 deletions test/space_game/grid_test.cljc
Original file line number Diff line number Diff line change
@@ -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)))))