99 lines
3 KiB
Rust
99 lines
3 KiB
Rust
use engine::prelude::*;
|
|
|
|
use paste::paste;
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
use crate::{config::save_game::SaveGame, items::Rarities};
|
|
|
|
macro_rules! check_consume {
|
|
($self: ident, $var: ident, $amount: ident) => {
|
|
paste! {
|
|
if $self.$var < $amount {
|
|
false
|
|
} else {
|
|
$self.$var = $self.$var - $amount;
|
|
|
|
true
|
|
}
|
|
}
|
|
};
|
|
}
|
|
|
|
#[derive(Default, Debug, Serialize, Deserialize, Clone)]
|
|
pub struct CraftingMaterials {
|
|
common: u32,
|
|
uncommon: u32,
|
|
magical: u32,
|
|
rare: u32,
|
|
epic: u32,
|
|
legendary: u32,
|
|
}
|
|
|
|
impl CraftingMaterials {
|
|
pub fn load(save_game: &SaveGame) -> Self {
|
|
let mut me = Self::default();
|
|
|
|
me.common = save_game.crafting_materials.common;
|
|
me.uncommon = save_game.crafting_materials.uncommon;
|
|
me.magical = save_game.crafting_materials.magical;
|
|
me.rare = save_game.crafting_materials.rare;
|
|
me.epic = save_game.crafting_materials.epic;
|
|
me.legendary = save_game.crafting_materials.legendary;
|
|
|
|
me
|
|
}
|
|
|
|
pub fn store(&self, save_game: &mut SaveGame) {
|
|
save_game.crafting_materials.common = self.common;
|
|
save_game.crafting_materials.uncommon = self.uncommon;
|
|
save_game.crafting_materials.magical = self.magical;
|
|
save_game.crafting_materials.rare = self.rare;
|
|
save_game.crafting_materials.epic = self.epic;
|
|
save_game.crafting_materials.legendary = self.legendary;
|
|
}
|
|
|
|
pub fn count(&self, rarity: Rarities) -> u32 {
|
|
match rarity {
|
|
Rarities::Common => self.common,
|
|
Rarities::Uncommon => self.uncommon,
|
|
Rarities::Magical => self.magical,
|
|
Rarities::Rare => self.rare,
|
|
Rarities::Epic => self.epic,
|
|
Rarities::Legendary => self.legendary,
|
|
}
|
|
}
|
|
|
|
pub fn increment(&mut self, rarity: Rarities) {
|
|
match rarity {
|
|
Rarities::Common => self.common += 1,
|
|
Rarities::Uncommon => self.uncommon += 1,
|
|
Rarities::Magical => self.magical += 1,
|
|
Rarities::Rare => self.rare += 1,
|
|
Rarities::Epic => self.epic += 1,
|
|
Rarities::Legendary => self.legendary += 1,
|
|
}
|
|
}
|
|
|
|
pub fn consume(&mut self, rarity: Rarities, amount: u32) -> bool {
|
|
match rarity {
|
|
Rarities::Common => check_consume!(self, common, amount),
|
|
Rarities::Uncommon => check_consume!(self, uncommon, amount),
|
|
Rarities::Magical => check_consume!(self, magical, amount),
|
|
Rarities::Rare => check_consume!(self, rare, amount),
|
|
Rarities::Epic => check_consume!(self, rare, amount),
|
|
Rarities::Legendary => check_consume!(self, legendary, amount),
|
|
}
|
|
}
|
|
}
|
|
|
|
impl EntityComponent for CraftingMaterials {
|
|
fn name(&self) -> &str {
|
|
Self::debug_name()
|
|
}
|
|
}
|
|
|
|
impl ComponentDebug for CraftingMaterials {
|
|
fn debug_name() -> &'static str {
|
|
"CraftingMaterials"
|
|
}
|
|
}
|