From 9101e0ae6da553c60788a761b6e111b541a1065b Mon Sep 17 00:00:00 2001 From: Joachim Lusiardi Date: Sat, 11 Nov 2023 18:09:12 +0100 Subject: [PATCH] Player is drawn --- src/level_widget.rs | 66 ++++++++++++++++++++++++--------------------- src/main.rs | 2 +- src/player.rs | 7 +++-- src/position.rs | 18 ++++++++++--- 4 files changed, 56 insertions(+), 37 deletions(-) diff --git a/src/level_widget.rs b/src/level_widget.rs index a7aecda..c1d95c0 100644 --- a/src/level_widget.rs +++ b/src/level_widget.rs @@ -1,14 +1,16 @@ use ratatui::prelude::*; use ratatui::widgets::Widget; +use crate::game::Game; use crate::level::{Level, LevelElement}; +use crate::player::Player; const FG_BROWN: Color = Color::Rgb(186, 74, 0); -pub struct LevelWidget { - pub content: Level, +pub struct LevelWidget<'a> { + 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) { buf. get_mut(x, y). @@ -17,8 +19,7 @@ impl LevelWidget { set_fg(fg); } - // ┃ ━ ┏ ┓ ┗ ┛ ┣ ┫ ┳ ┻ ╋ - fn foo(top: Option, right: Option, bottom: Option, left: Option) -> String { + fn calculate_wall(top: Option, right: Option, bottom: Option, left: Option) -> String { let mut val = 0; val += match top { Some(n) => { @@ -56,12 +57,6 @@ impl LevelWidget { } 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) { + 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 at = 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 self.content.get_element(level_x, level_y) { + 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 { 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); + let top = level.get_element(level_x, level_y - 1); + let right = level.get_element(level_x + 1, level_y); + let bottom = level.get_element(level_x, level_y + 1); + let left = level.get_element(level_x - 1, level_y); + let symbol = LevelWidget::calculate_wall(top, right, bottom, left); self.set_cell(buf, x, y, symbol.as_str(), FG_BROWN, Color::Gray); } 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() { let wall = Some(LevelElement::Wall); // 0 - let s = LevelWidget::foo(None, None, None, None); + let s = LevelWidget::calculate_wall(None, None, None, None); assert_eq!(s, "╋"); // 15 - let s = LevelWidget::foo(wall, wall, wall, wall); + let s = LevelWidget::calculate_wall(wall, wall, wall, wall); assert_eq!(s, "╋"); // 1 - let s = LevelWidget::foo(wall, None, None, None); + let s = LevelWidget::calculate_wall(wall, None, None, None); assert_eq!(s, "┃"); // 5 - let s = LevelWidget::foo(wall, None, wall, None); + let s = LevelWidget::calculate_wall(wall, None, wall, None); assert_eq!(s, "┃"); // 4 - let s = LevelWidget::foo(None, None, wall, None); + let s = LevelWidget::calculate_wall(None, None, wall, None); assert_eq!(s, "┃"); // 2 - let s = LevelWidget::foo(None, wall, None, None); + let s = LevelWidget::calculate_wall(None, wall, None, None); assert_eq!(s, "━"); // 10 - let s = LevelWidget::foo(None, wall, None, wall); + let s = LevelWidget::calculate_wall(None, wall, None, wall); assert_eq!(s, "━"); // 9 - let s = LevelWidget::foo(wall, None, None, wall); + let s = LevelWidget::calculate_wall(wall, None, None, wall); assert_eq!(s, "┛"); // 3 - let s = LevelWidget::foo(wall, wall, None, None); + let s = LevelWidget::calculate_wall(wall, wall, None, None); assert_eq!(s, "┗"); // 6 - let s = LevelWidget::foo( None, wall,wall,None); + let s = LevelWidget::calculate_wall(None, wall, wall, None); assert_eq!(s, "┏"); // 12 - let s = LevelWidget::foo(None, None, wall, wall); + let s = LevelWidget::calculate_wall(None, None, wall, wall); assert_eq!(s, "┓"); } diff --git a/src/main.rs b/src/main.rs index d2c6a45..a642ff0 100644 --- a/src/main.rs +++ b/src/main.rs @@ -53,7 +53,7 @@ fn main() -> Result<()> { width: level::LEVEL_WIDTH 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 { x: area.x + 50, diff --git a/src/player.rs b/src/player.rs index 8f1ed8f..51a6f05 100644 --- a/src/player.rs +++ b/src/player.rs @@ -20,11 +20,14 @@ impl Player { pub fn get_name(&self) -> String { return self.name.clone(); } - pub fn get_life(&self) -> i16 {self.life} - pub fn get_max_life(&self) -> i16 {self.max_life} + pub fn get_life(&self) -> i16 { self.life } + pub fn get_max_life(&self) -> i16 { self.max_life } pub fn change_life(&mut self, by: i16) { self.life = max(0, min(self.max_life, self.life + by)); } + pub fn get_position(&self) -> Position { + self.position + } } #[test] diff --git a/src/position.rs b/src/position.rs index 92e21d7..941ec75 100644 --- a/src/position.rs +++ b/src/position.rs @@ -1,15 +1,27 @@ +#[derive(Copy, Clone, Debug, PartialEq)] pub struct Position { level: u8, - x: u8, - y: u8, + x: u16, + y: u16, } impl Position { - pub fn new(level: u8, x: u8, y: u8) -> Self { + pub fn new(level: u8, x: u16, y: u16) -> Self { Self { level, x, 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 + } } \ No newline at end of file