TownManager.cs 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. using UnityEngine;
  2. using UnityEngine.UIElements;
  3. using System.Collections.Generic;
  4. public class TownManager : MonoBehaviour
  5. {
  6. [Header("Town Configuration")]
  7. public string townName = "Haven Village";
  8. public bool hasHarbor = true;
  9. [Header("UI References")]
  10. public UIDocument townUI;
  11. public Camera townCamera;
  12. [Header("Shop Manager")]
  13. public TownShopManager shopManager;
  14. [Header("Team Reference")]
  15. [HideInInspector]
  16. public TeamCharacter[] currentTeam;
  17. private VisualElement rootElement;
  18. void Start()
  19. {
  20. InitializeTown();
  21. SetupUI();
  22. LoadTeamData();
  23. }
  24. private void InitializeTown()
  25. {
  26. // Check if we have settlement context data
  27. if (SettlementContext.Instance != null)
  28. {
  29. // Use data from settlement context
  30. townName = SettlementContext.Instance.settlementName;
  31. hasHarbor = SettlementContext.Instance.hasHarbor;
  32. Debug.Log($"Initialized {townName} ({SettlementContext.Instance.settlementType}) from SettlementContext");
  33. // Update UI with settlement name
  34. UpdateTownNameDisplay();
  35. }
  36. else
  37. {
  38. Debug.Log($"Initialized {townName} (no SettlementContext - using defaults)");
  39. }
  40. // Initialize shop manager if not already done
  41. if (shopManager == null)
  42. {
  43. // First check if singleton instance exists
  44. if (TownShopManager.Instance != null)
  45. {
  46. shopManager = TownShopManager.Instance;
  47. Debug.Log("TownManager: Using existing TownShopManager singleton");
  48. }
  49. else
  50. {
  51. // Try to find any existing instance in scene
  52. shopManager = FindFirstObjectByType<TownShopManager>();
  53. if (shopManager != null)
  54. {
  55. Debug.Log("TownManager: Found existing TownShopManager in scene");
  56. }
  57. }
  58. }
  59. // If we still don't have a shop manager, create one
  60. if (shopManager == null)
  61. {
  62. Debug.Log("TownManager: Creating new TownShopManager");
  63. GameObject shopManagerObj = new GameObject("TownShopManager");
  64. shopManager = shopManagerObj.AddComponent<TownShopManager>();
  65. shopManager.townUI = townUI;
  66. }
  67. else
  68. {
  69. Debug.Log($"TownManager: Using existing TownShopManager: {shopManager.gameObject.name}");
  70. // Make sure the existing manager has the correct UI reference
  71. if (shopManager.townUI == null)
  72. {
  73. shopManager.townUI = townUI;
  74. Debug.Log("TownManager: Updated TownShopManager UI reference");
  75. }
  76. }
  77. }
  78. private void SetupUI()
  79. {
  80. if (townUI == null) return;
  81. rootElement = townUI.rootVisualElement;
  82. UpdateTownNameDisplay();
  83. // Let TownShopManager handle building interactions
  84. }
  85. private void UpdateTownNameDisplay()
  86. {
  87. if (rootElement == null) return;
  88. var townNameLabel = rootElement.Q<Label>("TownNameLabel");
  89. if (townNameLabel != null)
  90. townNameLabel.text = townName;
  91. }
  92. private void LoadTeamData()
  93. {
  94. // Load team data from GameStateManager or PlayerPrefs
  95. var gameStateManager = FindFirstObjectByType<GameStateManager>();
  96. if (gameStateManager != null && gameStateManager.savedTeam != null)
  97. {
  98. currentTeam = gameStateManager.savedTeam;
  99. Debug.Log($"Loaded team data: {currentTeam.Length} characters");
  100. }
  101. else
  102. {
  103. Debug.LogWarning("No team data found, using test data");
  104. CreateTestTeamData();
  105. }
  106. // Update money display
  107. UpdateMoneyDisplay();
  108. }
  109. private void CreateTestTeamData()
  110. {
  111. currentTeam = new TeamCharacter[1];
  112. currentTeam[0] = new TeamCharacter("Test Hero", true)
  113. {
  114. gold = 100,
  115. silver = 50,
  116. copper = 25
  117. };
  118. }
  119. private void UpdateMoneyDisplay()
  120. {
  121. //if (shopManager != null)
  122. //{
  123. // shopManager.UpdateMoneyDisplay();
  124. //}
  125. }
  126. private void SaveTeamData()
  127. {
  128. var gameStateManager = FindFirstObjectByType<GameStateManager>();
  129. if (gameStateManager != null)
  130. {
  131. gameStateManager.savedTeam = currentTeam;
  132. // Trigger save to PlayerPrefs if needed
  133. }
  134. }
  135. // Called by TownShopManager when a building is clicked
  136. public void OnBuildingClicked(TownShop shop)
  137. {
  138. Debug.Log($"TownManager: Shop clicked: {shop.buildingName} - handled by TownShopManager");
  139. // No longer creating duplicate UI here - TownShopManager handles shop opening
  140. }
  141. private void OpenShopDirect(TownShop shop)
  142. {
  143. // Find existing UI document from the town UI
  144. var existingUIDoc = FindFirstObjectByType<UIDocument>();
  145. // Create shop UI GameObject as a separate top-level object
  146. var shopUIObject = new GameObject("TownShopUI");
  147. // Add UIDocument component and load the UXML
  148. var uiDocument = shopUIObject.AddComponent<UIDocument>();
  149. // Copy settings from existing UI document if available
  150. if (existingUIDoc != null)
  151. {
  152. uiDocument.panelSettings = existingUIDoc.panelSettings;
  153. uiDocument.sortingOrder = existingUIDoc.sortingOrder + 100; // Ensure it's on top
  154. }
  155. uiDocument.visualTreeAsset = Resources.Load<VisualTreeAsset>("UI/TownShopUI");
  156. Debug.Log($"Loaded UXML asset: {uiDocument.visualTreeAsset != null}");
  157. // If not found in Resources, try direct path
  158. if (uiDocument.visualTreeAsset == null)
  159. {
  160. #if UNITY_EDITOR
  161. uiDocument.visualTreeAsset = UnityEditor.AssetDatabase.LoadAssetAtPath<VisualTreeAsset>("Assets/UI/TownShopUI.uxml");
  162. Debug.Log($"Loaded UXML from Assets: {uiDocument.visualTreeAsset != null}");
  163. #endif
  164. }
  165. if (uiDocument.visualTreeAsset == null)
  166. {
  167. Debug.LogError("Failed to load TownShopUI UXML!");
  168. Destroy(shopUIObject);
  169. return;
  170. }
  171. // Add TownShopUI component
  172. var shopUI = shopUIObject.AddComponent<TownShopUI>();
  173. // CRITICAL: Force the UIDocument to initialize properly
  174. uiDocument.enabled = true;
  175. // Set up the shop UI
  176. shopUI.SetShop(shop);
  177. // Add a small delay to ensure everything is properly set up
  178. StartCoroutine(OpenShopAfterFrame(shopUI));
  179. // Find first available team member as customer
  180. if (GameStateManager.Instance?.savedTeam != null)
  181. {
  182. foreach (var character in GameStateManager.Instance.savedTeam)
  183. {
  184. if (character != null)
  185. {
  186. shopUI.SetCustomer(character);
  187. break;
  188. }
  189. }
  190. }
  191. // Add a small delay to ensure everything is properly set up
  192. StartCoroutine(OpenShopAfterFrame(shopUI));
  193. Debug.Log($"Opened shop UI for {shop.buildingName} with sortOrder: {uiDocument.sortingOrder}");
  194. }
  195. private System.Collections.IEnumerator OpenShopAfterFrame(TownShopUI townShopUI)
  196. {
  197. yield return null; // Wait one frame for UI to initialize
  198. townShopUI.OpenShop();
  199. Debug.Log("Shop opened after frame delay");
  200. }
  201. public void ReturnToMap()
  202. {
  203. SaveTeamData();
  204. UnityEngine.SceneManagement.SceneManager.LoadScene("MapScene2");
  205. }
  206. }