 (def bounds-buffer 1)
 
 (defn- in-bounds? [grid-size pos]
-  (let [[w h] (apply vector (map #(- % bounds-buffer) grid-size))
-        min-bound [(- w) (- h)]
-        max-bound [w h]
-        pos-k (keep-indexed #(let [mini (min-bound %1)
-                                   maxi (max-bound %1)]
-                               (when (and (>= %2 mini) (<= %2 maxi)) %2)) pos)]
-    (= (count pos) (count pos-k))))
-
+  (let [[w h] (apply vector (map #(- % bounds-buffer) grid-size))]
+    (every?
+      #(and (>= (% 1) (- (% 0))) (<= (% 1) (% 0)))
+      (map vector [w h] pos))))
 
 (defn- dist-from-sqr [pos1 pos2]
   (reduce + (map #(* % %) (map - pos1 pos2))))


(def window-half-size (apply vector (map #(float (/ %1 2)) window-size)))

(def frame-rate 15)

(defn- mk-ghost [state ghost-def]
  (let [[forest id] (forest/add-node (:forest state) (:start-pos ghost-def))
        ghost       (ghost/add-active-node (ghost/new-ghost) id)
        ]
    [(assoc state :forest forest) (assoc ghost :ghost-def ghost-def)]))

(defn- mk-ghosts [state]
  (let [[state ghosts] (reduce (fn [[state ghosts] ghost-def]
                                    (let [[state ghost] (mk-ghost state ghost-def)]
                                      [state (cons ghost ghosts)]))
                               [state '()]
                               (:ghost-defs state))]
        (assoc state :ghosts ghosts)))

(defn- new-state []
  (-> {:frame-rate frame-rate
       :color-cycle-period 8
       :exit-wait-frames 40
       :tail-length 7
       :frame 0
       :grid-width 45 ; from the center
       :forest (forest/new-forest grid/isometric)
       :ghost-defs [;{:start-pos [2 2]
                    ; :color-fn (fn [state] (q/color 0 1 1))
                    ; ;:color-fn (fn [state]
                    ; ;            (q/color (mod (:frame state) (frames-per-color-cycle state)) 1 1))
                    ; }
                    {:start-pos [-2 -2]
                     :color-fn (fn [state]
                                 (let [frames-per-color-cycle
                                       (* (:color-cycle-period state) (:frame-rate state))]
                                   (q/color
                                     (/ (mod (:frame state) frames-per-color-cycle)
                                        frames-per-color-cycle)
                                     1 1)))
                     }
                    ]
       }
      (mk-ghosts)
      ))

(defn setup []

(defn- curr-second [state]
  (float (/ (:frame state) (:frame-rate state))))

(defn- scale [grid-size xy]
  (map-indexed #(* %2 (float (/ (window-half-size %1)
                                (grid-size %1)))) xy))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; poss-fn
