From 9ae713b6c85c04bb84a53382d5de4aac8dc95c49 Mon Sep 17 00:00:00 2001 From: Joachim Lusiardi Date: Tue, 22 Oct 2024 13:39:43 +0200 Subject: [PATCH] make variable inventory slots --- src/artifacts.rs | 11 ++++++----- src/main.rs | 5 +++-- src/player.rs | 8 +++++--- 3 files changed, 14 insertions(+), 10 deletions(-) diff --git a/src/artifacts.rs b/src/artifacts.rs index cb2aafd..e0e0d50 100644 --- a/src/artifacts.rs +++ b/src/artifacts.rs @@ -72,15 +72,21 @@ impl Potion { was_collected: false, } } + + pub fn get_health(&self) -> usize { + self.health + } } impl Artifact for Potion { fn get_representation(&self) -> (&str, Color) { ("P", Color::Green) } + fn get_immutable_position(&self) -> &Position { &self.position } + fn collect(&mut self, player: &mut Player, messages: &mut Vec) { // only consume potion of the player can gain at least one health point if !player.is_healthy() { @@ -105,8 +111,3 @@ impl Artifact for Potion { self.was_collected } } -impl Potion { - pub fn get_health(&self) -> usize { - self.health - } -} diff --git a/src/main.rs b/src/main.rs index 37bb337..a0627b8 100644 --- a/src/main.rs +++ b/src/main.rs @@ -103,13 +103,14 @@ fn main() -> Result<()> { .style(Style::default().bg(Color::Blue)); frame.render_widget( Paragraph::new(format!( - "Health: {}/{}\nExp: {}\nGold: {}\nLevel: {}\nInventory: {}/10", + "Health: {}/{}\nExp: {}\nGold: {}\nLevel: {}\nInventory: {}/{}", game.get_player().get_life(), game.get_player().get_max_life(), game.get_player().get_experience(), game.get_player().get_gold(), game.get_player().get_immutable_position().get_level() + 1, - game.get_player().inventory_size(), + game.get_player().inventory_size().0, + game.get_player().inventory_size().1, )) .block(block) .wrap(Wrap { trim: true }), diff --git a/src/player.rs b/src/player.rs index 5263a7a..e0eb10a 100644 --- a/src/player.rs +++ b/src/player.rs @@ -12,6 +12,7 @@ pub struct Player { gold: usize, experience: usize, inventory: Vec, + inventory_slots: usize, } impl Player { @@ -24,6 +25,7 @@ impl Player { gold: 0, experience: 0, inventory: vec![], + inventory_slots: 2, } } pub fn get_name(&self) -> String { @@ -70,7 +72,7 @@ impl Player { } pub fn add_to_inventory(&mut self, potion: &Potion) -> bool { - if self.inventory.len() < 10 { + if self.inventory.len() < self.inventory_slots { self.inventory.push(*potion); true } else { @@ -78,8 +80,8 @@ impl Player { } } - pub fn inventory_size(&self) -> usize { - self.inventory.len() + pub fn inventory_size(&self) -> (usize, usize) { + (self.inventory.len(), self.inventory_slots) } pub fn consume_inventory(&mut self) -> usize {