2025-09-01 16:14:21 +02:00
|
|
|
|
using UnityEngine;
|
|
|
|
|
|
|
|
|
|
|
|
public class Pickup : MonoBehaviour
|
|
|
|
|
|
{
|
|
|
|
|
|
public PickupItemData itemData;
|
|
|
|
|
|
public SpriteRenderer iconRenderer;
|
|
|
|
|
|
private Interactable interactable;
|
|
|
|
|
|
|
2025-09-04 00:00:46 +02:00
|
|
|
|
private bool pickupInProgress = false;
|
|
|
|
|
|
|
2025-09-01 16:14:21 +02:00
|
|
|
|
void Awake()
|
|
|
|
|
|
{
|
|
|
|
|
|
if (iconRenderer == null)
|
|
|
|
|
|
iconRenderer = GetComponent<SpriteRenderer>();
|
|
|
|
|
|
interactable = GetComponent<Interactable>();
|
|
|
|
|
|
if (interactable != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
interactable.Interacted += OnInteracted;
|
|
|
|
|
|
}
|
|
|
|
|
|
ApplyItemData();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void OnDestroy()
|
|
|
|
|
|
{
|
|
|
|
|
|
if (interactable != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
interactable.Interacted -= OnInteracted;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#if UNITY_EDITOR
|
|
|
|
|
|
void OnValidate()
|
|
|
|
|
|
{
|
|
|
|
|
|
if (iconRenderer == null)
|
|
|
|
|
|
iconRenderer = GetComponent<SpriteRenderer>();
|
|
|
|
|
|
ApplyItemData();
|
|
|
|
|
|
}
|
2025-09-04 00:00:46 +02:00
|
|
|
|
|
|
|
|
|
|
void OnDrawGizmos()
|
|
|
|
|
|
{
|
|
|
|
|
|
// Get stop distance from GameManager or default
|
|
|
|
|
|
float playerStopDistance = GameManager.Instance != null ? GameManager.Instance.PlayerStopDistance : 1.0f;
|
|
|
|
|
|
// Draw stop distance circle around pickup
|
|
|
|
|
|
Gizmos.color = Color.yellow;
|
|
|
|
|
|
Gizmos.DrawWireSphere(transform.position, playerStopDistance);
|
|
|
|
|
|
// Draw stop point (where player is told to move)
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
|
|
public void ApplyItemData()
|
|
|
|
|
|
{
|
|
|
|
|
|
if (itemData != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (iconRenderer != null)
|
|
|
|
|
|
iconRenderer.sprite = itemData.mapSprite;
|
|
|
|
|
|
gameObject.name = itemData.itemName;
|
|
|
|
|
|
// Optionally update other fields, e.g. description
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void OnInteracted()
|
|
|
|
|
|
{
|
2025-09-04 00:00:46 +02:00
|
|
|
|
if (pickupInProgress) return;
|
|
|
|
|
|
pickupInProgress = true;
|
|
|
|
|
|
// Find player and follower controllers
|
|
|
|
|
|
var playerObj = GameObject.FindGameObjectWithTag("Player");
|
|
|
|
|
|
var followerObj = GameObject.FindGameObjectWithTag("Pulver");
|
|
|
|
|
|
if (playerObj == null || followerObj == null)
|
|
|
|
|
|
{
|
|
|
|
|
|
Debug.LogWarning("Pickup: Player or Follower not found.");
|
|
|
|
|
|
pickupInProgress = false;
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
var playerController = playerObj.GetComponent<PlayerTouchController>();
|
|
|
|
|
|
var followerController = followerObj.GetComponent<FollowerController>();
|
|
|
|
|
|
if (playerController == null || followerController == null)
|
|
|
|
|
|
{
|
|
|
|
|
|
Debug.LogWarning("Pickup: PlayerTouchController or FollowerController missing.");
|
|
|
|
|
|
pickupInProgress = false;
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
// Get settings from GameManager
|
|
|
|
|
|
float playerStopDistance = GameManager.Instance != null ? GameManager.Instance.PlayerStopDistance : 1.0f;
|
|
|
|
|
|
float followerPickupDelay = GameManager.Instance != null ? GameManager.Instance.FollowerPickupDelay : 0.2f;
|
|
|
|
|
|
// Subscribe to player arrival event
|
|
|
|
|
|
void OnPlayerArrived()
|
|
|
|
|
|
{
|
|
|
|
|
|
playerController.OnArrivedAtTarget -= OnPlayerArrived;
|
|
|
|
|
|
// After player arrives, dispatch follower after delay
|
|
|
|
|
|
StartCoroutine(DispatchFollower());
|
|
|
|
|
|
}
|
|
|
|
|
|
System.Collections.IEnumerator DispatchFollower()
|
|
|
|
|
|
{
|
|
|
|
|
|
yield return new WaitForSeconds(followerPickupDelay);
|
|
|
|
|
|
// Subscribe to follower events
|
|
|
|
|
|
followerController.OnPickupArrived += OnFollowerArrived;
|
|
|
|
|
|
followerController.OnPickupReturned += OnFollowerReturned;
|
|
|
|
|
|
followerController.GoToPointAndReturn(transform.position, playerObj.transform);
|
|
|
|
|
|
}
|
|
|
|
|
|
void OnFollowerArrived()
|
|
|
|
|
|
{
|
|
|
|
|
|
followerController.OnPickupArrived -= OnFollowerArrived;
|
|
|
|
|
|
// Optionally: play pickup animation, etc.
|
|
|
|
|
|
}
|
|
|
|
|
|
void OnFollowerReturned()
|
|
|
|
|
|
{
|
|
|
|
|
|
followerController.OnPickupReturned -= OnFollowerReturned;
|
|
|
|
|
|
pickupInProgress = false;
|
|
|
|
|
|
}
|
|
|
|
|
|
playerController.OnArrivedAtTarget += OnPlayerArrived;
|
|
|
|
|
|
Vector3 stopPoint = transform.position + (playerObj.transform.position - transform.position).normalized * playerStopDistance;
|
|
|
|
|
|
playerController.MoveToAndNotify(stopPoint);
|
2025-09-01 16:14:21 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|