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/TempInventory.cs

54 lines
1.2 KiB
C#
Raw Normal View History

2023-06-01 17:03:48 +02:00
using System;
2023-04-18 04:29:21 +02:00
using System.Collections.Generic;
using UnityEngine;
2023-06-01 17:03:48 +02:00
[Serializable]
2023-04-18 04:29:21 +02:00
public class TempInventoryBuilderItem
{
public string name;
public int quantity;
}
public class TempInventory : MonoBehaviour
{
2023-06-01 17:03:48 +02:00
[SerializeField] private List<TempInventoryBuilderItem> initialInvent = new();
private readonly Dictionary<string, int> inventory = new();
// Start is called before the first frame update
private void Start()
{
foreach (var item in initialInvent) inventory[item.name] = item.quantity;
}
// Update is called once per frame
private void Update()
{
}
2023-04-18 04:29:21 +02:00
public int GetQuantityOf(string name)
{
2023-06-01 17:03:48 +02:00
if (inventory.ContainsKey(name)) return inventory[name];
2023-04-18 04:29:21 +02:00
return 0;
}
2023-06-01 17:03:48 +02:00
public bool Add(string name, int quantity = 1)
2023-04-18 04:29:21 +02:00
{
if (inventory.ContainsKey(name))
inventory[name] += quantity;
else
inventory.Add(name, quantity);
return true;
}
2023-06-01 17:03:48 +02:00
public bool Remove(string name, int quantity = 1)
2023-04-18 04:29:21 +02:00
{
if (inventory.ContainsKey(name))
{
inventory[name] = Mathf.Max(inventory[name] - quantity, 0);
return false;
}
2023-06-01 17:03:48 +02:00
return false;
2023-04-18 04:29:21 +02:00
}
2023-06-01 17:03:48 +02:00
}