|
| 1 | +use std::{ |
| 2 | + io::{self}, |
| 3 | + time::Duration, |
| 4 | +}; |
| 5 | + |
| 6 | +use crossterm::{ |
| 7 | + cursor::{MoveTo, MoveToNextLine}, |
| 8 | + event::{poll, read, Event, KeyCode, KeyModifiers}, |
| 9 | + execute, |
| 10 | + style::{style, Attribute, Color, PrintStyledContent, Stylize}, |
| 11 | + terminal::{disable_raw_mode, enable_raw_mode, Clear}, |
| 12 | +}; |
| 13 | + |
| 14 | +struct GameState { |
| 15 | + initialized: bool, |
| 16 | +} |
| 17 | + |
| 18 | +impl GameState { |
| 19 | + fn new() -> GameState { |
| 20 | + GameState { initialized: false } |
| 21 | + } |
| 22 | +} |
| 23 | + |
| 24 | +fn main_loop(game_state: &mut GameState) -> io::Result<(bool, Option<GameState>)> { |
| 25 | + // Wait up to 1s for some user event |
| 26 | + if poll(Duration::from_millis(1_000))? { |
| 27 | + // Read what even happened from the poll |
| 28 | + // It's guaranteed that read() won't block if `poll` returns `Ok(true)` |
| 29 | + match read()? { |
| 30 | + Event::Key(event) => { |
| 31 | + // detect exit request |
| 32 | + if event.modifiers == KeyModifiers::CONTROL && event.code == KeyCode::Char('c') { |
| 33 | + return Ok((true, None)); |
| 34 | + } |
| 35 | + // move forward game state |
| 36 | + if !game_state.initialized { |
| 37 | + // initialize game |
| 38 | + execute!( |
| 39 | + io::stdout(), |
| 40 | + // clear terminal |
| 41 | + Clear(crossterm::terminal::ClearType::All), |
| 42 | + // reset cursor position to top left |
| 43 | + MoveTo(0, 0), |
| 44 | + // write out new game state |
| 45 | + PrintStyledContent( |
| 46 | + "Game has begun." |
| 47 | + .with(Color::White) |
| 48 | + .on(Color::Red) |
| 49 | + .attribute(Attribute::Bold) |
| 50 | + ), |
| 51 | + )?; |
| 52 | + return Ok((false, Some(GameState { initialized: true }))); |
| 53 | + } |
| 54 | + // user event had no effect |
| 55 | + Ok((false, None)) |
| 56 | + } |
| 57 | + _ => Ok((false, None)), |
| 58 | + } |
| 59 | + } else { |
| 60 | + // Timeout expired, no event for 1s |
| 61 | + Ok((false, None)) |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +fn main() -> io::Result<()> { |
| 66 | + // initial splash screen |
| 67 | + execute!( |
| 68 | + io::stdout(), |
| 69 | + // clear terminal |
| 70 | + Clear(crossterm::terminal::ClearType::All), |
| 71 | + // reset cursor position to top left |
| 72 | + MoveTo(0, 0), |
| 73 | + // write out splash screen |
| 74 | + PrintStyledContent( |
| 75 | + "Merchant\n\nNavigate shifting markets and unreliable sources.\n\nBy samgqroberts" |
| 76 | + .with(Color::Yellow) |
| 77 | + .on(Color::Blue) |
| 78 | + .attribute(Attribute::Bold) |
| 79 | + ), |
| 80 | + // prompt user |
| 81 | + MoveToNextLine(2), |
| 82 | + PrintStyledContent( |
| 83 | + style("Press any key to begin") |
| 84 | + .with(Color::Blue) |
| 85 | + .on(Color::Yellow) |
| 86 | + .attribute(Attribute::Bold), |
| 87 | + ), |
| 88 | + )?; |
| 89 | + // set terminal into "non-canonical" mode so inputs are captured raw with no interpretation |
| 90 | + // https://docs.rs/crossterm/0.26.1/crossterm/terminal/index.html#raw-mode |
| 91 | + enable_raw_mode()?; |
| 92 | + // start main game loop, with fresh game state |
| 93 | + let mut game_state = GameState::new(); |
| 94 | + loop { |
| 95 | + // perform main loop logic, detect and handle potential io error |
| 96 | + match main_loop(&mut game_state) { |
| 97 | + Err(e) => { |
| 98 | + // an error was encountered in main game loop |
| 99 | + println!("Error: {:?}\r", e); |
| 100 | + } |
| 101 | + Ok((should_exit, new_game_state)) => { |
| 102 | + // main loop may have told us user requested an exit |
| 103 | + if should_exit { |
| 104 | + break; |
| 105 | + } |
| 106 | + // main loop may have given back a new game state |
| 107 | + if let Some(new_game_state) = new_game_state { |
| 108 | + game_state = new_game_state |
| 109 | + } |
| 110 | + } |
| 111 | + } |
| 112 | + } |
| 113 | + // set terminal back to canonical mode before exiting |
| 114 | + disable_raw_mode() |
| 115 | +} |
0 commit comments