Skip to content
Browse files

what have I done?

  • Loading branch information...
1 parent 4e25406 commit cf1c6b442a70e7b207d2fef8d35a41da112a4c5f @sjl committed
View
3 src/caves/entities/aspects/attacker.clj
@@ -1,4 +1,5 @@
-(ns caves.entities.aspects.attacker)
+(ns caves.entities.aspects.attacker
+ (:use [caves.entities.core :only [defaspect]]))
(defprotocol Attacker
View
12 src/caves/entities/aspects/mobile.clj
@@ -1,9 +1,13 @@
-(ns caves.entities.aspects.mobile)
+(ns caves.entities.aspects.mobile
+ (:use [caves.entities.core :only [defaspect]]
+ [caves.world :only [is-empty?]]))
-(defprotocol Mobile
+
+(defaspect Mobile
(move [this world dest]
- "Move this entity to a new location.")
+ {:pre [(can-move? this world dest)]}
+ (assoc-in world [:entities (:id this) :location] dest))
(can-move? [this world dest]
- "Return whether the entity can move to the new location."))
+ (is-empty? world dest)))
View
46 src/caves/entities/core.clj
@@ -13,3 +13,49 @@
(let [id @ids]
(alter ids inc)
id)))
+
+
+(defn make-fnmap [fns]
+ (into {} (for [[label fntail] (map (juxt first rest) fns)]
+ [(keyword label)
+ `(fn ~@fntail)])))
+
+(defn make-fnheads [fns]
+ (map #(take 2 %) fns))
+
+
+(defmacro defaspect [label & fns]
+ (let [fnmap (make-fnmap fns)
+ fnheads (make-fnheads fns)]
+ `(do
+ (defprotocol ~label
+ ~@fnheads)
+ (def ~label
+ (with-meta ~label {:defaults ~fnmap})))))
+
+(defmacro add-aspect [entity aspect & fns]
+ (let [fnmap (make-fnmap fns)]
+ `(extend ~entity ~aspect (merge (:defaults (meta ~aspect))
+ ~fnmap))))
+
+
+(comment
+
+ (macroexpand-1
+ '(defaspect Mobile
+ (move [world target]
+ (when (can-move? world target)
+ (println world)))
+ (can-move? [world target]
+ true)))
+
+ (macroexpand-1
+ '(add-aspect Player Mobile))
+
+ (macroexpand-1
+ '(add-aspect EE Mobile
+ (can-move? [world target]
+ true)))
+
+
+ )
View
9 src/caves/entities/player.clj
@@ -1,5 +1,5 @@
(ns caves.entities.player
- (:use [caves.entities.core :only [Entity]]
+ (:use [caves.entities.core :only [Entity add-aspect]]
[caves.entities.aspects.mobile :only [Mobile move can-move?]]
[caves.entities.aspects.digger :only [Digger dig can-dig?]]
[caves.entities.aspects.attacker :only [Attacker attack]]
@@ -21,12 +21,7 @@
(tick [this world]
world))
-(extend-type Player Mobile
- (move [this world dest]
- {:pre [(can-move? this world dest)]}
- (assoc-in world [:entities :player :location] dest))
- (can-move? [this world dest]
- (is-empty? world dest)))
+(add-aspect Player Mobile)
(extend-type Player Digger
(dig [this world dest]

0 comments on commit cf1c6b4

Please sign in to comment.
Something went wrong with that request. Please try again.