moving objects, fixes

master
Josha von Gizycki 7 years ago
parent b2baae0b9b
commit 91b35cba12

2
.gitignore vendored

@ -12,4 +12,4 @@ pom.xml.asc
.lein-failures
.nrepl-port
figwheel_server.log
resources/public/js/out
resources/public/js

@ -1,7 +0,0 @@
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>');

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

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

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

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