refactor: getter-traits durch pub-felder ersetzen, print_box wiederverwenden

This commit is contained in:
2026-08-11 14:39:59 +02:00
parent eacf0c1a01
commit 290d985bc6
5 changed files with 88 additions and 321 deletions

View File

@@ -1,20 +1,11 @@
use std::fmt::format;
use crate::stats::{Stats, StatsTrait};
use crate::items::print_box;
use crate::stats::Stats;
pub struct Player {
name: String,
stats: Stats,
next_level_exp: usize,
current_exp: usize,
}
pub trait PlayerTrait {
fn get_name(&self) -> String;
fn set_name(&mut self, name: String);
fn get_current_exp(&self) -> usize;
fn get_next_level_exp(&self) -> usize;
fn show_stats(&self);
pub name: String,
pub stats: Stats,
pub next_level_exp: usize,
pub current_exp: usize,
}
impl Player {
@@ -26,119 +17,21 @@ impl Player {
current_exp: 0,
}
}
}
impl PlayerTrait for Player {
fn get_name(&self) -> String {
self.name.clone()
}
pub fn show_stats(&self) {
let stats: &Stats = &self.stats;
fn set_name(&mut self, name: String) {
self.name = name;
}
fn show_stats(&self) {
let name: String = format!("Name: {}", self.get_name());
let level: String = format!(
"Level: {} ({}/{})",
self.get_level(),
self.get_current_exp(),
self.get_next_level_exp()
);
let strength: String = format!("Strength: {}", self.get_strengh());
let dexterity: String = format!("Dexterity: {}", self.get_dexterity());
let constitution: String = format!("Constitution: {}", self.get_constitution());
let charisma: String = format!("Charisma: {}", self.get_charisma());
let luck: String = format!("Luck: {}", self.get_luck());
let stats_data: [&String; 7] = [
&name,
&level,
&strength,
&dexterity,
&constitution,
&charisma,
&luck,
];
let mut longest: usize = 0;
for data in stats_data {
let length: usize = data.chars().count();
longest = length.max(longest);
}
let spacer: String = format!("+-{}-+", "-".repeat(longest));
println!("{}", spacer);
for data in stats_data {
let length: usize = data.chars().count();
println!("| {}{} |", data, " ".repeat(longest - length));
}
println!("{}", spacer);
}
fn get_current_exp(&self) -> usize {
self.current_exp
}
fn get_next_level_exp(&self) -> usize {
self.next_level_exp
}
}
impl StatsTrait for Player {
fn get_max_health(&self) -> f32 {
self.stats.get_max_health()
}
fn get_current_health(&self) -> f32 {
self.stats.get_current_health()
}
fn set_current_health(&mut self, value: f32) {
self.stats.set_current_health(value);
}
fn is_dead(&self) -> bool {
self.stats.is_dead()
}
fn get_max_stamina(&self) -> f32 {
self.stats.get_max_stamina()
}
fn get_current_stamina(&self) -> f32 {
self.stats.get_current_stamina()
}
fn set_current_stamina(&mut self, value: f32) {
self.stats.set_current_stamina(value);
}
fn get_strengh(&self) -> usize {
self.stats.get_strengh()
}
fn get_dexterity(&self) -> usize {
self.stats.get_dexterity()
}
fn get_constitution(&self) -> usize {
self.stats.get_constitution()
}
fn get_charisma(&self) -> usize {
self.stats.get_charisma()
}
fn get_luck(&self) -> usize {
self.stats.get_luck()
}
fn get_level(&self) -> usize {
self.stats.get_level()
}
fn level_up(&mut self) {
self.stats.level_up();
print_box(&[
format!("Name: {}", self.name),
format!(
"Level: {} ({}/{})",
stats.level, self.current_exp, self.next_level_exp
),
format!("Strength: {}", stats.strength),
format!("Dexterity: {}", stats.dexterity),
format!("Constitution: {}", stats.constitution),
format!("Charisma: {}", stats.charisma),
format!("Luck: {}", stats.luck),
]);
}
}