documentation

This commit is contained in:
Janis M
2022-02-22 11:37:55 +01:00
parent 94654c714a
commit 06355b269b
4 changed files with 103 additions and 48 deletions

View File

@@ -1,48 +1,41 @@
using System; using System;
using System.Collections;
using System.Collections.Generic; using System.Collections.Generic;
using UnityEngine; using UnityEngine;
/// <summary>
/// <c>Inventory</c> holds and managed a list of slots which can contain an item.
/// </summary>
public class Inventory : MonoBehaviour public class Inventory : MonoBehaviour
{ {
/// <summary> // Actions -> Used for Callback in other scripts
/// -------------------------------------------------------------------------------///
///
/// 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)
///
/// -------------------------------------------------------------------------------///
/// </summary>
public Item testItemType;
public static Action OnPlayerInventoryChanged; public static Action OnPlayerInventoryChanged;
public static Action OnPlayerItemAdded; public static Action OnPlayerItemAdded;
public static Action OnPlayerItemRemoved; public static Action OnPlayerItemRemoved;
// Serialized
[Header("Inventory")] [Header("Inventory")]
[Tooltip("The number of slots a player has in the inventory.")]
[SerializeField] [SerializeField]
int numberOfSlots = 20; int numberOfSlots = 20;
[Tooltip("Maximal count of items in ONE slot. How big is one stack.")]
[SerializeField] [SerializeField]
int maxSpaceOfSlots = 10; int maxSpaceOfSlots = 10;
[Tooltip("Is this inventory owned by the player")]
[SerializeField] [SerializeField]
bool isPlayerInventory = true; bool isPlayerInventory = true;
[Tooltip("List of the slots ('The inventory').")]
[SerializeField] [SerializeField]
List<Slot> inventory = new List<Slot>(); List<Slot> inventory = new List<Slot>();
public List<Slot> GetInventory() => inventory; public List<Slot> GetInventory() => inventory;
/// <summary>
/// Add slots to the inventory. You can extend the inventory space.
/// </summary>
/// <param name="_numberOfSlots">The count of slots which should be added to the inventory</param>
public void AddSlots(int _numberOfSlots) public void AddSlots(int _numberOfSlots)
{ {
for (int i = 0; i < _numberOfSlots; i++) for (int i = 0; i < _numberOfSlots; i++)
@@ -52,6 +45,11 @@ public class Inventory : MonoBehaviour
if(isPlayerInventory) if(isPlayerInventory)
OnPlayerInventoryChanged?.Invoke(); OnPlayerInventoryChanged?.Invoke();
} }
/// <summary>
/// <c>GetEmptySlot</c> gives you the first empty slot in the inventory.
/// </summary>
/// <returns>An empty slot of the inventory</returns>
Slot GetEmptySlot() Slot GetEmptySlot()
{ {
foreach (Slot slot in inventory) foreach (Slot slot in inventory)
@@ -61,6 +59,12 @@ public class Inventory : MonoBehaviour
} }
return null; return null;
} }
/// <summary>
/// <c>GetSlotByItem</c> lets you find a slot by the item it's containing.
/// </summary>
/// <param name="item"></param>
/// <returns>Slot with the specified item.</returns>
Slot GetSlotByItem(Item item) Slot GetSlotByItem(Item item)
{ {
foreach (Slot slot in inventory) foreach (Slot slot in inventory)
@@ -74,6 +78,12 @@ public class Inventory : MonoBehaviour
} }
return null; return null;
} }
/// <summary>
/// <c>GetRest</c> calculates the rest count of an item and a slot.
/// Example: Slot has a capacity of 20. Count of item is 22. Returns 2.
/// </summary>
/// <returns>The rest count of an item and a slot.</returns>
int GetRest(int count, Slot slot) int GetRest(int count, Slot slot)
{ {
if (CountFitsInSlot(count, slot)) if (CountFitsInSlot(count, slot))
@@ -85,10 +95,17 @@ public class Inventory : MonoBehaviour
return (count - (maxSpaceOfSlots - slot.GetCount())); return (count - (maxSpaceOfSlots - slot.GetCount()));
} }
} }
/// <summary>
/// <c>CountFitsInSlot</c> calculates if the item count fits in the slot capacity
/// </summary>
/// <param name="count"></param>
/// <param name="slot"></param>
/// <returns>True: Count fits. False: Count does not fit.</returns>
bool CountFitsInSlot(int count, Slot slot) bool CountFitsInSlot(int count, Slot slot)
{ {
int leftSize = maxSpaceOfSlots - slot.GetCount(); int leftSize = maxSpaceOfSlots - slot.GetCount();
if (leftSize > count) // left size > count if (leftSize > count)
{ {
return true; return true;
} }
@@ -97,7 +114,12 @@ public class Inventory : MonoBehaviour
return false; return false;
} }
} }
bool indexIsInRange(int index) /// <summary>
/// <c>IndexIsInRange</c> looks if an index of the inventory list is in range.
/// </summary>
/// <param name="index"></param>
/// <returns>True: Is in range. False: Is not in range.</returns>
bool IndexIsInRange(int index)
{ {
// Returns true if a given index is in the bounds of the inventory. // 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 // Example (maxSize = 10) index = -10 : false , index = 100: false, index = 7 : true
@@ -112,7 +134,13 @@ public class Inventory : MonoBehaviour
} }
} }
/// <summary>
/// <c>Remove</c> removes an item/s of the inventory list. It can be based in a specific index.
/// </summary>
/// <param name="itemType">which item should be removed</param>
/// <param name="count">Count of the item you want to remove</param>
/// <param name="invIndex">(optional) Index of the inventory list</param>
/// <returns>The rest of the count, which could not be removed.</returns>
public int Remove(Item itemType, int count, int invIndex = -1) public int Remove(Item itemType, int count, int invIndex = -1)
{ {
Slot slot = null; Slot slot = null;
@@ -124,7 +152,7 @@ public class Inventory : MonoBehaviour
// Get Slot // Get Slot
if (invIndex > -1) if (invIndex > -1)
{ {
if (indexIsInRange(invIndex)) if (IndexIsInRange(invIndex))
slot = inventory[invIndex]; slot = inventory[invIndex];
} }
else else
@@ -162,6 +190,13 @@ public class Inventory : MonoBehaviour
return -1; return -1;
} }
/// <summary>
/// <c>Add</c> adds an item/s of the inventory list. It can be based in a specific index.
/// </summary>
/// <param name="itemType">which item should be added</param>
/// <param name="count">Count of the item you want to add</param>
/// <param name="invIndex">(optional) Index of the inventory list</param>
/// <returns>The rest of the count, which could not be added.</returns>
public int Add(Item itemType, int count, int invIndex = -1) public int Add(Item itemType, int count, int invIndex = -1)
{ {
int rest = 0; int rest = 0;
@@ -174,7 +209,7 @@ public class Inventory : MonoBehaviour
// Get Slot // Get Slot
if (invIndex > -1) if (invIndex > -1)
{ {
if(indexIsInRange(invIndex)) if(IndexIsInRange(invIndex))
slot = inventory[invIndex]; slot = inventory[invIndex];
} }
else else
@@ -227,19 +262,7 @@ public class Inventory : MonoBehaviour
void Start() void Start()
{ {
// Initialize Inventory Slots
AddSlots(numberOfSlots); 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));
}
}
} }

