Commits

Steve Losh  committed d8b58bf

Radial messages, and styling.

  • Participants
  • Parent commits 8809191

Comments (0)

Files changed (6)

File src/caves/coords.clj

-(ns caves.coords)
+(ns caves.coords
+  (:use [caves.utils :only [abs]]))
+
+
+(defn radial-distance
+  "Return the radial distance between two points.
+  
+  There might be a better name for this, but in a nutshell:
+
+      3333333
+      3222223
+      3211123
+      321.123
+      3211123
+      3222223
+      3333333
+  
+  "
+  [[x1 y1] [x2 y2]]
+  (max (abs (- x1 x2))
+       (abs (- y1 y2))))
 
 
 (def directions

File src/caves/entities/aspects/receiver.clj

 (ns caves.entities.aspects.receiver
-  (:use [caves.entities.core :only [defaspect]]))
+  (:use [caves.entities.core :only [defaspect]]
+        [caves.world :only [get-entities-around]]))
 
 
 (defaspect Receiver
     (receive-message entity (apply format message args) world)
     world))
 
+(defn send-message-nearby [coord message world]
+  (let [entities (get-entities-around world coord 7)
+        sm #(send-message %2 message [] %1)]
+    (reduce sm world entities)))
+
+

File src/caves/entities/lichen.clj

 (ns caves.entities.lichen
   (:use [caves.entities.core :only [Entity get-id add-aspect]]
+        [caves.entities.aspects.receiver :only [send-message-nearby]]
         [caves.entities.aspects.destructible :only [Destructible]]
         [caves.world :only [find-empty-neighbor]]))
 
 (defn should-grow []
   (< (rand) (/ 1 500)))
 
-(defn grow [lichen world]
-  (if-let [target (find-empty-neighbor world (:location lichen))]
-    (let [new-lichen (make-lichen target)]
-      (assoc-in world [:entities (:id new-lichen)] new-lichen))
+(defn grow [{:keys [location]} world]
+  (if-let [target (find-empty-neighbor world location)]
+    (let [new-lichen (make-lichen target)
+          world (assoc-in world [:entities (:id new-lichen)] new-lichen)
+          world (send-message-nearby location "The lichen grows." world)]
+      world)
     world))
 
 

File src/caves/ui/drawing.clj

 (ns caves.ui.drawing
-  (:use [caves.utils :only (map2d shear)])
+  (:use [caves.utils :only (map2d shear enumerate)])
   (:require [lanterna.screen :as s]))
 
 
 
 
 (defn draw-messages [screen messages]
-  (when (seq messages)
-    (s/put-sheet screen 0 0 messages)))
+  (doseq [[i msg] (enumerate messages)]
+    (s/put-string screen 0 i msg {:fg :black :bg :white})))
 
 
 (defmethod draw-ui :play [ui game screen]

File src/caves/utils.clj

 (ns caves.utils)
 
 
+; lolclojure
+(defn abs [i]
+  (if (neg? i)
+    (- i)
+    i))
+
+
 (defn map2d
   "Map a function across a two-dimensional sequence."
   [f s]
   [s x y w h]
   (map #(slice % x w)
        (slice s y h)))
+
+
+(defn enumerate [s]
+  (map vector (iterate inc 0) s))

File src/caves/world.clj

 (ns caves.world
-  (:use [caves.coords :only [neighbors]]))
+  (:use [caves.coords :only [neighbors radial-distance]]))
 
 
 ; Constants -------------------------------------------------------------------
   (set-tile world coord (:floor tiles)))
 
 
+(defn get-entities-at [world coord]
+  (filter #(= coord (:location %))
+          (vals (:entities world))))
+
 (defn get-entity-at [world coord]
-  (first (filter #(= coord (:location %))
-                 (vals (:entities world)))))
+  (first (get-entities-at world coord)))
+
+(defn get-entities-around
+  ([world coord] (get-entities-around world coord 1))
+  ([world coord radius]
+     (filter #(<= (radial-distance coord (:location %))
+                  radius)
+             (vals (:entities world)))))
 
 (defn is-empty? [world coord]
   (and (#{:floor} (get-tile-kind world coord))