(Feat) add crafting discovery + notification + inventory tool tips
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using UnityEngine.Events;
|
||||
using Ashwild.Inventory;
|
||||
@@ -31,6 +32,7 @@ namespace Ashwild.Crafting
|
||||
|
||||
private PlayerInventory inventory;
|
||||
private bool bound;
|
||||
private readonly HashSet<CraftingRecipe> unlockedRecipes = new HashSet<CraftingRecipe>();
|
||||
|
||||
#endregion
|
||||
|
||||
@@ -43,19 +45,25 @@ namespace Ashwild.Crafting
|
||||
|
||||
/// <summary>
|
||||
/// Returns whether the local player can currently craft the recipe the given number of times.
|
||||
/// A recipe is only craftable once every ingredient has been discovered, on top of the usual
|
||||
/// inventory check — an ingredient still shown as a mystery ("?") blocks the craft.
|
||||
/// </summary>
|
||||
public bool CanCraft(CraftingRecipe recipe, int count = 1)
|
||||
{
|
||||
return PlayerInventory.Instance != null && recipe.CanCraft(PlayerInventory.Instance, count);
|
||||
return PlayerInventory.Instance != null
|
||||
&& IsFullyDiscovered(recipe)
|
||||
&& recipe.CanCraft(PlayerInventory.Instance, count);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Consumes the ingredients and produces the result if the local player can afford it.
|
||||
/// Consumes the ingredients and produces the result if the local player can afford it and has
|
||||
/// discovered every ingredient. The result item is added through the inventory, which in turn
|
||||
/// discovers it — so crafting an intermediate result reveals the recipes further down the chain.
|
||||
/// </summary>
|
||||
public bool TryCraft(CraftingRecipe recipe, int count = 1)
|
||||
{
|
||||
PlayerInventory inv = PlayerInventory.Instance;
|
||||
if (inv == null || !recipe.CanCraft(inv, count))
|
||||
if (inv == null || !IsFullyDiscovered(recipe) || !recipe.CanCraft(inv, count))
|
||||
return false;
|
||||
|
||||
// Consume ingredients
|
||||
@@ -82,6 +90,7 @@ namespace Ashwild.Crafting
|
||||
private void OnEnable()
|
||||
{
|
||||
PlayerEvents.LocalPlayerSpawned += HandleLocalPlayerSpawned;
|
||||
PlayerEvents.ItemDiscovered += HandleItemDiscovered;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -90,6 +99,7 @@ namespace Ashwild.Crafting
|
||||
private void OnDisable()
|
||||
{
|
||||
PlayerEvents.LocalPlayerSpawned -= HandleLocalPlayerSpawned;
|
||||
PlayerEvents.ItemDiscovered -= HandleItemDiscovered;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -98,7 +108,7 @@ namespace Ashwild.Crafting
|
||||
private void Start()
|
||||
{
|
||||
if (craftingUI != null)
|
||||
craftingUI.Build(recipes, CanCraft, CountItem, TryCraft);
|
||||
craftingUI.Build(recipes, CanCraft, CountItem, TryCraft, IsDiscovered, IsRecipeVisible);
|
||||
else
|
||||
Debug.LogError("[CraftingManager] No CraftingUI assigned — the crafting panel will not populate.", this);
|
||||
|
||||
@@ -134,6 +144,19 @@ namespace Ashwild.Crafting
|
||||
craftingUI.Refresh();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Reacts to a newly discovered item: announces any recipe it just unlocked (for the "NEW"
|
||||
/// craft notification), then refreshes the view so those recipes appear and their once-mystery
|
||||
/// ingredients are shown.
|
||||
/// </summary>
|
||||
private void HandleItemDiscovered(ItemData item)
|
||||
{
|
||||
NotifyNewlyUnlockedRecipes();
|
||||
|
||||
if (craftingUI != null)
|
||||
craftingUI.Refresh();
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Internal Helpers
|
||||
@@ -150,10 +173,41 @@ namespace Ashwild.Crafting
|
||||
|
||||
inventory.onSlotChanged.AddListener(HandleInventoryChanged);
|
||||
|
||||
SeedUnlockedRecipes();
|
||||
|
||||
if (craftingUI != null)
|
||||
craftingUI.Refresh();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Records the recipes already revealed at bind time (e.g. from restored discoveries) so they
|
||||
/// are treated as known and never fire a spurious "NEW" notification. With a fresh player this
|
||||
/// is empty since discovery starts at zero.
|
||||
/// </summary>
|
||||
private void SeedUnlockedRecipes()
|
||||
{
|
||||
for (int i = 0; i < recipes.Length; i++)
|
||||
if (IsRecipeVisible(recipes[i]))
|
||||
unlockedRecipes.Add(recipes[i]);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Raises a "NEW" notification for every recipe that has just become visible (its first
|
||||
/// discovered ingredient), each announced once. Called after an item is discovered.
|
||||
/// </summary>
|
||||
private void NotifyNewlyUnlockedRecipes()
|
||||
{
|
||||
for (int i = 0; i < recipes.Length; i++)
|
||||
{
|
||||
CraftingRecipe recipe = recipes[i];
|
||||
if (unlockedRecipes.Contains(recipe)) continue;
|
||||
if (!IsRecipeVisible(recipe)) continue;
|
||||
|
||||
unlockedRecipes.Add(recipe);
|
||||
PlayerEvents.RaiseRecipeDiscovered(recipe.Icon, recipe.RecipeName);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// How many of an item the local player currently holds (0 when offline).
|
||||
/// </summary>
|
||||
@@ -162,6 +216,37 @@ namespace Ashwild.Crafting
|
||||
return PlayerInventory.Instance != null ? PlayerInventory.Instance.CountItem(item) : 0;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Whether the local player has discovered the item (false when the discovery tracker isn't ready).
|
||||
/// </summary>
|
||||
private bool IsDiscovered(ItemData item)
|
||||
{
|
||||
return CraftDiscovery.Instance != null && CraftDiscovery.Instance.IsDiscovered(item);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Whether the recipe should appear at all: revealed as soon as at least one of its ingredients
|
||||
/// has been discovered. Undiscovered ingredients then show as a mystery until found.
|
||||
/// </summary>
|
||||
private bool IsRecipeVisible(CraftingRecipe recipe)
|
||||
{
|
||||
CraftingIngredient[] ingredients = recipe.Ingredients;
|
||||
for (int i = 0; i < ingredients.Length; i++)
|
||||
if (IsDiscovered(ingredients[i].item)) return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Whether every ingredient of the recipe has been discovered — the gate for actually crafting it.
|
||||
/// </summary>
|
||||
private bool IsFullyDiscovered(CraftingRecipe recipe)
|
||||
{
|
||||
CraftingIngredient[] ingredients = recipe.Ingredients;
|
||||
for (int i = 0; i < ingredients.Length; i++)
|
||||
if (!IsDiscovered(ingredients[i].item)) return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user