Files
Crash-Course/Assets/Scripts/Game/HatRespawn.cs

63 lines
2.0 KiB
C#
Raw Normal View History

2025-04-16 19:57:54 -04:00
using UnityEngine; using Game; using Music; using Player;
namespace Game
{
2025-03-17 19:59:41 -04:00
public class HatRespawn : MonoBehaviour
{
private float lastInteractionTime;
2025-03-31 18:28:05 -04:00
public const float respawnTime = 10f;
private bool isDropped;
2025-04-17 18:32:27 -04:00
public Vector2 initialSubhatPosition;
2025-03-29 15:18:27 -04:00
public static bool canBePickedUp = true; // Flag to check if the hat can be picked up
void Start()
{
2025-04-17 18:32:27 -04:00
initialSubhatPosition = transform.GetChild(0).transform.localPosition;
lastInteractionTime = Time.time;
isDropped = false;
2025-03-29 15:18:27 -04:00
transform.position = GameManager.Instance.hatSpawnPositions[Random.Range(0, GameManager.Instance.hatSpawnPositions.Count - 1)];
}
void Update() // Checks if the hat has been inactive for too long
{
2025-04-12 17:24:51 -04:00
if (GameManager.Instance.gameOver) GetComponent<BoxCollider2D>().enabled = false;
if (isDropped && Time.time - lastInteractionTime > respawnTime)
{
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"))
{
RespawnHat();
2025-03-17 19:59:41 -04:00
}
}
public void Interact() // Updates the player interaction time
{
lastInteractionTime = Time.time;
isDropped = false;
}
public void OnHatDropped() // Resets the timer when the hat is dropped
{
lastInteractionTime = Time.time;
isDropped = true;
}
private void RespawnHat() // Respawns the hat at the designated spawn position
{
2025-03-29 15:18:27 -04:00
transform.position = GameManager.Instance.hatSpawnPositions[Random.Range(0, GameManager.Instance.hatSpawnPositions.Count - 1)];
GetComponent<Rigidbody2D>().linearVelocity = Vector2.zero;
2025-03-29 15:18:27 -04:00
GetComponent<Rigidbody2D>().angularVelocity = 0f;
transform.rotation = Quaternion.identity;
lastInteractionTime = Time.time; // Reset the timer after respawning
isDropped = false;
}
2025-03-17 19:59:41 -04:00
}
2025-04-16 19:57:54 -04:00
}