Added remove method

Added add slot method
Added comments
This commit is contained in:
julius
2022-02-12 12:14:40 +01:00
parent 9201677c1b
commit f0a5fe201c
3 changed files with 85 additions and 75 deletions

View File

@@ -4,114 +4,131 @@ using UnityEngine;
public class Inventory : MonoBehaviour public class Inventory : MonoBehaviour
{ {
/// <summary>
/// -------------------------------------------------------------------------------///
///
/// 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>
[SerializeField]public List<Slot> inventory = new List<Slot>();
[SerializeField] int maxSize;
public List<Slot> getInventory { get => inventory; set => inventory = value; }
public void createEmptyInventory(int size) List<Slot> inventory = new List<Slot>();
public List<Slot> 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<Slot>(); inventory = new List<Slot>();
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; inventory.Add(new Slot(maxSpaceOfSlots));
}
else
{
return false;
} }
} }
public int addItemAt(int index, Item itemType,int count) 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 (indexIsInRange(index) && (inventory[index].ItemType == null || inventory[index].ItemType.id == itemType.id))
{ {
if (inventory[index].ItemType == null) 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) 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) 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; return 0;
} else } else
{ {
int rest = count - inventory[index].MaxItems - inventory[index].Count; 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; return rest;
} }
} }
else 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) public int removeItemAt(int index, int count)
{ {
// Removes a number of items (count = 5) from a specific slot (index = 4).
if (inventory[index].ItemType != null && indexIsInRange(index)) // 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;// Can't remove any items if the slot is empty.
{
return count - i;
}
} }
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 else
{ {
//index not in range return -1;//Index not in range.
return -1;
} }
} }
/* private bool indexIsInRange(int index)
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)
{ {
// 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; if (index < inventory.Count && index >= 0)
}
bool removeItemAt(int index,int count = -1)
{
}
Item getItemAt(int index)
{
if (index < maxSize && index >= 0)
{ {
return items[index]; return true;
} }
else else
{ {
return null; return false;
} }
} }
*/
} }

View File

@@ -11,13 +11,17 @@ public class InventoryController : MonoBehaviour
private void Awake() private void Awake()
{ {
inventory = transform.GetComponent<Inventory>(); inventory = transform.GetComponent<Inventory>();
inventory.createEmptyInventory(5); inventory.createEmptyInventory(5,10);
Debug.Log(inventory.addItemAt(0, item1, 8));
Debug.Log(inventory.addItemAt(0, item1, 1)); inventory.addItemAt(0, item1, 8);
Debug.Log(inventory.addItemAt(3, item2, 15)); inventory.addItemAt(0, item1, 1);
inventory.addItemAt(3, item2, 15);
/*
Debug.Log(inventory.addItemAt(0, item2, 15)); Debug.Log(inventory.addItemAt(0, item2, 15));
Debug.Log(inventory.getInventory[0].Count); Debug.Log(inventory.getInventory[0].Count);
Debug.Log(inventory.removeItemAt(0, 10));
Debug.Log(inventory.getInventory[0].Count);
*/
uiInventory.setInventory(inventory); uiInventory.setInventory(inventory);
} }
void Start() void Start()

View File

@@ -15,21 +15,10 @@ public class Slot
count = 0; count = 0;
} }
public bool removeItem() public bool removeItem(int count = 1)
{ {
if (count > 0) this.count -= count;
{ return true;
count--;
if (count == 0)
{
item = null;
}
return true;
}
else
{
return false;
}
} }
public bool addItem(int count = 1) public bool addItem(int count = 1)
{ {