feat: initial commit - item catalog, player and stats

This commit is contained in:
2026-08-11 14:36:51 +02:00
commit eacf0c1a01
11 changed files with 677 additions and 0 deletions

144
src/player.rs Normal file
View File

@@ -0,0 +1,144 @@
use std::fmt::format;
use crate::stats::{Stats, StatsTrait};
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);
}
impl Player {
pub fn new(name: String) -> Self {
Self {
name,
stats: Stats::new(),
next_level_exp: 10,
current_exp: 0,
}
}
}
impl PlayerTrait for Player {
fn get_name(&self) -> String {
self.name.clone()
}
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();
}
}