diff --git a/Assets/Scripts/Inventory/Inventory.cs b/Assets/Scripts/Inventory/Inventory.cs index c6cd943..3073367 100644 --- a/Assets/Scripts/Inventory/Inventory.cs +++ b/Assets/Scripts/Inventory/Inventory.cs @@ -24,9 +24,9 @@ public class Inventory : MonoBehaviour public Item testItemType; - public static Action OnInventoryChanged; - public static Action OnItemAdded; - public static Action OnItemRemoved; + public static Action OnPlayerInventoryChanged; + public static Action OnPlayerItemAdded; + public static Action OnPlayerItemRemoved; [Header("Inventory")] [SerializeField] @@ -35,16 +35,22 @@ public class Inventory : MonoBehaviour [SerializeField] int maxSpaceOfSlots = 10; + [SerializeField] + bool isPlayerInventory = true; + [SerializeField] List inventory = new List(); + public List GetInventory() => inventory; + public void AddSlots(int _numberOfSlots) { for (int i = 0; i < _numberOfSlots; i++) { inventory.Add(new Slot()); } - OnInventoryChanged?.Invoke(); + if(isPlayerInventory) + OnPlayerInventoryChanged?.Invoke(); } Slot GetEmptySlot() { @@ -143,8 +149,13 @@ public class Inventory : MonoBehaviour rest = count - slot.GetCount(); slot.Clear(); } - OnInventoryChanged?.Invoke(); - OnItemRemoved?.Invoke(); + + if (isPlayerInventory) + { + OnPlayerInventoryChanged?.Invoke(); + OnPlayerItemRemoved?.Invoke(); + } + return rest; } else @@ -204,8 +215,12 @@ public class Inventory : MonoBehaviour else return -1; - OnInventoryChanged?.Invoke(); - OnItemAdded?.Invoke(); + if (isPlayerInventory) + { + OnPlayerInventoryChanged?.Invoke(); + OnPlayerItemAdded?.Invoke(); + } + return rest; } diff --git a/Assets/Scripts/Inventory/Slot.cs b/Assets/Scripts/Inventory/Slot.cs index 4b702c3..ab49869 100644 --- a/Assets/Scripts/Inventory/Slot.cs +++ b/Assets/Scripts/Inventory/Slot.cs @@ -13,6 +13,7 @@ public class Slot int count = 0; public Item GetItem() => item; + public void SetItem(Item _item) => item = _item; public int GetCount() => count; @@ -38,7 +39,14 @@ public class Slot item = null; count = 0; } + public Slot Copy() + { + Slot slot = new Slot(); + slot.SetCount(count); + slot.SetItem(item); + return slot; + } public void Set(Item _item, int _count = 1) { item = _item; diff --git a/Assets/Scripts/Inventory/UI_Inventory.cs b/Assets/Scripts/Inventory/UI_Inventory.cs index aee3b59..33ee21f 100644 --- a/Assets/Scripts/Inventory/UI_Inventory.cs +++ b/Assets/Scripts/Inventory/UI_Inventory.cs @@ -6,14 +6,15 @@ using UnityEngine.UI; public class UI_Inventory : MonoBehaviour { - /* Inventory playerInventory; + [SerializeField] + Inventory inventory; public Transform inventoryContainer; public Transform itemSlotTemplate; private RectTransform[] UIItemSlots; private Button[] buttons; private Slot pickedItem = new Slot(); private RectTransform pickedItemSlot; - private void Awake() + private void Start() { //itemSlotTemplate = inventoryContainer.Find("containerTemplate"); pickedItemSlot = Instantiate(itemSlotTemplate, this.transform).GetComponent(); @@ -21,8 +22,8 @@ public class UI_Inventory : MonoBehaviour void pickItem(int index) { - pickedItem = playerInventory.getInventory[index].copy(); - playerInventory.getInventory[index].clear(); + pickedItem = inventory.GetInventory()[index].Copy(); + inventory.GetInventory()[index].Clear(); UIItemSlots[index].GetChild(0).Find("Icon").gameObject.SetActive(false); } void placeItem() @@ -31,18 +32,18 @@ public class UI_Inventory : MonoBehaviour } public void buttonEvent(int index) { - if (pickedItem.ItemType == null && playerInventory.getInventory[index].ItemType != null) + if (pickedItem.GetItem() == null && inventory.GetInventory()[index].GetItem() != null) { pickItem(index); } else { - if (pickedItem.ItemType != null) + if (pickedItem.GetItem() != null) { - int rest = playerInventory.addItemAt(index, pickedItem.ItemType, pickedItem.Count); + int rest = inventory.Add(pickedItem.GetItem(), pickedItem.GetCount(), index); if (rest == 0) { - pickedItem.clear(); + pickedItem.Clear(); } else { @@ -57,7 +58,6 @@ public class UI_Inventory : MonoBehaviour } public void setInventory(Inventory inventory) { - playerInventory = inventory; inventory.onItemChangedCallback += updateInventory; updateInventory(); } @@ -98,10 +98,10 @@ public class UI_Inventory : MonoBehaviour } } - UIItemSlots = new RectTransform[playerInventory.getInventory.Count]; - buttons = new Button[playerInventory.getInventory.Count]; + UIItemSlots = new RectTransform[inventory.Count]; + buttons = new Button[inventory.Count]; //inventoryContainer.GetComponent(). - foreach (Slot slot in playerInventory.getInventory) + foreach (Slot slot in inventory) { RectTransform itemSlotRectTransform = Instantiate(itemSlotTemplate, inventoryContainer).GetComponent(); buttons[index] = itemSlotRectTransform.Find("Button").GetComponent