el_diablo/src/artifacts.rs

76 lines
1.8 KiB
Rust
Raw Normal View History

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
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
fn collect(&mut self, player: &mut Player);
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 {
2023-12-03 23:59:05 +01:00
Self {
2023-12-03 13:44:29 +01:00
position,
2023-12-03 23:59:05 +01:00
gold: 10,
2023-12-03 13:44:29 +01:00
}
}
2023-12-03 23:59:05 +01:00
}
impl Artifact for Chest {
fn get_representation(&self) -> (&str, Color) {
("C", Color::Blue)
}
2023-12-03 23:59:05 +01:00
fn get_immutable_position(&self) -> &Position { &self.position }
fn collect(&mut self, player: &mut Player) {
player.retrieve_gold(self.gold);
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
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_representation(&self) -> (&str, Color) {
("P", Color::Green)
}
2023-12-03 23:59:05 +01:00
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;
}
2023-12-17 16:14:17 +01:00
fn was_collected(&self) -> bool {
self.health == 0
}
2023-12-03 23:59:05 +01:00
}