Skip to content

Commit

Permalink
Merge pull request #22 from dragoni7/dyanmic-aspects-fragments
Browse files Browse the repository at this point in the history
Dynamic aspects fragments
  • Loading branch information
Rorschach7552 authored Aug 25, 2024
2 parents c59ba27 + 81787b0 commit e731fc2
Showing 1 changed file with 48 additions and 15 deletions.
63 changes: 48 additions & 15 deletions src/features/subclass/AbilitiesModification.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,6 @@ const fetchMods = async (subclass: ManifestSubclass) => {
return modsData;
}
};

const AbilitiesModification: React.FC<AbilitiesModificationProps> = ({ subclass }) => {
const [mods, setMods] = useState<{
[key: string]: (ManifestPlug | ManifestAspect | ManifestStatPlug)[];
Expand Down Expand Up @@ -237,6 +236,10 @@ const AbilitiesModification: React.FC<AbilitiesModificationProps> = ({ subclass
}
}, [subclass]);

const calculateAvailableFragmentSlots = useCallback(() => {
return loadout.aspects.reduce((total, aspect) => total + (aspect.energyCapacity || 0), 0);
}, [loadout.aspects]);

const handleModSelect = (
category: string,
mod: ManifestPlug | ManifestAspect | ManifestStatPlug,
Expand All @@ -246,23 +249,22 @@ const AbilitiesModification: React.FC<AbilitiesModificationProps> = ({ subclass

switch (category) {
case 'ASPECTS':
updatedMods = [...loadout.aspects];
updatedMods = [...loadout.aspects] as ManifestAspect[];
break;
case 'FRAGMENTS':
updatedMods = [...loadout.fragments];
updatedMods = [...loadout.fragments] as ManifestStatPlug[];
break;
case 'SUPERS':
case 'CLASS_ABILITIES':
case 'MOVEMENT_ABILITIES':
case 'MELEE_ABILITIES':
case 'GRENADES':
updatedMods = [mod];
updatedMods = [mod] as ManifestPlug[];
break;
default:
return;
}

// Check if the mod already exists in another slot and replace it with an empty mod
if (category === 'ASPECTS' || category === 'FRAGMENTS') {
const modIndex = updatedMods.findIndex(
(existingMod) => existingMod.itemHash === mod.itemHash
Expand All @@ -272,11 +274,33 @@ const AbilitiesModification: React.FC<AbilitiesModificationProps> = ({ subclass
updatedMods[modIndex] = category === 'ASPECTS' ? EMPTY_ASPECT : EMPTY_FRAGMENT;
}

// Assign the mod to the selected slot
updatedMods[index!] = mod;
} else {
// Directly assign the mod for SUPERS and abilities
updatedMods[0] = mod;
updatedMods[index!] = mod as (typeof updatedMods)[number];

if (category === 'ASPECTS') {
const newAspects = updatedMods as ManifestAspect[];
const newAvailableSlots = newAspects.reduce(
(total, aspect) => total + (aspect.energyCapacity || 0),
0
);

const updatedFragments = loadout.fragments.map((fragment, idx) =>
idx < newAvailableSlots ? fragment : EMPTY_FRAGMENT
);

dispatch(
updateSubclassMods({
category: 'ASPECTS',
mods: newAspects,
})
);
dispatch(
updateSubclassMods({
category: 'FRAGMENTS',
mods: updatedFragments,
})
);
return;
}
}

dispatch(
Expand Down Expand Up @@ -312,21 +336,26 @@ const AbilitiesModification: React.FC<AbilitiesModificationProps> = ({ subclass

const SlotComponent = category === 'SUPERS' ? SuperModSlot : ModSlot;

const availableFragmentSlots = calculateAvailableFragmentSlots();
const isDisabled = category === 'FRAGMENTS' && index! >= availableFragmentSlots;

return (
<Box key={slotId} position="relative" display="inline-block">
<div onMouseEnter={(e) => handleMouseEnter(e, slotId)} onMouseLeave={handleMouseLeave}>
<SlotComponent
style={{
backgroundImage: currentMod ? `url(${currentMod.icon})` : 'none',
opacity: isDisabled ? 0.5 : 1,
pointerEvents: isDisabled ? 'none' : 'auto',
}}
>
{isEmptyMod && (
<Typography variant="caption" style={{ color: 'rgba(255, 255, 255, 0.7)' }}>
Empty
{isDisabled ? 'Locked' : 'Empty'}
</Typography>
)}
</SlotComponent>
{isHovered && (
{isHovered && !isDisabled && (
<SubmenuContainer
style={{
top: submenuPosition.top,
Expand All @@ -347,7 +376,7 @@ const AbilitiesModification: React.FC<AbilitiesModificationProps> = ({ subclass
</Box>
);
},
[mods, handleModSelect, hoveredSlot, submenuPosition]
[mods, handleModSelect, hoveredSlot, submenuPosition, calculateAvailableFragmentSlots]
);

if (loading) {
Expand Down Expand Up @@ -413,9 +442,13 @@ const AbilitiesModification: React.FC<AbilitiesModificationProps> = ({ subclass
<Box marginBottom={2}>
<StyledTitle variant="h6">FRAGMENTS</StyledTitle>
<Box display="flex" flexWrap="wrap" gap={2}>
{loadout.fragments.map((fragment, index) => (
{Array.from({ length: 5 }).map((_, index) => (
<React.Fragment key={index}>
{renderModCategory('FRAGMENTS', fragment, index)}
{renderModCategory(
'FRAGMENTS',
loadout.fragments[index] || EMPTY_FRAGMENT,
index
)}
</React.Fragment>
))}
</Box>
Expand Down

0 comments on commit e731fc2

Please sign in to comment.