102 lines
3.2 KiB
Rust
102 lines
3.2 KiB
Rust
use crossterm::{
|
|
event::{self, KeyCode, KeyEventKind},
|
|
terminal::{disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen},
|
|
ExecutableCommand,
|
|
};
|
|
use ratatui::{
|
|
prelude::{CrosstermBackend, Stylize, Terminal},
|
|
widgets::Paragraph,
|
|
};
|
|
use std::io::{stdout, Result};
|
|
use ratatui::prelude::*;
|
|
use ratatui::widgets::Block;
|
|
|
|
use crate::game::Game;
|
|
use crate::level::Level;
|
|
use crate::level_widget::LevelWidget;
|
|
use crate::player::Player;
|
|
|
|
mod game;
|
|
mod player;
|
|
mod level;
|
|
mod position;
|
|
mod level_widget;
|
|
|
|
fn main() -> Result<()> {
|
|
let mut g = Game {
|
|
player: Player::new("Teddy Tester", 10),
|
|
levels: [Level::new(); 25],
|
|
};
|
|
|
|
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_widget(LevelWidget { game: &g }, map_area);
|
|
|
|
let stats_area = Rect {
|
|
x: area.x + 50,
|
|
y: area.y,
|
|
width: 30,
|
|
height: 25,
|
|
};
|
|
frame.render_widget(
|
|
Paragraph::new(format!("{}\nHealth: {}/{}\n{}/{}/{}",
|
|
g.player.get_name(),
|
|
g.player.get_life(),
|
|
g.player.get_max_life(),
|
|
g.player.get_position().get_level(),
|
|
g.player.get_position().get_x(),
|
|
g.player.get_position().get_y()))
|
|
.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 && key.code == KeyCode::Left {
|
|
g.move_player(-1, 0);
|
|
}
|
|
if key.kind == KeyEventKind::Press && key.code == KeyCode::Right {
|
|
g.move_player(1, 0);
|
|
}
|
|
if key.kind == KeyEventKind::Press && key.code == KeyCode::Up {
|
|
g.move_player(0, -1);
|
|
}
|
|
if key.kind == KeyEventKind::Press && key.code == KeyCode::Down {
|
|
g.move_player(0, 1);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
stdout().execute(LeaveAlternateScreen)?;
|
|
disable_raw_mode()?;
|
|
Ok(())
|
|
}
|