Files
Crash-Course/Assets/Scripts/Game/HatRespawn.cs
djkellerman 0838aebf89 Lots of fixes. 2 BUGS FOUND (read)
Added white border to flag sprite.  Removed debug.log testing code. Fixed hat respawn bug. Positioned all flags/end zones in every map properly. Extensively tested each gamemode with 3 players and found 2 bugs I cannot seem to fix.

First bug I found is the Win Screen that displays during FreeForAll always only shows Player 1 as the winner, regardless of the actual winner. The Win Screen fails to update to the proper player and color after a player wins. This only happens while playing FreeForAll, on any map other than Clouds. The Clouds map, and every other gamemode and map combination works just fine.

The second bug I found could be considered a feature if we wanted. During a Keep Away game, when the Hat is inactive for too long, it appears to spaz over the screen for a second right before respawning. I'm not sure if this is an animation bug or a spawnpoint bug in the code.

Other than these two bugs, every map has been extensively tested and tweaked to work properly for every gamemode.
2025-04-18 00:34:33 -04:00

79 lines
2.5 KiB
C#

using UnityEngine;
using Game;
using Music;
using Player;
using System.Collections;
namespace Game
{
public class HatRespawn : MonoBehaviour
{
private float lastInteractionTime;
public const float respawnTime = 10f;
private bool isDropped;
public Vector2 initialSubhatPosition;
public static bool canBePickedUp = true; // Flag to check if the hat can be picked up
public Vector2 initialScale;
private void Awake()
{
initialScale = transform.lossyScale;
}
void Start()
{
//initialSubhatPosition = transform.GetChild(0).transform.localPosition;
lastInteractionTime = Time.time;
isDropped = false;
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
{
if (GameManager.Instance.gameOver) GetComponent<BoxCollider2D>().enabled = false;
if (isDropped && Time.time - lastInteractionTime > respawnTime)
{
StartCoroutine(RespawnHat());
}
}
void OnTriggerEnter2D(Collider2D collision) // Respawns the hat if it falls out of bounds
{
if (collision.gameObject.CompareTag("Platformer Hazard"))
{
isDropped = true;
StartCoroutine(RespawnHat());
}
}
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 IEnumerator RespawnHat() // Respawns the hat at the designated spawn position
{
GetComponentInChildren<Animator>().SetTrigger("respawn");
yield return new WaitForSeconds(1f / 3f / 2f);
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;
lastInteractionTime = Time.time; // Reset the timer after respawning
isDropped = false;
}
}
}