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-04 00:00:46 +02:00
|
|
|
|
private bool pickupInProgress = false;
|
|
|
|
|
|
|
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-04 00:00:46 +02:00
|
|
|
|
void OnDrawGizmos()
|
|
|
|
|
|
{
|
2025-09-04 11:12:19 +02:00
|
|
|
|
float playerStopDistance = GameManager.Instance.PlayerStopDistance;
|
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-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>
|
|
|
|
|
|
/// Handles the start of an interaction (player approaches, then follower picks up).
|
|
|
|
|
|
/// </summary>
|
2025-09-04 15:01:28 +02:00
|
|
|
|
private void OnStartedInteraction()
|
2025-09-01 16:14:21 +02:00
|
|
|
|
{
|
2025-09-04 00:00:46 +02:00
|
|
|
|
if (pickupInProgress) return;
|
|
|
|
|
|
var playerObj = GameObject.FindGameObjectWithTag("Player");
|
|
|
|
|
|
var followerObj = GameObject.FindGameObjectWithTag("Pulver");
|
|
|
|
|
|
if (playerObj == null || followerObj == null)
|
|
|
|
|
|
{
|
|
|
|
|
|
Debug.LogWarning("Pickup: Player or Follower not found.");
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
var playerController = playerObj.GetComponent<PlayerTouchController>();
|
|
|
|
|
|
var followerController = followerObj.GetComponent<FollowerController>();
|
|
|
|
|
|
if (playerController == null || followerController == null)
|
|
|
|
|
|
{
|
|
|
|
|
|
Debug.LogWarning("Pickup: PlayerTouchController or FollowerController missing.");
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
2025-09-04 11:12:19 +02:00
|
|
|
|
float playerStopDistance = GameManager.Instance.PlayerStopDistance;
|
|
|
|
|
|
float followerPickupDelay = GameManager.Instance.FollowerPickupDelay;
|
2025-09-06 21:01:54 +02:00
|
|
|
|
// --- Local event/coroutine handlers ---
|
2025-09-04 00:00:46 +02:00
|
|
|
|
void OnPlayerArrived()
|
|
|
|
|
|
{
|
|
|
|
|
|
playerController.OnArrivedAtTarget -= OnPlayerArrived;
|
2025-09-04 11:19:05 +02:00
|
|
|
|
playerController.OnMoveToCancelled -= OnPlayerMoveCancelled;
|
2025-09-04 15:01:28 +02:00
|
|
|
|
pickupInProgress = true;
|
2025-09-04 00:00:46 +02:00
|
|
|
|
StartCoroutine(DispatchFollower());
|
|
|
|
|
|
}
|
2025-09-04 11:19:05 +02:00
|
|
|
|
void OnPlayerMoveCancelled()
|
|
|
|
|
|
{
|
|
|
|
|
|
playerController.OnArrivedAtTarget -= OnPlayerArrived;
|
|
|
|
|
|
playerController.OnMoveToCancelled -= OnPlayerMoveCancelled;
|
|
|
|
|
|
pickupInProgress = false;
|
|
|
|
|
|
}
|
2025-09-04 00:00:46 +02:00
|
|
|
|
System.Collections.IEnumerator DispatchFollower()
|
|
|
|
|
|
{
|
|
|
|
|
|
yield return new WaitForSeconds(followerPickupDelay);
|
|
|
|
|
|
followerController.OnPickupArrived += OnFollowerArrived;
|
|
|
|
|
|
followerController.OnPickupReturned += OnFollowerReturned;
|
|
|
|
|
|
followerController.GoToPointAndReturn(transform.position, playerObj.transform);
|
|
|
|
|
|
}
|
|
|
|
|
|
void OnFollowerArrived()
|
|
|
|
|
|
{
|
|
|
|
|
|
followerController.OnPickupArrived -= OnFollowerArrived;
|
2025-09-04 13:08:14 +02:00
|
|
|
|
bool interactionSuccess = true;
|
|
|
|
|
|
if (interactable != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
interactionSuccess = interactable.OnFollowerArrived(followerController);
|
|
|
|
|
|
}
|
|
|
|
|
|
followerController.SetInteractionResult(interactionSuccess);
|
2025-09-04 00:00:46 +02:00
|
|
|
|
}
|
|
|
|
|
|
void OnFollowerReturned()
|
|
|
|
|
|
{
|
|
|
|
|
|
followerController.OnPickupReturned -= OnFollowerReturned;
|
|
|
|
|
|
pickupInProgress = false;
|
|
|
|
|
|
}
|
|
|
|
|
|
playerController.OnArrivedAtTarget += OnPlayerArrived;
|
2025-09-04 11:19:05 +02:00
|
|
|
|
playerController.OnMoveToCancelled += OnPlayerMoveCancelled;
|
2025-09-04 00:00:46 +02:00
|
|
|
|
Vector3 stopPoint = transform.position + (playerObj.transform.position - transform.position).normalized * playerStopDistance;
|
2025-09-04 14:24:28 +02:00
|
|
|
|
float dist = Vector2.Distance(new Vector2(playerObj.transform.position.x, playerObj.transform.position.y), new Vector2(stopPoint.x, stopPoint.y));
|
|
|
|
|
|
if (dist <= 0.2f)
|
|
|
|
|
|
{
|
|
|
|
|
|
OnPlayerArrived();
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
playerController.MoveToAndNotify(stopPoint);
|
|
|
|
|
|
}
|
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
|
|
|
|
}
|