diff --git a/character_window/src/character/mod.rs b/character_window/src/character/mod.rs index 648fab2..cf4af97 100644 --- a/character_window/src/character/mod.rs +++ b/character_window/src/character/mod.rs @@ -8,7 +8,7 @@ use rpg_components::{ level::Level, statistics::Statistics, }, - config::attributes::AttributeSettings, + config::{attributes::AttributeSettings, items::ItemSettings}, }; use super::{CharacterWindow, Page}; @@ -207,6 +207,7 @@ impl CharacterPage { let mut multi_mut = entity.multi_mut(); let attribute_settings = resources.get::(); + let item_settings = resources.get::(); let level = multi_mut.get::()?; let attributes = multi_mut.get::()?; @@ -217,7 +218,7 @@ impl CharacterPage { let statistics = multi_mut.get::()?; let items = multi_mut.get::()?; - statistics.update(attributes, attribute_settings, &*items); + statistics.update(attributes, attribute_settings, (&*items, item_settings)); upgraded = true; } diff --git a/character_window/src/inventory/content.rs b/character_window/src/inventory/content.rs index b453046..4573210 100644 --- a/character_window/src/inventory/content.rs +++ b/character_window/src/inventory/content.rs @@ -92,7 +92,7 @@ impl Content { statistics.update( attributes, resources.get::(), - &*hero_items, + (&*hero_items, resources.get::()), ); Ok(()) diff --git a/character_window/src/inventory/item_right_side.rs b/character_window/src/inventory/item_right_side.rs index 0f92a5a..fbe3487 100644 --- a/character_window/src/inventory/item_right_side.rs +++ b/character_window/src/inventory/item_right_side.rs @@ -3,7 +3,7 @@ use rpg_components::{ attributes::Attributes, character_status::CharacterStatus, inventory::Inventory, item_slots::ItemSlotContainer, statistics::Statistics, }, - config::attributes::AttributeSettings, + config::{attributes::AttributeSettings, items::ItemSettings}, items::{Item, ItemAffix, Tooltip}, }; @@ -257,7 +257,7 @@ mod macros { statistics.update( attributes, resources.get::(), - &*items + (&*items, resources.get::()) ); let status = multi_mut.get::()?; @@ -409,7 +409,7 @@ mod macros { statistics.update( attributes, resources.get::(), - &*items + (&*items, resources.get::()) ); let status = multi_mut.get::()?; diff --git a/character_window/src/inventory/jewel_right_side.rs b/character_window/src/inventory/jewel_right_side.rs index dd75b1d..13e4c6c 100644 --- a/character_window/src/inventory/jewel_right_side.rs +++ b/character_window/src/inventory/jewel_right_side.rs @@ -272,6 +272,7 @@ impl JewelRightSide { } ReferenceItemSource::Slots(opt_index) => { let attribute_settings = resources.get::(); + let item_settings = resources.get::(); let mut multi_mut = entity.multi_mut(); @@ -306,7 +307,11 @@ impl JewelRightSide { let statistics = multi_mut.get::()?; let attributes = multi_mut.get::()?; - statistics.update(attributes, attribute_settings, &*item_slots); + statistics.update( + attributes, + attribute_settings, + (&*item_slots, &*item_settings), + ); } } } diff --git a/gavania-core/src/game/content/objects/hero.rs b/gavania-core/src/game/content/objects/hero.rs index 7c86777..72e4bec 100644 --- a/gavania-core/src/game/content/objects/hero.rs +++ b/gavania-core/src/game/content/objects/hero.rs @@ -96,6 +96,7 @@ impl Hero { let exp_settings = &game.experience_settings; let attribute_settings = &game.attribute_settings; + let item_settings = &game.item_settings; let item_system = &game.item_system(); let level = Self::level(&hero_create_type, exp_settings); @@ -107,7 +108,7 @@ impl Hero { let movement = Self::movement(&mut entity_object); let mut stats = Statistics::default(); - stats.update(&mut attributes, attribute_settings, &items); + stats.update(&mut attributes, attribute_settings, (&items, item_settings)); let current_status = CharacterStatus::new_full(&stats); @@ -140,6 +141,7 @@ impl Hero { let exp_settings = &game.experience_settings; let attribute_settings = &game.attribute_settings; + let item_settings = &game.item_settings; let item_system = &game.item_system(); { @@ -154,7 +156,7 @@ impl Hero { let movement = Self::movement(&mut entity_object); let mut stats = Statistics::default(); - stats.update(&mut attributes, attribute_settings, &items); + stats.update(&mut attributes, attribute_settings, (&items, item_settings)); let current_status = CharacterStatus::new_full(&stats); @@ -404,14 +406,10 @@ impl Hero { let game = game_handle.upgrade(); let mut items = match hero_create_type { - HeroCreateType::SaveGame(save_game) => ItemSlotContainer::load( - save_game, - game.item_settings.clone(), - draw, - multi_mut.get::()?.item_meshes().clone(), - &game.item_system(), - )?, - HeroCreateType::New(_) => ItemSlotContainer::empty(game.item_settings.clone(), draw)?, + HeroCreateType::SaveGame(save_game) => { + ItemSlotContainer::load(save_game, &game.item_system())? + } + HeroCreateType::New(_) => ItemSlotContainer::new(), }; items.set_item_change_callback(draw, multi_mut.get::()?)?; diff --git a/rpg_components/src/components/item_slots.rs b/rpg_components/src/components/item_slots.rs index 18b9c6f..50f94b0 100644 --- a/rpg_components/src/components/item_slots.rs +++ b/rpg_components/src/components/item_slots.rs @@ -2,7 +2,7 @@ use anyhow::Result; use engine::prelude::*; use paste; -use std::{collections::HashMap, sync::Arc}; +use std::collections::HashMap; use crate::{ config::{items::ItemSettings, save_game::SaveGame}, @@ -156,14 +156,11 @@ pub struct ItemSlotContainer { slot_changed_callback: Option< Box) -> Result<()> + Send + Sync>, >, - - item_meshes: Arc>>, - item_settings: ItemSettings, } impl ItemSlotContainer { - fn new(item_settings: ItemSettings, draw: &Draw, query_meshes: bool) -> Result { - Ok(Self { + pub fn new() -> Self { + Self { helmet: None, chest_plate: None, belt: None, @@ -177,44 +174,11 @@ impl ItemSlotContainer { amulets: [None, None], slot_changed_callback: None, - - item_meshes: if query_meshes { - Arc::new( - Self::find_items(draw) - .map({ - let item_settings = item_settings.clone(); - - move |item_meshes| { - Self::create_rarity_color_items(item_meshes, &item_settings) - } - }) - .transpose()? - .unwrap_or_default(), - ) - } else { - Default::default() - }, - - item_settings, - }) - } - - pub fn empty(item_settings: ItemSettings, draw: &Draw) -> Result { - Self::new(item_settings, draw, true) - } - - pub fn load( - save_game: &SaveGame, - item_settings: ItemSettings, - draw: &Draw, - item_meshes: Arc>>, - item_system: &ItemSystem, - ) -> Result { - let mut me = Self::new(item_settings, draw, false)?; - - if me.item_meshes.is_empty() { - me.item_meshes = item_meshes; } + } + + pub fn load(save_game: &SaveGame, item_system: &ItemSystem) -> Result { + let mut me = Self::new(); load_item!(me, chest_plate, ChestPlate, save_game, item_system); load_item!(me, helmet, Helmet, save_game, item_system); @@ -273,7 +237,7 @@ impl ItemSlotContainer { ] } - fn create_rarity_color_items( + fn _create_rarity_color_items( item_meshes: HashMap, item_settings: &ItemSettings, ) -> Result>> { @@ -333,19 +297,38 @@ impl ItemSlotContainer { Ok(slot_meshes) } - pub fn item_meshes(&self) -> Arc>> { - self.item_meshes.clone() - } + pub fn set_item_change_callback( + &mut self, + _draw: &mut Draw, + _location: &Location, + ) -> Result<()> { + // TODO - pub fn set_item_change_callback(&mut self, draw: &mut Draw, location: &Location) -> Result<()> { - let item_rarity_meshes = self.item_meshes.clone(); + // item_meshes: if query_meshes { + // Arc::new( + // Self::find_items(draw) + // .map({ + // let item_settings = item_settings.clone(); - // only enable items that are equipped - self.slot_changed_callback = Some(Box::new(move |slot, rarity, is_some, multi_mut| { - Self::equipment_changed(multi_mut, &item_rarity_meshes, slot, rarity, is_some) - })); + // move |item_meshes| { + // Self::create_rarity_color_items(item_meshes, &item_settings) + // } + // }) + // .transpose()? + // .unwrap_or_default(), + // ) + // } else { + // Default::default() + // }, - Self::check_items(draw, location, self, &self.item_meshes)?; + // let item_rarity_meshes = self.item_meshes.clone(); + + // // only enable items that are equipped + // self.slot_changed_callback = Some(Box::new(move |slot, rarity, is_some, multi_mut| { + // Self::equipment_changed(multi_mut, &item_rarity_meshes, slot, rarity, is_some) + // })); + + // Self::check_items(draw, location, self, &self.item_meshes)?; Ok(()) } @@ -637,7 +620,7 @@ impl ItemSlotContainer { impl ItemSlotContainer { #[inline] - fn item( + fn _item( item_meshes: &mut HashMap, mesh: &AssetMesh, name: &str, @@ -651,16 +634,16 @@ impl ItemSlotContainer { } #[inline] - fn find_items(draw: &Draw) -> Option> { + fn _find_items(draw: &Draw) -> Option> { let mut item_meshes = HashMap::new(); for mesh in draw.iter() { - Self::item(&mut item_meshes, mesh, "Helmet", ItemSlots::Helmet); - Self::item(&mut item_meshes, mesh, "Shield", ItemSlots::OffHand); - Self::item(&mut item_meshes, mesh, "Sword", ItemSlots::MainHand); - Self::item(&mut item_meshes, mesh, "ChestPlate", ItemSlots::ChestPlate); - Self::item(&mut item_meshes, mesh, "Boots", ItemSlots::Boots); - Self::item(&mut item_meshes, mesh, "Gloves", ItemSlots::Gloves); + Self::_item(&mut item_meshes, mesh, "Helmet", ItemSlots::Helmet); + Self::_item(&mut item_meshes, mesh, "Shield", ItemSlots::OffHand); + Self::_item(&mut item_meshes, mesh, "Sword", ItemSlots::MainHand); + Self::_item(&mut item_meshes, mesh, "ChestPlate", ItemSlots::ChestPlate); + Self::_item(&mut item_meshes, mesh, "Boots", ItemSlots::Boots); + Self::_item(&mut item_meshes, mesh, "Gloves", ItemSlots::Gloves); } if !item_meshes.is_empty() { @@ -671,12 +654,12 @@ impl ItemSlotContainer { } #[inline] - fn undress(draw: &mut Draw, name: &str) { + fn _undress(draw: &mut Draw, name: &str) { draw.remove_by_name(name); } #[inline] - fn dress( + fn _dress( draw: &mut Draw, location: &Location, item_meshes: &HashMap>, @@ -705,7 +688,7 @@ impl ItemSlotContainer { } #[inline] - fn check_items( + fn _check_items( draw: &mut Draw, location: &Location, items: &ItemSlotContainer, @@ -715,9 +698,9 @@ impl ItemSlotContainer { ($func:ident, $name:expr) => { match items.$func() { Some(item) => { - Self::dress(draw, location, item_meshes, $name, item.slot, item.rarity)?; + Self::_dress(draw, location, item_meshes, $name, item.slot, item.rarity)?; } - None => Self::undress(draw, $name), + None => Self::_undress(draw, $name), } }; } @@ -732,7 +715,7 @@ impl ItemSlotContainer { Ok(()) } - fn equipment_changed( + fn _equipment_changed( multi_mut: &mut MultiMut<'_>, item_meshes: &HashMap>, slot: ItemSlots, @@ -756,9 +739,9 @@ impl ItemSlotContainer { }; if is_some { - Self::dress(draw, location, item_meshes, item_name, slot, rarity)?; + Self::_dress(draw, location, item_meshes, item_name, slot, rarity)?; } else { - Self::undress(draw, item_name); + Self::_undress(draw, item_name); } Ok(()) @@ -916,7 +899,10 @@ impl ItemSlotContainer { } } - pub fn collect_attribute_bonuses(&self) -> (Strength, Agility, Intelligence) { + pub fn collect_attribute_bonuses( + &self, + item_settings: &ItemSettings, + ) -> (Strength, Agility, Intelligence) { let mut strength = Strength::default(); let mut agility = Agility::default(); let mut intelligence = Intelligence::default(); @@ -924,12 +910,11 @@ impl ItemSlotContainer { for item in self.as_vec().iter().copied().flatten() { for affix in item.affixes.iter() { if let ItemAffix::Socket(Some(jewel)) = affix { - let value = self - .item_settings + let value = item_settings .jewel_rarity_multiplier .from_rarity(jewel.rarity) * jewel.level - * self.item_settings.general.jewel_level_multiplier; + * item_settings.general.jewel_level_multiplier; match jewel.attribute { Attribute::Agility => agility += Agility::from(value), diff --git a/rpg_components/src/components/statistics.rs b/rpg_components/src/components/statistics.rs index 17fb788..1c53b69 100644 --- a/rpg_components/src/components/statistics.rs +++ b/rpg_components/src/components/statistics.rs @@ -1,8 +1,9 @@ use engine::prelude::*; use crate::{ - config::attributes::{ - AgilitySettings, AttributeSettings, IntelligenceSettings, StrengthSettings, + config::{ + attributes::{AgilitySettings, AttributeSettings, IntelligenceSettings, StrengthSettings}, + items::ItemSettings, }, damage_type::DamageType, }; @@ -133,13 +134,14 @@ impl Statistics { &mut self, attributes: &mut Attributes, attribute_settings: &AttributeSettings, - items: impl Into>, + + items: impl Into>, ) { *self = Self::default(); let items = items.into(); - if let Some(items) = items { - let (agility, strength, intelligence) = items.collect_attribute_bonuses(); + if let Some((items, item_settings)) = items { + let (agility, strength, intelligence) = items.collect_attribute_bonuses(item_settings); attributes.bonus_agility = strength; attributes.bonus_strength = agility; @@ -162,7 +164,7 @@ impl Statistics { ); // apply items - if let Some(items) = items { + if let Some((items, _)) = items { let item_bonuses = items.collect_stat_bonuses(); for item_bonus in item_bonuses.into_iter() { diff --git a/rpg_components/src/config/save_game.rs b/rpg_components/src/config/save_game.rs index dd1cd2d..84f0bc2 100644 --- a/rpg_components/src/config/save_game.rs +++ b/rpg_components/src/config/save_game.rs @@ -12,7 +12,7 @@ use crate::{ use std::{env::var, sync::Arc}; -use super::{attributes::AttributeSettings, experience::ExperienceSettings}; +use super::{attributes::AttributeSettings, experience::ExperienceSettings, items::ItemSettings}; #[cfg(target_os = "windows")] pub fn save_game_dir(game: &str) -> String { @@ -364,13 +364,14 @@ impl SaveGame { let experience_settings = scene.resources.get::(); let attribute_settings = scene.resources.get::(); + let item_settings = scene.resources.get::(); let item_system = scene.resources.get::>(); let mut entity_object = engine.assets().empty_entity(); entity_object.insert_component(Draw::new(Vec::new())); entity_object.insert_component(Audio::new(engine.context(), None)?); - Location::new_and_setup(&mut entity_object); + Location::new_and_setup(&mut entity_object)?; let level = Level::load(self.general.level, self.general.exp, experience_settings); let mut attributes = Attributes::load( @@ -381,9 +382,9 @@ impl SaveGame { let inventory = Inventory::load(&self, &item_system)?; let abilities = AbilitySlots::load(item_system.clone(), &self)?; let crafting_materials = CraftingMaterials::load(&self); - let items = ItemSlotContainer::load()?; + let items = ItemSlotContainer::load(&self, &item_system)?; let mut statistics = Statistics::default(); - statistics.update(&mut attributes, attribute_settings, &items); + statistics.update(&mut attributes, attribute_settings, (&items, item_settings)); let current_status = CharacterStatus::new_full(&statistics); entity_object.insert_component(level);