144 lines
3 KiB
C#
144 lines
3 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using TMPro;
|
|
using Unity.Mathematics;
|
|
using UnityEngine;
|
|
|
|
public class InventoryManager : MonoBehaviour
|
|
{
|
|
#region Statication
|
|
|
|
public static InventoryManager instance;
|
|
|
|
private void Awake()
|
|
{
|
|
if (instance != null && instance != this)
|
|
{
|
|
Destroy(gameObject);
|
|
return;
|
|
}
|
|
DontDestroyOnLoad(this);
|
|
instance = this;
|
|
}
|
|
|
|
#endregion
|
|
[Serializable]
|
|
public class ItemAmount
|
|
{
|
|
public Item item;
|
|
public int amount;
|
|
}
|
|
public Item[] itemList; //list of all items in the game
|
|
public int maxCapacity;
|
|
public int currentlyStored;
|
|
public Dictionary<Item, int> storedItems = new();
|
|
public int money;
|
|
[Header("UI")]
|
|
public TextMeshProUGUI moneyUI;
|
|
public GameObject inventoryUI;
|
|
public InventoryUIObject templateUIObject;
|
|
public Transform inventoryGrid;
|
|
public Dictionary<Item, InventoryUIObject> allInventoryUIObjects = new();
|
|
|
|
private void Start()
|
|
{
|
|
UpdateMoneyUI();
|
|
}
|
|
|
|
public bool RemoveMoney(int change)
|
|
{
|
|
if (money - change >= 0)
|
|
{
|
|
money -= change;
|
|
UpdateMoneyUI();
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
public void AddMoney(int change)
|
|
{
|
|
money += change;
|
|
UpdateMoneyUI();
|
|
}
|
|
|
|
public void UpdateMoneyUI()
|
|
{
|
|
moneyUI.text = $"Money: {money}";
|
|
}
|
|
|
|
public int AddItem(Item itemToAdd, int amount = 1)
|
|
{
|
|
if (currentlyStored < maxCapacity)
|
|
{
|
|
if (!storedItems.ContainsKey(itemToAdd))
|
|
{
|
|
storedItems[itemToAdd] = 0;
|
|
}
|
|
if (currentlyStored + amount <= maxCapacity)
|
|
{
|
|
storedItems[itemToAdd] += amount;
|
|
currentlyStored += amount;
|
|
UpdateTaskTracker();
|
|
Debug.Log($"ADDED ALL {itemToAdd.itemName}");
|
|
return 0;
|
|
}
|
|
//return the amount that wasn't able to be stored.
|
|
int stored = maxCapacity - currentlyStored;
|
|
storedItems[itemToAdd] += stored;
|
|
currentlyStored += stored;
|
|
amount -= stored;
|
|
UpdateTaskTracker();
|
|
Debug.Log($"ADDED {stored} OF {itemToAdd.itemName} AND DROPPED {amount}");
|
|
return amount;
|
|
}
|
|
Debug.Log($"COULD NOT ADD {itemToAdd.itemName}; INVENTORY FULL");
|
|
return -1;
|
|
}
|
|
|
|
public void UpdateTaskTracker()
|
|
{
|
|
if (TaskManager.instance.activeTasks.Count > 0)
|
|
{
|
|
TaskManager.instance.CheckInventory();
|
|
}
|
|
}
|
|
public bool RemoveItem(Item itemToRemove, int amount = 1)
|
|
{
|
|
if (!storedItems.ContainsKey(itemToRemove) || storedItems[itemToRemove] < amount)
|
|
{
|
|
return false;
|
|
}
|
|
storedItems[itemToRemove] -= amount;
|
|
return true;
|
|
}
|
|
|
|
public int CheckItemAmount(Item itemToCheck)
|
|
{
|
|
return storedItems[itemToCheck];
|
|
}
|
|
public void UpdateInventoryUI()
|
|
{
|
|
foreach (var item in storedItems)
|
|
{
|
|
if (allInventoryUIObjects.ContainsKey(item.Key))
|
|
{
|
|
if (item.Value == 0)
|
|
{
|
|
Destroy(allInventoryUIObjects[item.Key]);
|
|
allInventoryUIObjects.Remove(item.Key);
|
|
}
|
|
else
|
|
{
|
|
allInventoryUIObjects[item.Key].SetObject(item.Key);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
InventoryUIObject newUIObject = Instantiate(templateUIObject, inventoryGrid);
|
|
newUIObject.gameObject.SetActive(true);
|
|
newUIObject.SetObject(item.Key);
|
|
allInventoryUIObjects[item.Key] = newUIObject;
|
|
}
|
|
}
|
|
}
|
|
}
|