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; private bool pickupInProgress = false; /// /// 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 (player approaches, then follower picks up). /// 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(); var followerController = followerObj.GetComponent(); 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 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); } } /// /// 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 } }