           :jump-count (inc jump-count)
           :flappy-start-time cur-time
           :initial-vel jump-vel)))
-
-(defmethod transform :jump [_ state]
-  (jump state))
-
-(defn start-game [event-chan]
-  (.requestAnimationFrame js/window (fn [time]
-                                      (put! event-chan [:start-game time]))))
-
-(defmethod transform :start-game [[_ time] state]
-  (reset-state state time))
 
 ;; derivatives
 


(defn px [n] (str n "px"))

(defn pillar [{:keys [cur-x upper-height lower-height]}]
  [:div.pillars
   [:div.pillar.pillar-upper {:style {:left (px cur-x)
                                       :height upper-height}}]
   [:div.pillar.pillar-lower {:style {:left (px cur-x)
                                       :height lower-height}}]])

(defn main-template [{:keys [score cur-time jump-count timer-running border-pos event-chan flappy-y pillar-list]}]
  (sab/html [:div.board { :onMouseDown (fn [e]
                                         #_(put! event-chan [:jump])
                                         (swap! flap-state jump)
                                         (.preventDefault e))}
             [:h1.score score]
             (if-not timer-running
               [:a.start-button {:onClick #(start-game event-chan)}
                (if (pos? jump-count) "RESTART" "START")]
               [:span])
             [:div (map pillar pillar-list)]
             [:div.flappy {:style {:top (px flappy-y)}}]
             [:div.scrolling-border {:style { :background-position-x (px border-pos)}}]]))

(defn render-to [react-comp node]
  (.renderComponent js/React react-comp node))

(let [node (.getElementById js/document "board-area")]
  (defn renderer [full-state]
    (render-to (main-template full-state) node)))

(defn start []
  (let [event-chan (chan (sliding-buffer 1))]
    (go-loop []
             (when-let [msg (<! event-chan)]
               (let [new-state (transform msg @flap-state)]
                 (reset! flap-state (-clone new-state))
                 (when (:timer-running new-state)
                   (<! (timeout 30))
                   (.requestAnimationFrame js/window (fn [time] (put! event-chan [:time time]))))
                 (recur))))
    (swap! flap-state assoc :event-chan event-chan )
    (add-watch flap-state :renderer (fn [_ _ _ n]
               (renderer (world n))))
    (put! event-chan [:time (or (:cur-time @flap-state) 0)])))

(defn stop []
  (remove-watch flap-state :renderer)
  (when (:event-chan @flap-state)
    (close! (:event-chan @flap-state))))

(defn restart []
  (stop)
  (start))

(fw/defonce starting-game (start))

(fw/watch-and-reload :jsload-callback (fn [url] (restart)))

      update-flappy
      update-pillars
      collision?
      score))

(defn jump [{:keys [cur-time jump-count] :as state}]
  (-> state
