Skip to content

Commit

Permalink
Refactor a bunch.
Browse files Browse the repository at this point in the history
  • Loading branch information
sjl committed Aug 4, 2012
1 parent 16fa6c7 commit 7cdc2e5
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 51 deletions.
15 changes: 6 additions & 9 deletions src/ruin/core.clj
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
(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.
Expand All @@ -29,10 +28,9 @@
(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."
Expand All @@ -47,8 +45,7 @@
"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)))

Expand Down
61 changes: 38 additions & 23 deletions src/ruin/drawing.clj
Original file line number Diff line number Diff line change
@@ -1,40 +1,55 @@
(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..."])))

(defmethod draw-ui :win [this {:keys [screen]}]
(s/put-sheet (:screen @game) 0 0
[" _____ _ _ _____ _ _"
"| __ \\| | | |_ _| \\ | |"
"| |__) | | | | | | | \\| |"
"| _ /| | | | | | | . ` |"
"| | \\ \\| |__| |_| |_| |\\ |"
"|_| \\_\\\\____/|_____|_| \\_|"
""
"press any key to begin..."])))

(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"])))

33 changes: 17 additions & 16 deletions src/ruin/input.clj
Original file line number Diff line number Diff line change
@@ -1,40 +1,41 @@
(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)))))

4 changes: 4 additions & 0 deletions src/ruin/state.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
(ns ruin.state)


(defonce game (ref nil))
7 changes: 4 additions & 3 deletions src/ruin/ui.clj
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
(ns ruin.ui)
(ns ruin.ui
(:use [ruin.state :only [game]]))


(defrecord UI [kind])
Expand All @@ -10,7 +11,7 @@
you want to synchronize it with another action, like popping a UI.
"
[game ui]
[ui]
(dosync
(alter game update-in [:uis] conj ui)))

Expand All @@ -21,7 +22,7 @@
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)
Expand Down

0 comments on commit 7cdc2e5

Please sign in to comment.