197 lines
5.8 KiB
C#
197 lines
5.8 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Text;
|
|
using OTGIntegrated.Core;
|
|
using OTGIntegrated.Player;
|
|
using OTGIntegrated.UI;
|
|
using UnityEngine;
|
|
|
|
namespace OTGIntegrated.Crafting
|
|
{
|
|
public sealed class CraftingSystem : MonoBehaviour, IInteractable
|
|
{
|
|
public List<RecipeData> availableRecipes = new List<RecipeData>();
|
|
[TextArea(1, 3)] public string lastMessage;
|
|
|
|
private OTGIntegrated.Inventory.Inventory playerInventory;
|
|
private UIManager uiManager;
|
|
|
|
public event Action OnCraftingChanged;
|
|
public Transform InteractTransform => transform;
|
|
|
|
public void Initialize(OTGIntegrated.Inventory.Inventory inventory, UIManager manager)
|
|
{
|
|
playerInventory = inventory;
|
|
uiManager = manager;
|
|
Refresh();
|
|
}
|
|
|
|
private void Awake()
|
|
{
|
|
Collider collider = GetComponent<Collider>();
|
|
if (collider != null)
|
|
{
|
|
collider.isTrigger = true;
|
|
}
|
|
}
|
|
|
|
public bool CanCraft(RecipeData recipe)
|
|
{
|
|
if (recipe == null || playerInventory == null || recipe.resultItem == null)
|
|
{
|
|
return false;
|
|
}
|
|
|
|
if (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.resultItem, recipe.resultAmount);
|
|
}
|
|
|
|
public bool Craft(RecipeData recipe)
|
|
{
|
|
if (recipe == null)
|
|
{
|
|
lastMessage = "Рецепт не выбран.";
|
|
Refresh();
|
|
return false;
|
|
}
|
|
|
|
if (playerInventory == null)
|
|
{
|
|
lastMessage = "Инвентарь не подключен.";
|
|
Refresh();
|
|
return false;
|
|
}
|
|
|
|
if (!HasIngredients(recipe))
|
|
{
|
|
lastMessage = "Не хватает ресурсов: " + GetMissingIngredientsText(recipe);
|
|
GameEvents.RaiseNotification(lastMessage);
|
|
Refresh();
|
|
return false;
|
|
}
|
|
|
|
if (recipe.resultItem == null || !playerInventory.CanAddItem(recipe.resultItem, recipe.resultAmount))
|
|
{
|
|
lastMessage = "Недостаточно места для результата.";
|
|
GameEvents.RaiseNotification(lastMessage);
|
|
Refresh();
|
|
return false;
|
|
}
|
|
|
|
for (int i = 0; i < recipe.ingredients.Length; i++)
|
|
{
|
|
Ingredient ingredient = recipe.ingredients[i];
|
|
playerInventory.RemoveItem(ingredient.item, ingredient.amount);
|
|
}
|
|
|
|
playerInventory.AddItem(recipe.resultItem, recipe.resultAmount);
|
|
lastMessage = $"Создано: {recipe.resultItem.displayName} x{recipe.resultAmount}";
|
|
GameEvents.RaiseNotification(lastMessage);
|
|
Refresh();
|
|
return true;
|
|
}
|
|
|
|
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.GetAmount(ingredient.item);
|
|
if (current >= ingredient.amount)
|
|
{
|
|
continue;
|
|
}
|
|
|
|
if (builder.Length > 0)
|
|
{
|
|
builder.Append(", ");
|
|
}
|
|
|
|
builder.Append(ingredient.item.displayName);
|
|
builder.Append(" ");
|
|
builder.Append(current);
|
|
builder.Append("/");
|
|
builder.Append(ingredient.amount);
|
|
}
|
|
|
|
return builder.Length == 0 ? "место в инвентаре" : builder.ToString();
|
|
}
|
|
|
|
public string GetPrompt()
|
|
{
|
|
return "E — открыть верстак";
|
|
}
|
|
|
|
public bool CanInteract(GameObject interactor)
|
|
{
|
|
return enabled;
|
|
}
|
|
|
|
public void Interact(GameObject interactor)
|
|
{
|
|
if (uiManager != null)
|
|
{
|
|
uiManager.ToggleCrafting();
|
|
}
|
|
}
|
|
|
|
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;
|
|
}
|
|
}
|
|
}
|