       (if item
         (if (valid-collision-item? item width height)
           (let [id (:id (nth item 4))
-                collisions (rbush-search spatial-index item)]
-            (if (some #(and (not-self? id %) (not-self-attack? id %))
+                collisions (r/rbush-search spatial-index item)]
+            (if (some #(not (or (self? id %) (self-attack? id %)))
                       collisions)
               (recur (rest items)
                      (assoc! accum id [(ev/mk-event {:collisions collisions}

  "System for checking collisions between entities"
  (:require [chocolatier.utils.logging :as log]
            [chocolatier.engine.ecs :as ecs]
            [chocolatier.engine.events :as ev]))


(defn rbush
  ([]
   (js/rbush))
  ([max-entries]
   (js/rbush max-entries)))

(defn rbush-item
  ([x1 y1 x2 y2]
   (rbush-item x1 y1 x2 y2 nil))
  ([x1 y1 x2 y2 data]
   (array x1 y1 x2 y2 data)))

(defn rbush-insert!
  [rbush item]
  (.insert rbush item))

(defn rbush-bulk-insert!
  [rbush items]
  (.load rbush items))

(defn rbush-remove!
  [rbush item]
  (.remove rbush item))

(defn rbush-clear!
  [rbush]
  (.clear rbush))

(defn rbush-collides?
  [rbush item]
  (.collides rbush item))

(defn rbush-search
  [rbush item]
  (.search rbush item))

(defn rbush-all
  [rbush]
  (.all rbush))

(def spatial-index-location
  [:state :spatial-index])


(defn get-or-create-spatial-index
  [state max-entries]
  (get-in state spatial-index-location (r/rbush max-entries)))

(defn mk-broad-collision-system
  "Returns a system function that creates a spatial index with max-entries.
