103 lines
3.2 KiB
C#
103 lines
3.2 KiB
C#
using System.Collections;
|
|
using UnityEngine;
|
|
|
|
/// <summary>
|
|
/// A generic component for objects that can be "cleaned" or "removed"
|
|
/// by fading them out. Requires a specific item in the inventory.
|
|
/// </summary>
|
|
public class FadableObject : MonoBehaviour, IInteractable
|
|
{
|
|
[Header("Settings")]
|
|
[Tooltip("The item required in the inventory to interact with this object.")]
|
|
[SerializeField] private ItemData requiredItem;
|
|
[Tooltip("How long it takes for the object to fade out completely.")]
|
|
[SerializeField] private float fadeDuration = 1.5f;
|
|
|
|
private bool isFading = false;
|
|
private Renderer _renderer;
|
|
|
|
private void Start()
|
|
{
|
|
_renderer = GetComponent<Renderer>();
|
|
if (_renderer == null)
|
|
{
|
|
_renderer = GetComponentInChildren<Renderer>();
|
|
}
|
|
if (requiredItem == null)
|
|
{
|
|
Debug.LogError($"FadableObject on '{gameObject.name}' is missing a 'requiredItem'.", this);
|
|
}
|
|
}
|
|
|
|
public void Interact()
|
|
{
|
|
if (isFading) return;
|
|
|
|
// Check if the player has the required item
|
|
if (InventorySystem.Instance.HasItem(requiredItem))
|
|
{
|
|
// We don't check for a specific quest here.
|
|
// We just notify the InteractionHandler, and the active quest will decide if it's relevant.
|
|
StartCoroutine(FadeOutRoutine());
|
|
}
|
|
else
|
|
{
|
|
Debug.Log($"A '{requiredItem?.itemName}' is needed to clean this!");
|
|
}
|
|
}
|
|
|
|
private IEnumerator FadeOutRoutine()
|
|
{
|
|
isFading = true;
|
|
|
|
// Notify the quest system that an interaction happened.
|
|
InteractionHandler.Instance.NotifyInteraction(gameObject);
|
|
|
|
// Trigger the appropriate animation based on the required item.
|
|
// This is a simple but effective way to manage different tool animations.
|
|
if (requiredItem.itemName.ToLower().Contains("broom"))
|
|
{
|
|
PlayerHandsController.Instance?.PlaySweepAnimation();
|
|
}
|
|
else if (requiredItem.itemName.ToLower().Contains("rag"))
|
|
{
|
|
PlayerHandsController.Instance?.PlayWipeAnimation();
|
|
}
|
|
|
|
// Fade-out logic
|
|
if (_renderer != null)
|
|
{
|
|
Material mat = _renderer.material;
|
|
Color startColor = mat.color;
|
|
float timer = 0f;
|
|
|
|
while (timer < fadeDuration)
|
|
{
|
|
timer += Time.deltaTime;
|
|
float alpha = Mathf.Lerp(startColor.a, 0f, timer / fadeDuration);
|
|
mat.color = new Color(startColor.r, startColor.g, startColor.b, alpha);
|
|
yield return null;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
yield return new WaitForSeconds(fadeDuration);
|
|
}
|
|
|
|
Debug.Log($"Object '{gameObject.name}' faded out and destroyed.");
|
|
Destroy(gameObject);
|
|
}
|
|
|
|
public string GetDescription()
|
|
{
|
|
if (isFading) return "";
|
|
|
|
// Provide a dynamic description
|
|
if (requiredItem != null)
|
|
{
|
|
if (requiredItem.itemName.ToLower().Contains("broom")) return "Sweep";
|
|
if (requiredItem.itemName.ToLower().Contains("rag")) return "Wipe";
|
|
}
|
|
return "Clean";
|
|
}
|
|
} |