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

57 lines
1.4 KiB
C#
Raw Permalink 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-02 06:30:58 +02:00
namespace Item
2023-04-18 04:29:21 +02:00
{
2023-06-02 06:30:58 +02:00
[Serializable]
public class TempInventoryBuilderItem
2023-06-01 17:03:48 +02:00
{
2023-06-02 06:30:58 +02:00
public string name;
public int quantity;
2023-06-01 17:03:48 +02:00
}
2023-06-02 06:30:58 +02:00
public class TempInventory : MonoBehaviour
2023-06-01 17:03:48 +02:00
{
2023-06-02 06:30:58 +02:00
[SerializeField] private List<TempInventoryBuilderItem> initialInvent = new();
2023-04-18 04:29:21 +02:00
2023-06-02 06:30:58 +02:00
private readonly Dictionary<string, int> inventory = new();
2023-06-01 17:03:48 +02:00
2023-06-02 06:30:58 +02:00
// Start is called before the first frame update
private void Start()
{
foreach (var item in initialInvent) inventory[item.name] = item.quantity;
}
2023-04-18 04:29:21 +02:00
2023-06-02 06:30:58 +02:00
// Update is called once per frame
private void Update()
2023-04-18 04:29:21 +02:00
{
}
2023-06-02 06:30:58 +02:00
public int GetQuantityOf(string name)
{
if (inventory.ContainsKey(name)) return inventory[name];
return 0;
}
public bool Add(string name, int quantity = 1)
{
if (inventory.ContainsKey(name))
inventory[name] += quantity;
else
inventory.Add(name, quantity);
return true;
}
public bool Remove(string name, int quantity = 1)
{
if (inventory.ContainsKey(name))
{
inventory[name] = Mathf.Max(inventory[name] - quantity, 0);
return false;
}
return false;
}
2023-04-18 04:29:21 +02:00
}
2023-06-01 17:03:48 +02:00
}