diff --git a/Assets/Scripts/Inventory/Inventory.cs b/Assets/Scripts/Inventory/Inventory.cs index 3073367..3d6596b 100644 --- a/Assets/Scripts/Inventory/Inventory.cs +++ b/Assets/Scripts/Inventory/Inventory.cs @@ -1,48 +1,41 @@ using System; -using System.Collections; using System.Collections.Generic; using UnityEngine; +/// +/// Inventory holds and managed a list of slots which can contain an item. +/// 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) - /// - /// -------------------------------------------------------------------------------/// - /// - - public Item testItemType; - + // Actions -> Used for Callback in other scripts public static Action OnPlayerInventoryChanged; public static Action OnPlayerItemAdded; public static Action OnPlayerItemRemoved; + // Serialized [Header("Inventory")] + [Tooltip("The number of slots a player has in the inventory.")] [SerializeField] int numberOfSlots = 20; + [Tooltip("Maximal count of items in ONE slot. How big is one stack.")] [SerializeField] int maxSpaceOfSlots = 10; + [Tooltip("Is this inventory owned by the player")] [SerializeField] bool isPlayerInventory = true; + [Tooltip("List of the slots ('The inventory').")] [SerializeField] List inventory = new List(); public List GetInventory() => inventory; + /// + /// Add slots to the inventory. You can extend the inventory space. + /// + /// The count of slots which should be added to the inventory public void AddSlots(int _numberOfSlots) { for (int i = 0; i < _numberOfSlots; i++) @@ -52,6 +45,11 @@ public class Inventory : MonoBehaviour if(isPlayerInventory) OnPlayerInventoryChanged?.Invoke(); } + + /// + /// GetEmptySlot gives you the first empty slot in the inventory. + /// + /// An empty slot of the inventory Slot GetEmptySlot() { foreach (Slot slot in inventory) @@ -61,6 +59,12 @@ public class Inventory : MonoBehaviour } return null; } + + /// + /// GetSlotByItem lets you find a slot by the item it's containing. + /// + /// + /// Slot with the specified item. Slot GetSlotByItem(Item item) { foreach (Slot slot in inventory) @@ -74,6 +78,12 @@ public class Inventory : MonoBehaviour } return null; } + + /// + /// GetRest calculates the rest count of an item and a slot. + /// Example: Slot has a capacity of 20. Count of item is 22. Returns 2. + /// + /// The rest count of an item and a slot. int GetRest(int count, Slot slot) { if (CountFitsInSlot(count, slot)) @@ -85,10 +95,17 @@ public class Inventory : MonoBehaviour return (count - (maxSpaceOfSlots - slot.GetCount())); } } + + /// + /// CountFitsInSlot calculates if the item count fits in the slot capacity + /// + /// + /// + /// True: Count fits. False: Count does not fit. bool CountFitsInSlot(int count, Slot slot) { int leftSize = maxSpaceOfSlots - slot.GetCount(); - if (leftSize > count) // left size > count + if (leftSize > count) { return true; } @@ -97,7 +114,12 @@ public class Inventory : MonoBehaviour return false; } } - bool indexIsInRange(int index) + /// + /// IndexIsInRange looks if an index of the inventory list is in range. + /// + /// + /// True: Is in range. False: Is not in range. + 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 @@ -112,7 +134,13 @@ public class Inventory : MonoBehaviour } } - + /// + /// Remove removes an item/s of the inventory list. It can be based in a specific index. + /// + /// which item should be removed + /// Count of the item you want to remove + /// (optional) Index of the inventory list + /// The rest of the count, which could not be removed. public int Remove(Item itemType, int count, int invIndex = -1) { Slot slot = null; @@ -124,7 +152,7 @@ public class Inventory : MonoBehaviour // Get Slot if (invIndex > -1) { - if (indexIsInRange(invIndex)) + if (IndexIsInRange(invIndex)) slot = inventory[invIndex]; } else @@ -162,6 +190,13 @@ public class Inventory : MonoBehaviour return -1; } + /// + /// Add adds an item/s of the inventory list. It can be based in a specific index. + /// + /// which item should be added + /// Count of the item you want to add + /// (optional) Index of the inventory list + /// The rest of the count, which could not be added. public int Add(Item itemType, int count, int invIndex = -1) { int rest = 0; @@ -174,7 +209,7 @@ public class Inventory : MonoBehaviour // Get Slot if (invIndex > -1) { - if(indexIsInRange(invIndex)) + if(IndexIsInRange(invIndex)) slot = inventory[invIndex]; } else @@ -227,19 +262,7 @@ public class Inventory : MonoBehaviour void Start() { + // Initialize Inventory Slots AddSlots(numberOfSlots); } - - private void Update() - { - if (Input.GetKeyDown(KeyCode.R)) - { - Debug.Log(Add(testItemType, 8, 4)); - } - - if (Input.GetKeyDown(KeyCode.T)) - { - Debug.Log(Remove(testItemType, 8, 4)); - } - } } diff --git a/Assets/Scripts/Inventory/Slot.cs b/Assets/Scripts/Inventory/Slot.cs index ab49869..dc07d87 100644 --- a/Assets/Scripts/Inventory/Slot.cs +++ b/Assets/Scripts/Inventory/Slot.cs @@ -1,44 +1,69 @@ using System; -using System.Collections; -using System.Collections.Generic; using UnityEngine; +/// +/// A Slot can contain an item and its count. Used for the inventory. +/// [Serializable] public class Slot { + [Header("Slot")] + [Tooltip("The item which is in this slot")] [SerializeField] Item item = null; - + + [Tooltip("The count of the contained item")] [SerializeField] int count = 0; + #region GETTER & SETTER public Item GetItem() => item; public void SetItem(Item _item) => item = _item; - public int GetCount() => count; - - public void AddCount(int value) => count += value; - public void SetCount(int newCount) { count = newCount; if (count <= 0) Clear(); } + #endregion + + + /// + /// AddCount adds a value to the current count. The slot does not have any logic for the maximum stack size. + /// + /// Value which should be added. + public void AddCount(int value) => count += value; + /// + /// RemoveCount removes a value from the current count. It clears the slot if count == 0. + /// + /// Value which should be removed. public void RemoveCount(int value) { count -= value; if (count <= 0) Clear(); } + + /// + /// Sets the count to 0 + /// public void ResetCount() => count = 0; + /// + /// Clears the hole slot + /// public void Clear() { item = null; count = 0; } + + /// + /// Gives you a copy of this slot. + /// + /// Copy of this slot public Slot Copy() { Slot slot = new Slot(); @@ -47,6 +72,12 @@ public class Slot return slot; } + + /// + /// Sets the item and count of this 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 33ee21f..247b0d8 100644 --- a/Assets/Scripts/Inventory/UI_Inventory.cs +++ b/Assets/Scripts/Inventory/UI_Inventory.cs @@ -6,6 +6,7 @@ using UnityEngine.UI; public class UI_Inventory : MonoBehaviour { + /* [SerializeField] Inventory inventory; public Transform inventoryContainer; @@ -132,5 +133,5 @@ public class UI_Inventory : MonoBehaviour UIItemSlots[index] = itemSlotRectTransform; index++; } - } + }*/ } diff --git a/UserSettings/EditorUserSettings.asset b/UserSettings/EditorUserSettings.asset index 1983656..efdad57 100644 --- a/UserSettings/EditorUserSettings.asset +++ b/UserSettings/EditorUserSettings.asset @@ -6,10 +6,10 @@ EditorUserSettings: serializedVersion: 4 m_ConfigSettings: RecentlyUsedSceneGuid-0: - value: 515250075c0c595e5f5a5e71122159444e4e4a2f7a7d7f602f284d66b4b76661 + value: 565403025d065d5e5a5a0a7445205c44124e192b7b7f733375791f6be4b76d39 flags: 0 RecentlyUsedSceneGuid-1: - value: 565403025d065d5e5a5a0a7445205c44124e192b7b7f733375791f6be4b76d39 + value: 515250075c0c595e5f5a5e71122159444e4e4a2f7a7d7f602f284d66b4b76661 flags: 0 vcSharedLogLevel: value: 0d5e400f0650