about summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
authorGriffin Smith <root@gws.fyi>2019-07-07T17·02-0400
committerGriffin Smith <root@gws.fyi>2019-07-07T17·02-0400
commit20f1ccb4600b88ac01768e912e6d5837534ca852 (patch)
tree95a7c86ed32e524d4f8a70250c3f871600b79fc3 /src
parentc643ee1dfcb8d44b8cd198c768f31dd7659f2ff9 (diff)
add "Previous message" command
ctrl+p, like nethack. Cycles through messages, also like nethack.

May want to add some sort of indicator of how many messages there have
been.
Diffstat (limited to 'src')
-rw-r--r--src/game.rs26
-rw-r--r--src/messages.rs1
-rw-r--r--src/types/command.rs9
3 files changed, 31 insertions, 5 deletions
diff --git a/src/game.rs b/src/game.rs
index b619f13423f5..daa5fa575f68 100644
--- a/src/game.rs
+++ b/src/game.rs
@@ -31,6 +31,10 @@ pub struct Game<'a> {
     /// The messages that have been said to the user, in forward time order
     messages: Vec<String>,
 
+    /// The index of the currently-displayed message. Used to track the index of
+    /// the currently displayed message when handling PreviousMessage commands
+    message_idx: usize,
+
     /// A global random number generator for the game
     rng: Rng,
 }
@@ -50,6 +54,7 @@ impl<'a> Game<'a> {
         Game {
             settings,
             rng,
+            message_idx: 0,
             viewport: Viewport::new(
                 BoundingBox::at_origin(Dimensions { w, h }),
                 BoundingBox::at_origin(Dimensions { w: w - 2, h: h - 2 }),
@@ -80,6 +85,16 @@ impl<'a> Game<'a> {
     fn say(&mut self, message_name: &str) -> io::Result<()> {
         let message = self.message(message_name);
         self.messages.push(message.to_string());
+        self.message_idx = self.messages.len() - 1;
+        self.viewport.write_message(message)
+    }
+
+    fn previous_message(&mut self) -> io::Result<()> {
+        if self.message_idx == 0 {
+            return Ok(());
+        }
+        self.message_idx -= 1;
+        let message = &self.messages[self.message_idx];
         self.viewport.write_message(message)
     }
 
@@ -89,23 +104,28 @@ impl<'a> Game<'a> {
         self.viewport.init()?;
         self.draw_entities()?;
         self.say("global.welcome")?;
+        self.say("somethign else")?;
         self.flush()?;
         loop {
             let mut old_position = None;
+            use Command::*;
             match Command::from_key(self.keys.next().unwrap().unwrap()) {
-                Some(Command::Quit) => {
+                Some(Quit) => {
                     info!("Quitting game due to user request");
                     break;
                 }
 
-                Some(Command::Move(direction)) => {
+                Some(Move(direction)) => {
                     let new_pos = self.character.position + direction;
                     if !self.collision_at(new_pos) {
                         old_position = Some(self.character.position);
                         self.character.position = new_pos;
                     }
                 }
-                _ => (),
+
+                Some(PreviousMessage) => self.previous_message()?,
+
+                None => (),
             }
 
             match old_position {
diff --git a/src/messages.rs b/src/messages.rs
index 2b9f098f98ca..03a96b4a0a64 100644
--- a/src/messages.rs
+++ b/src/messages.rs
@@ -175,7 +175,6 @@ lazy_static! {
 /// Look up a game message based on the given (dot-separated) name, with the
 /// given random generator used to select from choice-based messages
 pub fn message<R: Rng + ?Sized>(name: &str, rng: &mut R) -> &'static str {
-    use Message::*;
     MESSAGES
         .lookup(name)
         .and_then(|msg| msg.resolve(rng))
diff --git a/src/types/command.rs b/src/types/command.rs
index 86f83a12c181..2a51531c0a8a 100644
--- a/src/types/command.rs
+++ b/src/types/command.rs
@@ -1,11 +1,17 @@
 use super::Direction;
 use super::Direction::*;
 use termion::event::Key;
-use termion::event::Key::Char;
+use termion::event::Key::{Char, Ctrl};
 
 pub enum Command {
+    /// Quit the game
     Quit,
+
+    /// Move the character in a direction
     Move(Direction),
+
+    /// Display the previous message
+    PreviousMessage,
 }
 
 impl Command {
@@ -17,6 +23,7 @@ impl Command {
             Char('k') | Char('w') | Key::Up => Some(Move(Up)),
             Char('j') | Char('s') | Key::Down => Some(Move(Down)),
             Char('l') | Char('d') | Key::Right => Some(Move(Right)),
+            Ctrl('p') => Some(PreviousMessage),
             _ => None,
         }
     }