TravelSystemDebugger.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. using UnityEngine;
  2. /// <summary>
  3. /// Debug helper for the travel system - helps troubleshoot coordinate issues
  4. /// </summary>
  5. public class TravelSystemDebugger : MonoBehaviour
  6. {
  7. [Header("Debug Settings")]
  8. public bool enableDebugOverlay = false;
  9. public bool showCoordinates = true;
  10. public bool showCameraInfo = true;
  11. public KeyCode toggleKey = KeyCode.F9;
  12. [Header("Visual Debug")]
  13. public Color guiTextColor = Color.yellow;
  14. public int fontSize = 16;
  15. private Camera mapCamera;
  16. private MapMaker2 mapMaker;
  17. private SimpleTeamPlacement teamPlacement;
  18. private TeamTravelSystem travelSystem;
  19. void Start()
  20. {
  21. mapCamera = Camera.main;
  22. mapMaker = FindFirstObjectByType<MapMaker2>();
  23. teamPlacement = FindFirstObjectByType<SimpleTeamPlacement>();
  24. travelSystem = FindFirstObjectByType<TeamTravelSystem>();
  25. }
  26. void Update()
  27. {
  28. if (Input.GetKeyDown(toggleKey))
  29. {
  30. enableDebugOverlay = !enableDebugOverlay;
  31. Debug.Log($"🔧 Travel System Debug Overlay: {(enableDebugOverlay ? "ENABLED" : "DISABLED")}");
  32. }
  33. // Log mouse clicks for debugging
  34. if (Input.GetMouseButtonDown(0))
  35. {
  36. LogMouseClickInfo();
  37. }
  38. }
  39. void OnGUI()
  40. {
  41. if (!enableDebugOverlay) return;
  42. GUI.color = guiTextColor;
  43. GUIStyle style = new GUIStyle(GUI.skin.label);
  44. style.fontSize = fontSize;
  45. float yPos = 50f;
  46. float lineHeight = 25f;
  47. // Mouse position info
  48. if (showCoordinates)
  49. {
  50. Vector3 mousePos = Input.mousePosition;
  51. GUI.Label(new Rect(10, yPos, 400, lineHeight), $"Screen Mouse: {mousePos.x:F0}, {mousePos.y:F0}", style);
  52. yPos += lineHeight;
  53. if (mapCamera != null)
  54. {
  55. Vector3 worldPos = GetMouseWorldPosition();
  56. GUI.Label(new Rect(10, yPos, 400, lineHeight), $"World Mouse: {worldPos.x:F2}, {worldPos.y:F2}, {worldPos.z:F2}", style);
  57. yPos += lineHeight;
  58. Vector2Int tilePos = WorldToTilePosition(worldPos);
  59. GUI.Label(new Rect(10, yPos, 400, lineHeight), $"Tile Position: {tilePos.x}, {tilePos.y}", style);
  60. yPos += lineHeight;
  61. }
  62. }
  63. // Camera info
  64. if (showCameraInfo && mapCamera != null)
  65. {
  66. GUI.Label(new Rect(10, yPos, 400, lineHeight), $"Camera Pos: {mapCamera.transform.position.x:F1}, {mapCamera.transform.position.y:F1}, {mapCamera.transform.position.z:F1}", style);
  67. yPos += lineHeight;
  68. GUI.Label(new Rect(10, yPos, 400, lineHeight), $"Camera Size: {mapCamera.orthographicSize:F1}", style);
  69. yPos += lineHeight;
  70. }
  71. // System status
  72. yPos += lineHeight;
  73. GUI.Label(new Rect(10, yPos, 400, lineHeight), "=== SYSTEM STATUS ===", style);
  74. yPos += lineHeight;
  75. bool mapMakerOK = mapMaker != null;
  76. bool teamPlacementOK = teamPlacement != null && teamPlacement.IsTeamPlaced();
  77. bool travelSystemOK = travelSystem != null;
  78. GUI.color = mapMakerOK ? Color.green : Color.red;
  79. GUI.Label(new Rect(10, yPos, 400, lineHeight), $"MapMaker2: {(mapMakerOK ? "✅ OK" : "❌ Missing")}", style);
  80. yPos += lineHeight;
  81. GUI.color = teamPlacementOK ? Color.green : Color.red;
  82. GUI.Label(new Rect(10, yPos, 400, lineHeight), $"Team Placement: {(teamPlacementOK ? "✅ OK" : "❌ Missing/Not Placed")}", style);
  83. yPos += lineHeight;
  84. GUI.color = travelSystemOK ? Color.green : Color.red;
  85. GUI.Label(new Rect(10, yPos, 400, lineHeight), $"Travel System: {(travelSystemOK ? "✅ OK" : "❌ Missing")}", style);
  86. yPos += lineHeight;
  87. if (teamPlacementOK)
  88. {
  89. GUI.color = Color.cyan;
  90. Vector2Int teamPos = teamPlacement.GetTeamPosition();
  91. GUI.Label(new Rect(10, yPos, 400, lineHeight), $"Team Position: {teamPos.x}, {teamPos.y}", style);
  92. yPos += lineHeight;
  93. }
  94. if (mapMakerOK && mapMaker.GetMapData() != null)
  95. {
  96. GUI.color = Color.cyan;
  97. var mapData = mapMaker.GetMapData();
  98. GUI.Label(new Rect(10, yPos, 400, lineHeight), $"Map Size: {mapData.Width}x{mapData.Height}", style);
  99. yPos += lineHeight;
  100. float tileSize = mapMaker.mapVisualizer?.tileSize ?? 1f;
  101. GUI.Label(new Rect(10, yPos, 400, lineHeight), $"Tile Size: {tileSize}", style);
  102. yPos += lineHeight;
  103. }
  104. GUI.color = Color.white;
  105. }
  106. private void LogMouseClickInfo()
  107. {
  108. Debug.Log("=== MOUSE CLICK DEBUG ===");
  109. Debug.Log($"Screen Position: {Input.mousePosition}");
  110. if (mapCamera != null)
  111. {
  112. Vector3 worldPos = GetMouseWorldPosition();
  113. Debug.Log($"World Position: {worldPos}");
  114. Vector2Int tilePos = WorldToTilePosition(worldPos);
  115. Debug.Log($"Tile Position: {tilePos}");
  116. if (mapMaker?.GetMapData() != null)
  117. {
  118. var mapData = mapMaker.GetMapData();
  119. bool isValid = mapData.IsValidPosition(tilePos.x, tilePos.y);
  120. Debug.Log($"Valid Tile: {isValid}");
  121. if (isValid)
  122. {
  123. var tile = mapData.GetTile(tilePos.x, tilePos.y);
  124. Debug.Log($"Tile Info: {tile.terrainType}, {tile.featureType}");
  125. }
  126. }
  127. }
  128. else
  129. {
  130. Debug.LogWarning("No camera found for world position calculation");
  131. }
  132. Debug.Log("========================");
  133. }
  134. private Vector3 GetMouseWorldPosition()
  135. {
  136. if (mapCamera == null) return Vector3.zero;
  137. Ray ray = mapCamera.ScreenPointToRay(Input.mousePosition);
  138. // Try raycast first
  139. if (Physics.Raycast(ray, out RaycastHit hit, 1000f))
  140. {
  141. return hit.point;
  142. }
  143. // Fallback: use camera plane projection at Z=0 (map level)
  144. Plane mapPlane = new Plane(Vector3.back, Vector3.zero);
  145. if (mapPlane.Raycast(ray, out float distance))
  146. {
  147. return ray.GetPoint(distance);
  148. }
  149. return Vector3.zero;
  150. }
  151. private Vector2Int WorldToTilePosition(Vector3 worldPos)
  152. {
  153. float tileSize = mapMaker?.mapVisualizer?.tileSize ?? 1f;
  154. return new Vector2Int(
  155. Mathf.RoundToInt(worldPos.x / tileSize),
  156. Mathf.RoundToInt(worldPos.y / tileSize)
  157. );
  158. }
  159. }