using System.Collections; using UnityEngine; public class Pickup : MonoBehaviour { // Enum to define the types of pickups available private enum PickUpType { GoldCoin, StaminaGlobe, HealthGlobe, } [SerializeField] private PickUpType pickUpType; // Type of pickup (e.g., GoldCoin, StaminaGlobe, etc.) [SerializeField] private float pickUpDistance = 5f; // Distance at which the pickup starts moving toward the player [SerializeField] private float accelerationRate = .2f; // Rate at which the pickup accelerates towards the player [SerializeField] private float moveSpeed = 3f; // Initial speed of the pickup's movement towards the player [SerializeField] private AnimationCurve animCurve; // Animation curve for the spawn animation [SerializeField] private float heightY = 1.5f; // Maximum height for the spawn animation [SerializeField] private float popDuration = 1f; // Duration of the spawn animation private Vector3 moveDir; // Direction in which the pickup moves towards the player private Rigidbody2D rb; // Rigidbody2D component for controlling physics private void Awake() { rb = GetComponent(); // Initialize the Rigidbody2D component } private void Start() { StartCoroutine(AnimCurveSpawnRoutine()); // Start the spawn animation using an animation curve } private void Update() { if (PlayerController.Instance != null) { Vector3 playerPos = PlayerController.Instance.transform.position; // Get the player's current position // If the player is within the pickup distance, move towards the player if (Vector3.Distance(transform.position, playerPos) < pickUpDistance) { moveDir = (playerPos - transform.position).normalized; // Calculate the direction towards the player moveSpeed += accelerationRate; // Increase the movement speed } else { moveDir = Vector3.zero; // Stop moving if the player is out of range moveSpeed = 0; // Reset the movement speed } } } private void FixedUpdate() { // Move the pickup in the calculated direction at the current speed rb.velocity = moveDir * moveSpeed * Time.deltaTime; } private void OnTriggerStay2D(Collider2D other) { // Check if the player is within the pickup's trigger area if (other.gameObject.GetComponent() != null) { DetectPickupType(); // Determine the type of pickup and apply its effect Destroy(gameObject); // Destroy the pickup after it has been collected } } // Coroutine to animate the pickup's spawn using an animation curve private IEnumerator AnimCurveSpawnRoutine() { Vector2 startPoint = transform.position; // Initial position of the pickup float randomX = transform.position.x + Random.Range(-2f, 2f); // Random X position for the end point float randomY = transform.position.y + Random.Range(-1f, 1f); // Random Y position for the end point Vector2 endPoint = new Vector2(randomX, randomY); // Calculate the end point for the animation float timePassed = 0f; // Animate the pickup's position over time while (timePassed < popDuration) { timePassed += Time.deltaTime; float linearT = timePassed / popDuration; // Linear time progression float heightT = animCurve.Evaluate(linearT); // Evaluate the animation curve float height = Mathf.Lerp(0f, heightY, heightT); // Calculate the height based on the curve // Move the pickup along a path from the start to the end point with a height adjustment transform.position = Vector2.Lerp(startPoint, endPoint, linearT) + new Vector2(0f, height); yield return null; } } // Determine the type of pickup and apply the corresponding effect private void DetectPickupType() { switch (pickUpType) { case PickUpType.GoldCoin: EconomyManager.Instance.UpdateCurrentGold(); // Update the player's gold count break; case PickUpType.HealthGlobe: if (PlayerHealth.Instance != null) { PlayerHealth.Instance.HealPlayer(); // Heal the player } break; case PickUpType.StaminaGlobe: Stamina.Instance.RefreshStamina(); // Refresh the player's stamina break; } } }