Steve Losh avatar Steve Losh committed 2f89269

moar.

Comments (0)

Files changed (4)

src/ruin/buildings.clj

+(ns ruin.buildings)
+
+
+(defonce bid (atom 1))
+
+(defn get-building-id []
+  (swap! bid inc))
+
+
+(defn make-house []
+  (agent {:id (get-building-id)
+          :capacity 4
+          :type :house}))
+
+(defn make-silo
+  ([] (make-silo 0))
+  ([contents]
+   (agent {:id (get-building-id)
+           :contents contents
+           :capacity 50
+           :type :silo})))
+
+
+(defn start-building [b]
+  nil)

src/ruin/core.clj

 (ns ruin.core
   (:use [ruin.state :only [game]]
+        [ruin.entities :only [make-person]]
+        [ruin.buildings :only [make-house make-silo]]
         [ruin.ui :only [->UI]]
         [ruin.drawing :only [draw-ui]]
         [ruin.input :only [process-input]])
   (:require [lanterna.screen :as s]))
 
 
+(defn calc-score []
+  (letfn [(count-things [source kind]
+            (count (filter #(= kind (:type @%))
+                           (vals @(source @game)))))]
+    (dosync
+      (+ (* 100 (count-things :entities :person))
+         (*  10 (count-things :buildings :silo))
+         (*  15 (count-things :buildings :house))))))
+
+
+(defn -score-loop []
+  (let [score (calc-score)]
+    (dosync
+      (let [[peak _] @(:score @game)]
+        (ref-set (:score @game)
+                 [(dec (max peak score)) (dec score)]))))
+  (Thread/sleep 500))
+
+(defn score-loop
+  "Continually update the game's score, until the game stops running."
+  []
+  (-score-loop)
+  
+  (when (:state @game)
+    (recur)))
+
+
+(defn gen-things [n f]
+  (for [thing (repeatedly n f)]
+    [(:id @thing) thing]))
+
+
+(defn create-initial-population []
+  (into {}
+        (gen-things 10 make-person)))
+
+(defn create-initial-buildings []
+  (into {}
+        (concat (gen-things 4 make-house)
+                (gen-things 2 #(make-silo 25)))))
+
+
 (defn create-fresh-game
   "Refresh the game var with a new game.
 
                                     :font "Menlo"
                                     :font-size 16})]
       (ref-set game {:screen scr
-                     :entities {}
+                     :entities (ref (create-initial-population))
+                     :buildings (ref (create-initial-buildings))
+                     :resources (ref 20)
+                     :score (ref [0 0])
                      :state :running
                      :uis [(->UI :start)]})
       (s/start scr))))
   "Continually draw all the game's UIs, until the game stops running."
   []
   (draw-uis)
-  (Thread/sleep 500)
+  (Thread/sleep 300)
   (when (:state @game)
     (recur)))
 
   []
   (let [screen (:screen @game)]
     (future (draw-loop))
-    (future (input-loop))))
+    (future (input-loop))
+    (future (score-loop))
+    ))
 
 
 (defn main []

src/ruin/drawing.clj

                  (repeat map-height (repeat map-width \.)))))
 
 (defn draw-status []
-  (let [screen (:screen @game)
+  (let [game @game
+        screen (:screen game)
         [cols rows] (s/get-size screen)
-        x (- cols SIDEBAR-WIDTH)]
+        x (- cols SIDEBAR-WIDTH)
+        total-building-aspect (fn [aspect kind]
+                                (reduce + (map #(aspect @%)
+                                               (filter #(= kind (:type @%))
+                                                       (vals @(:buildings game))))))
+        population (count (filter #(= :person (:type @%))
+                                  (vals @(:entities game))))
+        housing (total-building-aspect :capacity :house)
+        food (total-building-aspect :contents :silo)
+        food-storage (total-building-aspect :capacity :silo)
+        resources @(:resources game)
+        [score-peak score-current] @(:score game)]
     (s/put-sheet screen x 0
                  ["SCORE"
-                  "peak   current   total"
-                  "4000 - 23      = 3977"
+                  (str score-peak " - " score-current " = " (- score-peak score-current))
                   ""
                   "STATUS"
                   "Time:         Year 45, Month 12"
-                  "Population:   50 / 100"
-                  "Food:         245 / 400"
+                  (str "Population:   " population " / " housing)
+                  (str "Food:         " food " / " food-storage)
                   "Satisfaction: 80%"
-                  "Resources:    1033"])))
+                  (str "Resources:    " resources)])))
 
 (defn draw-main-menu []
   (let [screen (:screen @game)

src/ruin/entities.clj

+(ns ruin.entities)
+
+
+(defonce eid (atom 1))
+
+(defn get-entity-id []
+  (swap! eid inc))
+
+(defn make-person []
+  (agent {:nutrition 100
+          :id (get-entity-id)
+          :type :person
+          :happiness 50}))
+
+
+(defn start-person [p]
+  nil)
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.