Browse files

Clean up a bunch of stuff before going to sleep.

  • Loading branch information...
1 parent 3f956ea commit 16fa6c72557b1120f1b370c0c07df0102b31345f @sjl committed Aug 4, 2012
Showing with 75 additions and 46 deletions.
  1. +0 −3 doc/intro.md
  2. +19 −6 src/ruin/core.clj
  3. +27 −19 src/ruin/drawing.clj
  4. +13 −9 src/ruin/input.clj
  5. +16 −2 src/ruin/ui.clj
  6. +0 −7 test/ruin/core_test.clj
View
3 doc/intro.md
@@ -1,3 +0,0 @@
-# Introduction to ruin
-
-TODO: write [great documentation](http://jacobian.org/writing/great-documentation/what-to-write/)
View
25 src/ruin/core.clj
@@ -7,7 +7,13 @@
(defonce game (ref nil))
-(defn create-fresh-game []
+(defn create-fresh-game
+ "Refresh the game var with a new game.
+
+ For now the screen is refreshed too. This may change.
+
+ "
+ []
(dosync
(let [scr (s/get-screen :swing {:cols 120
:rows 45
@@ -20,29 +26,36 @@
(s/start scr))))
-
-(defn draw-uis []
+(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))))
-(defn draw-loop []
+(defn draw-loop
+ "Continually draw all the game's UIs, until the game stops running."
+ []
(draw-uis)
(Thread/sleep 500)
(when (:state @game)
(recur)))
-(defn input-loop []
+(defn input-loop
+ "Continually process input, until the game stops running."
+ []
(process-input (last (:uis @game))
game
(s/get-key-blocking (:screen @game)))
(when (:state @game)
(recur)))
-(defn run-game []
+(defn run-game
+ "Start all the various loops of the game, then wait for it to be done."
+ []
(let [screen (:screen @game)]
(future (draw-loop))
(future (input-loop))))
View
46 src/ruin/drawing.clj
@@ -1,32 +1,40 @@
(ns ruin.drawing
(:require [lanterna.screen :as s]))
+
(defmulti draw-ui
+ "Draw the UI to the console. Does not clear or refresh."
(fn [ui game]
(:kind ui)))
(defmethod draw-ui :start [this {:keys [screen]}]
- (s/put-sheet screen 0 0 [" _____ _ _ _____ _ _"
- "| __ \\| | | |_ _| \\ | |"
- "| |__) | | | | | | | \\| |"
- "| _ /| | | | | | | . ` |"
- "| | \\ \\| |__| |_| |_| |\\ |"
- "|_| \\_\\\\____/|_____|_| \\_|"
- ""
- "press any key to begin..."]))
-
-(defmethod draw-ui :play [this {:keys [screen]}]
- (s/put-sheet screen 0 0 ["You are playing."
- ""
- "press enter to win, anything else to lose"]))
+ (io!
+ (s/put-sheet screen 0 0 [" _____ _ _ _____ _ _"
+ "| __ \\| | | |_ _| \\ | |"
+ "| |__) | | | | | | | \\| |"
+ "| _ /| | | | | | | . ` |"
+ "| | \\ \\| |__| |_| |_| |\\ |"
+ "|_| \\_\\\\____/|_____|_| \\_|"
+ ""
+ "press any key to begin..."])))
(defmethod draw-ui :win [this {:keys [screen]}]
- (s/put-sheet screen 0 0 ["Congratulations, you've won!"
- ""
- "press any key to continue..."]))
+ (io!
+ (s/put-sheet screen 0 0 ["Congratulations, you've won!"
+ ""
+ "press any key to continue..."])))
(defmethod draw-ui :lose [this {:keys [screen]}]
- (s/put-sheet screen 0 0 ["Sorry, you lost."
- ""
- "press any key to continue..."]))
+ (io!
+ (s/put-sheet screen 0 0 ["Sorry, you lost."
+ ""
+ "press any key to continue..."])))
+
+
+(defmethod draw-ui :play [this {:keys [screen]}]
+ (io!
+ (s/put-sheet screen 0 0 ["You are playing."
+ ""
+ "press enter to win, anything else to lose"])))
+
View
22 src/ruin/input.clj
@@ -4,9 +4,11 @@
(defmulti process-input
+ "Handle input from the console."
(fn [ui game input]
(:kind ui)))
+
(defmethod process-input :start [ui game input]
(cond
(#{:escape \q} input) (do
@@ -16,15 +18,6 @@
(pop-ui game)
(push-ui game (->UI :play)))))
-(defmethod process-input :play [ui game input]
- (case input
- :enter (dosync
- (pop-ui game)
- (push-ui game (->UI :win)))
- (dosync
- (pop-ui game)
- (push-ui game (->UI :lose)))))
-
(defmethod process-input :win [ui game input]
(dosync
(pop-ui game)
@@ -34,3 +27,14 @@
(dosync
(pop-ui game)
(push-ui game (->UI :start))))
+
+
+(defmethod process-input :play [ui game input]
+ (case input
+ :enter (dosync
+ (pop-ui game)
+ (push-ui game (->UI :win)))
+ (dosync
+ (pop-ui game)
+ (push-ui game (->UI :lose)))))
+
View
18 src/ruin/ui.clj
@@ -3,11 +3,25 @@
(defrecord UI [kind])
-(defn push-ui [game ui]
+(defn push-ui
+ "Push the UI onto the UI stack of the game.
+
+ Executes in a transaction, but make sure to use a transaction of your own if
+ you want to synchronize it with another action, like popping a UI.
+
+ "
+ [game ui]
(dosync
(alter game update-in [:uis] conj ui)))
-(defn pop-ui [game]
+(defn pop-ui
+ "Pop the last UI on the UI stack of the game off, and return it.
+
+ Executes in a transaction, but make sure to use a transaction of your own if
+ 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)
View
7 test/ruin/core_test.clj
@@ -1,7 +0,0 @@
-(ns ruin.core-test
- (:use clojure.test
- ruin.core))
-
-(deftest a-test
- (testing "FIXME, I fail."
- (is (= 0 1))))

0 comments on commit 16fa6c7

Please sign in to comment.