 
 ;;(repl/connect "http://localhost:9000/repl")
 
-;; gameassets
-(def gameassets)
+(def spritescale 2)
+(def basetilesize 12)
+(def tilespacing (* spritescale basetilesize))
 
-;; game state as an atom
-(def gamestate (atom {:stage nil :map nil :characters nil }))
+;; gameassets: images, sounds, etc. use (get gameassets "image") to
+;; get images
+(def gameassets {})
 
-(defn clearscreen [gamescreen]
+;; the current game state as an atom
+(def gamestate (atom {:map [] :characters [] }))
+
+;; contains various rendering objects. These are easelJS containers
+(def renderer (atom {:stage nil :stage-map nil :stage-characters nil}))
+
+;;
+;; sprite sheet cache
+;;
+
+(def spritecache (atom {}))
+
+(defn getspritesheet [spritesheetname]
+  (if (contains? @spritecache spritesheetname)
+    ;; spritesheet is already in the cache, just return it
+    (get @spritecache spritesheetname)
+    (let [spritesheetimage (get-in gameassets ["image" spritesheetname])
+          spritesheet-config (clj->js {:images [spritesheetimage] :frames {:width 12 :height 12}})
+          spritesheet (createjs/SpriteSheet. spritesheet-config)]
+      ;; add the new spritesheet to the cache and return it
+      (swap! spritecache assoc spritesheetname spritesheet)
+      spritesheet)))
+
+
+#_(defn clearscreen [gamescreen]
   (let [width (-> gamescreen .-canvas .-width)
         height (-> gamescreen .-canvas .-width)]
     (aset gamescreen "fillStyle" "#000000")
     (.fillRect gamescreen 0 0 width height)))
 
-(defn simpletitle [gamescreen]
+#_(defn simpletitle [gamescreen]
   (let [canvas (.-canvas gamescreen)
         width (.-width canvas)
         height (.-height canvas)

    (aset gamescreen "fillStyle" "orange")
    (.fillText gamescreen "argh!" textx texty)))

(defn addimage [stage img]
  (let [bitmap (createjs/Bitmap. img)]
    (.addChild stage bitmap)
    (aset bitmap "x" 400)
    (aset bitmap "y" 200)
    (.update stage)))

(defn putonstage [stage shape x y]
  (.addChild stage shape)
  (aset shape "x" x)
  (aset shape "y" y)
  stage)

(defn chooseterrain [x y]
  (if (> x y)
    0
    3))

(defn generatemap [{:keys [width height]}]
  (for [x (range 0 width)
        y (range 0 height)]
    {:x x :y y :terrain (chooseterrain x y)}))

(comment
  (defn initeaselold [canvas]
  (let [shape (createjs/Shape.)
        shape-graphics (aget shape "graphics")
        stage (createjs/Stage. canvas)
        text (createjs/Text. "text on the canvas" "36px Arial" "#0FF")
        checkX (js/Image.)
        ]
    (set! gamestage stage)
    (-> shape-graphics
        (.beginFill "blue")
        (.drawRoundRect 0 0 120 120 10))
    (-> stage
        (putonstage shape 100 100)
        (putonstage text 50 20))
    (aset text "rotation" 20))))

(defn setupboard []
  ;; should have the gamestate setup by now
  (let [scale 2
        tilespacing (* scale 12)
        stage (:stage @gamestate)
        gamemap (generatemap {:width 10 :height 10})
        mapicons (get-in gameassets ["image" "icons"])
        spritesheet-config (clj->js {:images [mapicons] :frames {:width 12 :height 12}})
        spritesheet (createjs/SpriteSheet. spritesheet-config)
        bitmapbase (createjs/BitmapAnimation. spritesheet)]
    ;; generate a bunch of icons based off of the map data
    #_(js/console.log mapicons)
    (doseq [tile gamemap
            :let [{:keys [x y terrain]} tile
                  freshtile (.clone bitmapbase)]]
      (aset freshtile "x" (* x tilespacing))
      (aset freshtile "y" (* y tilespacing))
      (aset freshtile "scaleX" scale)
      (aset freshtile "scaleY" scale)
      (.gotoAndStop freshtile terrain)
      (.addChild stage freshtile)
      )
    (.update stage)))

(defn startgame [loadedassets]
  (set! gameassets loadedassets)
  #_(js/console.log loadedassets)
  (setupboard))

(def manifest [{:id "icons" :src "gfx/placeholders_tigsource.png"}])

(defn createjsstartgame []
  (let [canvas (dom/get-element "target")
        stage (createjs/Stage. canvas)]
    (swap! renderer assoc :stage stage)
    ;; the preloader loads all the assets and then calls startgame with the results
    (preloader/preloadgame stage manifest startgame)))

