57 lines
1.6 KiB
C#
57 lines
1.6 KiB
C#
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
|
|
public class CraftingSystem : MonoBehaviour
|
|
{
|
|
#region Statication
|
|
|
|
public static CraftingSystem instance;
|
|
|
|
private void Awake()
|
|
{
|
|
if (instance != null && instance != this)
|
|
{
|
|
Destroy(gameObject);
|
|
return;
|
|
}
|
|
DontDestroyOnLoad(this);
|
|
instance = this;
|
|
}
|
|
|
|
#endregion
|
|
public List<Item> knownCraftables = new();
|
|
[Header("UI")]
|
|
public Transform craftableUIGrid;
|
|
public CraftableUIObject templateCraftButton;
|
|
public void UpdateKnownCraftables(Item newCraftable)
|
|
{
|
|
knownCraftables.Add(newCraftable);
|
|
CraftableUIObject newButton = Instantiate(templateCraftButton, craftableUIGrid);
|
|
newButton.SetObject(newCraftable);
|
|
}
|
|
public bool TryCraftingItem(Item requestedItem)
|
|
{
|
|
List<InventoryManager.ItemAmount> missingItems = new();
|
|
foreach (InventoryManager.ItemAmount requiredMaterial in requestedItem.requiredMaterials)
|
|
{
|
|
if (InventoryManager.instance.CheckItemAmount(requiredMaterial.item) < requiredMaterial.amount)
|
|
{
|
|
InventoryManager.ItemAmount newMissingItem = new();
|
|
newMissingItem.item = requiredMaterial.item;
|
|
newMissingItem.amount = requiredMaterial.amount - InventoryManager.instance.CheckItemAmount(requiredMaterial.item);
|
|
}
|
|
}
|
|
if (missingItems.Count > 0)
|
|
{
|
|
return false; //supposed to return what items were missing
|
|
}
|
|
foreach (InventoryManager.ItemAmount requiredMaterial in requestedItem.requiredMaterials)
|
|
{
|
|
InventoryManager.instance.RemoveItem(requiredMaterial.item, requiredMaterial.amount);
|
|
}
|
|
InventoryManager.instance.AddItem(requestedItem, requestedItem.craftedAmount);
|
|
return true;
|
|
|
|
}
|
|
}
|