using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UIElements; using UnityEngine.SceneManagement; using System.Linq; public class ItemShopManager : MonoBehaviour { [Header("Shop Settings")] public string shopName = "General Store"; [Header("Shop Items (ScriptableObjects)")] [SerializeField] private List availableItems = new List(); private UIDocument uiDocument; private TeamCharacter currentCustomer; // Callback for when character data changes public System.Action OnCharacterDataChanged; // UI Elements private VisualElement shopContainer; private TextField searchField; private DropdownField categoryFilter; private DropdownField customerDropdown; private ScrollView itemList; private Label shopTitle; private Label playerMoney; private Button closeButton; // Current filter state private string currentCategory = "All"; private string currentSearchTerm = ""; void Awake() { // Load all Item ScriptableObjects from Resources LoadAllItems(); } void Start() { // Use a coroutine to delay initialization by one frame // This ensures other scripts that might modify UIDocument have finished StartCoroutine(DelayedInitialization()); } private IEnumerator DelayedInitialization() { // Wait one frame to ensure all other Start() methods have completed yield return null; // Get all UIDocument components to debug UIDocument[] allUIDocuments = GetComponents(); Debug.Log($"Found {allUIDocuments.Length} UIDocument components on this GameObject"); // Initialize UI in Start() to ensure UIDocument is ready uiDocument = GetComponent(); if (uiDocument == null) { Debug.LogWarning("ItemShopManager: No UIDocument component found. Shop UI will be initialized when needed."); // Try to find UIDocument on parent or children as fallback uiDocument = GetComponentInParent(); if (uiDocument == null) { uiDocument = GetComponentInChildren(); } if (uiDocument != null) { Debug.Log("Found UIDocument on parent/child, using that instead"); } else { Debug.Log("No UIDocument found anywhere in hierarchy. Will create one when shop is opened."); yield break; // Exit gracefully instead of throwing an error } } if (uiDocument.visualTreeAsset == null) { Debug.LogWarning("ItemShopManager: UIDocument needs a Visual Tree Asset assigned! Will try to load when shop is opened."); yield break; } // Load and apply stylesheet var stylesheet = Resources.Load("UI/TownShopUI"); if (stylesheet != null) { uiDocument.rootVisualElement.styleSheets.Add(stylesheet); Debug.Log("✓ ItemShopManager: Stylesheet loaded successfully"); } else { Debug.LogWarning("⚠ ItemShopManager: Could not load TownShopUI stylesheet from Resources/UI/"); } // Ensure panel settings and proper sorting order are set EnsurePanelSettings(); InitializeUI(); } void LoadAllItems() { availableItems.Clear(); // Load all Item ScriptableObjects from Resources/Items folder and subfolders Item[] allItems = Resources.LoadAll("Items"); if (allItems.Length == 0) { Debug.LogWarning("No Item ScriptableObjects found in Resources/Items folder. Creating some default items..."); CreateDefaultItems(); } else { availableItems.AddRange(allItems); } // Also check for items in the direct Resources folder Item[] directItems = Resources.LoadAll(""); foreach (Item item in directItems) { if (!availableItems.Contains(item)) { availableItems.Add(item); } } Debug.Log($"Total items available in shop: {availableItems.Count}"); } void CreateDefaultItems() { // Create some default items if none are found // These will be temporary runtime items, but you should create proper ScriptableObject assets // Note: This creates runtime instances, not asset files var simpleSword = ScriptableObject.CreateInstance(); simpleSword.itemName = "Basic Sword"; // Match actual asset name simpleSword.description = "A basic sword for beginners."; simpleSword.itemType = ItemType.Weapon; simpleSword.goldCost = 10; simpleSword.minDamage = 1; simpleSword.maxDamage = 6; simpleSword.weaponType = WeaponType.Sword; simpleSword.searchTags = new string[] { "sword", "melee", "blade", "basic" }; availableItems.Add(simpleSword); var simpleBow = ScriptableObject.CreateInstance(); simpleBow.itemName = "Basic Bow"; // Match actual asset name simpleBow.description = "A basic bow for shooting arrows."; simpleBow.itemType = ItemType.Weapon; simpleBow.goldCost = 15; simpleBow.minDamage = 1; simpleBow.maxDamage = 4; simpleBow.range = 150; simpleBow.weaponType = WeaponType.Bow; simpleBow.searchTags = new string[] { "bow", "ranged", "arrow", "basic" }; availableItems.Add(simpleBow); var leatherArmor = ScriptableObject.CreateInstance(); leatherArmor.itemName = "Leather Armor"; leatherArmor.description = "Basic leather protection."; leatherArmor.itemType = ItemType.Armor; leatherArmor.goldCost = 12; leatherArmor.armorClass = 1; leatherArmor.armorType = ArmorType.Light; leatherArmor.armorSlot = ArmorSlot.Chest; leatherArmor.searchTags = new string[] { "leather", "armor", "chest", "light" }; availableItems.Add(leatherArmor); var healthPotion = ScriptableObject.CreateInstance(); healthPotion.itemName = "Health Potion"; healthPotion.description = "Restores health when consumed."; healthPotion.itemType = ItemType.Consumable; healthPotion.goldCost = 3; healthPotion.isConsumable = true; healthPotion.healthDiceCount = 1; healthPotion.healthDiceType = 6; healthPotion.healthBonus = 1; healthPotion.searchTags = new string[] { "potion", "health", "healing", "consumable" }; availableItems.Add(healthPotion); Debug.Log("Created default runtime items since no ScriptableObject assets were found."); } private bool SetupUIDocument() { // Try to get UIDocument component uiDocument = GetComponent(); if (uiDocument == null) { // Add UIDocument component if it doesn't exist uiDocument = gameObject.AddComponent(); } // Try to load the TownShopUI asset var shopUIAsset = Resources.Load("UI/TownShopUI"); if (shopUIAsset == null) { Debug.LogError("Could not load TownShopUI.uxml from Resources/UI/"); return false; } uiDocument.visualTreeAsset = shopUIAsset; // Load and apply stylesheet var stylesheet = Resources.Load("UI/TownShopUI"); if (stylesheet != null) { uiDocument.rootVisualElement.styleSheets.Add(stylesheet); Debug.Log("✓ ItemShopManager: Stylesheet loaded successfully"); } // Ensure panel settings EnsurePanelSettings(); return true; } private void InitializeUI() { var root = uiDocument.rootVisualElement; // Use the correct element names from TownShopUI.uxml shopContainer = root.Q("shop-container"); searchField = root.Q("search-field"); categoryFilter = root.Q("category-filter"); customerDropdown = root.Q("customer-dropdown"); itemList = root.Q("item-list"); shopTitle = root.Q