Skip to content

Instantly share code, notes, and snippets.

@lilrooness
Created December 27, 2015 14:03
Show Gist options
  • Select an option

  • Save lilrooness/57fb2c909966196fe4d5 to your computer and use it in GitHub Desktop.

Select an option

Save lilrooness/57fb2c909966196fe4d5 to your computer and use it in GitHub Desktop.
Clojure spritesheet rendering
(ns resource.core
(:require [clojure.browser.repl :as repl]
[resource.sprites :as sprites]
))
;; (defonce conn
;; (repl/connect "http://localhost:9000/repl"))
(enable-console-print!)
(println "Hello world!")
(def gamestate (atom {:map [[1,1,1] [1,1,1] [1,1,1]]}))
(sprites/loadAnimations)
(sprites/render gamestate)
(ns resource.sprites)
(def spriteSize 225)
(def tileSize 50)
(def sprites (atom {}))
(def animations (atom {}))
(def spritesheet (.getElementById js/document "source"))
;(def ctx (.getContext (.getElementById js/document "canvas") "2D"))
(defn addSpriteGen [spriteName x y]
(fn [state] (assoc state spriteName {:x x :y y})))
(defn addAnimationGen [animationName spriteList]
(fn [state] (assoc state animationName spriteList)))
(defn loadAnimations []
;; Define sprites position on spritesheet
(swap! sprites (addSpriteGen "tree" 0 0))
(swap! sprites (addSpriteGen "logPile" 1 0))
(swap! sprites (addSpriteGen "grass1" 0 1))
(swap! sprites (addSpriteGen "grass2" 1 1))
(swap! sprites (addSpriteGen "grass3" 2 1))
;; Define sprite animations
(swap! animations (addAnimationGen "grass" ["grass1" "grass2" "grass3"]))
(swap! animations (addAnimationGen "tree" ["tree"]))
(swap! animations (addAnimationGen "logPile" ["logPile"]))
)
(defn render_cell [cell x y] (let [sprite (@sprites "logPile")
context (.getContext (.getElementById js/document "canvas") "2d")]
(.drawImage context spritesheet
(* spriteSize (sprite :x)) (* spriteSize (sprite :y))
225 225
(* tileSize x) (* tileSize y)
tileSize tileSize)
(println (.-src spritesheet) x y sprite tileSize)
))
(defn render [gamestate] (let [m (:map @gamestate)]
(doall (map-indexed (fn [y row]
(doall (map-indexed (fn [x cell] (render_cell cell x y)) row)))
m))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment