From 2b8f7eebba43c810ddfc57924cb48f612979865c Mon Sep 17 00:00:00 2001 From: Joachim Lusiardi Date: Mon, 21 Oct 2024 21:52:07 +0200 Subject: [PATCH] unused potions now are collected in the inventory and can be consumed with the p key --- src/artifacts.rs | 18 +++++++++++++----- src/main.rs | 18 ++++++++++++++++-- src/player.rs | 25 +++++++++++++++++++++++++ 3 files changed, 54 insertions(+), 7 deletions(-) diff --git a/src/artifacts.rs b/src/artifacts.rs index 632b6a3..e7dc603 100644 --- a/src/artifacts.rs +++ b/src/artifacts.rs @@ -54,10 +54,12 @@ impl Artifact for Chest { } } +#[derive(Clone, Copy)] pub struct Potion { /// a potion that restores some health position: Position, health: usize, + was_collected: bool, } impl Potion { @@ -67,6 +69,7 @@ impl Potion { Self { position, health: rand::thread_rng().gen_range(min_health_gain..=max_health_gain), + was_collected: false, } } } @@ -89,15 +92,20 @@ impl Artifact for Potion { format!("picked up potion and gained {} health.", new - old).to_string(), ); self.health = 0; + self.was_collected = true; } else { - messages.insert( - 0, - "not using the potion because you're healthy.".to_string(), - ); + messages.insert(0, "move potion to inventory.".to_string()); + player.add_to_inventory(self); + self.was_collected = true; } } fn was_collected(&self) -> bool { - self.health == 0 + self.was_collected + } +} +impl Potion { + pub fn get_health(&self) -> usize { + self.health } } diff --git a/src/main.rs b/src/main.rs index 8e9ffe6..27dabc3 100644 --- a/src/main.rs +++ b/src/main.rs @@ -103,12 +103,13 @@ fn main() -> Result<()> { .style(Style::default().bg(Color::Blue)); frame.render_widget( Paragraph::new(format!( - "Health: {}/{}\nExp: {}\nGold: {}\nLevel: {}", + "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().get_immutable_position().get_level() + 1, + game.get_player().inventory_size(), )) .block(block) .wrap(Wrap { trim: true }), @@ -155,6 +156,19 @@ fn main() -> Result<()> { .to_string(), ); } + KeyCode::Char('p') => { + let gained_health = game.get_mutable_player().consume_inventory(); + if gained_health > 0 { + game.messages.insert( + 0, + format!( + "used a potion from inventory and gained {} health.", + gained_health + ) + .to_string(), + ); + } + } KeyCode::Char('q') => { break; } diff --git a/src/player.rs b/src/player.rs index b37e979..11f3248 100644 --- a/src/player.rs +++ b/src/player.rs @@ -1,6 +1,7 @@ use rand::Rng; use std::cmp::{max, min}; +use crate::artifacts::Potion; use crate::position::{HasPosition, Position}; pub struct Player { @@ -10,6 +11,7 @@ pub struct Player { max_life: i16, gold: usize, experience: usize, + inventory: Vec, } impl Player { @@ -21,6 +23,7 @@ impl Player { max_life, gold: 0, experience: 0, + inventory: vec![], } } pub fn get_name(&self) -> String { @@ -65,6 +68,25 @@ impl Player { pub fn damage(&self) -> usize { rand::thread_rng().gen_range(1..4) } + + pub fn add_to_inventory(&mut self, potion: &Potion) { + self.inventory.push(*potion); + } + + pub fn inventory_size(&self) -> usize { + self.inventory.len() + } + + pub fn consume_inventory(&mut self) -> usize { + if self.is_healthy() { + return 0; + } + if let Some(potion) = self.inventory.pop() { + self.change_life(potion.get_health() as i16); + return potion.get_health(); + } + 0 + } } impl HasPosition for Player { @@ -85,6 +107,7 @@ fn test_get_name() { max_life: 10, gold: 0, experience: 0, + inventory: vec![], }; assert_eq!(p.get_name(), "Teddy Tester"); } @@ -108,6 +131,7 @@ fn test_change_life() { max_life: 10, gold: 0, experience: 0, + inventory: vec![], }; assert_eq!(p.get_life(), 5); p.change_life(-2); @@ -141,6 +165,7 @@ fn test_max_life() { max_life: 10, gold: 0, experience: 0, + inventory: vec![], }; assert_eq!(p.get_max_life(), 10); }