using System.Collections.Generic;
using UnityEngine;
using System.Linq;
using UnityEngine.UIElements;
#if UNITY_EDITOR
using UnityEditor;
#endif
///
/// Handles post-battle looting when all enemies are defeated
/// Allows players to loot enemy corpses and manage inventory weight
///
public class PostBattleLootSystem : MonoBehaviour
{
[Header("UI Toolkit References")]
public UIDocument lootUIDocument;
public VisualTreeAsset lootScreenTemplate;
public PanelSettings panelSettings;
[Header("Currency Settings")]
public int baseGoldReward = 5;
public int baseSilverReward = 15;
public int baseCopperReward = 25;
[Header("Debug Settings")]
public bool showDebugLogs = true;
// Events
public event System.Action OnLootingComplete;
private List lootableEnemies = new List();
private bool isLootingActive = false;
private VisualElement rootElement;
private bool takeAllPressed = false;
///
/// Gets whether the looting process is currently active
///
public bool IsLootingActive => isLootingActive;
[System.Serializable]
public class LootableEnemy
{
public string enemyName;
public List dropItems = new List();
public int goldReward;
public int silverReward;
public int copperReward;
public bool hasBeenLooted = false;
public Vector3 corpsePosition;
public LootableEnemy(string name, Vector3 position)
{
enemyName = name;
corpsePosition = position;
}
}
[System.Serializable]
public class LootableItem
{
public string itemName;
public string description;
public int weight = 1;
public int value = 1; // In copper
public bool isSelected = false;
public LootableItem(string name, string desc, int itemWeight = 1, int itemValue = 1)
{
itemName = name;
description = desc;
weight = itemWeight;
value = itemValue;
}
}
///
/// Initialize looting system with defeated enemies
///
public void InitializeLootSystem(List defeatedEnemies)
{
lootableEnemies.Clear();
foreach (var enemy in defeatedEnemies)
{
if (enemy == null) continue;
Character enemyCharacter = enemy.GetComponent();
if (enemyCharacter == null || !enemyCharacter.IsDead) continue;
var lootableEnemy = new LootableEnemy(enemyCharacter.CharacterName, enemy.transform.position);
// Generate loot based on enemy type and random factors
GenerateEnemyLoot(lootableEnemy, enemyCharacter);
lootableEnemies.Add(lootableEnemy);
}
if (showDebugLogs)
Debug.Log($"š° Initialized loot system with {lootableEnemies.Count} lootable enemies");
}
///
/// Show the loot UI and start looting phase
///
public void StartLooting()
{
if (lootableEnemies.Count == 0)
{
if (showDebugLogs)
Debug.Log("š° No enemies to loot, ending battle");
OnLootingComplete?.Invoke();
return;
}
isLootingActive = true;
CreateAndShowLootUI();
if (showDebugLogs)
Debug.Log("š° Started post-battle looting phase");
}
///
/// Create and show the UI Toolkit loot interface
///
private void CreateAndShowLootUI()
{
// Find or create UI Document
if (lootUIDocument == null)
{
GameObject uiGO = new GameObject("LootUIDocument");
uiGO.transform.SetParent(transform);
lootUIDocument = uiGO.AddComponent();
}
// Set up the UI Document with proper references
SetupLootUIDocument();
// Get the root element
if (lootUIDocument.visualTreeAsset != null)
{
rootElement = lootUIDocument.rootVisualElement;
// Show the overlay
var overlay = rootElement.Q("LootScreenOverlay");
if (overlay != null)
{
overlay.style.display = DisplayStyle.Flex;
}
// Populate the UI with loot data
PopulateLootUI();
// Set up button callbacks
SetupUICallbacks();
}
else
{
// Fallback - create basic UI if template loading failed
Debug.LogWarning("PostBattleLootScreen.uxml not found. Creating basic UI.");
CreateBasicLootUI();
}
}
///
/// Set up the UIDocument with proper UXML template and panel settings
///
private void SetupLootUIDocument()
{
if (lootUIDocument == null) return;
// Load the UXML template if not assigned
if (lootScreenTemplate == null)
{
lootScreenTemplate = Resources.Load("UI/BattleSceneUI/PostBattleLootScreen");
if (lootScreenTemplate == null)
{
#if UNITY_EDITOR
// Try alternative path in editor
lootScreenTemplate = AssetDatabase.LoadAssetAtPath("Assets/UI/BattleSceneUI/PostBattleLootScreen.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
lootUIDocument.visualTreeAsset = lootScreenTemplate;
lootUIDocument.panelSettings = panelSettings;
// Load and apply stylesheet
var stylesheet = Resources.Load("UI/BattleSceneUI/PostBattleLootScreen");
if (stylesheet == null)
{
#if UNITY_EDITOR
stylesheet = AssetDatabase.LoadAssetAtPath("Assets/UI/BattleSceneUI/PostBattleLootScreen.uss");
#endif
}
if (stylesheet != null && lootUIDocument.rootVisualElement != null)
{
lootUIDocument.rootVisualElement.styleSheets.Add(stylesheet);
}
if (lootScreenTemplate == null)
{
Debug.LogError("PostBattleLootSystem: Could not load PostBattleLootScreen.uxml template!");
}
if (panelSettings == null)
{
Debug.LogWarning("PostBattleLootSystem: No PanelSettings found. UI may not display correctly.");
}
}
///
/// Create a basic fallback UI if the UXML template isn't available
///
private void CreateBasicLootUI()
{
// Create a simple overlay using UI Toolkit elements
rootElement = lootUIDocument.rootVisualElement;
// Create overlay
var overlay = new VisualElement();
overlay.name = "LootScreenOverlay";
overlay.style.position = Position.Absolute;
overlay.style.top = 0;
overlay.style.left = 0;
overlay.style.right = 0;
overlay.style.bottom = 0;
overlay.style.backgroundColor = new Color(0, 0, 0, 0.8f);
overlay.style.justifyContent = Justify.Center;
overlay.style.alignItems = Align.Center;
// Create background panel
var background = new VisualElement();
background.style.backgroundColor = new Color(0.1f, 0.1f, 0.2f, 0.95f);
background.style.borderTopWidth = 2;
background.style.borderBottomWidth = 2;
background.style.borderLeftWidth = 2;
background.style.borderRightWidth = 2;
background.style.borderTopColor = new Color(0.7f, 0.5f, 0.2f);
background.style.borderBottomColor = new Color(0.7f, 0.5f, 0.2f);
background.style.borderLeftColor = new Color(0.7f, 0.5f, 0.2f);
background.style.borderRightColor = new Color(0.7f, 0.5f, 0.2f);
background.style.borderTopLeftRadius = 15;
background.style.borderTopRightRadius = 15;
background.style.borderBottomLeftRadius = 15;
background.style.borderBottomRightRadius = 15;
background.style.paddingTop = 30;
background.style.paddingBottom = 30;
background.style.paddingLeft = 30;
background.style.paddingRight = 30;
background.style.width = new Length(80, LengthUnit.Percent);
background.style.maxWidth = 800;
// Add title
var title = new Label("š VICTORY! š");
title.style.fontSize = 36;
title.style.color = new Color(1f, 0.84f, 0f);
title.style.unityTextAlign = TextAnchor.MiddleCenter;
title.style.marginBottom = 20;
// Add loot info
var lootInfo = new Label();
lootInfo.name = "LootInfo";
lootInfo.style.fontSize = 14;
lootInfo.style.color = Color.white;
lootInfo.style.whiteSpace = WhiteSpace.Normal;
lootInfo.style.marginBottom = 20;
// Add continue button
var continueButton = new Button(() => CompleteLootingProcess());
continueButton.text = "Continue";
continueButton.name = "ContinueButton";
continueButton.style.fontSize = 16;
continueButton.style.paddingTop = 10;
continueButton.style.paddingBottom = 10;
continueButton.style.paddingLeft = 20;
continueButton.style.paddingRight = 20;
// Assemble the UI
background.Add(title);
background.Add(lootInfo);
background.Add(continueButton);
overlay.Add(background);
rootElement.Add(overlay);
// Populate basic loot info
PopulateBasicLootInfo(lootInfo);
}
///
/// Set up UI element callbacks for the loot interface
///
private void SetupUICallbacks()
{
if (rootElement == null) return;
// Set up Take All button
var takeAllButton = rootElement.Q