Files

139 lines
4.3 KiB
C#
Raw Permalink Normal View History

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