1. Steve Losh
  2. ruin

Commits

Steve Losh  committed 8ee99b0

Refactor a bunch.

  • Participants
  • Parent commits d336920
  • Branches default

Comments (0)

Files changed (5)

File src/ruin/core.clj

View file
 (ns ruin.core
-  (:use [ruin.ui :only [->UI]]
+  (:use [ruin.state :only [game]]
+        [ruin.ui :only [->UI]]
         [ruin.drawing :only [draw-ui]]
         [ruin.input :only [process-input]])
   (:require [lanterna.screen :as s]))
 
 
-(defonce game (ref nil))
-
 (defn create-fresh-game
   "Refresh the game var with a new game.
 
 (defn draw-uis
   "Draw each UI in the game in turn."
   []
-  (let [game @game]
-    (s/clear (:screen game))
-    (dorun (map #(draw-ui % game) (:uis game)))
-    (s/redraw (:screen game))))
+  (s/clear (:screen @game))
+  (dorun (map draw-ui (:uis @game)))
+  (s/redraw (:screen @game)))
 
 (defn draw-loop
   "Continually draw all the game's UIs, until the game stops running."
   "Continually process input, until the game stops running."
   []
   (process-input (last (:uis @game))
-                 game
-                 (s/get-key-blocking (:screen @game)))
+                 (io! (s/get-key-blocking (:screen @game))))
   (when (:state @game)
     (recur)))
 

File src/ruin/drawing.clj

View file
 (ns ruin.drawing
+  (:use [ruin.state :only [game]])
   (:require [lanterna.screen :as s]))
 
+(def SIDEBAR-WIDTH 40)
 
 (defmulti draw-ui
   "Draw the UI to the console.  Does not clear or refresh."
-  (fn [ui game]
+  (fn [ui]
     (:kind ui)))
 
 
-(defmethod draw-ui :start [this {:keys [screen]}]
+(defmethod draw-ui :start [ui]
   (io!
-    (s/put-sheet screen 0 0 [" _____  _    _ _____ _   _"
-                             "|  __ \\| |  | |_   _| \\ | |"
-                             "| |__) | |  | | | | |  \\| |"
-                             "|  _  /| |  | | | | | . ` |"
-                             "| | \\ \\| |__| |_| |_| |\\  |"
-                             "|_|  \\_\\\\____/|_____|_| \\_|"
-                             ""
-                             "press any key to begin..."])))
+    (s/put-sheet (:screen @game) 0 0
+                 [" _____  _    _ _____ _   _"
+                  "|  __ \\| |  | |_   _| \\ | |"
+                  "| |__) | |  | | | | |  \\| |"
+                  "|  _  /| |  | | | | | . ` |"
+                  "| | \\ \\| |__| |_| |_| |\\  |"
+                  "|_|  \\_\\\\____/|_____|_| \\_|"
+                  ""
+                  "press any key to begin..."])))
 
-(defmethod draw-ui :win [this {:keys [screen]}]
+(defmethod draw-ui :win [ui]
   (io!
-    (s/put-sheet screen 0 0 ["Congratulations, you've won!"
-                             ""
-                             "press any key to continue..."])))
+    (s/put-sheet (:screen @game) 0 0
+                 ["Congratulations, you've won!"
+                  ""
+                  "press any key to continue..."])))
 
-(defmethod draw-ui :lose [this {:keys [screen]}]
+(defmethod draw-ui :lose [ui]
   (io!
-    (s/put-sheet screen 0 0 ["Sorry, you lost."
-                             ""
-                             "press any key to continue..."])))
+    (s/put-sheet (:screen @game) 0 0
+                 ["Sorry, you lost."
+                  ""
+                  "press any key to continue..."])))
 
 
-(defmethod draw-ui :play [this {:keys [screen]}]
+(defn draw-map []
+  (let [screen (:screen @game)
+        [cols rows] (s/get-size screen)
+        map-height rows
+        map-width (- cols (inc SIDEBAR-WIDTH))]
+    (s/put-sheet screen 0 0
+                 (repeat map-height (repeat map-width \.)))))
+
+(defmethod draw-ui :play [ui]
   (io!
-    (s/put-sheet screen 0 0 ["You are playing."
-                             ""
-                             "press enter to win, anything else to lose"])))
+    (draw-map)
+    (s/put-sheet (:screen @game) 0 0
+                 ["You are playing."
+                  ""
+                  "press enter to win, anything else to lose"])))
 

File src/ruin/input.clj

View file
 (ns ruin.input
-  (:use [ruin.ui :only [push-ui pop-ui ->UI]])
+  (:use [ruin.state :only [game]]
+        [ruin.ui :only [push-ui pop-ui ->UI]])
   (:require [lanterna.screen :as s]))
 
 
 (defmulti process-input
   "Handle input from the console."
-  (fn [ui game input]
+  (fn [ui input]
     (:kind ui)))
 
 
-(defmethod process-input :start [ui game input]
+(defmethod process-input :start [ui input]
   (cond
     (#{:escape \q} input) (do
                             (dosync (alter game dissoc :state))
                             (s/stop (:screen @game)))
     :else (dosync
-            (pop-ui game)
-            (push-ui game (->UI :play)))))
+            (pop-ui)
+            (push-ui (->UI :play)))))
 
-(defmethod process-input :win [ui game input]
+(defmethod process-input :win [ui input]
   (dosync
-    (pop-ui game)
-    (push-ui game (->UI :start))))
+    (pop-ui)
+    (push-ui (->UI :start))))
 
-(defmethod process-input :lose [ui game input]
+(defmethod process-input :lose [ui input]
   (dosync
-    (pop-ui game)
-    (push-ui game (->UI :start))))
+    (pop-ui)
+    (push-ui (->UI :start))))
 
 
-(defmethod process-input :play [ui game input]
+(defmethod process-input :play [ui input]
   (case input
     :enter (dosync
-             (pop-ui game)
-             (push-ui game (->UI :win)))
+             (pop-ui)
+             (push-ui (->UI :win)))
     (dosync
-      (pop-ui game)
-      (push-ui game (->UI :lose)))))
+      (pop-ui)
+      (push-ui (->UI :lose)))))
 

File src/ruin/state.clj

View file
+(ns ruin.state)
+
+
+(defonce game (ref nil))

File src/ruin/ui.clj

View file
-(ns ruin.ui)
+(ns ruin.ui
+  (:use [ruin.state :only [game]]))
 
 
 (defrecord UI [kind])
   you want to synchronize it with another action, like popping a UI.
 
   "
-  [game ui]
+  [ui]
   (dosync
     (alter game update-in [:uis] conj ui)))
 
   you want to synchronize it with another action, like pushing another UI.
 
   "
-  [game]
+  []
   (dosync
     (let [result (last (:uis @game))]
       (alter game update-in [:uis] pop)