130e2fc7c495 interlude-1

macros and bunnies and silverfish, oh my!
[view raw] [browse files]
author Steve Losh <steve@stevelosh.com>
date Sat, 14 Jul 2012 17:05:59 -0400
parents c1fba0f3ec5f
children 348749b395c1
branches/tags interlude-1
files src/caves/core.clj src/caves/entities/bunny.clj src/caves/entities/lichen.clj src/caves/entities/silverfish.clj src/caves/ui/input.clj

Changes

--- a/src/caves/core.clj	Sat Jul 14 14:44:22 2012 -0400
+++ b/src/caves/core.clj	Sat Jul 14 17:05:59 2012 -0400
@@ -20,9 +20,9 @@
   (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 []
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/caves/entities/bunny.clj	Sat Jul 14 17:05:59 2012 -0400
@@ -0,0 +1,21 @@
+(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)
--- a/src/caves/entities/lichen.clj	Sat Jul 14 14:44:22 2012 -0400
+++ b/src/caves/entities/lichen.clj	Sat Jul 14 17:05:59 2012 -0400
@@ -1,6 +1,6 @@
 (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]]))
 
 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/caves/entities/silverfish.clj	Sat Jul 14 17:05:59 2012 -0400
@@ -0,0 +1,26 @@
+(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)
--- a/src/caves/ui/input.clj	Sat Jul 14 14:44:22 2012 -0400
+++ b/src/caves/ui/input.clj	Sat Jul 14 17:05:59 2012 -0400
@@ -2,19 +2,28 @@
   (: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)]