Created
November 10, 2013 17:50
-
-
Save robashton/7401420 to your computer and use it in GitHub Desktop.
polymorphism - am I doing it right?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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