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 geographicFeatures = new List(); private GeographicFeatureGenerator featureGenerator; private MapData currentMapData; public List GeographicFeatures => geographicFeatures; private void Awake() { featureGenerator = new GeographicFeatureGenerator(); } private void Start() { if (autoGenerateFeatures) { // Try to find MapMaker2 in the scene var mapMaker = FindFirstObjectByType(); 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 GetFeaturesByType(GeographicFeatureType type) { var result = new List(); 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(); if (mapLegend != null) { mapLegend.OnFeaturesGenerated(); } // Find and notify the location naming system var locationNaming = FindFirstObjectByType(); if (locationNaming != null) { locationNaming.OnFeaturesGenerated(); } } [ContextMenu("Regenerate Features")] public void RegenerateFeatures() { if (currentMapData != null) { GenerateFeatures(currentMapData); } else { var mapMaker = FindFirstObjectByType(); if (mapMaker != null && mapMaker.GetMapData() != null) { GenerateFeatures(mapMaker.GetMapData()); } else { Debug.LogWarning("GeographicFeatureManager: Cannot regenerate - no MapData available"); } } } public void ClearFeatures() { geographicFeatures.Clear(); NotifyFeaturesGenerated(); } }