2024-10-21 08:27:55 +02:00
|
|
|
use rand::Rng;
|
2023-12-09 20:05:39 +01:00
|
|
|
use ratatui::style::Color;
|
2023-12-18 16:11:19 +01:00
|
|
|
|
2023-12-03 23:59:05 +01:00
|
|
|
use crate::player::Player;
|
2023-12-18 16:11:19 +01:00
|
|
|
use crate::position::Position;
|
2023-12-03 13:44:29 +01:00
|
|
|
|
2023-12-03 23:59:05 +01:00
|
|
|
pub trait Artifact {
|
|
|
|
//! An artifact that can be collected by the player
|
2023-12-09 20:05:39 +01:00
|
|
|
fn get_representation(&self) -> (&str, Color);
|
2023-12-03 23:59:05 +01:00
|
|
|
/// get the position of the artifact in the level
|
|
|
|
fn get_immutable_position(&self) -> &Position;
|
|
|
|
/// call to apply the effects of the artifact to the player
|
2023-12-31 15:59:52 +01:00
|
|
|
fn collect(&mut self, player: &mut Player, messages: &mut Vec<String>);
|
2023-12-17 22:00:42 +01:00
|
|
|
/// returns if the artifact was collected and can be removed from the level
|
2023-12-17 16:14:17 +01:00
|
|
|
fn was_collected(&self) -> bool;
|
2023-12-03 23:59:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
pub struct Chest {
|
|
|
|
/// a chest that contains some gold
|
2023-12-03 13:44:29 +01:00
|
|
|
position: Position,
|
2023-12-03 23:59:05 +01:00
|
|
|
gold: usize,
|
2023-12-03 13:44:29 +01:00
|
|
|
}
|
|
|
|
|
2023-12-03 23:59:05 +01:00
|
|
|
impl Chest {
|
2023-12-03 13:44:29 +01:00
|
|
|
pub fn new(position: Position) -> Self {
|
2024-10-21 20:37:52 +02:00
|
|
|
let min_gold = 10 * (position.get_level() + 1);
|
|
|
|
let max_gold = min_gold + 10 * position.get_level();
|
2023-12-03 23:59:05 +01:00
|
|
|
Self {
|
2023-12-03 13:44:29 +01:00
|
|
|
position,
|
2024-10-21 08:27:55 +02:00
|
|
|
gold: rand::thread_rng().gen_range(min_gold..=max_gold),
|
2023-12-03 13:44:29 +01:00
|
|
|
}
|
|
|
|
}
|
2023-12-03 23:59:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Artifact for Chest {
|
2023-12-09 20:05:39 +01:00
|
|
|
fn get_representation(&self) -> (&str, Color) {
|
|
|
|
("C", Color::Blue)
|
|
|
|
}
|
|
|
|
|
2024-10-20 14:59:20 +02:00
|
|
|
fn get_immutable_position(&self) -> &Position {
|
|
|
|
&self.position
|
|
|
|
}
|
2023-12-31 15:59:52 +01:00
|
|
|
fn collect(&mut self, player: &mut Player, messages: &mut Vec<String>) {
|
2023-12-03 23:59:05 +01:00
|
|
|
player.retrieve_gold(self.gold);
|
2024-10-20 14:59:20 +02:00
|
|
|
messages.insert(
|
|
|
|
0,
|
|
|
|
format!("opened chest and collected {} gold.", self.gold).to_string(),
|
|
|
|
);
|
2023-12-03 23:59:05 +01:00
|
|
|
self.gold = 0;
|
2023-12-03 13:44:29 +01:00
|
|
|
}
|
2023-12-17 16:14:17 +01:00
|
|
|
|
|
|
|
fn was_collected(&self) -> bool {
|
|
|
|
self.gold == 0
|
|
|
|
}
|
2023-12-03 13:44:29 +01:00
|
|
|
}
|
2023-12-03 23:59:05 +01:00
|
|
|
|
2024-10-21 21:52:07 +02:00
|
|
|
#[derive(Clone, Copy)]
|
2023-12-03 23:59:05 +01:00
|
|
|
pub struct Potion {
|
|
|
|
/// a potion that restores some health
|
|
|
|
position: Position,
|
|
|
|
health: usize,
|
2024-10-21 21:52:07 +02:00
|
|
|
was_collected: bool,
|
2023-12-03 23:59:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Potion {
|
|
|
|
pub fn new(position: Position) -> Self {
|
2024-10-21 08:27:55 +02:00
|
|
|
let min_health_gain = 5 + position.get_level();
|
|
|
|
let max_health_gain = min_health_gain + 3 * position.get_level();
|
2023-12-03 23:59:05 +01:00
|
|
|
Self {
|
|
|
|
position,
|
2024-10-21 08:27:55 +02:00
|
|
|
health: rand::thread_rng().gen_range(min_health_gain..=max_health_gain),
|
2024-10-21 21:52:07 +02:00
|
|
|
was_collected: false,
|
2023-12-03 23:59:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Artifact for Potion {
|
2023-12-09 20:05:39 +01:00
|
|
|
fn get_representation(&self) -> (&str, Color) {
|
|
|
|
("P", Color::Green)
|
|
|
|
}
|
2024-10-20 14:59:20 +02:00
|
|
|
fn get_immutable_position(&self) -> &Position {
|
|
|
|
&self.position
|
|
|
|
}
|
2023-12-31 15:59:52 +01:00
|
|
|
fn collect(&mut self, player: &mut Player, messages: &mut Vec<String>) {
|
2023-12-30 16:44:23 +01:00
|
|
|
// only consume potion of the player can gain at least one health point
|
|
|
|
if !player.is_healthy() {
|
2023-12-31 15:59:52 +01:00
|
|
|
let old = player.get_life();
|
2023-12-30 16:44:23 +01:00
|
|
|
player.change_life(self.health.try_into().unwrap());
|
2023-12-31 15:59:52 +01:00
|
|
|
let new = player.get_life();
|
2024-10-20 14:59:20 +02:00
|
|
|
messages.insert(
|
|
|
|
0,
|
2024-10-21 08:26:51 +02:00
|
|
|
format!("picked up potion and gained {} health.", new - old).to_string(),
|
2024-10-20 14:59:20 +02:00
|
|
|
);
|
2023-12-30 16:44:23 +01:00
|
|
|
self.health = 0;
|
2024-10-21 21:52:07 +02:00
|
|
|
self.was_collected = true;
|
2023-12-31 15:59:52 +01:00
|
|
|
} else {
|
2024-10-21 21:52:07 +02:00
|
|
|
messages.insert(0, "move potion to inventory.".to_string());
|
|
|
|
player.add_to_inventory(self);
|
|
|
|
self.was_collected = true;
|
2023-12-30 16:44:23 +01:00
|
|
|
}
|
2023-12-03 23:59:05 +01:00
|
|
|
}
|
2023-12-17 16:14:17 +01:00
|
|
|
|
|
|
|
fn was_collected(&self) -> bool {
|
2024-10-21 21:52:07 +02:00
|
|
|
self.was_collected
|
|
|
|
}
|
|
|
|
}
|
|
|
|
impl Potion {
|
|
|
|
pub fn get_health(&self) -> usize {
|
|
|
|
self.health
|
2023-12-17 16:14:17 +01:00
|
|
|
}
|
2024-10-20 14:59:20 +02:00
|
|
|
}
|