SettlementCoordinateDiagnostic.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. using UnityEngine;
  2. /// <summary>
  3. /// Enhanced diagnostic script specifically for debugging settlement coordinate issues
  4. /// </summary>
  5. public class SettlementCoordinateDiagnostic : MonoBehaviour
  6. {
  7. [ContextMenu("Debug Settlement Coordinates")]
  8. public void DebugSettlementCoordinates()
  9. {
  10. Debug.Log("=== SETTLEMENT COORDINATE DIAGNOSTIC ===");
  11. var settlementManager = FindFirstObjectByType<SettlementInteractionManager>();
  12. var teamMarker = GameObject.Find("TeamMarker");
  13. var mapMaker = FindFirstObjectByType<MapMaker2>();
  14. if (settlementManager == null || teamMarker == null || mapMaker == null)
  15. {
  16. Debug.LogError("❌ Missing components for coordinate diagnostic");
  17. return;
  18. }
  19. var mapData = mapMaker.GetMapData();
  20. if (mapData == null)
  21. {
  22. Debug.LogError("❌ MapData is null");
  23. return;
  24. }
  25. // Get current team position
  26. Vector3 worldPos = teamMarker.transform.position;
  27. Vector2 teamWorldPos = new Vector2(worldPos.x, worldPos.z);
  28. Debug.Log($"🎯 TeamMarker World Position: {worldPos}");
  29. Debug.Log($"🎯 TeamMarker 2D Position: {teamWorldPos}");
  30. // Test coordinate conversion (replicate SettlementInteractionManager logic)
  31. Debug.Log("\n🔍 COORDINATE CONVERSION TEST:");
  32. // Get tile size from MapVisualizer
  33. float tileSize = 1f;
  34. var mapVisualizer = FindFirstObjectByType<MapVisualizer>();
  35. if (mapVisualizer != null && mapVisualizer.tileSize > 0)
  36. {
  37. tileSize = mapVisualizer.tileSize;
  38. Debug.Log($" MapVisualizer tileSize: {tileSize}");
  39. }
  40. else
  41. {
  42. Debug.Log($" Using default tileSize: {tileSize}");
  43. }
  44. // Convert world to map coordinates (same as SettlementInteractionManager)
  45. Vector2Int mapPos = new Vector2Int(
  46. Mathf.RoundToInt(teamWorldPos.x / tileSize),
  47. Mathf.RoundToInt(teamWorldPos.y / tileSize)
  48. );
  49. Debug.Log($" Converted Map Position: {mapPos}");
  50. // Check settlements near this position
  51. Debug.Log("\n🏛️ SETTLEMENT PROXIMITY CHECK:");
  52. var allSettlements = mapData.GetAllSettlements();
  53. Debug.Log($" Total settlements: {allSettlements.Count}");
  54. int foundNearby = 0;
  55. float interactionDistance = 2f; // Default from SettlementInteractionManager
  56. foreach (var settlement in allSettlements)
  57. {
  58. float distance = Vector2Int.Distance(mapPos, settlement.position);
  59. if (distance <= 5f) // Show all within 5 tiles for debugging
  60. {
  61. string status = distance <= interactionDistance ? "✅ WITHIN RANGE" : "❌ TOO FAR";
  62. Debug.Log($" Settlement '{settlement.name}' at {settlement.position}, distance: {distance:F1} {status}");
  63. if (distance <= interactionDistance)
  64. {
  65. foundNearby++;
  66. }
  67. }
  68. }
  69. Debug.Log($" Settlements within interaction distance ({interactionDistance}): {foundNearby}");
  70. // Test MapData.GetSettlementAt method
  71. Debug.Log("\n🔍 MAPDATA.GETSETTLEMENTAT TEST:");
  72. var foundSettlement = mapData.GetSettlementAt(mapPos, interactionDistance);
  73. Debug.Log($" MapData.GetSettlementAt({mapPos}, {interactionDistance}): {(foundSettlement != null ? foundSettlement.name : "NULL")}");
  74. // Manual verification - check exact position
  75. Debug.Log("\n🎯 EXACT POSITION CHECK:");
  76. foreach (var settlement in allSettlements)
  77. {
  78. if (settlement.position == mapPos)
  79. {
  80. Debug.Log($" ✅ EXACT MATCH: Settlement '{settlement.name}' at exactly {settlement.position}");
  81. }
  82. }
  83. }
  84. }