feat: initial commit - item catalog, player and stats
This commit is contained in:
144
src/player.rs
Normal file
144
src/player.rs
Normal 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();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user