 (enable-console-print!)
 
 (def world (atom {}))
+
+(def command-queue (atom cljs.core/PersistentQueue.EMPTY))
+
+(defn handle-input [event]
+  (let [key (.-key event)]
+    (swap! command-queue conj
+           (case key
+             "w" {:type :input :key :up}
+             "a" {:type :input :key :left}
+             "s" {:type :input :key :down}
+             "d" {:type :input :key :right}
+             "p" {:type :input :key :pause}))
+    (when (not (.-repeat event))
+      (print (.-repeat event)))))
+
+(defn process-input []
+  (loop [attempts 10]
+    (let [command (peek @command-queue)]
+      (if command
+        ;; change? What if somethign else pops between above and below
+        ;; have function check if peek and item are same item, yay! then do pop
+        ;; else, recur w/o doing anything to try again.
+        (do (swap! command-queue pop)
+            (print command)
+            (when (and (= (command :type) :input)
+                       (= (command :key) :pause))
+              (swap! world assoc :running
+                     (not (@world :running))))
+            (recur (dec attempts)))))))
 
 (defn update-world
   [delta-time]

    (.add scene light2)
    (.setSize renderer js/window.innerWidth js/window.innerHeight)
    (js/document.body.appendChild (.-domElement renderer))
    (swap! world assoc :prev js/Performance.now)
    (swap! world assoc :accum-time 0.0)
    (let [animate (fn animate [current-time]

        leftover-time (@world :accum-time)
        time-step 16.666666666666668]
    (swap! world assoc :prev-time now)
    (process-input)
    (when (@world :running)
      (swap! world assoc :leftover-time
             (loop [accumulated (+ leftover-time (- now prev))]
               (if (>= accumulated time-step)
                 (do (update-world time-step)
                     (recur (- accumulated time-step)))
                 accumulated))))))

(defn ^:export init-game
  []
