Skip to content

Instantly share code, notes, and snippets.

@ckirkendall
Last active August 29, 2015 13:56
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 ckirkendall/9254774 to your computer and use it in GitHub Desktop.
Save ckirkendall/9254774 to your computer and use it in GitHub Desktop.
Musing about a game engine
(defprotocol KeyListener
(key [this key-code]))
(defprotocol CollisionListener
(collision [this other]))
(defprotocol BoundsListener
(bounds [this other]))
(defprotocol GravityListener
(gravity [this time-dif]))
(defprotocol PhysicsListener
(physics [this time-dif]))
(defprotocol Pen
(draw [this ctx]))
(extend-protocol KeyListener
(key [this key-code]))
(extend-protocol CollisionListener
(collision [this other]))
(extend-protocol BoundsListener
(bounds [this other]))
(extend-protocol GravityListener
(gravity [this time-dif]))
(extend-protocol PhysicsListener
object
(physics [this ctx]))
(extend-protocol Pen
object
(draw [this ctx]))
(extend-protocol PhysicsListener
object
(physics [this ctx frame]))
(defn translate-coords [obj frame]
(assoc obj :x (- (:x obj) (:x frame))
:y (- (:y obj) (:y frame))))
(defrecord Block [width height x y radii]
Pen
(draw [this ctx frame]
(let [{:keys [w h color x y radii]} (translate-coords this frame)
r (+ x w)
b (+ y h)
[ul ur lr ll] (or radii [0 0 0 0])]
(doto ctx
(.beginPath)
(.fillStyle (:color trans))
(.moveTo (+ x ul) y)
(.lineTo (- r ur) y)
(.quadraticCurveTo r y r (+ y ur))
(.lineTo r (- b lr))
(.quadraticCurveTo r b (- r lr) b)
(.lineTo (+ x ll) b)
(.quadraticCurveTo x b x (- b ll))
(.lineTo x (+ y ul))
(.quadraticCurveTo x y (+ x ul) y)))))
(defrecord Enemy [img width height coords state])
(defrecord Hero [img width height x y vx vy health score]
Pen
(draw [this ctx frame]
(let [{:keys [w h img x y]} (translate-coords this frame))]
(.drawImage ctx img 0 0 w h)))
(defrecord Reward [img width height coords state])
(defn load-image [img]
(let [nimg (js/Imgage.)]
(set! (.-src nimg) img)
nimg))
(defn load-images [app-state]
(assoc app-state :bodies (reduce #(if (:img %2)
(conj %1 (assoc %2 :img (load-image img)))
%1)
[]
(:bodies app-state))))
(defn draw-world [world ctx]
(doseq [body (:bodies world)]
(draw body ctx (:frame world))))
(def world {:board {:width 1000 :height 400 :img nill :color "#7F7FFF"}
:frame {:width 400 :height 400 :x 0 :y 0}
:bodies [#bodies.Hero {:img "jim.png" :width 30 :height 40 :x 100 :y 200 :vx 0 :vy 0 :health 100 :score 0}
#bodies.Block {:color "#007F00" :width 200 :height 100 :x 0 :y 300 :radii [10 10 0 0]}
#bodies.Block {:color "#007F00" :width 200 :height 100 :x 200 :y 320 :radii [0 0 0 0]}
#bodies.Block {:color "#007F00" :width 200 :height 100 :x 400 :y 300 :radii [10 10 0 0]}
#bodies.Block {:color "#007F00" :width 200 :height 100 :x 600 :y 320 :radii [0 0 0 0]}
#bodies.Block {:color "#007F00" :width 200 :height 100 :x 800 :y 300 :radii [10 10 0 0]}
#bodies.Block {:color "#007F00" :width 200 :height 100 :x 1000 :y 320 :radii [0 10 0 0]}]})
(defn run-game []
(let [state (load-images world)]
(draw state)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment