forgotten file
This commit is contained in:
parent
51419ff0b8
commit
732c68448f
|
@ -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<LevelElement>, right: Option<LevelElement>, bottom: Option<LevelElement>, left: Option<LevelElement>) -> 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, "┓");
|
||||
}
|
||||
|
Loading…
Reference in New Issue