| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172 |
- using UnityEngine;
- using System.Collections.Generic;
- public class GeographicFeatureManager : MonoBehaviour
- {
- [Header("Feature Configuration")]
- public bool autoGenerateFeatures = true;
- public bool regenerateOnMapChange = true;
- [Header("Debug")]
- public bool debugMode = false;
- private List<GeographicFeature> geographicFeatures = new List<GeographicFeature>();
- private GeographicFeatureGenerator featureGenerator;
- private MapData currentMapData;
- public List<GeographicFeature> GeographicFeatures => geographicFeatures;
- private void Awake()
- {
- featureGenerator = new GeographicFeatureGenerator();
- }
- private void Start()
- {
- if (autoGenerateFeatures)
- {
- // Try to find MapMaker2 in the scene
- var mapMaker = FindFirstObjectByType<MapMaker2>();
- if (mapMaker != null)
- {
- StartCoroutine(WaitForMapDataAndGenerate(mapMaker));
- }
- else if (debugMode)
- {
- Debug.LogWarning("GeographicFeatureManager: MapMaker2 not found in scene");
- }
- }
- }
- private System.Collections.IEnumerator WaitForMapDataAndGenerate(MapMaker2 mapMaker)
- {
- // Wait a frame to ensure MapData is initialized
- yield return null;
- // Wait until MapData is available
- int attempts = 0;
- while (mapMaker.GetMapData() == null && attempts < 60) // Max 1 second wait
- {
- yield return new WaitForSeconds(0.1f);
- attempts++;
- }
- if (mapMaker.GetMapData() != null)
- {
- GenerateFeatures(mapMaker.GetMapData());
- }
- else if (debugMode)
- {
- Debug.LogError("GeographicFeatureManager: Failed to get MapData after waiting");
- }
- }
- public void GenerateFeatures(MapData mapData)
- {
- if (mapData == null)
- {
- if (debugMode)
- Debug.LogWarning("GeographicFeatureManager: Cannot generate features - MapData is null");
- return;
- }
- currentMapData = mapData;
- geographicFeatures.Clear();
- if (debugMode)
- Debug.Log("GeographicFeatureManager: Starting feature generation...");
- geographicFeatures = featureGenerator.GenerateFeatures(mapData);
- if (debugMode)
- {
- Debug.Log($"GeographicFeatureManager: Generated {geographicFeatures.Count} geographic features");
- foreach (var feature in geographicFeatures)
- {
- Debug.Log($"- {feature.name} ({feature.type}) at {feature.centerPosition} with {feature.tiles.Count} tiles");
- }
- }
- // Notify other systems that features have been generated
- NotifyFeaturesGenerated();
- }
- public List<GeographicFeature> GetFeaturesByType(GeographicFeatureType type)
- {
- var result = new List<GeographicFeature>();
- foreach (var feature in geographicFeatures)
- {
- if (feature.type == type)
- result.Add(feature);
- }
- return result;
- }
- public GeographicFeature GetFeatureAtPosition(Vector2Int position)
- {
- foreach (var feature in geographicFeatures)
- {
- if (feature.tiles.Contains(position))
- return feature;
- }
- return null;
- }
- public void SetFeatureDiscovered(GeographicFeature feature, bool discovered)
- {
- if (feature != null)
- {
- feature.isDiscovered = discovered;
- }
- }
- public void SetFeatureDiscoveredByName(string featureName, bool discovered)
- {
- var feature = geographicFeatures.Find(f => f.name == featureName);
- SetFeatureDiscovered(feature, discovered);
- }
- private void NotifyFeaturesGenerated()
- {
- // Find and notify the map legend system
- var mapLegend = FindFirstObjectByType<MapSceneLegendUI>();
- if (mapLegend != null)
- {
- mapLegend.OnFeaturesGenerated();
- }
- // Find and notify the location naming system
- var locationNaming = FindFirstObjectByType<MapLocationNameDisplay>();
- if (locationNaming != null)
- {
- locationNaming.OnFeaturesGenerated();
- }
- }
- [ContextMenu("Regenerate Features")]
- public void RegenerateFeatures()
- {
- if (currentMapData != null)
- {
- GenerateFeatures(currentMapData);
- }
- else
- {
- var mapMaker = FindFirstObjectByType<MapMaker2>();
- if (mapMaker != null && mapMaker.GetMapData() != null)
- {
- GenerateFeatures(mapMaker.GetMapData());
- }
- else
- {
- Debug.LogWarning("GeographicFeatureManager: Cannot regenerate - no MapData available");
- }
- }
- }
- public void ClearFeatures()
- {
- geographicFeatures.Clear();
- NotifyFeaturesGenerated();
- }
- }
|