use ratatui::prelude::*; use ratatui::widgets::Widget; use crate::game::Game; use crate::artifacts::Artifact; use crate::level::{Level, StructureElement}; const FG_BROWN: Color = Color::Rgb(186, 74, 0); //⌂ α Ω pub struct LevelWidget<'a> { pub game: &'a Game, } 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); } fn calculate_wall(top: Option, right: Option, bottom: Option, left: Option) -> String { let mut val = 0; val += match top { Some(n) => { match n { StructureElement::Wall => 1, _ => 0 } } None => 0 }; val += match right { Some(n) => { match n { StructureElement::Wall => 2, _ => 0 } } None => 0 }; val += match bottom { Some(n) => { match n { StructureElement::Wall => 4, _ => 0 } } None => 0 }; val += match left { Some(n) => { match n { StructureElement::Wall => 8, _ => 0 } } None => 0 }; [ "╋", "┃", "━", "┗", "┃", "┃", "┏", "┣", "━", "┛", "━", "┻", "┓", "┫", "┳", "╋", ][val].to_string() } } impl Widget for LevelWidget<'_> { fn render(self, area: Rect, buf: &mut Buffer) { let player= self.game.get_player(); // let player = &mut self.game.player; let player_pos = player.get_immutable_position(); // let level: &Level = &self.game.levels[player_pos.get_level()]; let al: u16 = area.left(); let at: u16 = area.top(); // draw the level elements for x in al..area.right() { for y in at..area.bottom() { // let level_x = (x - al) as i16; // let level_y = (y - at) as i16; // match level.get_element(level_x, level_y) { // (None, _) => {} // (_, Some(e)) => { // match e { // Artifact::Chest { gold: _ } => { // self.set_cell(buf, x, y, "⌂", Color::Black, Color::Gray); // } // } // } // (Some(e), _) => { // match e { // StructureElement::Wall => { // let top = level.get_element(level_x, level_y - 1).0; // let right = level.get_element(level_x + 1, level_y).0; // let bottom = level.get_element(level_x, level_y + 1).0; // let left = level.get_element(level_x - 1, level_y).0; // let symbol = LevelWidget::calculate_wall(top, right, bottom, left); // self.set_cell(buf, x, y, symbol.as_str(), 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); // } // StructureElement::Start => { // self.set_cell(buf, x, y, "α", Color::Black, Color::Gray); // } // StructureElement::End => { // self.set_cell(buf, x, y, "Ω", Color::Black, Color::Gray); // } // } // } // } } } // // // draw player 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); } } #[test] fn test_get_wall_symbol() { let wall = Some(StructureElement::Wall); // 0 let s = LevelWidget::calculate_wall(None, None, None, None); assert_eq!(s, "╋"); // 15 let s = LevelWidget::calculate_wall(wall, wall, wall, wall); assert_eq!(s, "╋"); // 1 let s = LevelWidget::calculate_wall(wall, None, None, None); assert_eq!(s, "┃"); // 5 let s = LevelWidget::calculate_wall(wall, None, wall, None); assert_eq!(s, "┃"); // 4 let s = LevelWidget::calculate_wall(None, None, wall, None); assert_eq!(s, "┃"); // 2 let s = LevelWidget::calculate_wall(None, wall, None, None); assert_eq!(s, "━"); // 10 let s = LevelWidget::calculate_wall(None, wall, None, wall); assert_eq!(s, "━"); // 9 let s = LevelWidget::calculate_wall(wall, None, None, wall); assert_eq!(s, "┛"); // 3 let s = LevelWidget::calculate_wall(wall, wall, None, None); assert_eq!(s, "┗"); // 6 let s = LevelWidget::calculate_wall(None, wall, wall, None); assert_eq!(s, "┏"); // 12 let s = LevelWidget::calculate_wall(None, None, wall, wall); assert_eq!(s, "┓"); }