using UnityEngine;
using Game;
using Music;
using Player;
using System.Collections;
namespace Game
{
///
/// This class manages the behavior of the hat in the game, including its respawn logic.
/// The hat can be picked up, dropped, and respawned after a certain amount of time.
///
public class HatRespawn : MonoBehaviour
{
///
/// The last time the hat was interacted with.
///
private float lastInteractionTime;
///
/// The amount of time (in seconds) before the hat respawns after being inactive.
///
public const float respawnTime = 10f;
///
/// Indicates whether the hat is currently dropped.
///
private bool isDropped;
///
/// The initial position of the subhat (if applicable).
///
public Vector2 initialSubhatPosition;
///
/// A flag to check if the hat can be picked up.
///
public static bool canBePickedUp = true;
///
/// The initial scale of the hat.
///
public Vector2 initialScale;
///
/// Saves the initial scale of the hat when the game starts.
///
private void Awake()
{
initialScale = transform.lossyScale;
}
///
/// Sets up the hat's initial position and state when the game starts.
///
private void Start()
{
lastInteractionTime = Time.time;
isDropped = false;
// Place the hat at a random spawn position
transform.position = GameManager.Instance.hatSpawnPositions[Random.Range(0, GameManager.Instance.hatSpawnPositions.Count - 1)];
}
///
/// Checks if the hat has been inactive for too long and respawns it if necessary.
///
private void Update()
{
// Disable the hat's collider if the game is over
if (GameManager.Instance.gameOver)
{
GetComponent().enabled = false;
}
// Respawn the hat if it has been dropped for too long
if (isDropped && Time.time - lastInteractionTime > respawnTime)
{
StartCoroutine(RespawnHat());
}
}
///
/// Respawns the hat if it falls out of bounds.
///
/// The object that collided with the hat.
private void OnTriggerEnter2D(Collider2D collision)
{
if (collision.gameObject.CompareTag("Platformer Hazard"))
{
isDropped = true;
StartCoroutine(RespawnHat());
}
}
///
/// Updates the last interaction time when a player interacts with the hat.
///
public void Interact()
{
lastInteractionTime = Time.time;
isDropped = false;
}
///
/// Marks the hat as dropped and resets the timer.
///
public void OnHatDropped()
{
lastInteractionTime = Time.time;
isDropped = true;
}
///
/// Respawns the hat at a random spawn position after a short delay.
///
/// An IEnumerator for coroutine execution.
private IEnumerator RespawnHat()
{
lastInteractionTime = Time.time;
// Play the respawn animation
GetComponentInChildren().SetTrigger("respawn");
// Wait briefly before respawning the hat
yield return new WaitForSeconds(1f / 3f / 2f);
// Move the hat to a random spawn position and reset its state
transform.position = GameManager.Instance.hatSpawnPositions[Random.Range(0, GameManager.Instance.hatSpawnPositions.Count - 1)];
GetComponent().linearVelocity = Vector2.zero;
GetComponent().angularVelocity = 0f;
transform.rotation = Quaternion.identity;
isDropped = false;
}
}
}