el_diablo/src/main.rs

108 lines
3.5 KiB
Rust

use std::io::Result;
use std::io::stdout;
use crossterm::{
event::{self, KeyCode, KeyEventKind},
ExecutableCommand,
terminal::{disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen},
};
use ratatui::{
prelude::{CrosstermBackend, Stylize, Terminal},
widgets::Paragraph,
};
use ratatui::prelude::*;
use ratatui::widgets::Block;
use whoami::realname;
use crate::game::Game;
use crate::level_widget::LevelWidget;
// use crate::level_widget::LevelWidget;
use crate::player::Player;
mod game;
mod player;
mod level;
mod position;
mod level_widget;
mod level_generator;
mod artifacts;
mod monster;
//
fn main() -> Result<()> {
let mut game = Game::new(Player::new(realname().as_str(), 10));
stdout().execute(EnterAlternateScreen)?;
enable_raw_mode()?;
let mut terminal = Terminal::new(CrosstermBackend::new(stdout()))?;
terminal.clear()?;
loop {
terminal.draw(|frame| {
let mut area = frame.size();
frame.render_widget(Block::default().style(Style::default().bg(Color::Green)), area);
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: 25,
};
frame.render_widget(
Paragraph::new(format!("{}\nHealth: {}/{}\nGold: {}\nLevel: {}",
game.get_player().get_name(),
game.get_player().get_life(),
game.get_player().get_max_life(),
game.get_player().get_gold(),
game.get_player().get_immutable_position().get_level()))
.white()
.on_blue(),
stats_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;
}
if key.kind == KeyEventKind::Press {
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();
}
}
}
}
stdout().execute(LeaveAlternateScreen)?;
disable_raw_mode()?;
Ok(())
}