Commits

Steve Losh committed 130e2fc

macros and bunnies and silverfish, oh my!

  • Participants
  • Parent commits c1fba0f
  • Tags interlude-1

Comments (0)

Files changed (5)

File src/caves/core.clj

   (loop [{:keys [input uis] :as game} game]
     (when (seq uis)
       (if (nil? input)
-        (do
+        (let [game (update-in game [:world] tick-all)]
           (draw-game game screen)
-          (recur (get-input (update-in game [:world] tick-all) screen)))
+          (recur (get-input game screen)))
         (recur (process-input (dissoc game :input) input))))))
 
 (defn new-game []

File src/caves/entities/bunny.clj

+(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.world :only [find-empty-neighbor]]))
+
+
+(defrecord Bunny [id glyph color location hp])
+
+(defn make-bunny [location]
+  (->Bunny (get-id) "v" :yellow location 1))
+
+
+(extend-type Bunny Entity
+  (tick [this world]
+    (if-let [target (find-empty-neighbor world (:location this))]
+      (move this world target)
+      world)))
+
+(add-aspect Bunny Mobile)
+(add-aspect Bunny Destructible)

File src/caves/entities/lichen.clj

 (ns caves.entities.lichen
   (:use [caves.entities.core :only [Entity get-id add-aspect]]
-        [caves.entities.aspects.destructible :only [Destructible take-damage]]
+        [caves.entities.aspects.destructible :only [Destructible]]
         [caves.world :only [find-empty-neighbor]]))
 
 

File src/caves/entities/silverfish.clj

+(ns caves.entities.silverfish
+  (: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.world :only [get-entity-at]]
+        [caves.coords :only [neighbors]]))
+
+
+(defrecord Silverfish [id glyph color location hp])
+
+(defn make-silverfish [location]
+  (->Silverfish (get-id) "~" :white location 1))
+
+
+(extend-type Silverfish Entity
+  (tick [this world]
+    (let [target (rand-nth (neighbors (:location this)))]
+      (if (get-entity-at world target)
+        world
+        (move this world target)))))
+
+(add-aspect Silverfish Mobile
+  (can-move? [this world dest]
+    (not (get-entity-at world dest))))
+
+(add-aspect Silverfish Destructible)

File src/caves/ui/input.clj

   (:use [caves.world :only [random-world smooth-world find-empty-tile]]
         [caves.ui.core :only [->UI]]
         [caves.entities.player :only [move-player make-player]]
-        [caves.entities.lichen :only [make-lichen]])
+        [caves.entities.lichen :only [make-lichen]]
+        [caves.entities.bunny :only [make-bunny]]
+        [caves.entities.silverfish :only [make-silverfish]])
   (:require [lanterna.screen :as s]))
 
 
-(defn add-lichen [world]
-  (let [{:as lichen :keys [id]} (make-lichen (find-empty-tile world))]
-    (assoc-in world [:entities id] lichen)))
+(defn add-creature [world make-creature]
+  (let [creature (make-creature (find-empty-tile world))]
+    (assoc-in world [:entities (:id creature)] creature)))
+
+(defn add-creatures [world make-creature n]
+  (nth (iterate #(add-creature % make-creature)
+                world)
+       n))
 
 (defn populate-world [world]
   (let [world (assoc-in world [:entities :player]
-                        (make-player (find-empty-tile world)))
-        world (nth (iterate add-lichen world) 30)]
-    world))
+                        (make-player (find-empty-tile world)))]
+    (-> world
+      (add-creatures make-lichen 30)
+      (add-creatures make-bunny 20)
+      (add-creatures make-silverfish 15))))
 
 (defn reset-game [game]
   (let [fresh-world (random-world)]