From 91b35cba12a261c1d58d7719f677c136262fe4b2 Mon Sep 17 00:00:00 2001 From: Josha von Gizycki Date: Tue, 17 Oct 2017 14:03:23 +0200 Subject: [PATCH] moving objects, fixes --- .gitignore | 2 +- resources/public/js/cljsbuild-main.js | 7 ---- src/cljs/topdown2d/core.cljs | 52 ++++++++++++++------------- src/cljs/topdown2d/demoscene.cljs | 46 +++++++++++++++++++++--- src/cljs/topdown2d/input.cljs | 27 ++++++++++++++ src/cljs/topdown2d/objects.cljs | 18 ++++++++++ 6 files changed, 116 insertions(+), 36 deletions(-) delete mode 100644 resources/public/js/cljsbuild-main.js create mode 100644 src/cljs/topdown2d/input.cljs create mode 100644 src/cljs/topdown2d/objects.cljs diff --git a/.gitignore b/.gitignore index cfb3da0..6535af0 100644 --- a/.gitignore +++ b/.gitignore @@ -12,4 +12,4 @@ pom.xml.asc .lein-failures .nrepl-port figwheel_server.log -resources/public/js/out \ No newline at end of file +resources/public/js \ No newline at end of file diff --git a/resources/public/js/cljsbuild-main.js b/resources/public/js/cljsbuild-main.js deleted file mode 100644 index c0f85e6..0000000 --- a/resources/public/js/cljsbuild-main.js +++ /dev/null @@ -1,7 +0,0 @@ -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 index f94b6bc..5f11ec1 100644 --- a/src/cljs/topdown2d/core.cljs +++ b/src/cljs/topdown2d/core.cljs @@ -1,5 +1,9 @@ (ns topdown2d.core - (:require [topdown2d.demoscene])) + (:require + [topdown2d.demoscene :as demoscene] + [topdown2d.input :as input])) + +(enable-console-print!) (def gamestate { :canvas (.getElementById js/document "gamecanvas") @@ -9,11 +13,13 @@ :now 0 :fps 0 } + :keys [] :scene :demo :scenes { :demo { - :update topdown2d.demoscene/update - :draw topdown2d.demoscene/draw + :update demoscene/update + :draw demoscene/draw + :init demoscene/init :data {} } } @@ -21,25 +27,6 @@ (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] @@ -71,7 +58,11 @@ (.fillText (:2d gamestate) (int (get-in gamestate [:timing :fps])) - 0 10)) + 0 10) + (let [scenekey (:scene gamestate) + scene (scenekey (:scenes gamestate)) + drawfunc (:draw scene)] + (drawfunc gamestate scene))) (defn mainloop [gamestate] (let [newstate (update-step gamestate)] @@ -82,4 +73,17 @@ #(mainloop newstate))) (/ 1000 30)))) -(mainloop gamestate) +(defn init-scenes [] + (assoc + gamestate + :scenes + (reduce + (fn [scenes [scenekey scenedata]] + (let [initfunc (:init scenedata) + newdata (initfunc gamestate scenedata)] + (assoc scenes + scenekey newdata))) + {} + (:scenes gamestate)))) + +(mainloop (init-scenes)) diff --git a/src/cljs/topdown2d/demoscene.cljs b/src/cljs/topdown2d/demoscene.cljs index 0eaa65c..7a0de49 100644 --- a/src/cljs/topdown2d/demoscene.cljs +++ b/src/cljs/topdown2d/demoscene.cljs @@ -1,7 +1,45 @@ -(ns topdown2d.demoscene) +(ns topdown2d.demoscene + (:require + [topdown2d.objects :as objects] + [topdown2d.input :as input])) + +(defn init [gamestate scenedata] + (assoc scenedata + :data + { + :bumper + { + :x 50 + :y 50 + :w 10 + :h 10 + :v 0 + } + :box + { + :x 5 + :y 5 + :w 10 + :h 10 + :v 5 + :d :? + } + })) (defn update [gamestate scenedata] - (.debug js/console "a") - scenedata) + (let [box (get-in scenedata [:data :box]) + dir (input/dirinput) + box (assoc box :d dir)] + (update-in scenedata + [:data :box] + #(objects/move box)))) -(defn draw [gamestate scenedata]) +(defn draw [gamestate scenedata] + (let [{{:keys [bumper box]} :data} scenedata + ctx (:2d gamestate)] + (let [{:keys [x y w h]} bumper] + (.fillRect ctx + x y w h)) + (let [{:keys [x y w h]} box] + (.strokeRect ctx + x y w h)))) diff --git a/src/cljs/topdown2d/input.cljs b/src/cljs/topdown2d/input.cljs new file mode 100644 index 0000000..9d06b88 --- /dev/null +++ b/src/cljs/topdown2d/input.cljs @@ -0,0 +1,27 @@ +(ns topdown2d.input) + +(def keysdown (atom {})) + +(defn keydown? [code] + (get @keysdown (name code) false)) + +(.addEventListener js/document + "keydown" + (fn [event] + (swap! keysdown #(assoc % (.-code event) true)) + false)) + +(.addEventListener js/document + "keyup" + (fn [event] + (swap! keysdown + #(assoc % (.-code event) false)) + false)) + +(defn dirinput [] + (cond + (keydown? :ArrowLeft) :w + (keydown? :ArrowRight) :e + (keydown? :ArrowUp) :n + (keydown? :ArrowDown) :s + :else :?)) diff --git a/src/cljs/topdown2d/objects.cljs b/src/cljs/topdown2d/objects.cljs new file mode 100644 index 0000000..f44ec42 --- /dev/null +++ b/src/cljs/topdown2d/objects.cljs @@ -0,0 +1,18 @@ +(ns topdown2d.objects) + +(defn move [obj] + (let [{:keys [x y v d]} obj] + (cond + (= d :w) + (assoc obj + :x (- x v)) + (= d :e) + (assoc obj + :x (+ x v)) + (= d :n) + (assoc obj + :y (- y v)) + (= d :s) + (assoc obj + :y (+ y v)) + :else obj)))