unused potions now are collected in the inventory and can be consumed with the p key

This commit is contained in:
Joachim Lusiardi 2024-10-21 21:52:07 +02:00
parent a0635de65a
commit 2b8f7eebba
3 changed files with 54 additions and 7 deletions

View File

@ -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
}
}

View File

@ -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;
}

View File

@ -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<Potion>,
}
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);
}