2025-09-04 13:08:14 +02:00
|
|
|
|
using UnityEngine;
|
|
|
|
|
|
|
2025-09-06 21:01:54 +02:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Interaction requirement that allows combining the follower's held item with this pickup if a valid combination rule exists.
|
|
|
|
|
|
/// </summary>
|
2025-09-04 13:08:14 +02:00
|
|
|
|
[RequireComponent(typeof(Pickup))]
|
|
|
|
|
|
public class CombineWithBehavior : InteractionRequirementBase
|
|
|
|
|
|
{
|
2025-09-06 21:01:54 +02:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Attempts to combine the follower's held item with this pickup's item.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="follower">The follower attempting the interaction.</param>
|
|
|
|
|
|
/// <returns>True if the combination was successful, false otherwise.</returns>
|
2025-09-04 13:08:14 +02:00
|
|
|
|
public override bool TryInteract(FollowerController follower)
|
|
|
|
|
|
{
|
2025-09-08 15:23:31 +02:00
|
|
|
|
var heldItem = follower.CurrentlyHeldItem;
|
2025-09-04 13:08:14 +02:00
|
|
|
|
var pickup = GetComponent<Pickup>();
|
|
|
|
|
|
if (heldItem == null)
|
|
|
|
|
|
{
|
2025-09-08 14:56:59 +02:00
|
|
|
|
// DebugUIMessage.Show("You need an item to combine.");
|
2025-09-04 13:08:14 +02:00
|
|
|
|
OnFailure?.Invoke();
|
2025-09-08 14:56:59 +02:00
|
|
|
|
return true;
|
2025-09-04 13:08:14 +02:00
|
|
|
|
}
|
|
|
|
|
|
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);
|
2025-09-08 16:13:13 +02:00
|
|
|
|
if (rule != null && rule.resultPrefab != null)
|
2025-09-04 13:08:14 +02:00
|
|
|
|
{
|
2025-09-08 16:13:13 +02:00
|
|
|
|
// Instantiate the result prefab at the pickup's position
|
|
|
|
|
|
var resultObj = GameObject.Instantiate(rule.resultPrefab, pickup.transform.position, Quaternion.identity);
|
|
|
|
|
|
var resultPickup = resultObj.GetComponent<Pickup>();
|
|
|
|
|
|
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;
|
|
|
|
|
|
}
|
2025-09-04 13:08:14 +02:00
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
2025-09-08 16:13:13 +02:00
|
|
|
|
// DebugUIMessage.Show($"Cannot combine {heldItem.itemName ?? \"an item\"} with {pickup.itemData.itemName ?? \"target item\"}.");
|
2025-09-04 13:08:14 +02:00
|
|
|
|
OnFailure?.Invoke();
|
2025-09-08 14:56:59 +02:00
|
|
|
|
return true;
|
2025-09-04 13:08:14 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|