using UnityEngine;
using UnityEngine.UIElements;
using System.Collections.Generic;
using System.Linq;
#if UNITY_EDITOR
using UnityEditor;
#endif
///
/// Manages the Game Over screen with statistics and final results using UI Toolkit
/// Shows when all player characters die
///
public class GameOverScreen : MonoBehaviour
{
[Header("UI Toolkit Settings")]
public UIDocument uiDocument;
public VisualTreeAsset gameOverTemplate;
public PanelSettings panelSettings;
[Header("Statistics Settings")]
public bool showDetailedStats = true;
[Header("Audio")]
public AudioClip gameOverMusic;
public AudioClip defeatSound;
private GameStatistics gameStats;
private VisualElement rootElement;
[System.Serializable]
public class GameStatistics
{
[Header("Time & Travel")]
public float totalPlayTime = 0f;
public float totalTravelTime = 0f;
public int daysTravaeled = 0;
public int locationsVisited = 0;
[Header("Combat")]
public int battlesWon = 0;
public int battlesLost = 0;
public int enemiesSlain = 0;
public int totalDamageDealt = 0;
public int totalDamageTaken = 0;
[Header("Social & Quests")]
public int peopleHelped = 0;
public int peopleSaved = 0;
public int questsCompleted = 0;
public int questsFailed = 0;
[Header("Character Development")]
public int finalFameLevel = 0;
public int totalExperienceGained = 0;
public int levelsGained = 0;
[Header("Economic")]
public int totalGoldEarned = 0;
public int totalGoldSpent = 0;
public int itemsBought = 0;
public int itemsSold = 0;
[Header("Survival")]
public int charactersLost = 0;
public int timesRested = 0;
public int healthPotionsUsed = 0;
public GameStatistics()
{
// Initialize with default values
}
///
/// Calculate derived statistics
///
public int GetTotalBattles() => battlesWon + battlesLost;
public float GetWinRate() => GetTotalBattles() > 0 ? (float)battlesWon / GetTotalBattles() * 100f : 0f;
public int GetNetGold() => totalGoldEarned - totalGoldSpent;
public float GetDamageRatio() => totalDamageTaken > 0 ? (float)totalDamageDealt / totalDamageTaken : 0f;
}
void Awake()
{
// Initialize game statistics
gameStats = new GameStatistics();
// Set up UI Document component
SetupUIDocument();
if (uiDocument != null && uiDocument.visualTreeAsset != null)
{
rootElement = uiDocument.rootVisualElement;
// Hide game over panel initially
if (rootElement != null)
rootElement.style.display = DisplayStyle.None;
// Set up button events
SetupUICallbacks();
}
}
///
/// Set up the UIDocument component with proper references
///
private void SetupUIDocument()
{
// Get or create UIDocument component
if (uiDocument == null)
uiDocument = GetComponent();
if (uiDocument == null)
uiDocument = gameObject.AddComponent();
// Load the UXML template if not assigned
if (gameOverTemplate == null)
{
gameOverTemplate = Resources.Load("UI/BattleSceneUI/GameOverScreen");
if (gameOverTemplate == null)
{
#if UNITY_EDITOR
// Try alternative path in editor
gameOverTemplate = AssetDatabase.LoadAssetAtPath("Assets/UI/BattleSceneUI/GameOverScreen.uxml");
#endif
}
}
// Load panel settings if not assigned
if (panelSettings == null)
{
panelSettings = Resources.Load("MainSettings");
if (panelSettings == null)
{
// Try alternative panel settings
panelSettings = Resources.Load("UI/TravelPanelSettings");
if (panelSettings == null)
{
// Try to find any PanelSettings in the project
var allPanelSettings = Resources.FindObjectsOfTypeAll();
if (allPanelSettings.Length > 0)
panelSettings = allPanelSettings[0];
}
}
}
// Configure the UIDocument
if (uiDocument != null)
{
uiDocument.visualTreeAsset = gameOverTemplate;
uiDocument.panelSettings = panelSettings;
// Load and apply stylesheet
var stylesheet = Resources.Load("UI/BattleSceneUI/GameOverScreen");
if (stylesheet == null)
{
#if UNITY_EDITOR
stylesheet = AssetDatabase.LoadAssetAtPath("Assets/UI/BattleSceneUI/GameOverScreen.uss");
#endif
}
if (stylesheet != null && uiDocument.rootVisualElement != null)
{
uiDocument.rootVisualElement.styleSheets.Add(stylesheet);
}
if (gameOverTemplate == null)
{
Debug.LogError("GameOverScreen: Could not load GameOverScreen.uxml template!");
}
if (panelSettings == null)
{
Debug.LogWarning("GameOverScreen: No PanelSettings found. UI may not display correctly.");
}
}
}
///
/// Set up UI element callbacks for the game over interface
///
private void SetupUICallbacks()
{
if (rootElement == null) return;
// Set up Restart Game button
var restartButton = rootElement.Q