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

76 lines
2.3 KiB
C#
Raw Normal View History

2025-03-08 13:33:19 -05:00
using System.Collections.Generic;
using TMPro;
2025-04-18 15:54:50 -04:00
using UnityEngine;
using Game;
using Music;
using Player;
2025-04-16 19:57:54 -04:00
namespace Game
{
2025-04-18 15:54:50 -04:00
/// <summary>
/// Manages the win screen display for the game.
/// Displays the winning player's information and triggers the win screen animation.
/// </summary>
public class WinScreen : MonoBehaviour
{
/// <summary>
/// A singleton instance of the <see cref="WinScreen"/> class.
/// Ensures only one instance of the WinScreen exists at a time.
/// </summary>
public static WinScreen Instance;
2025-03-08 13:33:19 -05:00
2025-04-18 15:54:50 -04:00
/// <summary>
/// A list of text elements used to display player information on the win screen.
/// </summary>
public List<TextMeshProUGUI> playerTexts;
2025-03-08 13:33:19 -05:00
2025-04-18 15:54:50 -04:00
/// <summary>
/// Ensures only one instance of the WinScreen exists.
/// Destroys duplicate instances if they are created.
/// </summary>
private void Awake()
2025-03-08 13:33:19 -05:00
{
2025-04-18 15:54:50 -04:00
if (Instance == null)
{
Instance = this;
}
else
{
Destroy(this.gameObject);
}
}
2025-04-18 15:54:50 -04:00
/// <summary>
/// Displays the win screen for the specified player.
/// Updates the text and color of the win screen to reflect the winning player.
/// </summary>
/// <param name="player">The number of the winning player (1-based index).</param>
public void ShowWinScreen(int player)
2025-03-08 13:33:19 -05:00
{
2025-04-18 15:54:50 -04:00
// Validate the player index
if (player - 1 < 0 || player - 1 >= GameManager.playerColors.Count)
2025-03-08 13:33:19 -05:00
{
2025-04-18 15:54:50 -04:00
return;
2025-03-08 13:33:19 -05:00
}
2025-04-18 15:54:50 -04:00
// Update the text and color for each player text element
foreach (TextMeshProUGUI playerText in playerTexts)
{
playerText.text = "Player " + player;
if (playerText.color != Color.black)
{
playerText.color = GameManager.playerColors[player - 1];
}
}
2025-04-18 15:54:50 -04:00
// Trigger the win screen animation
Animator animator = GetComponent<Animator>();
if (animator == null)
{
return;
}
2025-04-18 15:54:50 -04:00
animator.SetTrigger("win");
}
}
2025-04-16 19:57:54 -04:00
}