Fix character menu

This commit is contained in:
hodasemi 2025-04-06 09:23:11 +02:00
parent 60ba92aa76
commit be9a5109c8
6 changed files with 44 additions and 167 deletions

View file

@ -78,7 +78,7 @@ impl<A: Ability + 'static> Content<A, Item> {
let item = inventory.remove_item(item_index); let item = inventory.remove_item(item_index);
// add or swap items with equipment // add or swap items with equipment
if let Some(old_item) = hero_items.insert(item.clone(), attributes, entity)? { if let Some(old_item) = hero_items.insert(item.clone(), attributes)? {
inventory.insert_item(old_item, item_index); inventory.insert_item(old_item, item_index);
} }

View file

@ -251,7 +251,7 @@ mod macros {
} }
if found_item { if found_item {
items.[<unset_ $item>](entity)?; items.[<unset_ $item>]()?;
statistics.update( statistics.update(
attributes, attributes,

View file

@ -46,13 +46,6 @@ impl ReferenceObject {
ReferenceObject::Empty => None, ReferenceObject::Empty => None,
} }
} }
pub fn some_mut(&mut self) -> Option<&mut Self> {
match self {
ReferenceObject::Item { .. } | ReferenceObject::Jewel { .. } => Some(self),
ReferenceObject::Empty => None,
}
}
} }
#[derive(Default, Resource)] #[derive(Default, Resource)]

View file

