2025-09-06 21:01:54 +02:00
|
|
|
|
using Input;
|
|
|
|
|
|
using UnityEngine;
|
2025-09-01 16:14:21 +02:00
|
|
|
|
|
|
|
|
|
|
public class Pickup : MonoBehaviour
|
|
|
|
|
|
{
|
2025-09-06 21:01:54 +02:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Data for the pickup item (icon, name, etc).
|
|
|
|
|
|
/// </summary>
|
2025-09-01 16:14:21 +02:00
|
|
|
|
public PickupItemData itemData;
|
2025-09-06 21:01:54 +02:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Renderer for the pickup icon.
|
|
|
|
|
|
/// </summary>
|
2025-09-01 16:14:21 +02:00
|
|
|
|
public SpriteRenderer iconRenderer;
|
|
|
|
|
|
private Interactable interactable;
|
|
|
|
|
|
|
2025-09-06 21:01:54 +02:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Unity Awake callback. Sets up icon, interactable, and event handlers.
|
|
|
|
|
|
/// </summary>
|
2025-09-01 16:14:21 +02:00
|
|
|
|
void Awake()
|
|
|
|
|
|
{
|
|
|
|
|
|
if (iconRenderer == null)
|
|
|
|
|
|
iconRenderer = GetComponent<SpriteRenderer>();
|
|
|
|
|
|
interactable = GetComponent<Interactable>();
|
|
|
|
|
|
if (interactable != null)
|
|
|
|
|
|
{
|
2025-09-04 15:01:28 +02:00
|
|
|
|
interactable.StartedInteraction += OnStartedInteraction;
|
|
|
|
|
|
interactable.InteractionComplete += OnInteractionComplete;
|
2025-09-01 16:14:21 +02:00
|
|
|
|
}
|
|
|
|
|
|
ApplyItemData();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-09-06 21:01:54 +02:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Unity OnDestroy callback. Cleans up event handlers.
|
|
|
|
|
|
/// </summary>
|
2025-09-01 16:14:21 +02:00
|
|
|
|
void OnDestroy()
|
|
|
|
|
|
{
|
|
|
|
|
|
if (interactable != null)
|
|
|
|
|
|
{
|
2025-09-04 15:01:28 +02:00
|
|
|
|
interactable.StartedInteraction -= OnStartedInteraction;
|
|
|
|
|
|
interactable.InteractionComplete -= OnInteractionComplete;
|
2025-09-01 16:14:21 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#if UNITY_EDITOR
|
2025-09-06 21:01:54 +02:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Unity OnValidate callback. Ensures icon and data are up to date in editor.
|
|
|
|
|
|
/// </summary>
|
2025-09-01 16:14:21 +02:00
|
|
|
|
void OnValidate()
|
|
|
|
|
|
{
|
|
|
|
|
|
if (iconRenderer == null)
|
|
|
|
|
|
iconRenderer = GetComponent<SpriteRenderer>();
|
|
|
|
|
|
ApplyItemData();
|
|
|
|
|
|
}
|
2025-09-04 00:00:46 +02:00
|
|
|
|
|
2025-09-06 21:01:54 +02:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Draws gizmos for pickup interaction range in the editor.
|
|
|
|
|
|
/// </summary>
|
2025-09-08 08:45:13 +02:00
|
|
|
|
void OnDrawGizmos()
|
2025-09-04 00:00:46 +02:00
|
|
|
|
{
|
2025-09-08 08:45:13 +02:00
|
|
|
|
float playerStopDistance;
|
|
|
|
|
|
if (Application.isPlaying)
|
|
|
|
|
|
{
|
|
|
|
|
|
playerStopDistance = GameManager.Instance.PlayerStopDistance;
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
// Load settings directly from asset path in editor
|
|
|
|
|
|
var settings = UnityEditor.AssetDatabase.LoadAssetAtPath<GameSettings>("Assets/Data/Settings/DefaultSettings.asset");
|
|
|
|
|
|
playerStopDistance = settings != null ? settings.playerStopDistance : 1.0f;
|
|
|
|
|
|
}
|
2025-09-04 00:00:46 +02:00
|
|
|
|
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);
|
|
|
|
|
|
}
|
2025-09-08 08:45:13 +02:00
|
|
|
|
}
|
2025-09-01 16:14:21 +02:00
|
|
|
|
#endif
|
|
|
|
|
|
|
2025-09-06 21:01:54 +02:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Applies the item data to the pickup (icon, name, etc).
|
|
|
|
|
|
/// </summary>
|
2025-09-01 16:14:21 +02:00
|
|
|
|
public void ApplyItemData()
|
|
|
|
|
|
{
|
|
|
|
|
|
if (itemData != null)
|
|
|
|
|
|
{
|
2025-09-04 11:12:19 +02:00
|
|
|
|
if (iconRenderer != null && itemData.mapSprite != null)
|
|
|
|
|
|
{
|
2025-09-01 16:14:21 +02:00
|
|
|
|
iconRenderer.sprite = itemData.mapSprite;
|
2025-09-04 11:12:19 +02:00
|
|
|
|
}
|
2025-09-01 16:14:21 +02:00
|
|
|
|
gameObject.name = itemData.itemName;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-09-06 21:01:54 +02:00
|
|
|
|
/// <summary>
|
2025-09-10 16:42:43 +02:00
|
|
|
|
/// Handles the start of an interaction (for feedback/UI only).
|
2025-09-06 21:01:54 +02:00
|
|
|
|
/// </summary>
|
2025-09-04 15:01:28 +02:00
|
|
|
|
private void OnStartedInteraction()
|
2025-09-01 16:14:21 +02:00
|
|
|
|
{
|
2025-09-10 16:42:43 +02:00
|
|
|
|
// Optionally, add pickup-specific feedback here (e.g., highlight, sound).
|
2025-09-01 16:14:21 +02:00
|
|
|
|
}
|
2025-09-04 15:01:28 +02:00
|
|
|
|
|
2025-09-06 21:01:54 +02:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Handles completion of the interaction (e.g., after pickup is done).
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="success">Whether the interaction was successful.</param>
|
2025-09-04 15:01:28 +02:00
|
|
|
|
private void OnInteractionComplete(bool success)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (!success) return;
|
|
|
|
|
|
// Optionally, add logic to disable the pickup or provide feedback
|
|
|
|
|
|
}
|
2025-09-01 16:14:21 +02:00
|
|
|
|
}
|