el_diablo/src/level_widget.rs

94 lines
3.7 KiB
Rust
Raw Normal View History

use ratatui::buffer::Buffer;
use ratatui::layout::Rect;
use ratatui::style::Color;
use ratatui::widgets::{StatefulWidget, Widget};
2023-12-05 19:33:19 +01:00
use crate::game::Game;
use crate::level::StructureElement;
2023-12-05 19:33:19 +01:00
const FG_BROWN: Color = Color::Rgb(186, 74, 0);
pub struct LevelWidget {}
2023-12-05 19:33:19 +01:00
impl LevelWidget {
2023-12-05 19:33:19 +01:00
fn set_cell(&self, buf: &mut Buffer, x: u16, y: u16, symbol: &str, fg: Color, bg: Color) {
buf.
get_mut(x, y).
set_symbol(symbol).
set_bg(bg).
set_fg(fg);
}
}
impl StatefulWidget for LevelWidget {
type State = Game;
2023-12-05 19:33:19 +01:00
fn render(self, area: Rect, buf: &mut Buffer, state: &mut Self::State) {
2023-12-05 19:33:19 +01:00
let al: u16 = area.left();
let at: u16 = area.top();
{
let player = state.get_mutable_player();
let player_pos = player.get_immutable_position();
let player_level = player_pos.get_level();
// draw the level elements
for x in al..area.right() {
for y in at..area.bottom() {
let level = state.get_mutable_level(player_level);
let level_x = (x - al) as i16;
let level_y = (y - at) as i16;
match level.get_element(level_x, level_y) {
(Some(structure_element), None, None) => {
match structure_element {
StructureElement::Start => {
self.set_cell(buf, x, y, "α", Color::Black, Color::Gray);
}
StructureElement::End => {
self.set_cell(buf, x, y, "Ω", Color::Black, Color::Gray);
}
StructureElement::Wall => {
// TODO add fancy walls
self.set_cell(buf, x, y, "#", FG_BROWN, Color::Gray);
}
StructureElement::Floor => {
self.set_cell(buf, x, y, " ", FG_BROWN, Color::Gray);
}
StructureElement::StairDown => {
self.set_cell(buf, x, y, ">", Color::Black, Color::Gray);
}
StructureElement::StairUp => {
self.set_cell(buf, x, y, "<", Color::Black, Color::Gray);
}
StructureElement::Unknown => {
self.set_cell(buf, x, y, "", Color::DarkGray, Color::Gray);
}
}
}
2023-12-18 16:11:19 +01:00
(_, Some(m), _) => {
let (s, c) = m.get_representation();
self.set_cell(buf, x, y, s, c, Color::Gray);
}
(_, _, Some(t)) => {
let (s, c) = t.get_representation();
self.set_cell(buf, x, y, s, c, Color::Gray);
}
(None, None, None) => {}
};
}
2023-12-05 19:33:19 +01:00
}
}
{
let player = state.get_mutable_player();
let player_pos = player.get_immutable_position();
let player_x = al + player_pos.get_x() as u16;
let player_y = at + player_pos.get_y() as u16;
self.set_cell(buf, player_x, player_y, "8", Color::Red, Color::Gray);
}
2023-12-05 19:33:19 +01:00
}
}
impl Widget for LevelWidget {
2023-12-09 22:43:06 +01:00
fn render(self, _area: Rect, _buf: &mut Buffer) {}
}