From 20f1ccb4600b88ac01768e912e6d5837534ca852 Mon Sep 17 00:00:00 2001 From: Griffin Smith Date: Sun, 7 Jul 2019 13:02:50 -0400 Subject: 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. --- src/game.rs | 26 +++++++++++++++++++++++--- src/messages.rs | 1 - src/types/command.rs | 9 ++++++++- 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, + /// 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(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, } } -- cgit 1.4.1