Player is drawn

This commit is contained in:
Joachim Lusiardi 2023-11-11 18:09:12 +01:00
parent 732c68448f
commit 9101e0ae6d
4 changed files with 56 additions and 37 deletions

View File

@ -1,14 +1,16 @@
use ratatui::prelude::*; use ratatui::prelude::*;
use ratatui::widgets::Widget; use ratatui::widgets::Widget;
use crate::game::Game;
use crate::level::{Level, LevelElement}; use crate::level::{Level, LevelElement};
use crate::player::Player;
const FG_BROWN: Color = Color::Rgb(186, 74, 0); const FG_BROWN: Color = Color::Rgb(186, 74, 0);
pub struct LevelWidget { pub struct LevelWidget<'a> {
pub content: Level, pub game: &'a Game,
} }
impl LevelWidget { impl<'a> LevelWidget<'a> {
fn set_cell(&self, buf: &mut Buffer, x: u16, y: u16, symbol: &str, fg: Color, bg: Color) { fn set_cell(&self, buf: &mut Buffer, x: u16, y: u16, symbol: &str, fg: Color, bg: Color) {
buf. buf.
get_mut(x, y). get_mut(x, y).
@ -17,8 +19,7 @@ impl LevelWidget {
set_fg(fg); set_fg(fg);
} }
// ┃ ━ ┏ ┓ ┗ ┛ ┣ ┫ ┳ ┻ ╋ fn calculate_wall(top: Option<LevelElement>, right: Option<LevelElement>, bottom: Option<LevelElement>, left: Option<LevelElement>) -> String {
fn foo(top: Option<LevelElement>, right: Option<LevelElement>, bottom: Option<LevelElement>, left: Option<LevelElement>) -> String {
let mut val = 0; let mut val = 0;
val += match top { val += match top {
Some(n) => { Some(n) => {
@ -56,12 +57,6 @@ impl LevelWidget {
} }
None => 0 None => 0
}; };
// 0 ╋ 5 ┃ 9 ┛
// 1 ┃ 6 ┏ 10 ━
// 2 ━ 7 ┣ 11 ┻
// 3 ┗
// 4 ┃ 8 ━ 12 ┓
[ [
"", "", "", "", "", "", "", "",
"", "", "", "", "", "", "", "",
@ -71,24 +66,30 @@ impl LevelWidget {
} }
} }
impl Widget for LevelWidget { impl<'a> Widget for LevelWidget<'a> {
fn render(self, area: Rect, buf: &mut Buffer) { fn render(self, area: Rect, buf: &mut Buffer) {
let player = &self.game.player;
let player_pos = player.get_position();
let level = &self.game.levels[player_pos.get_level() as usize];
let al = area.left(); let al = area.left();
let at = area.top(); let at = area.top();
// draw the level elements
for x in al..area.right() { for x in al..area.right() {
for y in at..area.bottom() { for y in at..area.bottom() {
let level_x = (x - al) as i16; let level_x = (x - al) as i16;
let level_y = (y - at) as i16; let level_y = (y - at) as i16;
match self.content.get_element(level_x, level_y) { match level.get_element(level_x, level_y) {
None => {} None => {}
Some(e) => { Some(e) => {
match e { match e {
LevelElement::Wall => { LevelElement::Wall => {
let top = self.content.get_element(level_x, level_y-1); let top = level.get_element(level_x, level_y - 1);
let right = self.content.get_element(level_x+1, level_y); let right = level.get_element(level_x + 1, level_y);
let bottom = self.content.get_element(level_x, level_y+1); let bottom = level.get_element(level_x, level_y + 1);
let left = self.content.get_element(level_x-1, level_y); let left = level.get_element(level_x - 1, level_y);
let symbol = LevelWidget::foo(top, right, bottom, left); let symbol = LevelWidget::calculate_wall(top, right, bottom, left);
self.set_cell(buf, x, y, symbol.as_str(), FG_BROWN, Color::Gray); self.set_cell(buf, x, y, symbol.as_str(), FG_BROWN, Color::Gray);
} }
LevelElement::Floor => { LevelElement::Floor => {
@ -105,6 +106,9 @@ impl Widget for LevelWidget {
} }
} }
} }
// draw player
self.set_cell(buf, al + player_pos.get_x(), at + player_pos.get_y(), "8", Color::Red, Color::Gray);
} }
} }
@ -112,40 +116,40 @@ impl Widget for LevelWidget {
fn test_get_wall_symbol() { fn test_get_wall_symbol() {
let wall = Some(LevelElement::Wall); let wall = Some(LevelElement::Wall);
// 0 // 0
let s = LevelWidget::foo(None, None, None, None); let s = LevelWidget::calculate_wall(None, None, None, None);
assert_eq!(s, ""); assert_eq!(s, "");
// 15 // 15
let s = LevelWidget::foo(wall, wall, wall, wall); let s = LevelWidget::calculate_wall(wall, wall, wall, wall);
assert_eq!(s, ""); assert_eq!(s, "");
// 1 // 1
let s = LevelWidget::foo(wall, None, None, None); let s = LevelWidget::calculate_wall(wall, None, None, None);
assert_eq!(s, ""); assert_eq!(s, "");
// 5 // 5
let s = LevelWidget::foo(wall, None, wall, None); let s = LevelWidget::calculate_wall(wall, None, wall, None);
assert_eq!(s, ""); assert_eq!(s, "");
// 4 // 4
let s = LevelWidget::foo(None, None, wall, None); let s = LevelWidget::calculate_wall(None, None, wall, None);
assert_eq!(s, ""); assert_eq!(s, "");
// 2 // 2
let s = LevelWidget::foo(None, wall, None, None); let s = LevelWidget::calculate_wall(None, wall, None, None);
assert_eq!(s, ""); assert_eq!(s, "");
// 10 // 10
let s = LevelWidget::foo(None, wall, None, wall); let s = LevelWidget::calculate_wall(None, wall, None, wall);
assert_eq!(s, ""); assert_eq!(s, "");
// 9 // 9
let s = LevelWidget::foo(wall, None, None, wall); let s = LevelWidget::calculate_wall(wall, None, None, wall);
assert_eq!(s, ""); assert_eq!(s, "");
// 3 // 3
let s = LevelWidget::foo(wall, wall, None, None); let s = LevelWidget::calculate_wall(wall, wall, None, None);
assert_eq!(s, ""); assert_eq!(s, "");
// 6 // 6
let s = LevelWidget::foo( None, wall,wall,None); let s = LevelWidget::calculate_wall(None, wall, wall, None);
assert_eq!(s, ""); assert_eq!(s, "");
// 12 // 12
let s = LevelWidget::foo(None, None, wall, wall); let s = LevelWidget::calculate_wall(None, None, wall, wall);
assert_eq!(s, ""); assert_eq!(s, "");
} }

View File

@ -53,7 +53,7 @@ fn main() -> Result<()> {
width: level::LEVEL_WIDTH as u16, width: level::LEVEL_WIDTH as u16,
height: level::LEVEL_HEIGHT as u16, height: level::LEVEL_HEIGHT as u16,
}; };
frame.render_widget(LevelWidget { content: g.levels[0] }, map_area); frame.render_widget(LevelWidget { game: &g }, map_area);
let stats_area = Rect { let stats_area = Rect {
x: area.x + 50, x: area.x + 50,

View File

@ -25,6 +25,9 @@ impl Player {
pub fn change_life(&mut self, by: i16) { pub fn change_life(&mut self, by: i16) {
self.life = max(0, min(self.max_life, self.life + by)); self.life = max(0, min(self.max_life, self.life + by));
} }
pub fn get_position(&self) -> Position {
self.position
}
} }
#[test] #[test]

View File

@ -1,15 +1,27 @@
#[derive(Copy, Clone, Debug, PartialEq)]
pub struct Position { pub struct Position {
level: u8, level: u8,
x: u8, x: u16,
y: u8, y: u16,
} }
impl Position { impl Position {
pub fn new(level: u8, x: u8, y: u8) -> Self { pub fn new(level: u8, x: u16, y: u16) -> Self {
Self { Self {
level, level,
x, x,
y, y,
} }
} }
pub fn get_level(&self) -> u8 {
self.level
}
pub fn get_x(&self) -> u16 {
self.x
}
pub fn get_y(&self) -> u16 {
self.y
}
} }