using UnityEngine; using UnityEngine.UIElements; /// /// Simple test script to help debug and fix map location positioning issues. /// Add this to a GameObject in MapScene2 to test the location naming system. /// public class MapLocationTestHelper : MonoBehaviour { [Header("Test Settings")] public bool runTestOnStart = false; public KeyCode testKey = KeyCode.T; [Header("Debug")] public bool verboseLogging = true; private void Start() { if (runTestOnStart) { RunDiagnostics(); } } private void Update() { if (Input.GetKeyDown(testKey)) { RunDiagnostics(); } } [ContextMenu("Run Full Diagnostics")] public void RunDiagnostics() { Debug.Log("=== MAP LOCATION SYSTEM DIAGNOSTICS ==="); TestMapMaker(); TestGeographicFeatures(); TestNameDisplay(); TestLegend(); TestUISetup(); Debug.Log("=== DIAGNOSTICS COMPLETE ==="); } private void TestMapMaker() { Debug.Log("--- Testing MapMaker2 ---"); var mapMaker = FindFirstObjectByType(); if (mapMaker != null) { var mapData = mapMaker.GetMapData(); if (mapData != null) { Debug.Log($"✅ MapMaker2 found with MapData: {mapData.Width}x{mapData.Height}"); var settlements = mapData.GetAllSettlements(); Debug.Log($"✅ Settlements found: {settlements.Count}"); if (verboseLogging && settlements.Count > 0) { foreach (var settlement in settlements) { Debug.Log($" - {settlement.name} ({settlement.Type}) at {settlement.position}"); } } } else { Debug.LogError("❌ MapMaker2 found but MapData is null"); } } else { Debug.LogError("❌ MapMaker2 not found in scene"); } } private void TestGeographicFeatures() { Debug.Log("--- Testing Geographic Features ---"); var featureManager = FindFirstObjectByType(); if (featureManager != null) { var features = featureManager.GeographicFeatures; Debug.Log($"✅ GeographicFeatureManager found with {features.Count} features"); if (verboseLogging && features.Count > 0) { foreach (var feature in features) { Debug.Log($" - {feature.name} ({feature.type}) at {feature.centerPosition} with {feature.tiles.Count} tiles"); } } } else { Debug.LogError("❌ GeographicFeatureManager not found in scene"); } } private void TestNameDisplay() { Debug.Log("--- Testing Name Display ---"); var nameDisplay = FindFirstObjectByType(); if (nameDisplay != null) { Debug.Log($"✅ MapLocationNameDisplay found"); Debug.Log($" - Show Settlements: {nameDisplay.showSettlementNames}"); Debug.Log($" - Show Forests: {nameDisplay.showForestNames}"); Debug.Log($" - Show Lakes: {nameDisplay.showLakeNames}"); // Test if it has a UIDocument if (nameDisplay.mapUIDocument != null) { Debug.Log($"✅ UIDocument assigned"); if (nameDisplay.mapUIDocument.rootVisualElement != null) { Debug.Log($"✅ Root visual element exists"); var mapContainer = nameDisplay.mapUIDocument.rootVisualElement.Q("map-container"); if (mapContainer != null) { Debug.Log($"✅ Map container found: {mapContainer.worldBound}"); } else { Debug.LogWarning("⚠️ Map container not found"); } } else { Debug.LogError("❌ Root visual element is null"); } } else { Debug.LogError("❌ UIDocument not assigned to MapLocationNameDisplay"); } } else { Debug.LogError("❌ MapLocationNameDisplay not found in scene"); } } private void TestLegend() { Debug.Log("--- Testing Legend ---"); var legend = FindFirstObjectByType(); if (legend != null) { Debug.Log($"✅ MapSceneLegendUI found"); Debug.Log($" - Start Visible: {legend.startVisible}"); Debug.Log($" - Toggle Key: {legend.toggleKey}"); if (legend.uiDocument != null) { Debug.Log($"✅ UIDocument assigned"); if (legend.uiDocument.rootVisualElement != null) { Debug.Log($"✅ Root visual element exists with {legend.uiDocument.rootVisualElement.childCount} children"); } else { Debug.LogError("❌ Root visual element is null"); } } else { Debug.LogError("❌ UIDocument not assigned to MapSceneLegendUI"); } } else { Debug.LogError("❌ MapSceneLegendUI not found in scene"); } } private void TestUISetup() { Debug.Log("--- Testing UI Setup ---"); var uiDocuments = FindObjectsByType(FindObjectsSortMode.None); Debug.Log($"Found {uiDocuments.Length} UIDocument components in scene"); foreach (var doc in uiDocuments) { if (doc.rootVisualElement != null) { Debug.Log($" - {doc.gameObject.name}: Root element with {doc.rootVisualElement.childCount} children"); } else { Debug.LogWarning($" - {doc.gameObject.name}: No root element"); } } } [ContextMenu("Force Refresh All")] public void ForceRefreshAll() { Debug.Log("=== FORCING REFRESH OF ALL SYSTEMS ==="); // Refresh geographic features var featureManager = FindFirstObjectByType(); if (featureManager != null) { featureManager.RegenerateFeatures(); Debug.Log("✅ Regenerated geographic features"); } // Refresh name display var nameDisplay = FindFirstObjectByType(); if (nameDisplay != null) { nameDisplay.RefreshLocationNames(); Debug.Log("✅ Refreshed location names"); } // Refresh legend var legend = FindFirstObjectByType(); if (legend != null) { legend.OnFeaturesGenerated(); Debug.Log("✅ Refreshed legend"); } Debug.Log("=== REFRESH COMPLETE ==="); } [ContextMenu("Test Coordinate Conversion")] public void TestCoordinateConversion() { var nameDisplay = FindFirstObjectByType(); var mapMaker = FindFirstObjectByType(); if (nameDisplay != null && mapMaker != null) { var mapData = mapMaker.GetMapData(); if (mapData != null) { Debug.Log("=== TESTING COORDINATE CONVERSION ==="); // Test corner coordinates var testPoints = new Vector2Int[] { new Vector2Int(0, 0), new Vector2Int(mapData.Width / 2, mapData.Height / 2), new Vector2Int(mapData.Width - 1, mapData.Height - 1) }; foreach (var point in testPoints) { // We can't directly call WorldToUIPosition as it's private, // but we can test with settlements at those positions Debug.Log($"World point {point} would be at map bounds"); } } } } }