From 732c68448f3c801ded7ad0f8e1f91650c0d63d61 Mon Sep 17 00:00:00 2001 From: Joachim Lusiardi Date: Sat, 11 Nov 2023 16:01:44 +0100 Subject: [PATCH] forgotten file --- src/level_widget.rs | 151 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 151 insertions(+) create mode 100644 src/level_widget.rs diff --git a/src/level_widget.rs b/src/level_widget.rs new file mode 100644 index 0000000..a7aecda --- /dev/null +++ b/src/level_widget.rs @@ -0,0 +1,151 @@ +use ratatui::prelude::*; +use ratatui::widgets::Widget; +use crate::level::{Level, LevelElement}; + +const FG_BROWN: Color = Color::Rgb(186, 74, 0); + +pub struct LevelWidget { + pub content: Level, +} + +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 foo(top: Option, right: Option, bottom: Option, left: Option) -> String { + let mut val = 0; + val += match top { + Some(n) => { + match n { + LevelElement::Wall => 1, + _ => 0 + } + } + None => 0 + }; + val += match right { + Some(n) => { + match n { + LevelElement::Wall => 2, + _ => 0 + } + } + None => 0 + }; + val += match bottom { + Some(n) => { + match n { + LevelElement::Wall => 4, + _ => 0 + } + } + None => 0 + }; + val += match left { + Some(n) => { + match n { + LevelElement::Wall => 8, + _ => 0 + } + } + None => 0 + }; + + // 0 ╋ 5 ┃ 9 ┛ + // 1 ┃ 6 ┏ 10 ━ + // 2 ━ 7 ┣ 11 ┻ + // 3 ┗ + // 4 ┃ 8 ━ 12 ┓ + [ + "╋", "┃", "━", "┗", + "┃", "┃", "┏", "┣", + "━", "┛", "━", "┻", + "┓", "┫", "┳", "╋", + ][val].to_string() + } +} + +impl Widget for LevelWidget { + fn render(self, area: Rect, buf: &mut Buffer) { + let al = area.left(); + let at = area.top(); + 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 self.content.get_element(level_x, level_y) { + None => {} + Some(e) => { + match e { + LevelElement::Wall => { + let top = self.content.get_element(level_x, level_y-1); + let right = self.content.get_element(level_x+1, level_y); + let bottom = self.content.get_element(level_x, level_y+1); + let left = self.content.get_element(level_x-1, level_y); + let symbol = LevelWidget::foo(top, right, bottom, left); + self.set_cell(buf, x, y, symbol.as_str(), FG_BROWN, Color::Gray); + } + LevelElement::Floor => { + self.set_cell(buf, x, y, " ", FG_BROWN, Color::Gray); + } + LevelElement::StairDown => { + self.set_cell(buf, x, y, ">", FG_BROWN, Color::Gray); + } + LevelElement::StairUp => { + self.set_cell(buf, x, y, "<", FG_BROWN, Color::Gray); + } + } + } + } + } + } + } +} + +#[test] +fn test_get_wall_symbol() { + let wall = Some(LevelElement::Wall); + // 0 + let s = LevelWidget::foo(None, None, None, None); + assert_eq!(s, "╋"); + // 15 + let s = LevelWidget::foo(wall, wall, wall, wall); + assert_eq!(s, "╋"); + + // 1 + let s = LevelWidget::foo(wall, None, None, None); + assert_eq!(s, "┃"); + // 5 + let s = LevelWidget::foo(wall, None, wall, None); + assert_eq!(s, "┃"); + // 4 + let s = LevelWidget::foo(None, None, wall, None); + assert_eq!(s, "┃"); + + // 2 + let s = LevelWidget::foo(None, wall, None, None); + assert_eq!(s, "━"); + // 10 + let s = LevelWidget::foo(None, wall, None, wall); + assert_eq!(s, "━"); + + // 9 + let s = LevelWidget::foo(wall, None, None, wall); + assert_eq!(s, "┛"); + // 3 + let s = LevelWidget::foo(wall, wall, None, None); + assert_eq!(s, "┗"); + // 6 + let s = LevelWidget::foo( None, wall,wall,None); + assert_eq!(s, "┏"); + // 12 + let s = LevelWidget::foo(None, None, wall, wall); + assert_eq!(s, "┓"); +} +