 
 (defcard draw-scene
          (fn [scene _owner]
-           (let [scale 2]
-             (sab/html [:div {:style {:backgroundColor "blue" :width (str (* scale 320) "px") :height (str (* scale 240) "px") :position "relative"}}
-                        (map (fn [{x :x y :y w :w h :h}]
-                               [:div {:style {:width           (str (* scale w) "px") :height (str (* scale h) "px") :borderRadius (str (* scale w) "px")
-                                              :backgroundColor "brown"
-                                              :position        "absolute" :left (str (* scale x) "px") :bottom (str (* scale y) "px")}}])
+           (let [scale 2
+                 view-h (str (* scale 240) "px")]
+             (sab/html [:div {:style {:backgroundColor "blue" :width (str (* scale 320) "px") :height view-h :position "relative"}}
+                        (map (fn [{x :x y :y w :w h :h :as ha}]
+                               [:div
+                                (map (fn [trans]
+                                       (println trans (:target trans))
+                                       (let [[s e] (:interval trans)
+                                             sx (* scale (:x (extrapolate ha s)))
+                                             ex (* scale (:x (extrapolate ha e)))]
+                                         [:div {:style {:height view-h :width (.abs js/Math (- sx ex)) :position :absolute :left (.min js/Math sx ex) :backgroundColor "grey"}}
+                                          [:div {:style {:position :absolute :top "64px" :width "200px"}} (str (:id ha) "-" (:target (:transition trans)))]
+                                          [:div {:style {:height view-h :width (* scale 2) :backgroundColor "red"}}]
+                                          [:div {:style {:height view-h :width (* scale 2) :backgroundColor "green"}}]]))
+                                     (vector (first (transition-intervals (:objects scene)
+                                                                 ha
+                                                                 Infinity
+                                                                 (required-transitions ha)))))])
+                             (map #(extrapolate % (:now @scene)) (vals (:objects @scene))))
+                        (map (fn [{x :x y :y w :w h :h :as ha}]
+                               [:div
+                                [:div {:style {:width           (str (* scale w) "px") :height (str (* scale h) "px") :borderRadius (str (* scale w) "px")
+                                               :backgroundColor "brown"
+                                               :position        "absolute" :left (str (* scale x) "px") :bottom (str (* scale y) "px")}}]])
                              (map #(extrapolate % (:now @scene)) (vals (:objects @scene))))
                         [:button {:onClick #(do (swap! scene (fn [s] (assoc s :playing (not (:playing s))))) true)} (if (:playing @scene) "PAUSE" "PLAY")]
                         [:button {:onClick #(reset-scene-a!)} "RESET"]])))

  (:require
    #_[om.core :as om :include-macros true]
    [clojure.set :refer [union]]
    [sablono.core :as sab :include-macros true])
  (:require-macros
    [devcards.core :as dc :refer [defcard deftest]]))

(defonce scene-a (atom {}))
(defonce last-time nil)

(defn goomba [id x y speed state others]
  (make-ha id                                               ;id
           {:x     x :y y                                   ;init
            :w     16 :h 16
            :state state}
           (make-state
             :right                                         ;name
             {:x speed}                                     ;flows
             ;edges
             ; x < 16 && x + dx*frame >= 16 --> x >= 16 - dx*frame
             (make-edge :left [[:lt :x 64] [:geq :x '(- 64 [:d :x])]] #{:required})
             ; x + 16 < x2 + dx2*frame && x + dx*frame + 16 >= x2 + dx2*frame
             ; x - x2 < -16 + dx2*frame && x - x2 >= dx2*frame - dx*frame - 16
             (map #(make-edge :left [[:lt :x [% :x] (list '+ -16 [:d % :x])]
                                     [:geq :x [% :x] (list '- [:d % :x] [:d :x] 16)]]
                              #{:required}) others))
           (make-state
             :left                                          ;name
             {:x (- speed)}                                 ;flows
             ;edges
             ; x > 8 && x + dx*frame <= 8 --> x <= 8 - dx*frame
             (make-edge :right [[:gt :x 8] [:leq :x '(- 8 [:d :x])]] #{:required})
             ; x > x2 + 16 + dx2 && x + dx*frame <= x2 + dx2*frame + 16 --> x - x2 > 16 + dx2 && x - x2 <= dx2*frame + 16 - dx*frame
             ; x - x2 > 16 + dx2
             (map #(make-edge :right [[:gt :x [% :x] (list '+ 16 [:d % :x])]
                                      [:leq :x [% :x] (list '+ [:d % :x] 16 (list '- [:d :x]))]]
                              #{:required}) others))))

(defn make-scene-a [x] (let [objects [(goomba :a x 8 8 :right #{:b}) (goomba :b (* x 4) 8 8 :left #{:a})]
                             obj-ids (map :id objects)
                             obj-dict (zipmap obj-ids objects)
                             ; got to let every HA enter its current (initial) state to set up state invariants like
