using UnityEngine;
///
/// Interaction requirement that allows combining the follower's held item with this pickup if a valid combination rule exists.
///
[RequireComponent(typeof(Pickup))]
public class CombineWithBehavior : InteractionRequirementBase
{
///
/// Attempts to combine the follower's held item with this pickup's item.
///
/// The follower attempting the interaction.
/// True if the combination was successful, false otherwise.
public override bool TryInteract(FollowerController follower)
{
var heldItem = follower.CurrentlyHeldItem;
var pickup = GetComponent();
if (heldItem == null)
{
// DebugUIMessage.Show("You need an item to combine.");
OnFailure?.Invoke();
return true;
}
if (pickup == null || pickup.itemData == null)
{
DebugUIMessage.Show("Target item is missing or invalid.");
OnFailure?.Invoke();
return false;
}
var rule = GameManager.Instance.GetCombinationRule(heldItem, pickup.itemData);
if (rule != null && rule.resultPrefab != null)
{
// Instantiate the result prefab at the pickup's position
var resultObj = GameObject.Instantiate(rule.resultPrefab, pickup.transform.position, Quaternion.identity);
var resultPickup = resultObj.GetComponent();
if (resultPickup != null)
{
// Hide and parent to follower
resultObj.SetActive(false);
resultObj.transform.SetParent(follower.transform);
// Set held item and icon from the spawned prefab
follower.SetHeldItem(resultPickup.itemData, resultPickup.iconRenderer);
// Cache the spawned object as the held item
follower.CacheHeldPickupObject(resultObj);
follower.justCombined = true;
OnSuccess?.Invoke();
return true;
}
else
{
Debug.LogWarning("Result prefab does not have a Pickup component.");
GameObject.Destroy(resultObj);
OnFailure?.Invoke();
return false;
}
}
else
{
// DebugUIMessage.Show($"Cannot combine {heldItem.itemName ?? \"an item\"} with {pickup.itemData.itemName ?? \"target item\"}.");
OnFailure?.Invoke();
return true;
}
}
}