Player is drawn
This commit is contained in:
parent
732c68448f
commit
9101e0ae6d
|
@ -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<LevelElement>, right: Option<LevelElement>, bottom: Option<LevelElement>, left: Option<LevelElement>) -> String {
|
||||
fn calculate_wall(top: Option<LevelElement>, right: Option<LevelElement>, bottom: Option<LevelElement>, left: Option<LevelElement>) -> 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) {
|
||||
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, "┓");
|
||||
}
|
||||
|
||||
|
|
|
@ -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,
|
||||
|
|
|
@ -25,6 +25,9 @@ impl Player {
|
|||
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]
|
||||
|
|
|
@ -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
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue