Files
AppleHillsProduction/Assets/Scripts/Interactions/Pickup.cs

183 lines
6.5 KiB
C#

using Input;
using UnityEngine;
public class Pickup : MonoBehaviour
{
/// <summary>
/// Data for the pickup item (icon, name, etc).
/// </summary>
public PickupItemData itemData;
/// <summary>
/// Renderer for the pickup icon.
/// </summary>
public SpriteRenderer iconRenderer;
private Interactable interactable;
private bool pickupInProgress = false;
/// <summary>
/// Unity Awake callback. Sets up icon, interactable, and event handlers.
/// </summary>
void Awake()
{
if (iconRenderer == null)
iconRenderer = GetComponent<SpriteRenderer>();
interactable = GetComponent<Interactable>();
if (interactable != null)
{
interactable.StartedInteraction += OnStartedInteraction;
interactable.InteractionComplete += OnInteractionComplete;
}
ApplyItemData();
}
/// <summary>
/// Unity OnDestroy callback. Cleans up event handlers.
/// </summary>
void OnDestroy()
{
if (interactable != null)
{
interactable.StartedInteraction -= OnStartedInteraction;
interactable.InteractionComplete -= OnInteractionComplete;
}
}
#if UNITY_EDITOR
/// <summary>
/// Unity OnValidate callback. Ensures icon and data are up to date in editor.
/// </summary>
void OnValidate()
{
if (iconRenderer == null)
iconRenderer = GetComponent<SpriteRenderer>();
ApplyItemData();
}
/// <summary>
/// Draws gizmos for pickup interaction range in the editor.
/// </summary>
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<GameSettings>("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
/// <summary>
/// Applies the item data to the pickup (icon, name, etc).
/// </summary>
public void ApplyItemData()
{
if (itemData != null)
{
if (iconRenderer != null && itemData.mapSprite != null)
{
iconRenderer.sprite = itemData.mapSprite;
}
gameObject.name = itemData.itemName;
}
}
/// <summary>
/// Handles the start of an interaction (player approaches, then follower picks up).
/// </summary>
private void OnStartedInteraction()
{
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;
}
float playerStopDistance = GameManager.Instance.PlayerStopDistance;
float followerPickupDelay = GameManager.Instance.FollowerPickupDelay;
// --- Local event/coroutine handlers ---
void OnPlayerArrived()
{
playerController.OnArrivedAtTarget -= OnPlayerArrived;
playerController.OnMoveToCancelled -= OnPlayerMoveCancelled;
pickupInProgress = true;
StartCoroutine(DispatchFollower());
}
void OnPlayerMoveCancelled()
{
playerController.OnArrivedAtTarget -= OnPlayerArrived;
playerController.OnMoveToCancelled -= OnPlayerMoveCancelled;
pickupInProgress = false;
}
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;
bool interactionSuccess = true;
if (interactable != null)
{
interactionSuccess = interactable.OnFollowerArrived(followerController);
}
followerController.SetInteractionResult(interactionSuccess);
}
void OnFollowerReturned()
{
followerController.OnPickupReturned -= OnFollowerReturned;
pickupInProgress = false;
}
playerController.OnArrivedAtTarget += OnPlayerArrived;
playerController.OnMoveToCancelled += OnPlayerMoveCancelled;
Vector3 stopPoint = transform.position + (playerObj.transform.position - transform.position).normalized * playerStopDistance;
float distToPickup = Vector2.Distance(new Vector2(playerObj.transform.position.x, playerObj.transform.position.y), new Vector2(transform.position.x, transform.position.y));
float dist = Vector2.Distance(new Vector2(playerObj.transform.position.x, playerObj.transform.position.y), new Vector2(stopPoint.x, stopPoint.y));
if (distToPickup <= playerStopDistance || dist <= 0.2f)
{
OnPlayerArrived();
}
else
{
playerController.MoveToAndNotify(stopPoint);
}
}
/// <summary>
/// Handles completion of the interaction (e.g., after pickup is done).
/// </summary>
/// <param name="success">Whether the interaction was successful.</param>
private void OnInteractionComplete(bool success)
{
if (!success) return;
// Optionally, add logic to disable the pickup or provide feedback
}
}