Add variable artifacts

This commit is contained in:
2023-12-03 23:59:05 +01:00
parent 07c629bdaa
commit 2eeb95e3a3
2 changed files with 57 additions and 18 deletions

View File

@@ -1,16 +1,56 @@
use crate::position::Position;
use crate::player::Player;
pub struct Artifact {
position: Position,
pub trait Artifact {
//! An artifact that can be collected by the player
/// 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
fn collect(&mut self, player: &mut Player);
}
impl Artifact {
pub struct Chest {
/// a chest that contains some gold
position: Position,
gold: usize,
}
impl Chest {
pub fn new(position: Position) -> Self {
Artifact {
Self {
position,
gold: 10,
}
}
pub fn get_immutable_position(&self) -> &Position {
&self.position
}
impl Artifact for Chest {
fn get_immutable_position(&self) -> &Position { &self.position }
fn collect(&mut self, player: &mut Player) {
player.retrieve_gold(self.gold);
self.gold = 0;
}
}
pub struct Potion {
/// a potion that restores some health
position: Position,
health: usize,
}
impl Potion {
pub fn new(position: Position) -> Self {
Self {
position,
health: 5,
}
}
}
impl Artifact for Potion {
fn get_immutable_position(&self) -> &Position { &self.position }
fn collect(&mut self, player: &mut Player) {
player.change_life(self.health.try_into().unwrap());
self.health = 0;
}
}