30fps loop, scene handling, keydowns, almost nothing to see

master
Josha von Gizycki 7 years ago
parent f0e66456b1
commit fff66a6e54

2
.gitignore vendored

@ -11,3 +11,5 @@ pom.xml.asc
.lein-plugins/ .lein-plugins/
.lein-failures .lein-failures
.nrepl-port .nrepl-port
figwheel_server.log
resources/public/js/out

@ -0,0 +1,7 @@
var CLOSURE_UNCOMPILED_DEFINES = null;
if(typeof goog == "undefined") document.write('<script src="out/goog/base.js"></script>');
document.write('<script src="out/cljs_deps.js"></script>');
document.write('<script>if (typeof goog == "undefined") console.warn("ClojureScript could not load :main, did you forget to specify :asset-path?");</script>');
document.write("<script>if (typeof goog != \"undefined\") { goog.require(\"figwheel.connect.build_default\"); }</script>");
document.write('<script>goog.require("topdown2d.core");</script>');

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

@ -0,0 +1,10 @@
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<canvas id="gamecanvas" height="400" width="600"></canvas>
<script src="js/cljsbuild-main.js"></script>
</body>
</html>

@ -0,0 +1,7 @@
var CLOSURE_UNCOMPILED_DEFINES = null;
if(typeof goog == "undefined") document.write('<script src="js/out/goog/base.js"></script>');
document.write('<script src="js/out/cljs_deps.js"></script>');
document.write('<script>if (typeof goog == "undefined") console.warn("ClojureScript could not load :main, did you forget to specify :asset-path?");</script>');
document.write("<script>if (typeof goog != \"undefined\") { goog.require(\"figwheel.connect.build_default\"); }</script>");
document.write('<script>goog.require("topdown2d.core");</script>');

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

@ -0,0 +1,7 @@
(ns topdown2d.demoscene)
(defn update [gamestate scenedata]
(.debug js/console "a")
scenedata)
(defn draw [gamestate scenedata])

@ -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))))
Loading…
Cancel
Save