From f0a5fe201c4f5378f6011877c17607eda8b0f30e Mon Sep 17 00:00:00 2001 From: julius Date: Sat, 12 Feb 2022 12:14:40 +0100 Subject: [PATCH] Added remove method Added add slot method Added comments --- Assets/Scripts/Inventory/Inventory.cs | 129 ++++++++++-------- .../Scripts/Inventory/InventoryController.cs | 14 +- Assets/Scripts/Inventory/Slot.cs | 17 +-- 3 files changed, 85 insertions(+), 75 deletions(-) diff --git a/Assets/Scripts/Inventory/Inventory.cs b/Assets/Scripts/Inventory/Inventory.cs index 37ee16d..706802c 100644 --- a/Assets/Scripts/Inventory/Inventory.cs +++ b/Assets/Scripts/Inventory/Inventory.cs @@ -4,114 +4,131 @@ using UnityEngine; public class Inventory : MonoBehaviour { + /// + /// -------------------------------------------------------------------------------/// + /// + /// Inventory holds a list of Slots where Items can be added. + /// + ///[IMPORTANT] + /// !!createEmptyInventory(int numberOfSlots, int maxSpaceOfSlots) : Has to be called after the Inventory has been initialized!! + /// + ///[Methods] + /// getInventory() : Gets the list of Slots. + /// addItemAt(int index, Item itemType,int count) : Adds a number (count) of items of type itemType (itemType) to an item slot(index). + /// removeItemAt(int index, int count) : Removes a number of items (count) from a specific slot (index). + /// addInventorySlots(int numberOfSlots, int maxSpaceOfSlots = 10) + /// + /// -------------------------------------------------------------------------------/// + /// - [SerializeField]public List inventory = new List(); - [SerializeField] int maxSize; - public List getInventory { get => inventory; set => inventory = value; } - public void createEmptyInventory(int size) + + List inventory = new List(); + + public List getInventory { get => inventory;} + public void createEmptyInventory(int numberOfSlots, int maxSpaceOfSlots = 10) { + // Initializes the inventory with a specific number of slots and slotsize. + // !!!Has to be called bevore adding any items!!! + // Example createEmptyInventory(5, 10) : 5 Slots with space for 10 items each inventory = new List(); - for (int i = 0; i < size; i++) + for (int i = 0; i < numberOfSlots; i++) { - inventory.Add(new Slot(10)); + inventory.Add(new Slot(maxSpaceOfSlots)); } } - bool indexIsInRange(int index) + public void addInventorySlots(int numberOfSlots, int maxSpaceOfSlots = 10) { - if (index < maxSize && index >= 0) + // Adds a specific number of slots and slotsize to the inventory. + // Example addInventorySlots(5, 10) : Adds 5 Slots with space for 10 items each + for (int i = 0; i < numberOfSlots; i++) { - return true; - } - else - { - return false; + inventory.Add(new Slot(maxSpaceOfSlots)); } + } public int addItemAt(int index, Item itemType,int count) { + // Adds a number (count = 7) of items of type itemType (itemType = Stone) to an item slot(index = 3). + //Returns the number of items that could not be added because of unsufficent space in the specefied slot + //or Return -1 if the index is out of bound or the item slot is already filled with a different itemType. + // + // Example inventory.addItemAt(3,Stone,7); if (indexIsInRange(index) && (inventory[index].ItemType == null || inventory[index].ItemType.id == itemType.id)) { if (inventory[index].ItemType == null) { - inventory[index].ItemType = Instantiate(itemType); + inventory[index].ItemType = Instantiate(itemType); // Set the itemType if the slot was empty. } if (inventory[index].MaxItems == inventory[index].Count) { - return count; + return count; // Can't add any items if the slot is full. } else if (inventory[index].MaxItems >= inventory[index].Count + count) { - inventory[index].addItem(count); + inventory[index].addItem(count); // Adds all items if there is enought space. return 0; } else { int rest = count - inventory[index].MaxItems - inventory[index].Count; - inventory[index].addItem(inventory[index].MaxItems - inventory[index].Count); + inventory[index].addItem(inventory[index].MaxItems - inventory[index].Count); // Adds the number of items until the slot is full and returns the number of items that didn't fit. return rest; } } else { - //Wrong item or index not in range - return -1; + + return -1;//Wrong item or index not in range. } } public int removeItemAt(int index, int count) { - - if (inventory[index].ItemType != null && indexIsInRange(index)) + // Removes a number of items (count = 5) from a specific slot (index = 4). + // Example inventory.removeItemAt(4,5) + if (indexIsInRange(index)) { - for (int i = 0; i < count; i++) + if (inventory[index].ItemType == null) { - if (!inventory[index].removeItem()) - { - return count - i; - } + return count;// Can't remove any items if the slot is empty. } - return 0; + else if (inventory[index].Count > count) + { + inventory[index].removeItem(count);// Removes the number of items if there are more or equal items in the slot. + return 0; + } + else if (inventory[index].Count <= count) + { + int rest = count - inventory[index].Count; + inventory[index].Count = 0; // Removes all the items from the slot and returns the number of items that could not be removed. + inventory[index].ItemType = null; // When the slot is empty the itemType can also be removed. + return rest; + } + else + { + return -1; // Something went wrong (you should never end up in here). + } + } else { - //index not in range - return -1; + return -1;//Index not in range. } } - /* - Item findItem(Item itemType) - { - for (int i = 0; i < items.Length; i++) - { - if (items[i] == null) - { - return i; - } - } - return -1; - } - bool removeItem(Item item,int count = 1) + private bool indexIsInRange(int index) { + // Returns true if a given index is in the bounds of the inventory. + // Example (maxSize = 10) index = -10 : false , index = 100: false, index = 7 : true - return true; - } - bool removeItemAt(int index,int count = -1) - { - - } - Item getItemAt(int index) - { - if (index < maxSize && index >= 0) + if (index < inventory.Count && index >= 0) { - return items[index]; + return true; } - else + else { - return null; + return false; } - } - */ } diff --git a/Assets/Scripts/Inventory/InventoryController.cs b/Assets/Scripts/Inventory/InventoryController.cs index 54cbe31..6f91780 100644 --- a/Assets/Scripts/Inventory/InventoryController.cs +++ b/Assets/Scripts/Inventory/InventoryController.cs @@ -11,13 +11,17 @@ public class InventoryController : MonoBehaviour private void Awake() { inventory = transform.GetComponent(); - inventory.createEmptyInventory(5); - Debug.Log(inventory.addItemAt(0, item1, 8)); - Debug.Log(inventory.addItemAt(0, item1, 1)); - Debug.Log(inventory.addItemAt(3, item2, 15)); + inventory.createEmptyInventory(5,10); + + inventory.addItemAt(0, item1, 8); + inventory.addItemAt(0, item1, 1); + inventory.addItemAt(3, item2, 15); + /* Debug.Log(inventory.addItemAt(0, item2, 15)); Debug.Log(inventory.getInventory[0].Count); - + Debug.Log(inventory.removeItemAt(0, 10)); + Debug.Log(inventory.getInventory[0].Count); + */ uiInventory.setInventory(inventory); } void Start() diff --git a/Assets/Scripts/Inventory/Slot.cs b/Assets/Scripts/Inventory/Slot.cs index 78e67b6..cb1d819 100644 --- a/Assets/Scripts/Inventory/Slot.cs +++ b/Assets/Scripts/Inventory/Slot.cs @@ -15,21 +15,10 @@ public class Slot count = 0; } - public bool removeItem() + public bool removeItem(int count = 1) { - if (count > 0) - { - count--; - if (count == 0) - { - item = null; - } - return true; - } - else - { - return false; - } + this.count -= count; + return true; } public bool addItem(int count = 1) {