 
 (def app-state (atom {:text "Hello Chestnut!"
                       :hero {:move false
+                             :dx 0
+                             :dy 0
                              :life 100
                              :position (hero-start-position grid-content)
                              :jump {:time nil :bottom nil}


(defn hero-classes [app]
  (let [
        left-class (when (= :left (get-in app [:hero :move]))
          "left "
          )]
    (if (get-in app [:hero :move])
      (str "hero " left-class "step-" (step-class app))
      (str "hero " left-class)
      )


(defn in-lava [app]
  (let [position (get-in app [:hero :position])]
    (some #{4} (blocks position))
    )
  )

(defn move-horizontally [hero]
  (let [left-position (get-in hero [:position :left])]
    (if (moving? hero)
      (update-in hero [:position :left] (fn [old-left] (+ old-left (hero :dx))))
      hero
      )))

(defn gravity [current-vertical jump-time hero]
  (let [last-bottom (get-in hero [:position :bottom])
        initial-bottom (get-in hero [:jump :bottom])
        dt (/ (- (.getTime (js/Date.)) jump-time) 1000)
        vforce (* 1000 dt)
        gforce (/ (* 2000 dt dt) 2)
        height (+ (- vforce gforce) initial-bottom)
        ]
      (update-in hero [:position :bottom] (fn [_] height))

      )
  )

;; (defn grav [hero]
;;   (let [vertical-velocity (get-in hero [:vertical-velocity])]
;;     (if vertical-velocity
;;       (let [new-hero (update-in hero [:vertical-velocity] (fn [original] (- original 5)))
;;             new-velocity (get-in new-hero [:vertical-velocity])]
;;         (update-in (get-in hero [:position :bottom] (fn [original] (+ original new-velocity))))
;;         )
;;       hero
;;       )
;;     )
;;   )

(defn move-vertically [hero]
  (let [current-vertical (get-in hero [:position :bottom])
        jump-time (get-in hero [:jump :start])]
    (if jump-time
      (gravity current-vertical jump-time hero)
      hero
      )
    )
  )

(defn ground-resistance [hero]
  (let [position (get-in hero [:position])
        coord-y (vertical-coord position)
        current-tiles (blocks position)]
    (if (some #{1} current-tiles)
      (let [new-hero (update-in hero [:position :bottom] (fn [_] (* (+ 1 coord-y) 70)))]
        (update-in new-hero [:jump :start] (fn [_] nil)))
      hero
      )
    ))

(defn block-resistance [hero]
  (let [position (get-in hero [:position])
        coords-x (horizontal-coords position)
        current-tiles (blocks position)]
    (if (some #{1} current-tiles)
      (condp = (moving? hero)
        :left (update-in hero [:position :left] (fn [_] (* (+ (coords-x :left) 1) 70)))

        :right (update-in hero [:position :left] (fn [_] (* (- (coords-x :left) 1) 70))))
      hero
      )
    ))

(defn lava-damage [hero]
  hero)

(defn spike-damage [hero]
  hero)

(defn off-screen [hero]
  hero)

(defn hero [app owner]
  (reify
