Skip to content

Instantly share code, notes, and snippets.

@nvbn
Created June 23, 2015 15:14
Show Gist options
  • Save nvbn/cfdfeacbcaaeacc2f6d3 to your computer and use it in GitHub Desktop.
Save nvbn/cfdfeacbcaaeacc2f6d3 to your computer and use it in GitHub Desktop.
rerenderer samples
(ns ^:figwheel-always rerenderer.examples.core
(:require-macros [cljs.core.async.macros :refer [go-loop go]])
(:require [cljs.core.async :refer [chan <! timeout]]
[rerenderer.examples.bird :as b]
[rerenderer.core :as r :include-macros true]
[rerenderer.browser :refer [browser]]))
(enable-console-print!)
(defn rotating-rectangle
[ctx {:keys [angle]} _]
(r/call! ctx save)
(r/call! ctx (clearRect 0 0 100 100))
(r/set! (.. ctx -fillStyle) "red")
(r/call! ctx (translate 35 35))
(let [rangle (* angle (/ js/Math.PI 180))]
(r/call! ctx (rotate rangle))
(r/call! ctx (fillRect -25 -25 50 50)))
(r/call! ctx restore))
(defn rotate-rectangle
[state]
(go-loop []
(<! (timeout 5))
(swap! state update-in [:angle]
#(-> % inc (mod 360)))
(recur)))
(defn scene-1
[canvas]
(let [state (atom {:angle 0})]
(r/init! (browser canvas) rotating-rectangle state {})
(rotate-rectangle state)))
(defn rotating-and-moving-rectangle
[ctx {:keys [angle x]} _]
(r/call! ctx save)
(r/call! ctx (clearRect 0 0 700 100))
(r/set! (.. ctx -fillStyle) "red")
(r/call! ctx (translate (+ x 25) 35))
(let [rangle (* angle (/ js/Math.PI 180))]
(r/call! ctx (rotate rangle))
(r/call! ctx (fillRect -25 -25 50 50)))
(r/call! ctx restore))
(defn rotate-and-move-rectangle
[state]
(go-loop []
(<! (timeout 5))
(swap! state update-in [:angle]
#(-> % inc (mod 360)))
(swap! state update-in [:x]
#(-> % inc (mod 600)))
(recur)))
(defn scene-2
[canvas]
(let [state (atom {:angle 0
:x 10})]
(r/init! (browser canvas) rotating-and-moving-rectangle state {})
(rotate-and-move-rectangle state)))
(def sprites
{:right {:run [328 320]
:run-1 [354 320]
:run-2 [378 320]
:jump [335 240]
:stand [214 240]}
:left {:run [60 320]
:run-1 [34 320]
:run-2 [8 320]
:jump [54 240]
:stand [174 240]}})
(defn mario
[ctx atlas mario-state x y]
(when mario-state
(let [[sx sy] (get-in sprites mario-state)]
(r/call! ctx (drawImage atlas sx sy 20 40 x y 40 80)))))
(defn all-marios
[ctx {:keys [mario-0 mario-1 mario-2 mario-3 mario-4 mario-5]} {:keys [atlas]}]
(r/call! ctx (clearRect 0 0 300 300))
(mario ctx atlas mario-0 10 10)
(mario ctx atlas mario-1 60 10)
(mario ctx atlas mario-2 110 10)
(mario ctx atlas mario-3 10 80)
(mario ctx atlas mario-4 60 80)
(mario ctx atlas mario-5 110 80))
(defn handle-mario
[state state-key draw-state-key]
(go-loop []
(<! (timeout 100))
(let [[mario-direction mario-state] (get @state state-key)
[draw-direction draw-state] (get @state draw-state-key)
next-draw-state (if (and (= mario-direction draw-direction)
(= mario-state :run))
; Changes drawing state (and sprite) when Mario running:
(condp = draw-state
:run :run-1
:run-1 :run-2
:run)
mario-state)]
(swap! state assoc draw-state-key [mario-direction next-draw-state]))
(recur)))
(defn scene-3
[canvas]
(go (let [state (atom {:mario-0-state [:right :stand]
:mario-1-state [:right :run]
:mario-2-state [:right :jump]
:mario-3-state [:left :stand]
:mario-4-state [:left :run]
:mario-5-state [:left :jump]})
platform (browser canvas)
options {:atlas (<! (r/image platform "mario.png"))}]
(r/init! platform all-marios state options)
(handle-mario state :mario-0-state :mario-0)
(handle-mario state :mario-1-state :mario-1)
(handle-mario state :mario-2-state :mario-2)
(handle-mario state :mario-3-state :mario-3)
(handle-mario state :mario-4-state :mario-4)
(handle-mario state :mario-5-state :mario-5))))
(defn mario-scenario
[state]
(go-loop []
; Stand a half
(swap! state assoc-in [:mario-state 1] :stand)
(<! (timeout 500))
; Jump
(swap! state assoc-in [:mario-state 1] :jump)
(dotimes [_ 20]
(<! (timeout 5))
(swap! state update-in [:mario-y] dec))
(dotimes [_ 20]
(<! (timeout 5))
(swap! state update-in [:mario-y] inc))
; Stand a half
(swap! state assoc-in [:mario-state 1] :stand)
(<! (timeout 500))
; Go right
(swap! state assoc-in [:mario-state 1] :run)
(dotimes [_ 300]
(<! (timeout 5))
(swap! state update-in [:mario-x] inc))
; Stand a second
(swap! state assoc-in [:mario-state 1] :stand)
(<! (timeout 500))
(swap! state assoc-in [:mario-state 0] :left)
(<! (timeout 500))
; Jump
(swap! state assoc-in [:mario-state 1] :jump)
(dotimes [_ 20]
(<! (timeout 5))
(swap! state update-in [:mario-y] dec))
(dotimes [_ 20]
(<! (timeout 5))
(swap! state update-in [:mario-y] inc))
;Stand a half
(swap! state assoc-in [:mario-state 1] :stand)
(<! (timeout 500))
; Go back
(swap! state assoc-in [:mario-state 1] :run)
(dotimes [_ 300]
(<! (timeout 5))
(swap! state update-in [:mario-x] dec))
; Stand a half
(swap! state assoc-in [:mario-state 0] :right)
(<! (timeout 500))
(recur)))
(defn moving-mario
[ctx {:keys [mario-draw-state mario-x mario-y]} {:keys [atlas]}]
(r/call! ctx (clearRect 0 0 500 300))
(r/set! (.. ctx -fillStyle) "green")
(r/call! ctx (fillRect 0 75 400 20))
(mario ctx atlas mario-draw-state mario-x mario-y))
(defn scene-4
[canvas]
(go (let [state (atom {:mario-state [:right :stand]
:mario-x 20
:mario-y 15})
platform (browser canvas)
options {:atlas (<! (r/image platform "mario.png"))}]
(r/init! platform moving-mario state options)
(handle-mario state :mario-state :mario-draw-state)
(mario-scenario state))))
(condp = (.. js/document -location -hash)
"#scene-1" (scene-1 (.getElementById js/document "canvas-1"))
"#scene-2" (scene-2 (.getElementById js/document "canvas-1"))
"#scene-3" (scene-3 (.getElementById js/document "canvas-1"))
"#scene-4" (scene-4 (.getElementById js/document "canvas-1"))
(b/init! (.getElementById js/document "canvas-1")))
;(condp = (.. js/document -location -hash)
; "#android" (a/init!)
; "#simple" (s/init! (.getElementById js/document "canvas-1"))
; (b/init! (.getElementById js/document "canvas-1")))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment