Permalink
Browse files

Refactor the world code into separate files.

This pulls out the world generation code into its own file.  It was
getting a bit crowded in there.
  • Loading branch information...
1 parent d0c257e commit e07c05340757521ed967042e5e61c3411fd1ac65 @sjl committed Aug 2, 2012
View
@@ -59,3 +59,5 @@
(main :swing false)
(main :swing true)
)
+
+
@@ -16,7 +16,7 @@
(send-message this "You strike the %s for %d damage!"
[(:name target) damage])
(send-message target "The %s strikes you for %d damage!"
- [(:name target) damage]))))
+ [(:name this) damage]))))
(attack-value [this world]
(get this :attack 1)))
@@ -1,6 +1,6 @@
(ns caves.entities.aspects.digger
(:use [caves.entities.core :only [defaspect]]
- [caves.world :only [check-tile set-tile-floor]]))
+ [caves.world.core :only [check-tile set-tile-floor]]))
(defaspect Digger
@@ -1,6 +1,6 @@
(ns caves.entities.aspects.mobile
(:use [caves.entities.core :only [defaspect]]
- [caves.world :only [is-empty?]]))
+ [caves.world.core :only [is-empty?]]))
(defaspect Mobile
@@ -1,6 +1,6 @@
(ns caves.entities.aspects.receiver
(:use [caves.entities.core :only [defaspect]]
- [caves.world :only [get-entities-around]]))
+ [caves.world.core :only [get-entities-around]]))
(defaspect Receiver
@@ -15,7 +15,8 @@
(defn send-message-nearby [coord message world]
(let [entities (get-entities-around world coord 7)
- sm #(send-message %2 message [] %1)]
+ sm (fn [world entity]
+ (send-message entity message [] world))]
(reduce sm world entities)))
@@ -2,7 +2,7 @@
(:use [caves.entities.core :only [Entity get-id add-aspect]]
[caves.entities.aspects.destructible :only [Destructible]]
[caves.entities.aspects.mobile :only [Mobile move]]
- [caves.world :only [find-empty-neighbor]]))
+ [caves.world.core :only [find-empty-neighbor]]))
(defrecord Bunny [id glyph color location hp max-hp name])
@@ -82,7 +82,7 @@
This is similar to extend-type, with two differences:
- * It must be used on a protocol defined with defaspect
+ * It must be used on a protocol defined with defaspect.
* It will use the aspect's default function implementation for any functions
not given.
@@ -2,7 +2,7 @@
(: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]]))
+ [caves.world.core :only [find-empty-neighbor]]))
(defrecord Lichen [id glyph color location hp max-hp name])
@@ -6,7 +6,7 @@
[caves.entities.aspects.attacker :only [Attacker attack]]
[caves.entities.aspects.destructible :only [Destructible]]
[caves.coords :only [destination-coords]]
- [caves.world :only [get-entity-at]]))
+ [caves.world.core :only [get-entity-at]]))
(defrecord Player [id glyph color location hp max-hp attack name])
@@ -2,7 +2,7 @@
(: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 get-tile-kind]]
+ [caves.world.core :only [get-entity-at get-tile-kind]]
[caves.coords :only [neighbors]]))
@@ -1,5 +1,6 @@
(ns caves.ui.input
- (:use [caves.world :only [random-world smooth-world find-empty-tile]]
+ (:use [caves.world.generation :only [random-world smooth-world]]
+ [caves.world.core :only [find-empty-tile]]
[caves.ui.core :only [->UI]]
[caves.entities.player :only [move-player make-player]]
[caves.entities.lichen :only [make-lichen]]
@@ -1,4 +1,4 @@
-(ns caves.world
+(ns caves.world.core
(:use [caves.coords :only [neighbors radial-distance]]))
@@ -12,6 +12,8 @@
(def tiles
{:floor (->Tile :floor "." :white)
:wall (->Tile :wall "#" :white)
+ :up (->Tile :up "<" :white)
+ :down (->Tile :down ">" :white)
:bound (->Tile :bound "X" :black)})
@@ -24,54 +26,6 @@
[(rand-int cols) (rand-int rows)]))
-; World generation ------------------------------------------------------------
-(defn random-tiles []
- (let [[cols rows] world-size]
- (letfn [(random-tile []
- (tiles (rand-nth [:floor :wall])))
- (random-row []
- (vec (repeatedly cols random-tile)))]
- (vec (repeatedly rows random-row)))))
-
-
-(defn get-smoothed-tile [block]
- (let [tile-counts (frequencies (map :kind block))
- floor-threshold 5
- floor-count (get tile-counts :floor 0)
- result (if (>= floor-count floor-threshold)
- :floor
- :wall)]
- (tiles result)))
-
-(defn block-coords [x y]
- (for [dx [-1 0 1]
- dy [-1 0 1]]
- [(+ x dx) (+ y dy)]))
-
-(defn get-block [tiles x y]
- (map (partial get-tile-from-tiles tiles)
- (block-coords x y)))
-
-(defn get-smoothed-row [tiles y]
- (mapv (fn [x]
- (get-smoothed-tile (get-block tiles x y)))
- (range (count (first tiles)))))
-
-(defn get-smoothed-tiles [tiles]
- (mapv (fn [y]
- (get-smoothed-row tiles y))
- (range (count tiles))))
-
-(defn smooth-world [{:keys [tiles] :as world}]
- (assoc world :tiles (get-smoothed-tiles tiles)))
-
-
-(defn random-world []
- (let [world (->World (random-tiles) {})
- world (nth (iterate smooth-world world) 3)]
- world))
-
-
; Querying a world ------------------------------------------------------------
(defn get-tile [world coord]
(get-tile-from-tiles (:tiles world) coord))
@@ -0,0 +1,51 @@
+(ns caves.world.generation
+ (:use [caves.world.core :only [tiles get-tile-from-tiles random-coordinate
+ world-size ->World]]))
+
+
+
+(defn random-tiles []
+ (let [[cols rows] world-size]
+ (letfn [(random-tile []
+ (tiles (rand-nth [:floor :wall])))
+ (random-row []
+ (vec (repeatedly cols random-tile)))]
+ (vec (repeatedly rows random-row)))))
+
+
+(defn get-smoothed-tile [block]
+ (let [tile-counts (frequencies (map :kind block))
+ floor-threshold 5
+ floor-count (get tile-counts :floor 0)
+ result (if (>= floor-count floor-threshold)
+ :floor
+ :wall)]
+ (tiles result)))
+
+(defn block-coords [x y]
+ (for [dx [-1 0 1]
+ dy [-1 0 1]]
+ [(+ x dx) (+ y dy)]))
+
+(defn get-block [tiles x y]
+ (map (partial get-tile-from-tiles tiles)
+ (block-coords x y)))
+
+(defn get-smoothed-row [tiles y]
+ (mapv (fn [x]
+ (get-smoothed-tile (get-block tiles x y)))
+ (range (count (first tiles)))))
+
+(defn get-smoothed-tiles [tiles]
+ (mapv (fn [y]
+ (get-smoothed-row tiles y))
+ (range (count tiles))))
+
+(defn smooth-world [{:keys [tiles] :as world}]
+ (assoc world :tiles (get-smoothed-tiles tiles)))
+
+
+(defn random-world []
+ (let [world (->World (random-tiles) {})
+ world (nth (iterate smooth-world world) 3)]
+ world))

0 comments on commit e07c053

Please sign in to comment.