MapLocationTestHelper.cs 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. using UnityEngine;
  2. using UnityEngine.UIElements;
  3. /// <summary>
  4. /// Simple test script to help debug and fix map location positioning issues.
  5. /// Add this to a GameObject in MapScene2 to test the location naming system.
  6. /// </summary>
  7. public class MapLocationTestHelper : MonoBehaviour
  8. {
  9. [Header("Test Settings")]
  10. public bool runTestOnStart = false;
  11. public KeyCode testKey = KeyCode.T;
  12. [Header("Debug")]
  13. public bool verboseLogging = false;
  14. private void Start()
  15. {
  16. if (runTestOnStart)
  17. {
  18. RunDiagnostics();
  19. }
  20. }
  21. private void Update()
  22. {
  23. if (Input.GetKeyDown(testKey))
  24. {
  25. RunDiagnostics();
  26. }
  27. }
  28. [ContextMenu("Run Full Diagnostics")]
  29. public void RunDiagnostics()
  30. {
  31. Debug.Log("=== MAP LOCATION SYSTEM DIAGNOSTICS ===");
  32. TestMapMaker();
  33. TestGeographicFeatures();
  34. TestNameDisplay();
  35. TestLegend();
  36. TestUISetup();
  37. Debug.Log("=== DIAGNOSTICS COMPLETE ===");
  38. }
  39. private void TestMapMaker()
  40. {
  41. Debug.Log("--- Testing MapMaker2 ---");
  42. var mapMaker = FindFirstObjectByType<MapMaker2>();
  43. if (mapMaker != null)
  44. {
  45. var mapData = mapMaker.GetMapData();
  46. if (mapData != null)
  47. {
  48. Debug.Log($"✅ MapMaker2 found with MapData: {mapData.Width}x{mapData.Height}");
  49. var settlements = mapData.GetAllSettlements();
  50. Debug.Log($"✅ Settlements found: {settlements.Count}");
  51. if (verboseLogging && settlements.Count > 0)
  52. {
  53. foreach (var settlement in settlements)
  54. {
  55. Debug.Log($" - {settlement.name} ({settlement.Type}) at {settlement.position}");
  56. }
  57. }
  58. }
  59. else
  60. {
  61. Debug.LogError("❌ MapMaker2 found but MapData is null");
  62. }
  63. }
  64. else
  65. {
  66. Debug.LogError("❌ MapMaker2 not found in scene");
  67. }
  68. }
  69. private void TestGeographicFeatures()
  70. {
  71. Debug.Log("--- Testing Geographic Features ---");
  72. var featureManager = FindFirstObjectByType<GeographicFeatureManager>();
  73. if (featureManager != null)
  74. {
  75. var features = featureManager.GeographicFeatures;
  76. Debug.Log($"✅ GeographicFeatureManager found with {features.Count} features");
  77. if (verboseLogging && features.Count > 0)
  78. {
  79. foreach (var feature in features)
  80. {
  81. Debug.Log($" - {feature.name} ({feature.type}) at {feature.centerPosition} with {feature.tiles.Count} tiles");
  82. }
  83. }
  84. }
  85. else
  86. {
  87. Debug.LogError("❌ GeographicFeatureManager not found in scene");
  88. }
  89. }
  90. private void TestNameDisplay()
  91. {
  92. Debug.Log("--- Testing Name Display ---");
  93. var nameDisplay = FindFirstObjectByType<MapLocationNameDisplay>();
  94. if (nameDisplay != null)
  95. {
  96. Debug.Log($"✅ MapLocationNameDisplay found");
  97. Debug.Log($" - Show Settlements: {nameDisplay.showSettlementNames}");
  98. Debug.Log($" - Show Forests: {nameDisplay.showForestNames}");
  99. Debug.Log($" - Show Lakes: {nameDisplay.showLakeNames}");
  100. // Test if it has a UIDocument
  101. if (nameDisplay.mapUIDocument != null)
  102. {
  103. Debug.Log($"✅ UIDocument assigned");
  104. if (nameDisplay.mapUIDocument.rootVisualElement != null)
  105. {
  106. Debug.Log($"✅ Root visual element exists");
  107. var mapContainer = nameDisplay.mapUIDocument.rootVisualElement.Q<VisualElement>("map-container");
  108. if (mapContainer != null)
  109. {
  110. Debug.Log($"✅ Map container found: {mapContainer.worldBound}");
  111. }
  112. else
  113. {
  114. Debug.LogWarning("⚠️ Map container not found");
  115. }
  116. }
  117. else
  118. {
  119. Debug.LogError("❌ Root visual element is null");
  120. }
  121. }
  122. else
  123. {
  124. Debug.LogError("❌ UIDocument not assigned to MapLocationNameDisplay");
  125. }
  126. }
  127. else
  128. {
  129. Debug.LogError("❌ MapLocationNameDisplay not found in scene");
  130. }
  131. }
  132. private void TestLegend()
  133. {
  134. Debug.Log("--- Testing Legend ---");
  135. var legend = FindFirstObjectByType<MapSceneLegendUI>();
  136. if (legend != null)
  137. {
  138. Debug.Log($"✅ MapSceneLegendUI found");
  139. Debug.Log($" - Start Visible: {legend.startVisible}");
  140. Debug.Log($" - Toggle Key: {legend.toggleKey}");
  141. if (legend.uiDocument != null)
  142. {
  143. Debug.Log($"✅ UIDocument assigned");
  144. if (legend.uiDocument.rootVisualElement != null)
  145. {
  146. Debug.Log($"✅ Root visual element exists with {legend.uiDocument.rootVisualElement.childCount} children");
  147. }
  148. else
  149. {
  150. Debug.LogError("❌ Root visual element is null");
  151. }
  152. }
  153. else
  154. {
  155. Debug.LogError("❌ UIDocument not assigned to MapSceneLegendUI");
  156. }
  157. }
  158. else
  159. {
  160. Debug.LogError("❌ MapSceneLegendUI not found in scene");
  161. }
  162. }
  163. private void TestUISetup()
  164. {
  165. Debug.Log("--- Testing UI Setup ---");
  166. var uiDocuments = FindObjectsByType<UIDocument>(FindObjectsSortMode.None);
  167. Debug.Log($"Found {uiDocuments.Length} UIDocument components in scene");
  168. foreach (var doc in uiDocuments)
  169. {
  170. if (doc.rootVisualElement != null)
  171. {
  172. Debug.Log($" - {doc.gameObject.name}: Root element with {doc.rootVisualElement.childCount} children");
  173. }
  174. else
  175. {
  176. Debug.LogWarning($" - {doc.gameObject.name}: No root element");
  177. }
  178. }
  179. }
  180. [ContextMenu("Force Refresh All")]
  181. public void ForceRefreshAll()
  182. {
  183. Debug.Log("=== FORCING REFRESH OF ALL SYSTEMS ===");
  184. // Refresh geographic features
  185. var featureManager = FindFirstObjectByType<GeographicFeatureManager>();
  186. if (featureManager != null)
  187. {
  188. featureManager.RegenerateFeatures();
  189. Debug.Log("✅ Regenerated geographic features");
  190. }
  191. // Refresh name display
  192. var nameDisplay = FindFirstObjectByType<MapLocationNameDisplay>();
  193. if (nameDisplay != null)
  194. {
  195. nameDisplay.RefreshLocationNames();
  196. Debug.Log("✅ Refreshed location names");
  197. }
  198. // Refresh legend
  199. var legend = FindFirstObjectByType<MapSceneLegendUI>();
  200. if (legend != null)
  201. {
  202. legend.OnFeaturesGenerated();
  203. Debug.Log("✅ Refreshed legend");
  204. }
  205. Debug.Log("=== REFRESH COMPLETE ===");
  206. }
  207. [ContextMenu("Test Coordinate Conversion")]
  208. public void TestCoordinateConversion()
  209. {
  210. var nameDisplay = FindFirstObjectByType<MapLocationNameDisplay>();
  211. var mapMaker = FindFirstObjectByType<MapMaker2>();
  212. if (nameDisplay != null && mapMaker != null)
  213. {
  214. var mapData = mapMaker.GetMapData();
  215. if (mapData != null)
  216. {
  217. Debug.Log("=== TESTING COORDINATE CONVERSION ===");
  218. // Test corner coordinates
  219. var testPoints = new Vector2Int[]
  220. {
  221. new Vector2Int(0, 0),
  222. new Vector2Int(mapData.Width / 2, mapData.Height / 2),
  223. new Vector2Int(mapData.Width - 1, mapData.Height - 1)
  224. };
  225. foreach (var point in testPoints)
  226. {
  227. // We can't directly call WorldToUIPosition as it's private,
  228. // but we can test with settlements at those positions
  229. Debug.Log($"World point {point} would be at map bounds");
  230. }
  231. }
  232. }
  233. }
  234. }