Skip to content

Instantly share code, notes, and snippets.

@robashton
Created November 10, 2013 17:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save robashton/7401420 to your computer and use it in GitHub Desktop.
Save robashton/7401420 to your computer and use it in GitHub Desktop.
polymorphism - am I doing it right?
(defmulti tick (fn [e] (if (:tick e) :custom :default)))
(defmethod tick :custom [entity] ((:tick entity) entity))
(defmethod tick :default [entity]
(-> entity
(update-in [:x] #(+ %1 (:velx entity)))
(update-in [:y] #(+ %1 (:vely entity)))))
(defmulti draw (fn [ctx e] (if (:draw e) :custom :default)))
(defmethod draw :custom [entity] ((:draw entity) ctx entity))
(defmethod draw :default [ctx {:keys [x y w h color]}]
(set! (. ctx -fillStyle) color)
(.fillRect ctx x y w h color))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment