|
|
@@ -1,6 +1,8 @@
|
|
|
+using System.Collections;
|
|
|
using System.Collections.Generic;
|
|
|
using UnityEngine;
|
|
|
using UnityEngine.UIElements;
|
|
|
+using UnityEngine.SceneManagement;
|
|
|
using System.Linq;
|
|
|
|
|
|
public class ItemShopManager : MonoBehaviour
|
|
|
@@ -21,6 +23,7 @@ public class ItemShopManager : MonoBehaviour
|
|
|
private VisualElement shopContainer;
|
|
|
private TextField searchField;
|
|
|
private DropdownField categoryFilter;
|
|
|
+ private DropdownField customerDropdown;
|
|
|
private ScrollView itemList;
|
|
|
private Label shopTitle;
|
|
|
private Label playerMoney;
|
|
|
@@ -38,18 +41,60 @@ public class ItemShopManager : MonoBehaviour
|
|
|
|
|
|
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<UIDocument>();
|
|
|
+ Debug.Log($"Found {allUIDocuments.Length} UIDocument components on this GameObject");
|
|
|
+
|
|
|
// Initialize UI in Start() to ensure UIDocument is ready
|
|
|
uiDocument = GetComponent<UIDocument>();
|
|
|
if (uiDocument == null)
|
|
|
{
|
|
|
- Debug.LogError("ItemShopManager requires a UIDocument component");
|
|
|
- return;
|
|
|
+ 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<UIDocument>();
|
|
|
+ if (uiDocument == null)
|
|
|
+ {
|
|
|
+ uiDocument = GetComponentInChildren<UIDocument>();
|
|
|
+ }
|
|
|
+
|
|
|
+ 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.LogError("ItemShopManager: UIDocument needs a Visual Tree Asset assigned! Please assign ShopUI.uxml");
|
|
|
- return;
|
|
|
+ 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<StyleSheet>("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
|
|
|
@@ -57,7 +102,6 @@ public class ItemShopManager : MonoBehaviour
|
|
|
|
|
|
InitializeUI();
|
|
|
}
|
|
|
-
|
|
|
void LoadAllItems()
|
|
|
{
|
|
|
availableItems.Clear();
|
|
|
@@ -73,7 +117,6 @@ public class ItemShopManager : MonoBehaviour
|
|
|
else
|
|
|
{
|
|
|
availableItems.AddRange(allItems);
|
|
|
- Debug.Log($"Loaded {allItems.Length} items from Resources/Items folder");
|
|
|
}
|
|
|
|
|
|
// Also check for items in the direct Resources folder
|
|
|
@@ -87,10 +130,6 @@ public class ItemShopManager : MonoBehaviour
|
|
|
}
|
|
|
|
|
|
Debug.Log($"Total items available in shop: {availableItems.Count}");
|
|
|
- foreach (Item item in availableItems)
|
|
|
- {
|
|
|
- Debug.Log($"- {item.itemName} ({item.itemType})");
|
|
|
- }
|
|
|
}
|
|
|
|
|
|
void CreateDefaultItems()
|
|
|
@@ -148,28 +187,55 @@ public class ItemShopManager : MonoBehaviour
|
|
|
Debug.Log("Created default runtime items since no ScriptableObject assets were found.");
|
|
|
}
|
|
|
|
|
|
- void InitializeUI()
|
|
|
+ private bool SetupUIDocument()
|
|
|
+ {
|
|
|
+ // Try to get UIDocument component
|
|
|
+ uiDocument = GetComponent<UIDocument>();
|
|
|
+ if (uiDocument == null)
|
|
|
+ {
|
|
|
+ // Add UIDocument component if it doesn't exist
|
|
|
+ uiDocument = gameObject.AddComponent<UIDocument>();
|
|
|
+ }
|
|
|
+
|
|
|
+ // Try to load the TownShopUI asset
|
|
|
+ var shopUIAsset = Resources.Load<VisualTreeAsset>("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<StyleSheet>("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;
|
|
|
- Debug.Log("ItemShopManager: Initializing UI elements...");
|
|
|
|
|
|
- shopContainer = root.Q<VisualElement>("ShopContainer");
|
|
|
- searchField = root.Q<TextField>("SearchField");
|
|
|
- categoryFilter = root.Q<DropdownField>("CategoryFilter");
|
|
|
- itemList = root.Q<ScrollView>("ItemList");
|
|
|
- shopTitle = root.Q<Label>("ShopTitle");
|
|
|
- playerMoney = root.Q<Label>("PlayerMoney");
|
|
|
- closeButton = root.Q<Button>("CloseButton");
|
|
|
+ // Use the correct element names from TownShopUI.uxml
|
|
|
+ shopContainer = root.Q<VisualElement>("shop-container");
|
|
|
+ searchField = root.Q<TextField>("search-field");
|
|
|
+ categoryFilter = root.Q<DropdownField>("category-filter");
|
|
|
+ customerDropdown = root.Q<DropdownField>("customer-dropdown");
|
|
|
+ itemList = root.Q<ScrollView>("item-list");
|
|
|
+ shopTitle = root.Q<Label>("shop-name");
|
|
|
+ playerMoney = root.Q<Label>("money-label");
|
|
|
+ closeButton = root.Q<Button>("close-button");
|
|
|
|
|
|
// Debug which elements were found
|
|
|
- Debug.Log($"ShopContainer found: {shopContainer != null}");
|
|
|
- Debug.Log($"SearchField found: {searchField != null}");
|
|
|
- Debug.Log($"CategoryFilter found: {categoryFilter != null}");
|
|
|
- Debug.Log($"ItemList found: {itemList != null}");
|
|
|
- Debug.Log($"ShopTitle found: {shopTitle != null}");
|
|
|
- Debug.Log($"PlayerMoney found: {playerMoney != null}");
|
|
|
- Debug.Log($"CloseButton found: {closeButton != null}");
|
|
|
-
|
|
|
// Setup category filter
|
|
|
if (categoryFilter != null)
|
|
|
{
|
|
|
@@ -178,6 +244,14 @@ public class ItemShopManager : MonoBehaviour
|
|
|
categoryFilter.RegisterValueChangedCallback(OnCategoryChanged);
|
|
|
}
|
|
|
|
|
|
+ // Setup customer dropdown (will be populated when opening shop)
|
|
|
+ if (customerDropdown != null)
|
|
|
+ {
|
|
|
+ // Initialize with empty choices to prevent index errors
|
|
|
+ customerDropdown.choices = new List<string>();
|
|
|
+ customerDropdown.RegisterValueChangedCallback(OnCustomerChanged);
|
|
|
+ }
|
|
|
+
|
|
|
// Setup search field
|
|
|
if (searchField != null)
|
|
|
{
|
|
|
@@ -230,18 +304,32 @@ public class ItemShopManager : MonoBehaviour
|
|
|
// Ensure UI is initialized
|
|
|
if (shopContainer == null)
|
|
|
{
|
|
|
- Debug.Log("ItemShopManager: UI not initialized, initializing now...");
|
|
|
+ Debug.Log("ItemShopManager: UI not initialized, attempting to set up...");
|
|
|
+
|
|
|
+ // Try to set up UIDocument if not already done
|
|
|
+ if (uiDocument == null)
|
|
|
+ {
|
|
|
+ if (!SetupUIDocument())
|
|
|
+ {
|
|
|
+ Debug.LogError("ItemShopManager: Could not set up UIDocument. Cannot open shop.");
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
InitializeUI();
|
|
|
}
|
|
|
|
|
|
if (shopContainer == null)
|
|
|
{
|
|
|
- Debug.LogError("ItemShopManager: shopContainer is null! Make sure ShopUI.uxml is assigned to the UIDocument and contains a 'ShopContainer' element.");
|
|
|
+ Debug.LogError("ItemShopManager: shopContainer is null! Make sure TownShopUI.uxml is assigned to the UIDocument and contains a 'shop-container' element.");
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
currentCustomer = customer;
|
|
|
|
|
|
+ // Update customer dropdown
|
|
|
+ UpdateCustomerDropdown();
|
|
|
+
|
|
|
// Update UI
|
|
|
if (shopTitle != null)
|
|
|
{
|
|
|
@@ -255,15 +343,6 @@ public class ItemShopManager : MonoBehaviour
|
|
|
if (shopContainer != null)
|
|
|
{
|
|
|
shopContainer.style.display = DisplayStyle.Flex;
|
|
|
- Debug.Log("ItemShopManager: Shop container visibility set to Flex");
|
|
|
-
|
|
|
- // Additional debugging
|
|
|
- Debug.Log($"Shop container style.display: {shopContainer.style.display}");
|
|
|
- Debug.Log($"Shop container resolvedStyle.display: {shopContainer.resolvedStyle.display}");
|
|
|
- Debug.Log($"Shop container visible: {shopContainer.visible}");
|
|
|
- Debug.Log($"Shop container position: {shopContainer.style.position}");
|
|
|
- Debug.Log($"Shop container width: {shopContainer.resolvedStyle.width}");
|
|
|
- Debug.Log($"Shop container height: {shopContainer.resolvedStyle.height}");
|
|
|
}
|
|
|
|
|
|
Debug.Log($"Opened {shopName} for {customer.name}");
|
|
|
@@ -277,7 +356,6 @@ public class ItemShopManager : MonoBehaviour
|
|
|
}
|
|
|
|
|
|
currentCustomer = null;
|
|
|
- Debug.Log("Closed shop");
|
|
|
}
|
|
|
|
|
|
private void OnCategoryChanged(ChangeEvent<string> evt)
|
|
|
@@ -357,41 +435,40 @@ public class ItemShopManager : MonoBehaviour
|
|
|
var itemContainer = new VisualElement();
|
|
|
itemContainer.AddToClassList("shop-item");
|
|
|
|
|
|
- // Item header with name and cost
|
|
|
- var header = new VisualElement();
|
|
|
- header.AddToClassList("item-header");
|
|
|
- header.style.flexDirection = FlexDirection.Row;
|
|
|
- header.style.justifyContent = Justify.SpaceBetween;
|
|
|
-
|
|
|
+ // Item name
|
|
|
var nameLabel = new Label(item.itemName);
|
|
|
nameLabel.AddToClassList("item-name");
|
|
|
- header.Add(nameLabel);
|
|
|
-
|
|
|
- var costLabel = new Label(item.GetCostString());
|
|
|
- costLabel.AddToClassList("item-cost");
|
|
|
- header.Add(costLabel);
|
|
|
-
|
|
|
- itemContainer.Add(header);
|
|
|
-
|
|
|
- // Item description
|
|
|
- var descriptionLabel = new Label(item.description);
|
|
|
- descriptionLabel.AddToClassList("item-description");
|
|
|
- descriptionLabel.style.whiteSpace = WhiteSpace.Normal;
|
|
|
- itemContainer.Add(descriptionLabel);
|
|
|
+ itemContainer.Add(nameLabel);
|
|
|
|
|
|
- // Item stats (if applicable)
|
|
|
+ // Item description with stats
|
|
|
+ string description = item.description;
|
|
|
string statsString = GetItemStatsString(item);
|
|
|
if (!string.IsNullOrEmpty(statsString))
|
|
|
{
|
|
|
- var statsLabel = new Label(statsString);
|
|
|
- statsLabel.AddToClassList("item-stats");
|
|
|
- itemContainer.Add(statsLabel);
|
|
|
+ description += $" {statsString}";
|
|
|
}
|
|
|
|
|
|
+ var descriptionLabel = new Label(description);
|
|
|
+ descriptionLabel.AddToClassList("item-description");
|
|
|
+ itemContainer.Add(descriptionLabel);
|
|
|
+
|
|
|
+ // Price and buy button container
|
|
|
+ var priceButtonContainer = new VisualElement();
|
|
|
+ priceButtonContainer.style.flexDirection = FlexDirection.Row;
|
|
|
+ priceButtonContainer.style.justifyContent = Justify.SpaceBetween;
|
|
|
+ priceButtonContainer.style.alignItems = Align.Center;
|
|
|
+
|
|
|
+ var costLabel = new Label(item.GetCostString());
|
|
|
+ costLabel.AddToClassList("item-price");
|
|
|
+ priceButtonContainer.Add(costLabel);
|
|
|
+
|
|
|
// Purchase button
|
|
|
var purchaseButton = new Button(() => PurchaseItem(item));
|
|
|
purchaseButton.text = "Buy";
|
|
|
- purchaseButton.AddToClassList("purchase-button");
|
|
|
+ purchaseButton.AddToClassList("buy-button");
|
|
|
+ priceButtonContainer.Add(purchaseButton);
|
|
|
+
|
|
|
+ itemContainer.Add(priceButtonContainer);
|
|
|
|
|
|
// Check if player can afford the item
|
|
|
bool canAfford = currentCustomer != null && CanCustomerAfford(item);
|
|
|
@@ -536,12 +613,10 @@ public class ItemShopManager : MonoBehaviour
|
|
|
{
|
|
|
case ItemType.Weapon:
|
|
|
currentCustomer.weapons.Add(item.itemName);
|
|
|
- Debug.Log($"Added weapon '{item.itemName}' to {currentCustomer.name}'s weapon inventory");
|
|
|
break;
|
|
|
|
|
|
case ItemType.Armor:
|
|
|
currentCustomer.armor.Add(item.itemName);
|
|
|
- Debug.Log($"Added armor '{item.itemName}' to {currentCustomer.name}'s armor inventory");
|
|
|
break;
|
|
|
|
|
|
case ItemType.Miscellaneous:
|
|
|
@@ -549,12 +624,11 @@ public class ItemShopManager : MonoBehaviour
|
|
|
case ItemType.Tool:
|
|
|
case ItemType.Accessory:
|
|
|
currentCustomer.miscItems.Add(item.itemName);
|
|
|
- Debug.Log($"Added item '{item.itemName}' to {currentCustomer.name}'s misc inventory");
|
|
|
break;
|
|
|
|
|
|
default:
|
|
|
currentCustomer.miscItems.Add(item.itemName);
|
|
|
- Debug.LogWarning($"Unknown item type for '{item.itemName}', added to misc items");
|
|
|
+ if (Application.isEditor) Debug.LogWarning($"Unknown item type for '{item.itemName}', added to misc items");
|
|
|
break;
|
|
|
}
|
|
|
|
|
|
@@ -781,4 +855,152 @@ public class ItemShopManager : MonoBehaviour
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+ private void OnCustomerChanged(ChangeEvent<string> evt)
|
|
|
+ {
|
|
|
+ // When customer is changed from dropdown, find the corresponding TeamCharacter
|
|
|
+ string selectedName = evt.newValue;
|
|
|
+
|
|
|
+ // Skip if no valid selection or error states
|
|
|
+ if (string.IsNullOrEmpty(selectedName) || selectedName == "No Customer" || selectedName.StartsWith("Error"))
|
|
|
+ {
|
|
|
+ Debug.Log("Customer dropdown selection is invalid, skipping");
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ // Try to find the character from different sources depending on the scene
|
|
|
+ TeamCharacter selectedCharacter = FindCharacterByName(selectedName);
|
|
|
+
|
|
|
+ if (selectedCharacter != null)
|
|
|
+ {
|
|
|
+ currentCustomer = selectedCharacter;
|
|
|
+ UpdatePlayerMoney();
|
|
|
+ Debug.Log($"Customer changed to: {selectedCharacter.name}");
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ Debug.LogWarning($"Could not find character with name: {selectedName}");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private void UpdateCustomerDropdown()
|
|
|
+ {
|
|
|
+ if (customerDropdown == null)
|
|
|
+ {
|
|
|
+ Debug.LogWarning("CustomerDropdown is null, cannot update");
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ List<string> customerOptions = new List<string>();
|
|
|
+
|
|
|
+ try
|
|
|
+ {
|
|
|
+ // Check what scene we're in and populate accordingly
|
|
|
+ if (UnityEngine.SceneManagement.SceneManager.GetActiveScene().name == "MainTeamSelectScene" ||
|
|
|
+ GameObject.Find("MainTeamSelectScript") != null)
|
|
|
+ {
|
|
|
+ // In MainTeamSelectScene - populate with team members
|
|
|
+ var mainTeamSelect = FindFirstObjectByType<MainTeamSelectScript>();
|
|
|
+ if (mainTeamSelect != null)
|
|
|
+ {
|
|
|
+ var configuredCharacters = mainTeamSelect.GetConfiguredCharacters();
|
|
|
+ if (configuredCharacters != null)
|
|
|
+ {
|
|
|
+ foreach (var character in configuredCharacters)
|
|
|
+ {
|
|
|
+ if (character != null && !string.IsNullOrEmpty(character.name))
|
|
|
+ {
|
|
|
+ customerOptions.Add(character.name);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // If no team members found, just add the current customer
|
|
|
+ if (customerOptions.Count == 0 && currentCustomer != null && !string.IsNullOrEmpty(currentCustomer.name))
|
|
|
+ {
|
|
|
+ customerOptions.Add(currentCustomer.name);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ // In TownScene or other scenes - use GameStateManager or other sources
|
|
|
+ if (GameStateManager.Instance?.savedTeam != null)
|
|
|
+ {
|
|
|
+ foreach (var character in GameStateManager.Instance.savedTeam)
|
|
|
+ {
|
|
|
+ if (character != null && !string.IsNullOrEmpty(character.name))
|
|
|
+ {
|
|
|
+ customerOptions.Add(character.name);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // Fallback to current customer
|
|
|
+ if (customerOptions.Count == 0 && currentCustomer != null && !string.IsNullOrEmpty(currentCustomer.name))
|
|
|
+ {
|
|
|
+ customerOptions.Add(currentCustomer.name);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // Ensure we have at least one option
|
|
|
+ if (customerOptions.Count == 0)
|
|
|
+ {
|
|
|
+ customerOptions.Add("No Customer");
|
|
|
+ }
|
|
|
+
|
|
|
+ // Set the dropdown options
|
|
|
+ customerDropdown.choices = customerOptions;
|
|
|
+
|
|
|
+ // Set the current selection safely
|
|
|
+ if (currentCustomer != null && !string.IsNullOrEmpty(currentCustomer.name) && customerOptions.Contains(currentCustomer.name))
|
|
|
+ {
|
|
|
+ customerDropdown.value = currentCustomer.name;
|
|
|
+ }
|
|
|
+ else if (customerOptions.Count > 0)
|
|
|
+ {
|
|
|
+ customerDropdown.value = customerOptions[0];
|
|
|
+ }
|
|
|
+
|
|
|
+ Debug.Log($"Customer dropdown populated with {customerOptions.Count} customers: {string.Join(", ", customerOptions)}");
|
|
|
+ }
|
|
|
+ catch (System.Exception ex)
|
|
|
+ {
|
|
|
+ Debug.LogError($"Error updating customer dropdown: {ex.Message}");
|
|
|
+ // Fallback to safe state
|
|
|
+ customerDropdown.choices = new List<string> { "Error - No Customer" };
|
|
|
+ customerDropdown.value = "Error - No Customer";
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private TeamCharacter FindCharacterByName(string characterName)
|
|
|
+ {
|
|
|
+ // Try MainTeamSelectScript first
|
|
|
+ var mainTeamSelect = FindFirstObjectByType<MainTeamSelectScript>();
|
|
|
+ if (mainTeamSelect != null)
|
|
|
+ {
|
|
|
+ var configuredCharacters = mainTeamSelect.GetConfiguredCharacters();
|
|
|
+ foreach (var character in configuredCharacters)
|
|
|
+ {
|
|
|
+ if (character != null && character.name == characterName)
|
|
|
+ {
|
|
|
+ return character;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // Try GameStateManager
|
|
|
+ if (GameStateManager.Instance?.savedTeam != null)
|
|
|
+ {
|
|
|
+ foreach (var character in GameStateManager.Instance.savedTeam)
|
|
|
+ {
|
|
|
+ if (character != null && character.name == characterName)
|
|
|
+ {
|
|
|
+ return character;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return null;
|
|
|
+ }
|
|
|
}
|