diff --git a/.gitignore b/.gitignore index a9fe6fb..cfb3da0 100644 --- a/.gitignore +++ b/.gitignore @@ -11,3 +11,5 @@ pom.xml.asc .lein-plugins/ .lein-failures .nrepl-port +figwheel_server.log +resources/public/js/out \ No newline at end of file diff --git a/main.js b/main.js new file mode 100644 index 0000000..b8178be --- /dev/null +++ b/main.js @@ -0,0 +1,7 @@ +var CLOSURE_UNCOMPILED_DEFINES = null; +if(typeof goog == "undefined") document.write(''); +document.write(''); +document.write(''); + +document.write(""); +document.write(''); \ No newline at end of file diff --git a/project.clj b/project.clj new file mode 100644 index 0000000..2067d97 --- /dev/null +++ b/project.clj @@ -0,0 +1,40 @@ +(defproject topdown2d "0.1.0-SNAPSHOT" + :description "FIXME: write description" + :url "http://example.com/FIXME" + :license {:name "Eclipse Public License" + :url "http://www.eclipse.org/legal/epl-v10.html"} + + :dependencies [ + [org.clojure/clojure "1.8.0"] + [org.clojure/clojurescript "1.9.521"] + ] + + :plugins [ + [lein-cljsbuild "1.1.7"] + [lein-figwheel "0.5.14"] + ] + + :cljsbuild { + :builds [{ + :id "default" + :source-paths ["src/cljs"] + :figwheel true + :compiler { + :main topdown2d.core + :output-to "resources/public/js/cljsbuild-main.js" + :output-dir "resources/public/js/out" + :asset-path "js/out" + :pretty-print true + } + }] + } + + :figwheel { + :css-dirs ["resources/css"] + } + + ;:hooks [leiningen.cljsbuild] + + :main ^:skip-aot topdown2d.core + :target-path "target/%s" + :profiles {:uberjar {:aot :all}}) diff --git a/resources/public/index.html b/resources/public/index.html new file mode 100644 index 0000000..0a1baf6 --- /dev/null +++ b/resources/public/index.html @@ -0,0 +1,10 @@ + + +
+ + + + + + + diff --git a/resources/public/js/cljsbuild-main.js b/resources/public/js/cljsbuild-main.js new file mode 100644 index 0000000..c0f85e6 --- /dev/null +++ b/resources/public/js/cljsbuild-main.js @@ -0,0 +1,7 @@ +var CLOSURE_UNCOMPILED_DEFINES = null; +if(typeof goog == "undefined") document.write(''); +document.write(''); +document.write(''); + +document.write(""); +document.write(''); \ No newline at end of file diff --git a/src/cljs/topdown2d/core.cljs b/src/cljs/topdown2d/core.cljs new file mode 100644 index 0000000..f94b6bc --- /dev/null +++ b/src/cljs/topdown2d/core.cljs @@ -0,0 +1,85 @@ +(ns topdown2d.core + (:require [topdown2d.demoscene])) + +(def gamestate { + :canvas (.getElementById js/document "gamecanvas") + :2d (.getContext (.getElementById js/document "gamecanvas") "2d") + :timing { + :prev 0 + :now 0 + :fps 0 + } + :scene :demo + :scenes { + :demo { + :update topdown2d.demoscene/update + :draw topdown2d.demoscene/draw + :data {} + } + } +}) + +(aset (:2d gamestate) "font" "10px monospace") + +(def keysdown (atom [])) + +(defn keydown? [keycode] + (some #{keycode} @keysdown)) + +(.addEventListener js/document + "keydown" + (fn [event] + (let [code (symbol (.-code event))] + (swap! keysdown #(set (conj %1 code)))))) + +(.addEventListener js/document + "keyup" + (fn [event] + (let [code (symbol (.-code event))] + (swap! keysdown + (fn [coll] + (remove #(= % code) coll)))))) + +(defn set-timing [state timingkey] + (update-in state + [:timing timingkey] + #(.now js/performance))) + +(defn set-fps [state] + (let [newstate (set-timing state :now) + now (get-in newstate [:timing :now]) + prev (get-in newstate [:timing :prev]) + duration (- now prev) + fps (/ 1000 duration)] + (update-in newstate [:timing :fps] (fn [] fps)))) + +(defn update-scene [gamestate] + (let [scenekey (:scene gamestate) + scenedata (get-in gamestate [:scenes scenekey]) + updatefunc (:update scenedata) + newdata (updatefunc gamestate scenedata)] + (update-in gamestate [:scenes scenekey] (fn [] newdata)))) + +(defn update-step [gamestate] + (-> gamestate + (set-fps) + (set-timing :prev) + (update-scene))) + +(defn draw-step [gamestate] + (.clearRect (:2d gamestate) 0 0 400 600) + (.fillText + (:2d gamestate) + (int (get-in gamestate [:timing :fps])) + 0 10)) + +(defn mainloop [gamestate] + (let [newstate (update-step gamestate)] + (draw-step newstate) + (.setTimeout js/window + (fn [] + (.requestAnimationFrame js/window + #(mainloop newstate))) + (/ 1000 30)))) + +(mainloop gamestate) diff --git a/src/cljs/topdown2d/demoscene.cljs b/src/cljs/topdown2d/demoscene.cljs new file mode 100644 index 0000000..0eaa65c --- /dev/null +++ b/src/cljs/topdown2d/demoscene.cljs @@ -0,0 +1,7 @@ +(ns topdown2d.demoscene) + +(defn update [gamestate scenedata] + (.debug js/console "a") + scenedata) + +(defn draw [gamestate scenedata]) diff --git a/test/topdown2d/core_test.clj b/test/topdown2d/core_test.clj new file mode 100644 index 0000000..331acaa --- /dev/null +++ b/test/topdown2d/core_test.clj @@ -0,0 +1,7 @@ +(ns topdown2d.core-test + (:require [clojure.test :refer :all] + [topdown2d.core :refer :all])) + +(deftest a-test + (testing "FIXME, I fail." + (is (= 0 1))))