Show manual
extract playing the game and the manual from main
This commit is contained in:
parent
4a31994ac9
commit
1c6e5d26d0
100
src/main.rs
100
src/main.rs
|
@ -1,4 +1,5 @@
|
||||||
use std::io::Result;
|
use std::cmp::min;
|
||||||
|
use std::io::{Result, Stdout};
|
||||||
use std::io::stdout;
|
use std::io::stdout;
|
||||||
use std::string::ToString;
|
use std::string::ToString;
|
||||||
use std::time::Instant;
|
use std::time::Instant;
|
||||||
|
@ -48,47 +49,26 @@ struct CliOptions {
|
||||||
fn main() -> Result<()> {
|
fn main() -> Result<()> {
|
||||||
let cli_options = CliOptions::parse();
|
let cli_options = CliOptions::parse();
|
||||||
|
|
||||||
|
|
||||||
let player_stats = PlayerStats::create_random();
|
|
||||||
let mut game = Game::new(Player::new(realname().as_str(), player_stats));
|
|
||||||
|
|
||||||
stdout().execute(EnterAlternateScreen)?;
|
stdout().execute(EnterAlternateScreen)?;
|
||||||
enable_raw_mode()?;
|
enable_raw_mode()?;
|
||||||
let mut terminal = Terminal::new(CrosstermBackend::new(stdout()))?;
|
let mut terminal = Terminal::new(CrosstermBackend::new(stdout()))?;
|
||||||
terminal.clear()?;
|
terminal.clear()?;
|
||||||
|
|
||||||
if cli_options.manual {
|
if cli_options.manual {
|
||||||
loop {
|
show_manual(&mut terminal);
|
||||||
let _ = terminal.draw(|frame| {
|
|
||||||
let mut area = frame.size();
|
|
||||||
let w = area.width / 2;
|
|
||||||
let h = area.height / 2;
|
|
||||||
area.x += w - 20;
|
|
||||||
area.y += h - 10;
|
|
||||||
area.width = 40;
|
|
||||||
area.height = 20;
|
|
||||||
let block = Block::default()
|
|
||||||
.title(Title::from(" Manual ").alignment(Alignment::Center).position(Position::Top))
|
|
||||||
.title(Title::from("Press `q` to quit!").position(Position::Bottom))
|
|
||||||
.borders(Borders::ALL)
|
|
||||||
.border_style(Style::default().fg(Color::White))
|
|
||||||
.border_type(BorderType::Rounded)
|
|
||||||
.style(Style::default().bg(Color::Black));
|
|
||||||
let mut text = "To control your character (depicted by the 8) you can use the cursor keys. \n\n\
|
|
||||||
To interact simply walk into the entity to interact with. Possible entities are creates \
|
|
||||||
(depicted by C), potions (depicted by P) or enemies.".to_string();
|
|
||||||
let paragraph = Paragraph::new(text).block(block).wrap(Wrap { trim: true });
|
|
||||||
frame.render_widget(paragraph, area);
|
|
||||||
});
|
|
||||||
if event::poll(std::time::Duration::from_millis(16))? {
|
|
||||||
if let event::Event::Key(key) = event::read()? {
|
|
||||||
if key.kind == KeyEventKind::Press && key.code == KeyCode::Char('q') {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
|
play_game(&mut terminal)?;
|
||||||
|
}
|
||||||
|
|
||||||
|
stdout().execute(LeaveAlternateScreen)?;
|
||||||
|
disable_raw_mode()?;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
/// play the game ;)
|
||||||
|
fn play_game(terminal: &mut Terminal<CrosstermBackend<Stdout>>) -> Result<()> {
|
||||||
|
let player_stats = PlayerStats::create_random();
|
||||||
|
let mut game = Game::new(Player::new(realname().as_str(), player_stats));
|
||||||
let start_time = Instant::now();
|
let start_time = Instant::now();
|
||||||
let mut ticks = 0;
|
let mut ticks = 0;
|
||||||
loop {
|
loop {
|
||||||
|
@ -268,9 +248,51 @@ fn main() -> Result<()> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
stdout().execute(LeaveAlternateScreen)?;
|
|
||||||
disable_raw_mode()?;
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// this function shows some manual text
|
||||||
|
fn show_manual(terminal: &mut Terminal<CrosstermBackend<Stdout>>) {
|
||||||
|
let mut page = 0;
|
||||||
|
let pages = [
|
||||||
|
"Background story\n\n\
|
||||||
|
You are a traveller and on your journey....
|
||||||
|
".to_string(),
|
||||||
|
"To control your character (depicted by the 8) you can use the cursor keys (←, ↑, →, ↓). \n\n\
|
||||||
|
To interact simply walk into the entity to interact with. Possible entities are creates \
|
||||||
|
(depicted by C), potions (depicted by P) or enemies.".to_string(),
|
||||||
|
];
|
||||||
|
loop {
|
||||||
|
let _ = terminal.draw(|frame| {
|
||||||
|
let mut area = frame.size();
|
||||||
|
let w = area.width / 2;
|
||||||
|
let h = area.height / 2;
|
||||||
|
area.x += w - 20;
|
||||||
|
area.y += h - 10;
|
||||||
|
area.width = 40;
|
||||||
|
area.height = 20;
|
||||||
|
let block = Block::default()
|
||||||
|
.title(Title::from(format!(" Manual ({}/{})", page + 1, pages.len())).alignment(Alignment::Center).position(Position::Top))
|
||||||
|
.title(Title::from("`q` to quit, `↑` and `↓` to scroll.").position(Position::Bottom))
|
||||||
|
.borders(Borders::ALL)
|
||||||
|
.border_style(Style::default().fg(Color::White))
|
||||||
|
.border_type(BorderType::Rounded)
|
||||||
|
.style(Style::default().bg(Color::Black));
|
||||||
|
let paragraph = Paragraph::new(pages[page].as_str()).block(block).wrap(Wrap { trim: true });
|
||||||
|
frame.render_widget(paragraph, area);
|
||||||
|
});
|
||||||
|
if event::poll(std::time::Duration::from_millis(16)).unwrap_or(false) {
|
||||||
|
if let event::Event::Key(key) = event::read().unwrap() {
|
||||||
|
if key.kind == KeyEventKind::Press && key.code == KeyCode::Char('q') {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
if key.kind == KeyEventKind::Press && key.code == KeyCode::Up {
|
||||||
|
page = page.saturating_sub(1);
|
||||||
|
}
|
||||||
|
if key.kind == KeyEventKind::Press && key.code == KeyCode::Down {
|
||||||
|
page = min(pages.len() - 1, page + 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue