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

108
src/items.rs Normal file
View File

@@ -0,0 +1,108 @@
use serde::Deserialize;
use serde::de::DeserializeOwned;
pub mod armor;
pub mod weapon;
pub use armor::ItemArmor;
pub use weapon::ItemWeapon;
// === ENUMS ===
#[derive(Clone, Debug, Deserialize)]
pub enum ItemType {
Weapon,
OffHand,
Helmet,
Chestplate,
Gloves,
Boots,
Consumable,
Currency,
}
// === STRUCTS ===
#[derive(Deserialize)]
pub struct Item {
id: String,
name: String,
value: u32,
item_type: ItemType,
}
/// Rohform einer Item-Datei. Jede TOML nutzt `[[items]]` als Wurzel,
/// dadurch bedient dieser eine Wrapper alle Dateien.
#[derive(Deserialize)]
struct ItemFile<T> {
items: Vec<T>,
}
/// Laufzeit-Registry: einmal beim Start laden, danach als `&ItemCatalog` durchreichen.
// TODO: OffHand, Consumable und Currency haben noch keine Structs/TOMLs — hier ergaenzen.
pub struct ItemCatalog {
pub weapons: Vec<ItemWeapon>,
pub armors: Vec<ItemArmor>,
}
// === TRAITS ===
pub trait ItemTrait {
fn get_id(&self) -> String;
fn get_name(&self) -> String;
fn get_value(&self) -> u32;
fn get_type(&self) -> ItemType;
}
// === IMPLEMENTS ===
impl ItemTrait for Item {
fn get_id(&self) -> String {
self.id.clone()
}
fn get_name(&self) -> String {
self.name.clone()
}
fn get_value(&self) -> u32 {
self.value
}
fn get_type(&self) -> ItemType {
self.item_type.clone()
}
}
// === FUNCTIONS ===
pub fn load_items() -> ItemCatalog {
ItemCatalog {
weapons: parse(include_str!("items/weapons.toml"), "weapons.toml"),
armors: parse(include_str!("items/armors.toml"), "armors.toml"),
}
}
fn parse<T: DeserializeOwned>(raw: &str, file: &str) -> Vec<T> {
let parsed: ItemFile<T> =
toml::from_str(raw).unwrap_or_else(|e| panic!("{} ist kaputt:\n{}", file, e));
parsed.items
}
/// Rahmen um beliebig viele Zeilen. Wird von jedem `print_values` genutzt.
pub(crate) fn print_box(lines: &[String]) {
let longest: usize = lines.iter().map(|l| l.chars().count()).max().unwrap_or(0);
println!("+-{}-+", "-".repeat(longest));
for line in lines {
println!("| {}{} |", line, " ".repeat(longest - line.chars().count()));
}
println!("+-{}-+", "-".repeat(longest));
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn item_files_parse() {
let catalog: ItemCatalog = load_items();
assert!(!catalog.weapons.is_empty());
assert!(!catalog.armors.is_empty());
}
}

46
src/items/armor.rs Normal file
View File

@@ -0,0 +1,46 @@
use serde::Deserialize;
use super::{Item, ItemTrait, ItemType, print_box};
#[derive(Deserialize)]
pub struct ItemArmor {
armor: u32,
#[serde(flatten)]
item: Item,
}
impl ItemTrait for ItemArmor {
fn get_id(&self) -> String {
self.item.get_id()
}
fn get_name(&self) -> String {
self.item.get_name()
}
fn get_value(&self) -> u32 {
self.item.get_value()
}
fn get_type(&self) -> ItemType {
self.item.get_type()
}
}
impl ItemArmor {
pub fn get_armor(&self) -> u32 {
self.armor
}
pub fn print_values(&self) {
let lines: Vec<String> = vec![
format!("Stats for {} ", self.get_name()),
format!("ID: {}", self.get_id()),
format!("Slot: {:?}", self.get_type()),
format!("Armor: {}", self.get_armor()),
format!("Value: {}", self.get_value()),
];
print_box(&lines);
}
}

13
src/items/armors.toml Normal file
View File

@@ -0,0 +1,13 @@
[[items]]
id = "iron_helmet"
name = "Iron Helmet"
armor = 4
value = 6
item_type = "Helmet"
[[items]]
id = "leather_boots"
name = "Leather Boots"
armor = 2
value = 3
item_type = "Boots"

62
src/items/weapon.rs Normal file
View File

