 (defn animate-frame [objs]
   (render/animate-frame (reduce concat (vals objs))))
 
+(defn level-check [objs game]
+  (let [roid-count (count (:roids objs))
+        timer (:level-timer game)
+        [o g] (cond
+               (and (= 0 roid-count) (= nil timer))
+               [nil {:level-timer 100}]
+         
+               (and (= 0 roid-count) (= 0 timer))
+               [{:roids (roid/create-roids init-roid-count)} {:level-timer nil}]
+
+               (and (= 0 roid-count) (> timer 0))
+               [nil {:level-timer (dec timer)}]
+         
+               :else
+               [nil nil])]
+    [(merge objs o) (merge game g)]))
+
+(defn ship-check [objs game]
+  (let [ship-count (count (:ship objs))
+        timer (:ship-timer game)
+        [o g] (cond
+               (and (= 0 ship-count) (= nil timer)) [nil {:ship-timer 100}]
+               (and (= 0 ship-count) (= 0 timer)) [{:ship (ship/create)} {:ship-timer nil}]
+               (and (= 0 ship-count) (> timer 0)) [nil {:ship-timer (dec timer)}]
+               :else [nil nil])]
+    [(merge objs o) (merge game g)]))
+
 (defn game-loop [init-objs]
   (let [input-chan (input/user-input)]
-    (go-loop [objs init-objs start (timestamp)]
+    (go-loop [objs init-objs game nil start (timestamp)]
       (let [objs (merge objs
                         {:input (alt! [input-chan] ([v] v) :default [])}
                         (update-ship objs)

  (:require-macros
   [cljs.core.async.macros :refer [alt! go-loop]]))

(defn timestamp [] (.getTime (js/Date.)))

(defn update-ship [{:keys [ship flames input]}]
  (let [ship (->> ship
                  (map (ship/control input))
                 (ship/update))
        flames (concat flames (ship/flames ship))]
    {:ship ship :flames flames}))


            objs (merge objs (collisions/shot-roid objs))
            objs (merge objs (collisions/ship-roid objs))
            objs (merge objs {:explosions (explode/update (:explosions objs))})
            [objs game] (level-check objs game)
            [objs game] (ship-check objs game)
            end (- (timestamp) start)]
        (animate-frame objs)
        (<! (timeout (- 33 end)))
        (recur objs game (timestamp))))))

; create some asteroids, the ship, and enter the game loop
(game-loop {:roids (roid/create-roids init-roid-count)
            :ship (ship/create)})


