   (:require [re-frame.core :as re-frame]
             [wombats-web-client.utils.canvas :as canvas]))
 
+(defonce spritesheet-png "/images/spritesheet.png")
+
 (defn- draw-image
-  [canvas-element img-name x y width height rotation flipped]
+  [canvas-element img-name x y width height]
   (let [spritesheet (re-frame/subscribe [:spritesheet])
         sprite-info (get @spritesheet (keyword img-name))
         frame (:frame sprite-info)]
     (when-not (nil? frame)
       (let [img (js/Image.)]
-        (set! (.-src img) "/images/spritesheet.png")
+        (set! (.-src img) spritesheet-png)
         (.requestAnimationFrame js/window
                                 (fn []
-                                  (if flipped
-                                    (canvas/draw-image-flipped-horizontally canvas-element
-                                                                            img
-                                                                            (:x frame)
-                                                                            (:y frame)
-                                                                            (:w frame)
-                                                                            (:h frame)
-                                                                            x y
-                                                                            width
-                                                                            height)
-
-                                    (canvas/draw-image-rotated canvas-element
-                                                               img
-                                                               (:x frame)
-                                                               (:y frame)
-                                                               (:w frame)
-                                                               (:h frame)
-                                                               x y
-                                                               width
-                                                               height
-                                                               rotation))))))))
+                                  (canvas/draw-image canvas-element
+                                                     img
+                                                     (:x frame)
+                                                     (:y frame)
+                                                     (:w frame)
+                                                     (:h frame)
+                                                     x
+                                                     y
+                                                     width
+                                                     height)))))))
+
+(defn- draw-image-rotated
+  [canvas-element img-name x y width height rotation]
+  (let [spritesheet (re-frame/subscribe [:spritesheet])
+        sprite-info (get @spritesheet (keyword img-name))
+        frame (:frame sprite-info)]
+    (when-not (nil? frame)
+      (let [img (js/Image.)]
+        (set! (.-src img) spritesheet-png)
+        (.requestAnimationFrame js/window
+                                (fn []
+                                  (canvas/draw-image-rotated canvas-element
+                                                             img
+                                                             (:x frame)
+                                                             (:y frame)
+                                                             (:w frame)
+                                                             (:h frame)
+                                                             x y
+                                                             width
+                                                             height
+                                                             rotation)))))))
+
+(defn- draw-image-flipped-horizontally
+  [canvas-element img-name x y width height]
+
+  (let [spritesheet (re-frame/subscribe [:spritesheet])
+        sprite-info (get @spritesheet (keyword img-name))
+        frame (:frame sprite-info)]
+    (when-not (nil? frame)
+      (let [img (js/Image.)]
+        (set! (.-src img) spritesheet-png)
+        (.requestAnimationFrame js/window
+                                (fn []
+                                  (canvas/draw-image-flipped-horizontally canvas-element
+                                                                          img
+                                                                          (:x frame)
+                                                                          (:y frame)
+                                                                          (:w frame)
+                                                                          (:h frame)
+                                                                          x y
+                                                                          width
+                                                                          height)))))))
 
 (defn- draw-background
   "This draws the background of a cell (only called for cells that need it)"
   [canvas-element x y width height]
   (draw-image canvas-element
               "arena_bg.png"
-              x y width height 0 false))
+              x y width height))
 
 (defn- meta-value
   "Gets a score for a meta type for sorting draw level"

                       :medium "2"
                       "1")
                     ".png")
                x y width height 0 false)
    (draw-meta canvas-element contents meta x y width height)))

(defn- draw-food
  [canvas-element contents meta x y width height]
  (draw-image canvas-element
              "food_cherry.png"
              x y width height 0 false)
  (draw-meta canvas-element contents meta x y width height))

(defn- draw-poison
  [canvas-element contents meta x y width height]
  (draw-image canvas-element
              "poison_vial_2.png"
              x y width height 0 false)
  (draw-meta canvas-element contents meta x y width height))

(defn- draw-open

           orientation :orientation} (sort-meta meta)]
    (case type
      :shot
      (draw-image-rotated canvas-element
                          "fire_shot_right.png"
                          x y width height
                          (case orientation
                            :n 270
                            :w 180
                            :s 90
                            0))

      :smoke
      (draw-image canvas-element
                  "smoke.png"
                  x y width height)

      :explosion
      (draw-image canvas-element
                  "explosion.png"
                  x y width height)

      (js/console.log type))))