@ -456,7 +456,7 @@ impl<A: Ability + 'static> Page for InventoryPage<A> {
// check if jewel page is open // check if jewel page is open
if self.current_mode == 1 { if self.current_mode == 1 {
if JewelRightSide::combine::<A>(world, self.hero)? { if JewelRightSide::combine::<A>(world, self.hero)? {
JewelRightSide::clear(world); JewelRightSide::clear(world)?;
self.update_page(world, true)?; self.update_page(world, true)?;
} }

View file

@ -125,10 +125,8 @@ macro_rules! store_item {
macro_rules! setter { macro_rules! setter {
($item:ident) => { ($item:ident) => {
paste::paste! { paste::paste! {
fn [<set_ $item>](&mut self, $item: Option<Item>, entity: &mut EntityObject) { fn [<set_ $item>](&mut self, $item: Option<Item>) {
self.$item = $item; self.$item = $item;
self.[<apply_ $item _change>](entity).unwrap();
} }
} }
}; };
@ -152,10 +150,6 @@ pub struct ItemSlotContainer {
// jewelry slots // jewelry slots
rings: [Option<Item>; RING_COUNT], rings: [Option<Item>; RING_COUNT],
amulets: [Option<Item>; AMULET_COUNT], amulets: [Option<Item>; AMULET_COUNT],
slot_changed_callback: Option<
Box<dyn Fn(ItemSlots, Rarities, bool, &mut EntityObject) -> Result<()> + Send + Sync>,
>,
} }
impl ItemSlotContainer { impl ItemSlotContainer {
@ -172,8 +166,6 @@ impl ItemSlotContainer {
rings: [None, None, None, None], rings: [None, None, None, None],
amulets: [None, None], amulets: [None, None],
slot_changed_callback: None,
} }
} }
@ -348,104 +340,6 @@ impl ItemSlotContainer {
fn set_amulets(&mut self, amulet: Option<Item>, index: usize) { fn set_amulets(&mut self, amulet: Option<Item>, index: usize) {
self.amulets[index] = amulet; self.amulets[index] = amulet;
} }
fn apply_helmet_change(&mut self, entity: &mut EntityObject) -> Result<()> {
self.apply_item_change(
ItemSlots::Helmet,
match &self.helmet {
Some(helmet) => helmet.rarity,
None => Rarities::Common,
},
self.helmet.is_some(),
entity,
)
}
fn apply_belt_change(&mut self, entity: &mut EntityObject) -> Result<()> {
self.apply_item_change(
ItemSlots::Belt,
match &self.belt {
Some(belt) => belt.rarity,
None => Rarities::Common,
},
self.belt.is_some(),
entity,
)
}
fn apply_chest_plate_change(&mut self, entity: &mut EntityObject) -> Result<()> {
self.apply_item_change(
ItemSlots::ChestPlate,
match &self.chest_plate {
Some(chest_plate) => chest_plate.rarity,
None => Rarities::Common,
},
self.chest_plate.is_some(),
entity,
)
}
fn apply_gloves_change(&mut self, entity: &mut EntityObject) -> Result<()> {
self.apply_item_change(
ItemSlots::Gloves,
match &self.gloves {
Some(gloves) => gloves.rarity,
None => Rarities::Common,
},
self.gloves.is_some(),
entity,
)
}
fn apply_boots_change(&mut self, entity: &mut EntityObject) -> Result<()> {
self.apply_item_change(
ItemSlots::Boots,
match &self.boots {
Some(boots) => boots.rarity,
None => Rarities::Common,
},
self.boots.is_some(),
entity,
)
}
fn apply_main_hand_change(&mut self, entity: &mut EntityObject) -> Result<()> {
self.apply_item_change(
ItemSlots::MainHand,
match &self.main_hand {
Some(main_hand) => main_hand.rarity,
None => Rarities::Common,
},
self.main_hand.is_some(),
entity,
)
}
fn apply_off_hand_change(&mut self, entity: &mut EntityObject) -> Result<()> {
self.apply_item_change(
ItemSlots::OffHand,
match &self.off_hand {
Some(off_hand) => off_hand.rarity,
None => Rarities::Common,
},
self.off_hand.is_some(),
entity,
)
}
fn apply_item_change(
&self,
item_slot: ItemSlots,
rarity: Rarities,
is_some: bool,
entity: &mut EntityObject,
) -> Result<()> {
if let Some(slot_changed_callback) = &self.slot_changed_callback {
slot_changed_callback(item_slot, rarity, is_some, entity)?;
}
Ok(())
}
} }
// getter // getter
@ -503,99 +397,99 @@ impl ItemSlotContainer {
// setter // setter
impl ItemSlotContainer { impl ItemSlotContainer {
fn _update_helmet(&mut self, helmet: Item, entity: &mut EntityObject) { fn _update_helmet(&mut self, helmet: Item) {
debug_assert!(helmet.slot == ItemSlots::Helmet); debug_assert!(helmet.slot == ItemSlots::Helmet);
self.set_helmet(Some(helmet), entity); self.set_helmet(Some(helmet));
} }
pub fn unset_helmet(&mut self, entity: &mut EntityObject) -> Result<()> { pub fn unset_helmet(&mut self) -> Result<()> {
if self.helmet.is_some() { if self.helmet.is_some() {
self.set_helmet(None, entity); self.set_helmet(None);
} }
Ok(()) Ok(())
} }
fn update_chest(&mut self, chest: Item, entity: &mut EntityObject) { fn update_chest(&mut self, chest: Item) {
debug_assert!(chest.slot == ItemSlots::ChestPlate); debug_assert!(chest.slot == ItemSlots::ChestPlate);
self.set_chest_plate(Some(chest), entity); self.set_chest_plate(Some(chest));
} }
pub fn unset_chest(&mut self, entity: &mut EntityObject) -> Result<()> { pub fn unset_chest(&mut self) -> Result<()> {
if self.chest_plate.is_some() { if self.chest_plate.is_some() {
self.set_chest_plate(None, entity); self.set_chest_plate(None);
} }
Ok(()) Ok(())
} }
fn _update_belt(&mut self, belt: Item, entity: &mut EntityObject) { fn _update_belt(&mut self, belt: Item) {
debug_assert!(belt.slot == ItemSlots::Belt); debug_assert!(belt.slot == ItemSlots::Belt);
self.set_belt(Some(belt), entity); self.set_belt(Some(belt));
} }
pub fn unset_belt(&mut self, entity: &mut EntityObject) -> Result<()> { pub fn unset_belt(&mut self) -> Result<()> {
if self.belt.is_some() { if self.belt.is_some() {
self.set_belt(None, entity); self.set_belt(None);
} }
Ok(()) Ok(())
} }
fn _update_gloves(&mut self, gloves: Item, entity: &mut EntityObject) { fn _update_gloves(&mut self, gloves: Item) {
debug_assert!(gloves.slot == ItemSlots::Gloves); debug_assert!(gloves.slot == ItemSlots::Gloves);
self.set_gloves(Some(gloves), entity); self.set_gloves(Some(gloves));
} }
pub fn unset_gloves(&mut self, entity: &mut EntityObject) -> Result<()> { pub fn unset_gloves(&mut self) -> Result<()> {
if self.gloves.is_some() { if self.gloves.is_some() {
self.set_gloves(None, entity); self.set_gloves(None);
} }
Ok(()) Ok(())
} }
fn _update_boots(&mut self, boots: Item, entity: &mut EntityObject) { fn _update_boots(&mut self, boots: Item) {
debug_assert!(boots.slot == ItemSlots::Boots); debug_assert!(boots.slot == ItemSlots::Boots);
self.set_boots(Some(boots), entity); self.set_boots(Some(boots));
} }
pub fn unset_boots(&mut self, entity: &mut EntityObject) -> Result<()> { pub fn unset_boots(&mut self) -> Result<()> {
if self.boots.is_some() { if self.boots.is_some() {
self.set_boots(None, entity); self.set_boots(None);
} }
Ok(()) Ok(())
} }
fn update_primary_hand(&mut self, primary_hand: Item, entity: &mut EntityObject) { fn update_primary_hand(&mut self, primary_hand: Item) {
debug_assert!(primary_hand.slot == ItemSlots::MainHand); debug_assert!(primary_hand.slot == ItemSlots::MainHand);
self.set_main_hand(Some(primary_hand), entity); self.set_main_hand(Some(primary_hand));
} }
pub fn unset_primary_hand(&mut self, entity: &mut EntityObject) -> Result<()> { pub fn unset_primary_hand(&mut self) -> Result<()> {
if self.main_hand.is_some() { if self.main_hand.is_some() {
self.set_main_hand(None, entity); self.set_main_hand(None);
} }
Ok(()) Ok(())
} }
fn update_secondary_hand(&mut self, secondary_hand: Item, entity: &mut EntityObject) { fn update_secondary_hand(&mut self, secondary_hand: Item) {
debug_assert!(secondary_hand.slot == ItemSlots::OffHand); debug_assert!(secondary_hand.slot == ItemSlots::OffHand);
self.set_off_hand(Some(secondary_hand), entity); self.set_off_hand(Some(secondary_hand));
} }
pub fn unset_secondary_hand(&mut self, entity: &mut EntityObject) -> Result<()> { pub fn unset_secondary_hand(&mut self) -> Result<()> {
if self.off_hand.is_some() { if self.off_hand.is_some() {
self.set_off_hand(None, entity); self.set_off_hand(None);
} }
Ok(()) Ok(())
@ -689,14 +583,14 @@ impl ItemSlotContainer {
#[inline] #[inline]
fn _check_items( fn _check_items(
&self,
draw: &mut Draw, draw: &mut Draw,
location: &Location, location: &Location,
items: &ItemSlotContainer,
item_meshes: &HashMap<ItemSlots, HashMap<Rarities, AssetMesh>>, item_meshes: &HashMap<ItemSlots, HashMap<Rarities, AssetMesh>>,
) -> Result<()> { ) -> Result<()> {
macro_rules! check { macro_rules! check {
($func:ident, $name:expr) => { ($func:ident, $name:expr) => {
match items.$func() { match self.$func() {
Some(item) => { Some(item) => {
Self::_dress(draw, location, item_meshes, $name, item.slot, item.rarity)?; Self::_dress(draw, location, item_meshes, $name, item.slot, item.rarity)?;
} }
@ -779,53 +673,44 @@ impl ItemSlotContainer {
} }
/// inserts the item, returns the current item at this slot /// inserts the item, returns the current item at this slot
pub fn insert( pub fn insert(&mut self, item: Item, attributes: &Attributes) -> Result<Option<Item>> {
&mut self,
item: Item,
attributes: &Attributes,
entity: &mut EntityObject,
) -> Result<Option<Item>> {
// check attribute requirements // check attribute requirements
if attributes < &item.attributes { if attributes < &item.attributes {
return Ok(Some(item)); return Ok(Some(item));
} }
self.insert_unchecked(item, entity) self.insert_unchecked(item)
} }
pub fn insert_unchecked( pub fn insert_unchecked(&mut self, item: Item) -> Result<Option<Item>> {
&mut self,
item: Item,
entity: &mut EntityObject,
) -> Result<Option<Item>> {
Ok(match item.slot { Ok(match item.slot {
ItemSlots::Helmet => { ItemSlots::Helmet => {
let current_helmet = self.helmet.clone(); let current_helmet = self.helmet.clone();
self._update_helmet(item, entity); self._update_helmet(item);
current_helmet current_helmet
} }
ItemSlots::ChestPlate => { ItemSlots::ChestPlate => {
let current_chest_plate = self.chest_plate.clone(); let current_chest_plate = self.chest_plate.clone();
self.update_chest(item, entity); self.update_chest(item);
current_chest_plate current_chest_plate
} }
ItemSlots::Belt => { ItemSlots::Belt => {
let current_belt = self.belt.clone(); let current_belt = self.belt.clone();
self._update_belt(item, entity); self._update_belt(item);
current_belt current_belt
} }
ItemSlots::Gloves => { ItemSlots::Gloves => {
let current_gloves = self.gloves.clone(); let current_gloves = self.gloves.clone();
self._update_gloves(item, entity); self._update_gloves(item);
current_gloves current_gloves
} }
ItemSlots::Boots => { ItemSlots::Boots => {
let current_boots = self.boots.clone(); let current_boots = self.boots.clone();
self._update_boots(item, entity); self._update_boots(item);
current_boots current_boots
} }
@ -855,13 +740,13 @@ impl ItemSlotContainer {
} }
ItemSlots::MainHand => { ItemSlots::MainHand => {
let current_main_hand = self.main_hand.clone(); let current_main_hand = self.main_hand.clone();
self.update_primary_hand(item, entity); self.update_primary_hand(item);
current_main_hand current_main_hand
} }
ItemSlots::OffHand => { ItemSlots::OffHand => {
let current_off_hand = self.off_hand.clone(); let current_off_hand = self.off_hand.clone();
self.update_secondary_hand(item, entity); self.update_secondary_hand(item);
current_off_hand current_off_hand
} }

View file

@ -135,7 +135,6 @@ impl Statistics {
&mut self, &mut self,
attributes: &mut Attributes, attributes: &mut Attributes,
attribute_settings: &AttributeSettings, attribute_settings: &AttributeSettings,
items: impl Into<Option<(&'a ItemSlotContainer, &'a ItemSettings)>>, items: impl Into<Option<(&'a ItemSlotContainer, &'a ItemSettings)>>,
) { ) {
*self = Self::default(); *self = Self::default();