el_diablo/src/main.rs

255 lines
9.5 KiB
Rust

use std::io::stdout;
use std::io::Result;
use std::time::Instant;
use crossterm::{
event::{self, KeyCode, KeyEventKind},
terminal::{disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen},
ExecutableCommand,
};
use ratatui::prelude::*;
use ratatui::widgets::block::{Position, Title};
use ratatui::widgets::{Block, BorderType, Borders, Wrap};
use ratatui::{
prelude::{CrosstermBackend, Terminal},
widgets::Paragraph,
};
use whoami::realname;
use crate::game::{Game, GameState};
use crate::level_widget::LevelWidget;
use crate::player::Player;
use crate::position::HasPosition;
mod artifacts;
mod game;
mod level;
mod level_generator;
mod level_widget;
mod monster;
mod player;
mod position;
/// length of a game frame in ms
pub const FRAME_LENGTH: u64 = 100;
//
fn main() -> Result<()> {
let mut game = Game::new(Player::new(realname().as_str(), 30));
stdout().execute(EnterAlternateScreen)?;
enable_raw_mode()?;
let mut terminal = Terminal::new(CrosstermBackend::new(stdout()))?;
terminal.clear()?;
let start_time = Instant::now();
let mut ticks = 0;
loop {
terminal.draw(|frame| {
let mut area = frame.area();
frame.render_widget(
Block::default().style(Style::default().bg(Color::Green)),
area,
);
// don't draw stuff except an info box if the terminal is too small (less than 80x25)
// to prevent the read drawing code from crashing the game.
if area.width < 80 || area.height < 25 {
let block = Block::default()
.title("Info")
.borders(Borders::ALL)
.border_style(Style::default().fg(Color::White))
.border_type(BorderType::Rounded)
.style(Style::default().bg(Color::Black));
let paragraph = Paragraph::new("Terminal needs to be at leas 80x25!")
.block(block)
.wrap(Wrap { trim: true });
frame.render_widget(paragraph, area);
return;
}
if area.width > 80 {
area.x = (area.width - 80) / 2;
area.width = 80;
}
if area.height > 25 {
area.y = (area.height - 25) / 2;
area.height = 25;
}
let map_area = Rect {
x: area.x,
y: area.y,
width: level::LEVEL_WIDTH as u16,
height: level::LEVEL_HEIGHT as u16,
};
frame.render_stateful_widget(LevelWidget {}, map_area, &mut game);
let stats_area = Rect {
x: area.x + 50,
y: area.y,
width: 30,
height: 15,
};
let block = Block::default()
.title(
Title::from(format!(" {} ", game.get_player().get_name()))
.alignment(Alignment::Center)
.position(Position::Top),
)
.borders(Borders::TOP)
.border_style(Style::default().fg(Color::White))
.border_type(BorderType::Rounded)
.style(Style::default().bg(Color::Blue));
frame.render_widget(
Paragraph::new(format!(
"Health: {}/{}\nExp: {}\nGold: {}\nLevel: {}\nInventory: {}",
game.get_player().get_life(),
game.get_player().get_max_life(),
game.get_player().get_experience(),
game.get_player().get_gold(),
game.get_player().get_immutable_position().get_level() + 1,
game.get_player().inventory_size(),
))
.block(block)
.wrap(Wrap { trim: true }),
stats_area,
);
let messages_area = Rect {
x: area.x + 50,
y: area.y + 15,
width: 30,
height: 10,
};
// Display the latest messages from the game to the user
let block = Block::default()
.title(
Title::from(" messages ")
.alignment(Alignment::Center)
.position(Position::Top),
)
.borders(Borders::TOP)
.border_style(Style::default().fg(Color::White))
.border_type(BorderType::Rounded)
.style(Style::default().bg(Color::Blue));
let paragraph1 = if game.messages.is_empty() {
"".to_string()
} else {
format!("> {}", game.messages.join("\n> "))
};
frame.render_widget(
Paragraph::new(paragraph1)
.block(block)
.wrap(Wrap { trim: true }),
messages_area,
);
})?;
if event::poll(std::time::Duration::from_millis(FRAME_LENGTH))? {
if let event::Event::Key(key) = event::read()? {
if key.kind == KeyEventKind::Press {
match key.code {
KeyCode::Char('v') => {
game.messages.insert(
0,
format!("You are playing version '{}'.", env!("GIT_HASH"))
.to_string(),
);
}
KeyCode::Char('p') => {
let gained_health = game.get_mutable_player().consume_inventory();
if gained_health > 0 {
game.messages.insert(
0,
format!(
"used a potion from inventory and gained {} health.",
gained_health
)
.to_string(),
);
}
}
KeyCode::Char('q') => {
break;
}
KeyCode::Left | KeyCode::Down | KeyCode::Right | KeyCode::Up => {
let new_pos = match key.code {
KeyCode::Left => game.move_player(-1, 0),
KeyCode::Right => game.move_player(1, 0),
KeyCode::Up => game.move_player(0, -1),
KeyCode::Down => game.move_player(0, 1),
_ => (0, 0),
};
if !game.player_fights_monster() {
// player attacked monster but did not kill it
game.move_player(new_pos.0, new_pos.1);
}
game.player_collects_artifact();
}
_ => {}
}
}
}
}
game.update_level(ticks);
if game.get_game_state() != GameState::Running {
break;
}
ticks += 1;
}
let playtime = start_time.elapsed();
loop {
let _ = terminal.draw(|frame| {
let mut area = frame.area();
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(" Game ended ")
.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 = match game.get_game_state() {
GameState::Running => {
"Quitting is for cowards! You'll better try again!".to_string()
}
GameState::Lost => {
"Sorry, you died in the dungeon. Better luck next time!".to_string()
}
GameState::Won => {
"Congratulation! You mastered your way through the dungeon and won the game."
.to_string()
}
};
text += format!(
"\nYou gained {} experience.",
game.get_player().get_experience()
)
.as_str();
text += format!("\nYou collected {} gold.", game.get_player().get_gold()).as_str();
text += format!("\nYou played {} seconds.", playtime.as_secs()).as_str();
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;
}
}
}
}
stdout().execute(LeaveAlternateScreen)?;
disable_raw_mode()?;
Ok(())
}