View File

@@ -1,44 +1,69 @@
using System; using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine; using UnityEngine;
/// <summary>
/// A Slot can contain an item and its count. Used for the inventory.
/// </summary>
[Serializable] [Serializable]
public class Slot public class Slot
{ {
[Header("Slot")]
[Tooltip("The item which is in this slot")]
[SerializeField] [SerializeField]
Item item = null; Item item = null;
[Tooltip("The count of the contained item")]
[SerializeField] [SerializeField]
int count = 0; int count = 0;
#region GETTER & SETTER
public Item GetItem() => item; public Item GetItem() => item;
public void SetItem(Item _item) => item = _item; public void SetItem(Item _item) => item = _item;
public int GetCount() => count; public int GetCount() => count;
public void AddCount(int value) => count += value;
public void SetCount(int newCount) public void SetCount(int newCount)
{ {
count = newCount; count = newCount;
if (count <= 0) if (count <= 0)
Clear(); Clear();
} }
#endregion
/// <summary>
/// <c>AddCount</c> adds a value to the current count. The slot does not have any logic for the maximum stack size.
/// </summary>
/// <param name="value">Value which should be added.</param>
public void AddCount(int value) => count += value;
/// <summary>
/// <c>RemoveCount</c> removes a value from the current count. It clears the slot if count == 0.
/// </summary>
/// <param name="value">Value which should be removed.</param>
public void RemoveCount(int value) public void RemoveCount(int value)
{ {
count -= value; count -= value;
if (count <= 0) if (count <= 0)
Clear(); Clear();
} }
/// <summary>
/// Sets the count to 0
/// </summary>
public void ResetCount() => count = 0; public void ResetCount() => count = 0;
/// <summary>
/// Clears the hole slot
/// </summary>
public void Clear() public void Clear()
{ {
item = null; item = null;
count = 0; count = 0;
} }
/// <summary>
/// Gives you a copy of this slot.
/// </summary>
/// <returns>Copy of this slot</returns>
public Slot Copy() public Slot Copy()
{ {
Slot slot = new Slot(); Slot slot = new Slot();
@@ -47,6 +72,12 @@ public class Slot
return slot; return slot;
} }
/// <summary>
/// Sets the item and count of this slot.
/// </summary>
/// <param name="_item"></param>
/// <param name="_count"></param>
public void Set(Item _item, int _count = 1) public void Set(Item _item, int _count = 1)
{ {
item = _item; item = _item;

View File

@@ -6,6 +6,7 @@ using UnityEngine.UI;
public class UI_Inventory : MonoBehaviour public class UI_Inventory : MonoBehaviour
{ {
/*
[SerializeField] [SerializeField]
Inventory inventory; Inventory inventory;
public Transform inventoryContainer; public Transform inventoryContainer;
@@ -132,5 +133,5 @@ public class UI_Inventory : MonoBehaviour
UIItemSlots[index] = itemSlotRectTransform; UIItemSlots[index] = itemSlotRectTransform;
index++; index++;
} }
} }*/
} }

View File

@@ -6,10 +6,10 @@ EditorUserSettings:
serializedVersion: 4 serializedVersion: 4
m_ConfigSettings: m_ConfigSettings:
RecentlyUsedSceneGuid-0: RecentlyUsedSceneGuid-0:
value: 515250075c0c595e5f5a5e71122159444e4e4a2f7a7d7f602f284d66b4b76661 value: 565403025d065d5e5a5a0a7445205c44124e192b7b7f733375791f6be4b76d39
flags: 0 flags: 0
RecentlyUsedSceneGuid-1: RecentlyUsedSceneGuid-1:
value: 565403025d065d5e5a5a0a7445205c44124e192b7b7f733375791f6be4b76d39 value: 515250075c0c595e5f5a5e71122159444e4e4a2f7a7d7f602f284d66b4b76661
flags: 0 flags: 0
vcSharedLogLevel: vcSharedLogLevel:
value: 0d5e400f0650 value: 0d5e400f0650