using Input;
using UnityEngine;
public class Pickup : MonoBehaviour
{
///
/// Data for the pickup item (icon, name, etc).
///
public PickupItemData itemData;
///
/// Renderer for the pickup icon.
///
public SpriteRenderer iconRenderer;
private Interactable interactable;
///
/// Unity Awake callback. Sets up icon, interactable, and event handlers.
///
void Awake()
{
if (iconRenderer == null)
iconRenderer = GetComponent();
interactable = GetComponent();
if (interactable != null)
{
interactable.StartedInteraction += OnStartedInteraction;
interactable.InteractionComplete += OnInteractionComplete;
}
ApplyItemData();
}
///
/// Unity OnDestroy callback. Cleans up event handlers.
///
void OnDestroy()
{
if (interactable != null)
{
interactable.StartedInteraction -= OnStartedInteraction;
interactable.InteractionComplete -= OnInteractionComplete;
}
}
#if UNITY_EDITOR
///
/// Unity OnValidate callback. Ensures icon and data are up to date in editor.
///
void OnValidate()
{
if (iconRenderer == null)
iconRenderer = GetComponent();
ApplyItemData();
}
///
/// Draws gizmos for pickup interaction range in the editor.
///
void OnDrawGizmos()
{
float playerStopDistance;
if (Application.isPlaying)
{
playerStopDistance = GameManager.Instance.PlayerStopDistance;
}
else
{
// Load settings directly from asset path in editor
var settings = UnityEditor.AssetDatabase.LoadAssetAtPath("Assets/Data/Settings/DefaultSettings.asset");
playerStopDistance = settings != null ? settings.playerStopDistance : 1.0f;
}
Gizmos.color = Color.yellow;
Gizmos.DrawWireSphere(transform.position, playerStopDistance);
GameObject playerObj = GameObject.FindGameObjectWithTag("Player");
if (playerObj != null)
{
Vector3 stopPoint = transform.position + (playerObj.transform.position - transform.position).normalized * playerStopDistance;
Gizmos.color = Color.cyan;
Gizmos.DrawSphere(stopPoint, 0.15f);
}
}
#endif
///
/// Applies the item data to the pickup (icon, name, etc).
///
public void ApplyItemData()
{
if (itemData != null)
{
if (iconRenderer != null && itemData.mapSprite != null)
{
iconRenderer.sprite = itemData.mapSprite;
}
gameObject.name = itemData.itemName;
}
}
///
/// Handles the start of an interaction (for feedback/UI only).
///
private void OnStartedInteraction()
{
// Optionally, add pickup-specific feedback here (e.g., highlight, sound).
}
///
/// Handles completion of the interaction (e.g., after pickup is done).
///
/// Whether the interaction was successful.
private void OnInteractionComplete(bool success)
{
if (!success) return;
// Optionally, add logic to disable the pickup or provide feedback
}
}