@@ -0,0 +1,62 @@
use serde::Deserialize;
use super::{Item, ItemTrait, ItemType, print_box};
#[derive(Deserialize)]
pub struct ItemWeapon {
base_damage: u32,
critical_factor: f32,
#[serde(flatten)]
item: Item,
}
impl ItemTrait for ItemWeapon {
fn get_id(&self) -> String {
self.item.get_id()
}
fn get_name(&self) -> String {
self.item.get_name()
}
fn get_value(&self) -> u32 {
self.item.get_value()
}
fn get_type(&self) -> ItemType {
self.item.get_type()
}
}
impl ItemWeapon {
pub fn get_base_damage(&self) -> u32 {
self.base_damage
}
pub fn get_critical_factor(&self) -> f32 {
self.critical_factor
}
pub fn get_critical_factor_readable(&self) -> String {
let factor: i32 = ((self.get_critical_factor() - 1.0) * 100.0).round() as i32;
format!("{}%", factor)
}
pub fn print_values(&self) {
let crit_dmg: u32 =
(self.get_base_damage() as f32 * self.get_critical_factor()).ceil() as u32;
let lines: Vec<String> = vec![
format!("Stats for {} ", self.get_name()),
format!("ID: {}", self.get_id()),
format!("Base Damage: {} ({})", self.get_base_damage(), crit_dmg),
format!(
"Critical Factor: {}",
self.get_critical_factor_readable()
),
format!("Value: {}", self.get_value()),
];
print_box(&lines);
}
}

15
src/items/weapons.toml Normal file
View File

@@ -0,0 +1,15 @@
[[items]]
id = "iron_sword"
name = "Iron Sword"
base_damage = 10
critical_factor = 1.1
value = 5
item_type = "Weapon"
[[items]]
id = "iron_dagger"
name = "Iron Dagger"
base_damage = 8
critical_factor = 1.5
value = 3
item_type = "Weapon"

20
src/main.rs Normal file
View File

@@ -0,0 +1,20 @@
mod items;
mod player;
mod stats;
use items::*;
use player::*;
fn main() {
let catalog: ItemCatalog = load_items();
for weapon in &catalog.weapons {
weapon.print_values();
}
for armor in &catalog.armors {
armor.print_values();
}
let player: Player = Player::new("Reya".to_string());
player.show_stats();
}

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();
}
}

108
src/stats.rs Normal file
View File

@@ -0,0 +1,108 @@
pub struct Stats {
max_health: f32,
current_health: f32,
max_stamina: f32,
current_stamina: f32,
strength: usize, // Determines base_damage modifier
dexterity: usize, // Determines Critical Factor Modifier
constitution: usize, // Determines Max Health Bonuses
charisma: usize, // Determines Shop Prices
luck: usize, // Determines Loot Drop & Critical Chance
level: usize,
}
pub trait StatsTrait {
fn get_max_health(&self) -> f32;
fn get_current_health(&self) -> f32;
fn set_current_health(&mut self, value: f32);
fn is_dead(&self) -> bool;
fn get_max_stamina(&self) -> f32;
fn get_current_stamina(&self) -> f32;
fn set_current_stamina(&mut self, value: f32);
fn get_strengh(&self) -> usize;
fn get_dexterity(&self) -> usize;
fn get_constitution(&self) -> usize;
fn get_charisma(&self) -> usize;
fn get_luck(&self) -> usize;
fn get_level(&self) -> usize;
fn level_up(&mut self);
}
impl Stats {
pub fn new() -> Self {
let health: f32 = 100.0;
let stamina: f32 = 100.0;
Self {
max_health: health,
current_health: health,
max_stamina: stamina,
current_stamina: stamina,
strength: 1,
dexterity: 1,
constitution: 1,
charisma: 1,
luck: 1,
level: 1,
}
}
}
impl StatsTrait for Stats {
fn get_max_health(&self) -> f32 {
self.max_health
}
fn get_current_health(&self) -> f32 {
self.current_health
}
fn set_current_health(&mut self, value: f32) {
let new_val: f32 = self.current_health + value;
self.current_health = new_val.clamp(0.0, self.max_health);
}
fn is_dead(&self) -> bool {
self.current_health <= 0.0
}
fn get_max_stamina(&self) -> f32 {
self.max_stamina
}
fn get_current_stamina(&self) -> f32 {
self.current_stamina
}
fn set_current_stamina(&mut self, value: f32) {
let new_val: f32 = self.current_stamina + value;
self.current_stamina = new_val.clamp(0.0, self.max_stamina);
}
fn get_strengh(&self) -> usize {
self.strength
}
fn get_dexterity(&self) -> usize {
self.dexterity
}
fn get_constitution(&self) -> usize {
self.constitution
}
fn get_charisma(&self) -> usize {
self.charisma
}
fn get_luck(&self) -> usize {
self.luck
}
fn get_level(&self) -> usize {
self.level
}
fn level_up(&mut self) {
self.level = (self.level + 1).clamp(1, 50);
}
}