811f328099c6

Real attacking, and some refactoring.
[view raw] [browse files]
author Steve Losh <steve@stevelosh.com>
date Tue, 17 Jul 2012 23:22:14 -0400
parents ef03fb8bb7e4
children 88091919d482
branches/tags (none)
files project.clj src/caves/core.clj src/caves/entities/aspects/attacker.clj src/caves/entities/aspects/destructible.clj src/caves/entities/aspects/digger.clj src/caves/entities/aspects/mobile.clj src/caves/entities/bunny.clj src/caves/entities/player.clj src/caves/entities/silverfish.clj

Changes

--- a/project.clj	Sun Jul 15 15:24:14 2012 -0400
+++ b/project.clj	Tue Jul 17 23:22:14 2012 -0400
@@ -8,6 +8,6 @@
                  ]
 
   ; :repositories {"sonatype-snapshots" "https://oss.sonatype.org/content/repositories/snapshots"}
-  ; :main caves.core
+  :main ^{:skip-aot true} caves.core
 
   )
--- a/src/caves/core.clj	Sun Jul 15 15:24:14 2012 -0400
+++ b/src/caves/core.clj	Tue Jul 17 23:22:14 2012 -0400
@@ -49,6 +49,7 @@
                       :else           :auto)]
     (main screen-type true)))
 
+
 (comment
   (main :swing false)
   (main :swing true)
--- a/src/caves/entities/aspects/attacker.clj	Sun Jul 15 15:24:14 2012 -0400
+++ b/src/caves/entities/aspects/attacker.clj	Tue Jul 17 23:22:14 2012 -0400
@@ -4,12 +4,19 @@
         [caves.entities.core :only [defaspect]]))
 
 
+(declare get-damage)
+
 (defaspect Attacker
-  (attack [this world target]
+  (attack [this target world]
     {:pre [(satisfies? Destructible target)]}
-    (let [damage (inc (rand-int (max 0 (- (attack-value this world)
-                                          (defense-value target world)))))]
-      (take-damage target world damage)))
+    (take-damage target (get-damage this target world) world))
   (attack-value [this world]
     (get this :attack 1)))
 
+
+(defn get-damage [attacker target world]
+  (let [attack (attack-value attacker world)
+        defense (defense-value target world)
+        max-damage (max 0 (- attack defense))
+        damage (inc (rand-int max-damage))]
+    damage))
--- a/src/caves/entities/aspects/destructible.clj	Sun Jul 15 15:24:14 2012 -0400
+++ b/src/caves/entities/aspects/destructible.clj	Tue Jul 17 23:22:14 2012 -0400
@@ -3,7 +3,7 @@
 
 
 (defaspect Destructible
-  (take-damage [{:keys [id] :as this} world damage]
+  (take-damage [{:keys [id] :as this} damage world]
     (let [damaged-this (update-in this [:hp] - damage)]
       (if-not (pos? (:hp damaged-this))
         (update-in world [:entities] dissoc id)
--- a/src/caves/entities/aspects/digger.clj	Sun Jul 15 15:24:14 2012 -0400
+++ b/src/caves/entities/aspects/digger.clj	Tue Jul 17 23:22:14 2012 -0400
@@ -4,8 +4,8 @@
 
 
 (defaspect Digger
-  (dig [this world dest]
-    {:pre [(can-dig? this world dest)]}
+  (dig [this dest world]
+    {:pre [(can-dig? this dest world)]}
     (set-tile-floor world dest))
-  (can-dig? [this world dest]
+  (can-dig? [this dest world]
     (check-tile world dest #{:wall})))
--- a/src/caves/entities/aspects/mobile.clj	Sun Jul 15 15:24:14 2012 -0400
+++ b/src/caves/entities/aspects/mobile.clj	Tue Jul 17 23:22:14 2012 -0400
@@ -4,9 +4,9 @@
 
 
 (defaspect Mobile
-  (move [this world dest]
-    {:pre [(can-move? this world dest)]}
+  (move [this dest world]
+    {:pre [(can-move? this dest world)]}
     (assoc-in world [:entities (:id this) :location] dest))
-  (can-move? [this world dest]
+  (can-move? [this dest world]
     (is-empty? world dest)))
 
--- a/src/caves/entities/bunny.clj	Sun Jul 15 15:24:14 2012 -0400
+++ b/src/caves/entities/bunny.clj	Tue Jul 17 23:22:14 2012 -0400
@@ -1,7 +1,7 @@
 (ns caves.entities.bunny
   (:use [caves.entities.core :only [Entity get-id add-aspect]]
         [caves.entities.aspects.destructible :only [Destructible]]
-        [caves.entities.aspects.mobile :only [Mobile move can-move?]]
+        [caves.entities.aspects.mobile :only [Mobile move]]
         [caves.world :only [find-empty-neighbor]]))
 
 
@@ -19,7 +19,7 @@
 (extend-type Bunny Entity
   (tick [this world]
     (if-let [target (find-empty-neighbor world (:location this))]
-      (move this world target)
+      (move this target world)
       world)))
 
 (add-aspect Bunny Mobile)
--- a/src/caves/entities/player.clj	Sun Jul 15 15:24:14 2012 -0400
+++ b/src/caves/entities/player.clj	Tue Jul 17 23:22:14 2012 -0400
@@ -33,7 +33,7 @@
         target (destination-coords (:location player) dir)
         entity-at-target (get-entity-at world target)]
     (cond
-      entity-at-target (attack player world entity-at-target)
-      (can-move? player world target) (move player world target)
-      (can-dig? player world target) (dig player world target)
+      entity-at-target (attack player entity-at-target world)
+      (can-move? player target world) (move player target world)
+      (can-dig? player target world) (dig player target world)
       :else world)))
--- a/src/caves/entities/silverfish.clj	Sun Jul 15 15:24:14 2012 -0400
+++ b/src/caves/entities/silverfish.clj	Tue Jul 17 23:22:14 2012 -0400
@@ -22,10 +22,10 @@
     (let [target (rand-nth (neighbors (:location this)))]
       (if (get-entity-at world target)
         world
-        (move this world target)))))
+        (move this target world)))))
 
 (add-aspect Silverfish Mobile
-  (can-move? [this world dest]
+  (can-move? [this dest world]
     (not (get-entity-at world dest))))
 
 (add-aspect Silverfish Destructible)