61 lines
1.5 KiB
C#
61 lines
1.5 KiB
C#
using UnityEngine;
|
|
using System.Collections.Generic;
|
|
|
|
public class Inventory : MonoBehaviour
|
|
{
|
|
public int maxCapacity = 20;
|
|
protected Dictionary<string, int> items = new();
|
|
public List<InventoryEntry> startingItems; //List for inspector
|
|
private int gold;
|
|
public int Gold
|
|
{
|
|
get { return gold; }
|
|
set { gold = Mathf.Max(0, value); }
|
|
}
|
|
|
|
void Start()
|
|
{
|
|
// Initialize inventory with starting items
|
|
foreach (var entry in startingItems)
|
|
{
|
|
for (int i = 0; i < entry.quantity; i++)
|
|
{
|
|
AddItem(entry.itemName);
|
|
}
|
|
}
|
|
}
|
|
// 'virtual' allows this method to be overridden by subclasses like PlayerInventory
|
|
public virtual void AddItem(string itemName)
|
|
{
|
|
if (items.Count >= maxCapacity && !items.ContainsKey(itemName))
|
|
{
|
|
Debug.Log("Inventory is full. Cannot add new item: " + itemName);
|
|
return;
|
|
}
|
|
|
|
if (items.ContainsKey(itemName))
|
|
{
|
|
items[itemName]++;
|
|
}
|
|
else
|
|
{
|
|
items[itemName] = 1;
|
|
}
|
|
Debug.Log($"Added {itemName} to an inventory. Total: {items[itemName]}");
|
|
}
|
|
|
|
public void RemoveItem(string itemName)
|
|
{
|
|
items.Remove(itemName);
|
|
}
|
|
public int GetItemCount(string itemName)
|
|
{
|
|
return items.ContainsKey(itemName) ? items[itemName] : 0;
|
|
}
|
|
}
|
|
[System.Serializable]
|
|
public class InventoryEntry
|
|
{
|
|
public string itemName;
|
|
public int quantity;
|
|
} |