unused potions now are collected in the inventory and can be consumed with the p key
This commit is contained in:
@@ -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);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user