Skip to content

Commit 13f77c2

Browse files
committed
Video 20 Edible Item p2
1 parent 039c6dc commit 13f77c2

File tree

3 files changed

+36
-2
lines changed

3 files changed

+36
-2
lines changed

InventoryController.cs

+13-1
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,19 @@ private void PrepareUI()
5656

5757
private void HandleItemActionRequest(int itemIndex)
5858
{
59-
59+
InventoryItem inventoryItem = inventoryData.GetItemAt(itemIndex);
60+
if (inventoryItem.IsEmpty)
61+
return;
62+
IItemAction itemAction = inventoryItem.item as IItemAction;
63+
if(itemAction != null)
64+
{
65+
itemAction.PerformAction(gameObject);
66+
}
67+
IDestroyableItem destroyableItem = inventoryItem.item as IDestroyableItem;
68+
if(destroyableItem != null)
69+
{
70+
inventoryData.RemoveItem(itemIndex,1);
71+
}
6072
}
6173

6274
private void HandleDragging(int itemIndex)

Model/InventorySO.cs

+17
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,23 @@ private int AddStackableItem(ItemSO item, int quantity)
102102
return quantity;
103103
}
104104

105+
public void RemoveItem(int itemIndex, int amount)
106+
{
107+
if (inventoryItems.Count > itemIndex)
108+
{
109+
if (inventoryItems[itemIndex].IsEmpty)
110+
return;
111+
int reminder = inventoryItems[itemIndex].quantity - amount;
112+
if (reminder <= 0)
113+
inventoryItems[itemIndex] = InventoryItem.GetEmptyItem();
114+
else
115+
inventoryItems[itemIndex] = inventoryItems[itemIndex]
116+
.ChangeQuantity(reminder);
117+
118+
InformAboutChange();
119+
}
120+
}
121+
105122
public void AddItem(InventoryItem item)
106123
{
107124
AddItem(item.item, item.quantity);

UI/UIInventoryPage.cs

+6-1
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,12 @@ public void UpdateData(int itemIndex,
7979

8080
private void HandleShowItemActions(UIInventoryItem inventoryItemUI)
8181
{
82-
82+
int index = listOfUIItems.IndexOf(inventoryItemUI);
83+
if (index == -1)
84+
{
85+
return;
86+
}
87+
OnItemActionRequested?.Invoke(index);
8388
}
8489

8590
private void HandleEndDrag(UIInventoryItem inventoryItemUI)

0 commit comments

Comments
 (0)