235 lines
6.6 KiB
C#
235 lines
6.6 KiB
C#
using Core;
|
|
using Core.Lifecycle;
|
|
using TMPro;
|
|
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
|
|
namespace Minigames.FortFight.UI
|
|
{
|
|
/// <summary>
|
|
/// Game Over UI - displays when a fort is defeated.
|
|
/// Shows match time, winner, and restart button.
|
|
/// </summary>
|
|
public class GameOverUI : ManagedBehaviour
|
|
{
|
|
#region Inspector Properties
|
|
|
|
[Header("UI References")]
|
|
[Tooltip("Root GameObject to show/hide the entire UI")]
|
|
[SerializeField] private GameObject rootPanel;
|
|
|
|
[Tooltip("Text showing elapsed time")]
|
|
[SerializeField] private TextMeshProUGUI elapsedTimeText;
|
|
|
|
[Tooltip("Text showing winner")]
|
|
[SerializeField] private TextMeshProUGUI winnerText;
|
|
|
|
[Tooltip("Restart button")]
|
|
[SerializeField] private Button restartButton;
|
|
|
|
[Header("Optional Visuals")]
|
|
[Tooltip("Optional canvas group for fade-in")]
|
|
[SerializeField] private CanvasGroup canvasGroup;
|
|
|
|
#endregion
|
|
|
|
#region Lifecycle
|
|
|
|
internal override void OnManagedAwake()
|
|
{
|
|
base.OnManagedAwake();
|
|
|
|
// Validate references
|
|
if (rootPanel == null)
|
|
{
|
|
Logging.Error("[GameOverUI] Root panel not assigned!");
|
|
}
|
|
|
|
if (elapsedTimeText == null)
|
|
{
|
|
Logging.Warning("[GameOverUI] Elapsed time text not assigned!");
|
|
}
|
|
|
|
if (winnerText == null)
|
|
{
|
|
Logging.Warning("[GameOverUI] Winner text not assigned!");
|
|
}
|
|
|
|
if (restartButton == null)
|
|
{
|
|
Logging.Error("[GameOverUI] Restart button not assigned!");
|
|
}
|
|
|
|
// Setup button listener
|
|
if (restartButton != null)
|
|
{
|
|
restartButton.onClick.AddListener(OnRestartClicked);
|
|
}
|
|
|
|
// Ensure canvas group exists for fade
|
|
if (canvasGroup == null && rootPanel != null)
|
|
{
|
|
canvasGroup = rootPanel.GetComponent<CanvasGroup>();
|
|
}
|
|
|
|
// Start hidden
|
|
Hide();
|
|
}
|
|
|
|
internal override void OnManagedDestroy()
|
|
{
|
|
base.OnManagedDestroy();
|
|
|
|
// Remove button listener
|
|
if (restartButton != null)
|
|
{
|
|
restartButton.onClick.RemoveListener(OnRestartClicked);
|
|
}
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region Event Handlers
|
|
|
|
private void OnRestartClicked()
|
|
{
|
|
Logging.Debug("[GameOverUI] Restart button clicked, reloading scene...");
|
|
RestartGame();
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region Display
|
|
|
|
/// <summary>
|
|
/// Show the game over UI with match results
|
|
/// Called by FortFightGameManager when game ends
|
|
/// </summary>
|
|
public void Show()
|
|
{
|
|
if (rootPanel != null)
|
|
{
|
|
rootPanel.SetActive(true);
|
|
}
|
|
|
|
// Get game manager for elapsed time
|
|
var gameManager = Core.FortFightGameManager.Instance;
|
|
if (gameManager != null)
|
|
{
|
|
float elapsedTime = gameManager.ElapsedGameTime;
|
|
UpdateElapsedTime(elapsedTime);
|
|
|
|
// Determine winner
|
|
DetermineWinner();
|
|
}
|
|
|
|
// Optional: Fade in
|
|
if (canvasGroup != null)
|
|
{
|
|
canvasGroup.alpha = 0f;
|
|
StartCoroutine(FadeIn());
|
|
}
|
|
|
|
Logging.Debug("[GameOverUI] Game over UI shown");
|
|
}
|
|
|
|
/// <summary>
|
|
/// Hide the game over UI
|
|
/// </summary>
|
|
public void Hide()
|
|
{
|
|
if (rootPanel != null)
|
|
{
|
|
rootPanel.SetActive(false);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Update the elapsed time display
|
|
/// </summary>
|
|
private void UpdateElapsedTime(float seconds)
|
|
{
|
|
if (elapsedTimeText == null) return;
|
|
|
|
// Format as MM:SS
|
|
int minutes = Mathf.FloorToInt(seconds / 60f);
|
|
int secs = Mathf.FloorToInt(seconds % 60f);
|
|
|
|
elapsedTimeText.text = $"{minutes:00}:{secs:00}";
|
|
}
|
|
|
|
/// <summary>
|
|
/// Determine and display the winner
|
|
/// </summary>
|
|
private void DetermineWinner()
|
|
{
|
|
if (winnerText == null) return;
|
|
|
|
var fortManager = Core.FortManager.Instance;
|
|
if (fortManager == null) return;
|
|
|
|
bool playerDefeated = fortManager.PlayerFort?.IsDefeated ?? false;
|
|
bool enemyDefeated = fortManager.EnemyFort?.IsDefeated ?? false;
|
|
|
|
if (playerDefeated && enemyDefeated)
|
|
{
|
|
winnerText.text = "DRAW!";
|
|
}
|
|
else if (playerDefeated)
|
|
{
|
|
winnerText.text = "PLAYER TWO WINS!";
|
|
}
|
|
else if (enemyDefeated)
|
|
{
|
|
winnerText.text = "PLAYER ONE WINS!";
|
|
}
|
|
else
|
|
{
|
|
winnerText.text = "GAME OVER";
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Fade in the UI over time
|
|
/// </summary>
|
|
private System.Collections.IEnumerator FadeIn()
|
|
{
|
|
float duration = 0.5f;
|
|
float elapsed = 0f;
|
|
|
|
while (elapsed < duration)
|
|
{
|
|
elapsed += Time.deltaTime;
|
|
if (canvasGroup != null)
|
|
{
|
|
canvasGroup.alpha = Mathf.Lerp(0f, 1f, elapsed / duration);
|
|
}
|
|
yield return null;
|
|
}
|
|
|
|
if (canvasGroup != null)
|
|
{
|
|
canvasGroup.alpha = 1f;
|
|
}
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region Restart
|
|
|
|
/// <summary>
|
|
/// Restart the game by reloading the current scene
|
|
/// </summary>
|
|
private void RestartGame()
|
|
{
|
|
// Use Unity's SceneManager to reload current scene
|
|
string currentScene = UnityEngine.SceneManagement.SceneManager.GetActiveScene().name;
|
|
Logging.Debug($"[GameOverUI] Reloading scene: {currentScene}");
|
|
UnityEngine.SceneManagement.SceneManager.LoadScene(currentScene);
|
|
}
|
|
|
|
#endregion
|
|
}
|
|
}
|
|
|