This repository has been archived on 2023-09-13. You can view files and clone it, but cannot push or open issues or pull requests.
station_obscurum_unity/Assets/Scripts/Inventory/Inventory.cs
2023-03-13 19:59:59 -04:00

74 lines
2.0 KiB
C#

using System.Collections;
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.
CarryableItem[] items = Resources.FindObjectsOfTypeAll<CarryableItem>();
foreach (CarryableItem 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;
}
}