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

70 lines
2.2 KiB
C#
Raw Normal View History

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