StationObscurum/Assets/Scripts/Inventory/Inventory.cs
2023-06-01 08:03:48 -07:00

67 lines
1.9 KiB
C#

using System.Collections.Generic;
using UnityEngine;
/// <summary>
/// Inventory:
/// <list type="bullet">Inventory Name: The name of the inventory.</list>
/// <list type="bullet">Inventory Size: The amount of size the inventory has.</list>
/// <list type="bullet">
/// Invetory Items: List of all items in the inventory. No items in the world are "destroyed"
/// instead all items in inventory are disabled, but can be looked up by their item name.
/// </list>
/// </summary>
public class Inventory : MonoBehaviour
{
[SerializeField] private string inventoryName;
[SerializeField] private int inventorySize;
[SerializeField] private List<string> inventoryItems;
private int inventoryReserved;
/// <summary>
/// Adds item to inventory. Does not disable.
/// </summary>
/// <param name="item"></param>
/// <returns></returns>
public bool AddItem(CarryableItem item)
{
if (item.ItemSize + inventoryReserved > inventorySize) return false;
inventoryItems.Add(item.ItemName);
inventoryReserved += item.ItemSize;
//item.gameObject.SetActive(false);
return true;
}
private bool FindItemOfName(string name, out CarryableItem item)
{
//NOTE: May not work. May need to move instead of disable objects.
var items = Resources.FindObjectsOfTypeAll<CarryableItem>();
foreach (var item2 in items)
if (item2.ItemName == name)
{
item = item2;
return true;
}
item = null;
return false;
}
public bool RemoveItem(string name)
{
CarryableItem itemFound;
if (FindItemOfName(name, out itemFound))
{
itemFound.gameObject.SetActive(true);
inventoryItems.Remove(itemFound.ItemName);
inventoryReserved -= itemFound.ItemSize;
return true;
}
return false;
}
}