limit inventory to 10 slots

This commit is contained in:
Joachim Lusiardi 2024-10-21 21:59:55 +02:00
parent 2b8f7eebba
commit 5949b2a2d6
3 changed files with 11 additions and 5 deletions

View File

@ -93,10 +93,11 @@ impl Artifact for Potion {
);
self.health = 0;
self.was_collected = true;
} else {
} else if player.add_to_inventory(self) {
messages.insert(0, "move potion to inventory.".to_string());
player.add_to_inventory(self);
self.was_collected = true;
} else {
messages.insert(0, "inventory is full.".to_string());
}
}

View File

@ -103,7 +103,7 @@ fn main() -> Result<()> {
.style(Style::default().bg(Color::Blue));
frame.render_widget(
Paragraph::new(format!(
"Health: {}/{}\nExp: {}\nGold: {}\nLevel: {}\nInventory: {}",
"Health: {}/{}\nExp: {}\nGold: {}\nLevel: {}\nInventory: {}/10",
game.get_player().get_life(),
game.get_player().get_max_life(),
game.get_player().get_experience(),

View File

@ -69,8 +69,13 @@ impl Player {
rand::thread_rng().gen_range(1..4)
}
pub fn add_to_inventory(&mut self, potion: &Potion) {
pub fn add_to_inventory(&mut self, potion: &Potion) -> bool {
if self.inventory.len() < 10 {
self.inventory.push(*potion);
true
} else {
false
}
}
pub fn inventory_size(&self) -> usize {