89 lines
2.1 KiB
Rust
89 lines
2.1 KiB
Rust
use std::fmt;
|
|
|
|
use crate::entity::{Entity, Location, EntityId};
|
|
|
|
#[derive(Clone)]
|
|
pub enum ItemType {
|
|
Weapon(Weapon),
|
|
Armor(Armor),
|
|
}
|
|
|
|
|
|
#[derive(Clone, PartialEq, Eq)]
|
|
pub enum ItemOwner {
|
|
Held(EntityId),
|
|
Dropped(Location),
|
|
Lost,
|
|
}
|
|
|
|
#[derive(Clone)]
|
|
pub struct Item {
|
|
pub entity: Entity,
|
|
pub owner: ItemOwner,
|
|
pub name: String,
|
|
pub item_type: ItemType,
|
|
}
|
|
|
|
#[derive(Clone)]
|
|
pub struct Weapon {
|
|
pub damage_base: u32,
|
|
pub damage_dice: u32,
|
|
pub damage_sides: u32,
|
|
}
|
|
|
|
#[derive(Clone)]
|
|
pub struct Armor {
|
|
pub armor_class: u32,
|
|
}
|
|
pub struct ItemGenerator;
|
|
|
|
impl Item {
|
|
pub fn value(&self) -> u32 {
|
|
match &self.item_type {
|
|
ItemType::Weapon(weapon) => self.weapon_value(weapon),
|
|
ItemType::Armor(armor) => self.armor_value(armor),
|
|
}
|
|
}
|
|
|
|
fn weapon_value(&self, weapon: &Weapon) -> u32 {
|
|
weapon.damage_base + weapon.damage_dice * weapon.damage_sides
|
|
}
|
|
|
|
fn armor_value(&self, armor: &Armor) -> u32 {
|
|
armor.armor_class * 10
|
|
}
|
|
}
|
|
|
|
impl ItemGenerator {
|
|
pub fn generate_item() -> Item {
|
|
Item {
|
|
entity: Entity::new_item(),
|
|
name: "Sword".to_string(),
|
|
item_type: ItemType::Weapon(Weapon {
|
|
damage_base: 0,
|
|
damage_dice: 1,
|
|
damage_sides: 6,
|
|
}),
|
|
owner: ItemOwner::Lost,
|
|
}
|
|
}
|
|
}
|
|
|
|
impl fmt::Display for ItemType {
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
match self {
|
|
ItemType::Weapon(weapon) => write!(f, "Weapon: {}d{}+{}", weapon.damage_dice, weapon.damage_sides, weapon.damage_base),
|
|
ItemType::Armor(armor) => write!(f, "Armor: {}", armor.armor_class),
|
|
}
|
|
}
|
|
}
|
|
|
|
impl fmt::Display for ItemOwner {
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
match self {
|
|
ItemOwner::Held(entity_id) => write!(f, "Held by {:?}", entity_id),
|
|
ItemOwner::Dropped(location) => write!(f, "Dropped at {}", location),
|
|
ItemOwner::Lost => write!(f, "Lost"),
|
|
}
|
|
}
|
|
} |