ef03fb8bb7e4

Add real hp, attack and defense.
[view raw] [browse files]
author Steve Losh <steve@stevelosh.com>
date Sun, 15 Jul 2012 15:24:14 -0400
parents d18013e50134
children 811f328099c6
branches/tags (none)
files src/caves/core.clj src/caves/entities/aspects/attacker.clj src/caves/entities/aspects/destructible.clj src/caves/entities/bunny.clj src/caves/entities/lichen.clj src/caves/entities/player.clj src/caves/entities/silverfish.clj src/caves/ui/drawing.clj src/caves/ui/input.clj

Changes

--- a/src/caves/core.clj	Sun Jul 15 00:50:23 2012 -0400
+++ b/src/caves/core.clj	Sun Jul 15 15:24:14 2012 -0400
@@ -48,3 +48,8 @@
                       (args ":text")  :text
                       :else           :auto)]
     (main screen-type true)))
+
+(comment
+  (main :swing false)
+  (main :swing true)
+  )
--- a/src/caves/entities/aspects/attacker.clj	Sun Jul 15 00:50:23 2012 -0400
+++ b/src/caves/entities/aspects/attacker.clj	Sun Jul 15 15:24:14 2012 -0400
@@ -1,11 +1,15 @@
 (ns caves.entities.aspects.attacker
-  (:use [caves.entities.aspects.destructible :only [Destructible take-damage]]
+  (:use [caves.entities.aspects.destructible :only [Destructible take-damage
+                                                    defense-value]]
         [caves.entities.core :only [defaspect]]))
 
 
 (defaspect Attacker
   (attack [this world target]
     {:pre [(satisfies? Destructible target)]}
-    (let [damage 1]
-      (take-damage target world damage))))
+    (let [damage (inc (rand-int (max 0 (- (attack-value this world)
+                                          (defense-value target world)))))]
+      (take-damage target world damage)))
+  (attack-value [this world]
+    (get this :attack 1)))
 
--- a/src/caves/entities/aspects/destructible.clj	Sun Jul 15 00:50:23 2012 -0400
+++ b/src/caves/entities/aspects/destructible.clj	Sun Jul 15 15:24:14 2012 -0400
@@ -7,4 +7,6 @@
     (let [damaged-this (update-in this [:hp] - damage)]
       (if-not (pos? (:hp damaged-this))
         (update-in world [:entities] dissoc id)
-        (assoc-in world [:entities id] damaged-this)))))
+        (assoc-in world [:entities id] damaged-this))))
+  (defense-value [this world]
+    (get this :defense 0)))
--- a/src/caves/entities/bunny.clj	Sun Jul 15 00:50:23 2012 -0400
+++ b/src/caves/entities/bunny.clj	Sun Jul 15 15:24:14 2012 -0400
@@ -5,10 +5,15 @@
         [caves.world :only [find-empty-neighbor]]))
 
 
-(defrecord Bunny [id glyph color location hp])
+(defrecord Bunny [id glyph color location hp max-hp])
 
 (defn make-bunny [location]
-  (->Bunny (get-id) "v" :yellow location 1))
+  (map->Bunny {:id (get-id)
+               :glyph "v"
+               :color :yellow
+               :location location
+               :hp 4
+               :max-hp 4}))
 
 
 (extend-type Bunny Entity
--- a/src/caves/entities/lichen.clj	Sun Jul 15 00:50:23 2012 -0400
+++ b/src/caves/entities/lichen.clj	Sun Jul 15 15:24:14 2012 -0400
@@ -4,10 +4,15 @@
         [caves.world :only [find-empty-neighbor]]))
 
 
-(defrecord Lichen [id glyph color location hp])
+(defrecord Lichen [id glyph color location hp max-hp])
 
 (defn make-lichen [location]
-  (->Lichen (get-id) "F" :green location 1))
+  (map->Lichen {:id (get-id)
+                :glyph "F"
+                :color :green
+                :location location
+                :hp 6
+                :max-hp 6}))
 
 (defn should-grow []
   (< (rand) (/ 1 500)))
--- a/src/caves/entities/player.clj	Sun Jul 15 00:50:23 2012 -0400
+++ b/src/caves/entities/player.clj	Sun Jul 15 15:24:14 2012 -0400
@@ -3,11 +3,12 @@
         [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]]
+        [caves.entities.aspects.destructible :only [Destructible]]
         [caves.coords :only [destination-coords]]
         [caves.world :only [get-entity-at]]))
 
 
-(defrecord Player [id glyph color location])
+(defrecord Player [id glyph color location hp max-hp attack])
 
 (extend-type Player Entity
   (tick [this world]
@@ -16,9 +17,16 @@
 (add-aspect Player Mobile)
 (add-aspect Player Digger)
 (add-aspect Player Attacker)
+(add-aspect Player Destructible)
 
 (defn make-player [location]
-  (->Player :player "@" :white location))
+  (map->Player {:id :player
+                :glyph "@"
+                :color :white
+                :location location
+                :hp 40
+                :max-hp 40
+                :attack 10}))
 
 (defn move-player [world dir]
   (let [player (get-in world [:entities :player])
--- a/src/caves/entities/silverfish.clj	Sun Jul 15 00:50:23 2012 -0400
+++ b/src/caves/entities/silverfish.clj	Sun Jul 15 15:24:14 2012 -0400
@@ -6,10 +6,15 @@
         [caves.coords :only [neighbors]]))
 
 
-(defrecord Silverfish [id glyph color location hp])
+(defrecord Silverfish [id glyph color location hp max-hp])
 
 (defn make-silverfish [location]
-  (->Silverfish (get-id) "~" :white location 1))
+  (map->Silverfish {:id (get-id)
+                    :glyph "~"
+                    :color :white
+                    :location location
+                    :hp 15
+                    :max-hp 15}))
 
 
 (extend-type Silverfish Entity
--- a/src/caves/ui/drawing.clj	Sun Jul 15 00:50:23 2012 -0400
+++ b/src/caves/ui/drawing.clj	Sun Jul 15 15:24:14 2012 -0400
@@ -79,11 +79,13 @@
   (map - coords origin))
 
 
-(defn draw-hud [screen game [ox oy]]
+(defn draw-hud [screen game]
   (let [hud-row (dec (second (s/get-size screen)))
-        [x y] (get-in game [:world :entities :player :location])
-        info (str "player loc: [" x "-" y "]")
-        info (str info " viewport origin: [" ox "-" oy "]")]
+        player (get-in game [:world :entities :player])
+        {:keys [location hp max-hp]} player
+        [x y] location
+        info (str "hp [" hp "/" max-hp "]")
+        info (str info " loc: [" x "-" y "]")]
     (s/put-string screen 0 hud-row info)))
 
 
@@ -116,7 +118,7 @@
     (draw-world screen vrows vcols origin tiles)
     (doseq [entity (vals entities)]
       (draw-entity screen origin entity))
-    (draw-hud screen game origin)
+    (draw-hud screen game)
     (highlight-player screen origin player)))
 
 
--- a/src/caves/ui/input.clj	Sun Jul 15 00:50:23 2012 -0400
+++ b/src/caves/ui/input.clj	Sun Jul 15 15:24:14 2012 -0400
@@ -23,7 +23,7 @@
     (-> world
       (add-creatures make-lichen 30)
       (add-creatures make-bunny 20)
-      (add-creatures make-silverfish 15))))
+      (add-creatures make-silverfish 4))))
 
 (defn reset-game [game]
   (let [fresh-world (random-world)]