use ratatui::buffer::Buffer; use ratatui::layout::Rect; use ratatui::style::Color; use ratatui::widgets::{StatefulWidget, Widget}; use crate::game::Game; use crate::level::StructureElement; const FG_BROWN: Color = Color::Rgb(186, 74, 0); pub struct LevelWidget {} impl LevelWidget { 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; fn render(self, area: Rect, buf: &mut Buffer, state: &mut Self::State) { 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); } } } (_, 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) => {} }; } } } { 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); } } } impl Widget for LevelWidget { fn render(self, _area: Rect, _buf: &mut Buffer) {} }