GeographicFeatureManager.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. using UnityEngine;
  2. using System.Collections.Generic;
  3. public class GeographicFeatureManager : MonoBehaviour
  4. {
  5. [Header("Feature Configuration")]
  6. public bool autoGenerateFeatures = true;
  7. public bool regenerateOnMapChange = true;
  8. [Header("Debug")]
  9. public bool debugMode = false;
  10. private List<GeographicFeature> geographicFeatures = new List<GeographicFeature>();
  11. private GeographicFeatureGenerator featureGenerator;
  12. private MapData currentMapData;
  13. public List<GeographicFeature> GeographicFeatures => geographicFeatures;
  14. private void Awake()
  15. {
  16. featureGenerator = new GeographicFeatureGenerator();
  17. }
  18. private void Start()
  19. {
  20. if (autoGenerateFeatures)
  21. {
  22. // Try to find MapMaker2 in the scene
  23. var mapMaker = FindFirstObjectByType<MapMaker2>();
  24. if (mapMaker != null)
  25. {
  26. StartCoroutine(WaitForMapDataAndGenerate(mapMaker));
  27. }
  28. else if (debugMode)
  29. {
  30. Debug.LogWarning("GeographicFeatureManager: MapMaker2 not found in scene");
  31. }
  32. }
  33. }
  34. private System.Collections.IEnumerator WaitForMapDataAndGenerate(MapMaker2 mapMaker)
  35. {
  36. // Wait a frame to ensure MapData is initialized
  37. yield return null;
  38. // Wait until MapData is available
  39. int attempts = 0;
  40. while (mapMaker.GetMapData() == null && attempts < 60) // Max 1 second wait
  41. {
  42. yield return new WaitForSeconds(0.1f);
  43. attempts++;
  44. }
  45. if (mapMaker.GetMapData() != null)
  46. {
  47. GenerateFeatures(mapMaker.GetMapData());
  48. }
  49. else if (debugMode)
  50. {
  51. Debug.LogError("GeographicFeatureManager: Failed to get MapData after waiting");
  52. }
  53. }
  54. public void GenerateFeatures(MapData mapData)
  55. {
  56. if (mapData == null)
  57. {
  58. if (debugMode)
  59. Debug.LogWarning("GeographicFeatureManager: Cannot generate features - MapData is null");
  60. return;
  61. }
  62. currentMapData = mapData;
  63. geographicFeatures.Clear();
  64. if (debugMode)
  65. Debug.Log("GeographicFeatureManager: Starting feature generation...");
  66. geographicFeatures = featureGenerator.GenerateFeatures(mapData);
  67. if (debugMode)
  68. {
  69. Debug.Log($"GeographicFeatureManager: Generated {geographicFeatures.Count} geographic features");
  70. foreach (var feature in geographicFeatures)
  71. {
  72. Debug.Log($"- {feature.name} ({feature.type}) at {feature.centerPosition} with {feature.tiles.Count} tiles");
  73. }
  74. }
  75. // Notify other systems that features have been generated
  76. NotifyFeaturesGenerated();
  77. }
  78. public List<GeographicFeature> GetFeaturesByType(GeographicFeatureType type)
  79. {
  80. var result = new List<GeographicFeature>();
  81. foreach (var feature in geographicFeatures)
  82. {
  83. if (feature.type == type)
  84. result.Add(feature);
  85. }
  86. return result;
  87. }
  88. public GeographicFeature GetFeatureAtPosition(Vector2Int position)
  89. {
  90. foreach (var feature in geographicFeatures)
  91. {
  92. if (feature.tiles.Contains(position))
  93. return feature;
  94. }
  95. return null;
  96. }
  97. public void SetFeatureDiscovered(GeographicFeature feature, bool discovered)
  98. {
  99. if (feature != null)
  100. {
  101. feature.isDiscovered = discovered;
  102. }
  103. }
  104. public void SetFeatureDiscoveredByName(string featureName, bool discovered)
  105. {
  106. var feature = geographicFeatures.Find(f => f.name == featureName);
  107. SetFeatureDiscovered(feature, discovered);
  108. }
  109. private void NotifyFeaturesGenerated()
  110. {
  111. // Find and notify the map legend system
  112. var mapLegend = FindFirstObjectByType<MapSceneLegendUI>();
  113. if (mapLegend != null)
  114. {
  115. mapLegend.OnFeaturesGenerated();
  116. }
  117. // Find and notify the location naming system
  118. var locationNaming = FindFirstObjectByType<MapLocationNameDisplay>();
  119. if (locationNaming != null)
  120. {
  121. locationNaming.OnFeaturesGenerated();
  122. }
  123. }
  124. [ContextMenu("Regenerate Features")]
  125. public void RegenerateFeatures()
  126. {
  127. if (currentMapData != null)
  128. {
  129. GenerateFeatures(currentMapData);
  130. }
  131. else
  132. {
  133. var mapMaker = FindFirstObjectByType<MapMaker2>();
  134. if (mapMaker != null && mapMaker.GetMapData() != null)
  135. {
  136. GenerateFeatures(mapMaker.GetMapData());
  137. }
  138. else
  139. {
  140. Debug.LogWarning("GeographicFeatureManager: Cannot regenerate - no MapData available");
  141. }
  142. }
  143. }
  144. public void ClearFeatures()
  145. {
  146. geographicFeatures.Clear();
  147. NotifyFeaturesGenerated();
  148. }
  149. }