2025-03-17 19:59:41 -04:00
|
|
|
using UnityEngine;
|
|
|
|
|
|
|
|
|
|
public class HatRespawn : MonoBehaviour
|
|
|
|
|
{
|
2025-03-28 22:50:33 -04:00
|
|
|
private float lastInteractionTime;
|
|
|
|
|
private const float respawnTime = 10f;
|
|
|
|
|
private bool isDropped;
|
|
|
|
|
|
|
|
|
|
void Start()
|
|
|
|
|
{
|
|
|
|
|
lastInteractionTime = Time.time;
|
|
|
|
|
isDropped = false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Update() // Checks if the hat has been inactive for too long
|
|
|
|
|
{
|
|
|
|
|
if (isDropped && Time.time - lastInteractionTime > respawnTime)
|
|
|
|
|
{
|
2025-03-28 23:07:33 -04:00
|
|
|
Debug.Log("Hat has been inactive for too long. Respawning...");
|
2025-03-28 22:50:33 -04:00
|
|
|
RespawnHat();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void OnTriggerEnter2D(Collider2D collision) // Respawns the hat if it falls out of bounds
|
2025-03-17 19:59:41 -04:00
|
|
|
{
|
|
|
|
|
if (collision.gameObject.CompareTag("Platformer Hazard"))
|
|
|
|
|
{
|
2025-03-28 23:07:33 -04:00
|
|
|
Debug.Log("Hat collided with Platformer Hazard. Respawning...");
|
2025-03-28 22:50:33 -04:00
|
|
|
RespawnHat();
|
2025-03-17 19:59:41 -04:00
|
|
|
}
|
|
|
|
|
}
|
2025-03-28 22:50:33 -04:00
|
|
|
|
|
|
|
|
public void Interact() // Updates the player interaction time
|
|
|
|
|
{
|
|
|
|
|
lastInteractionTime = Time.time;
|
|
|
|
|
isDropped = false;
|
2025-03-28 23:07:33 -04:00
|
|
|
Debug.Log("Hat interacted with. Resetting timer.");
|
2025-03-28 22:50:33 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void OnHatDropped() // Resets the timer when the hat is dropped
|
|
|
|
|
{
|
|
|
|
|
lastInteractionTime = Time.time;
|
|
|
|
|
isDropped = true;
|
2025-03-28 23:07:33 -04:00
|
|
|
Debug.Log("Hat dropped. Starting respawn timer.");
|
2025-03-28 22:50:33 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void RespawnHat() // Respawns the hat at the designated spawn position
|
|
|
|
|
{
|
|
|
|
|
transform.position = GameManager.Instance.hatSpawnPosition;
|
|
|
|
|
GetComponent<Rigidbody2D>().linearVelocity = Vector2.zero;
|
|
|
|
|
transform.rotation = Quaternion.identity;
|
|
|
|
|
lastInteractionTime = Time.time; // Reset the timer after respawning
|
|
|
|
|
isDropped = false;
|
2025-03-28 23:07:33 -04:00
|
|
|
Debug.Log("Hat respawned at designated position.");
|
2025-03-28 22:50:33 -04:00
|
|
|
}
|
2025-03-17 19:59:41 -04:00
|
|
|
}
|