StationObscurum/Assets/Scripts/Inventory/Inventory.cs

67 lines
1.9 KiB
C#
Raw Normal View History

2023-03-14 00:59:59 +01:00
using System.Collections.Generic;
using UnityEngine;
/// <summary>
2023-06-01 17:03:48 +02:00
/// 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>
2023-03-14 00:59:59 +01:00
/// </summary>
public class Inventory : MonoBehaviour
{
2023-06-01 17:03:48 +02:00
[SerializeField] private string inventoryName;
[SerializeField] private int inventorySize;
[SerializeField] private List<string> inventoryItems;
2023-03-14 00:59:59 +01:00
private int inventoryReserved;
/// <summary>
2023-06-01 17:03:48 +02:00
/// Adds item to inventory. Does not disable.
2023-03-14 00:59:59 +01:00
/// </summary>
/// <param name="item"></param>
/// <returns></returns>
public bool AddItem(CarryableItem item)
{
2023-06-01 17:03:48 +02:00
if (item.ItemSize + inventoryReserved > inventorySize) return false;
2023-03-14 00:59:59 +01:00
inventoryItems.Add(item.ItemName);
inventoryReserved += item.ItemSize;
//item.gameObject.SetActive(false);
return true;
}
2023-06-01 17:03:48 +02:00
2023-03-14 00:59:59 +01:00
private bool FindItemOfName(string name, out CarryableItem item)
{
//NOTE: May not work. May need to move instead of disable objects.
2023-06-01 17:03:48 +02:00
var items = Resources.FindObjectsOfTypeAll<CarryableItem>();
foreach (var item2 in items)
2023-03-14 00:59:59 +01:00
if (item2.ItemName == name)
{
item = item2;
return true;
}
2023-06-01 17:03:48 +02:00
2023-03-14 00:59:59 +01:00
item = null;
return false;
}
2023-06-01 17:03:48 +02:00
2023-03-14 00:59:59 +01:00
public bool RemoveItem(string name)
{
CarryableItem itemFound;
2023-06-01 17:03:48 +02:00
if (FindItemOfName(name, out itemFound))
2023-03-14 00:59:59 +01:00
{
itemFound.gameObject.SetActive(true);
inventoryItems.Remove(itemFound.ItemName);
inventoryReserved -= itemFound.ItemSize;
return true;
}
2023-04-18 04:29:21 +02:00
2023-06-01 17:03:48 +02:00
return false;
}
}