Files

157 lines
4.3 KiB
C#
Raw Permalink Normal View History

2026-06-04 18:31:31 +03:00
using System;
using System.Collections.Generic;
using System.Text;
using UnityEngine;
public class CraftingSystem : MonoBehaviour
{
public Inventory playerInventory;
public List<RecipeData> availableRecipes = new List<RecipeData>();
[TextArea(1, 3)] public string lastMessage;
public event Action OnCraftingChanged;
public bool CanCraft(RecipeData recipe)
{
if (recipe == null || playerInventory == null || recipe.result == null)
{
return false;
}
if (!recipe.unlockedByDefault || recipe.ingredients == null)
{
return false;
}
for (int i = 0; i < recipe.ingredients.Length; i++)
{
Ingredient ingredient = recipe.ingredients[i];
if (ingredient == null || ingredient.item == null || ingredient.amount <= 0)
{
return false;
}
if (!playerInventory.HasItem(ingredient.item, ingredient.amount))
{
return false;
}
}
return playerInventory.CanAddItem(recipe.result, recipe.resultAmount);
}
public void Craft(RecipeData recipe)
{
if (recipe == null)
{
lastMessage = "Рецепт не выбран.";
Refresh();
return;
}
if (playerInventory == null)
{
lastMessage = "Инвентарь не подключен.";
Refresh();
return;
}
if (!HasIngredients(recipe))
{
lastMessage = "Не хватает ресурсов: " + GetMissingIngredientsText(recipe);
Refresh();
return;
}
if (recipe.result == null || !playerInventory.CanAddItem(recipe.result, recipe.resultAmount))
{
lastMessage = "Недостаточно места в инвентаре для результата.";
Refresh();
return;
}
// Ingredients are removed only after confirming that the result can be added.
for (int i = 0; i < recipe.ingredients.Length; i++)
{
Ingredient ingredient = recipe.ingredients[i];
playerInventory.RemoveItem(ingredient.item, ingredient.amount);
}
playerInventory.AddItem(recipe.result, recipe.resultAmount);
lastMessage = "Создан предмет: " + recipe.GetResultText();
Refresh();
}
public string GetMissingIngredientsText(RecipeData recipe)
{
if (recipe == null || recipe.ingredients == null || playerInventory == null)
{
return "нет данных";
}
StringBuilder builder = new StringBuilder();
for (int i = 0; i < recipe.ingredients.Length; i++)
{
Ingredient ingredient = recipe.ingredients[i];
if (ingredient == null || ingredient.item == null)
{
continue;
}
int current = playerInventory.GetItemAmount(ingredient.item);
if (current >= ingredient.amount)
{
continue;
}
if (builder.Length > 0)
{
builder.Append(", ");
}
builder.Append(ingredient.item.itemName);
builder.Append(" ");
builder.Append(current);
builder.Append("/");
builder.Append(ingredient.amount);
}
return builder.Length == 0 ? "место в инвентаре" : builder.ToString();
}
public void SetRecipes(List<RecipeData> recipes)
{
availableRecipes = recipes ?? new List<RecipeData>();
Refresh();
}
public void Refresh()
{
OnCraftingChanged?.Invoke();
}
private bool HasIngredients(RecipeData recipe)
{
if (recipe == null || recipe.ingredients == null || playerInventory == null)
{
return false;
}
for (int i = 0; i < recipe.ingredients.Length; i++)
{
Ingredient ingredient = recipe.ingredients[i];
if (ingredient == null || ingredient.item == null || ingredient.amount <= 0)
{
return false;
}
if (!playerInventory.HasItem(ingredient.item, ingredient.amount))
{
return false;
}
}
return true;
}
}