SettlementInteractionFixer.cs 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. using UnityEngine;
  2. /// <summary>
  3. /// Quick fix to properly setup SettlementInteractionManager references
  4. /// Run this in play mode to fix the settlement interaction system
  5. /// </summary>
  6. public class SettlementInteractionFixer : MonoBehaviour
  7. {
  8. [Header("Fix Settlement Interaction")]
  9. [Space]
  10. [TextArea(3, 5)]
  11. public string instructions = "1. Run this in Play Mode\n2. Click 'Fix Settlement Interaction' button\n3. Move TeamMarker near settlements\n4. Press E to enter";
  12. [ContextMenu("Fix Settlement Interaction")]
  13. public void FixSettlementInteraction()
  14. {
  15. Debug.Log("=== Fixing Settlement Interaction ===");
  16. // Find components
  17. var settlementManager = FindFirstObjectByType<SettlementInteractionManager>();
  18. var teamMarker = GameObject.Find("TeamMarker");
  19. var mapMaker = FindFirstObjectByType<MapMaker2>();
  20. if (settlementManager == null)
  21. {
  22. Debug.LogError("❌ SettlementInteractionManager not found! Create it first.");
  23. return;
  24. }
  25. // Fix TeamMarker reference
  26. if (teamMarker != null)
  27. {
  28. settlementManager.SetTeamMarker(teamMarker.transform);
  29. Debug.Log("✅ TeamMarker reference set");
  30. }
  31. else
  32. {
  33. Debug.LogError("❌ TeamMarker GameObject not found!");
  34. }
  35. // Check MapMaker2 and MapData
  36. if (mapMaker != null)
  37. {
  38. var mapData = mapMaker.GetMapData();
  39. if (mapData != null)
  40. {
  41. var settlements = mapData.GetAllSettlements();
  42. Debug.Log($"✅ MapData found with {settlements?.Count ?? 0} settlements");
  43. }
  44. else
  45. {
  46. Debug.LogError("❌ MapData is null!");
  47. }
  48. }
  49. else
  50. {
  51. Debug.LogError("❌ MapMaker2 not found!");
  52. }
  53. // Force enable debug logging
  54. settlementManager.enableDebugLogs = true;
  55. Debug.Log("✅ Debug logging enabled");
  56. // Test current position
  57. if (teamMarker != null)
  58. {
  59. Vector3 teamPos = teamMarker.transform.position;
  60. Debug.Log($"📍 TeamMarker position: {teamPos}");
  61. // Force check for nearby settlements
  62. var currentSettlement = settlementManager.GetCurrentNearbySettlement();
  63. Debug.Log($"🏠 Current nearby settlement: {(currentSettlement?.name ?? "None")}");
  64. }
  65. Debug.Log("=== Fix Complete ===");
  66. Debug.Log("Now move TeamMarker near a settlement and press E");
  67. }
  68. [ContextMenu("Test Settlement Detection")]
  69. public void TestSettlementDetection()
  70. {
  71. var settlementManager = FindFirstObjectByType<SettlementInteractionManager>();
  72. var teamMarker = GameObject.Find("TeamMarker");
  73. var mapMaker = FindFirstObjectByType<MapMaker2>();
  74. if (settlementManager == null || teamMarker == null || mapMaker == null)
  75. {
  76. Debug.LogError("Missing components for testing!");
  77. return;
  78. }
  79. Vector2 teamWorldPos = new Vector2(teamMarker.transform.position.x, teamMarker.transform.position.z);
  80. Debug.Log($"🎯 TeamMarker world position: {teamWorldPos}");
  81. // Convert to map coordinates manually to check conversion
  82. float tileSize = 1f;
  83. var mapVisualizer = FindFirstObjectByType<MapVisualizer>();
  84. if (mapVisualizer != null && mapVisualizer.tileSize > 0)
  85. {
  86. tileSize = mapVisualizer.tileSize;
  87. Debug.Log($"📏 MapVisualizer tile size: {tileSize}");
  88. }
  89. else
  90. {
  91. Debug.LogWarning("⚠️ MapVisualizer not found, using default tile size 1.0");
  92. }
  93. Vector2Int mapPos = new Vector2Int(
  94. Mathf.RoundToInt(teamWorldPos.x / tileSize),
  95. Mathf.RoundToInt(teamWorldPos.y / tileSize)
  96. );
  97. Debug.Log($"🗺️ Converted map position: {mapPos}");
  98. // Test different coordinate interpretations
  99. Vector2Int mapPosFloor = new Vector2Int(
  100. Mathf.FloorToInt(teamWorldPos.x / tileSize),
  101. Mathf.FloorToInt(teamWorldPos.y / tileSize)
  102. );
  103. Vector2Int mapPosCeil = new Vector2Int(
  104. Mathf.CeilToInt(teamWorldPos.x / tileSize),
  105. Mathf.CeilToInt(teamWorldPos.y / tileSize)
  106. );
  107. Debug.Log($"🔍 Alternative positions - Floor: {mapPosFloor}, Ceil: {mapPosCeil}");
  108. // Check settlements manually
  109. var mapData = mapMaker.GetMapData();
  110. if (mapData != null)
  111. {
  112. var settlements = mapData.GetAllSettlements();
  113. Debug.Log($"📊 Total settlements: {settlements?.Count ?? 0}");
  114. if (settlements != null)
  115. {
  116. Debug.Log("🏘️ Checking all settlements:");
  117. foreach (var settlement in settlements)
  118. {
  119. float distanceRound = Vector2Int.Distance(mapPos, settlement.position);
  120. float distanceFloor = Vector2Int.Distance(mapPosFloor, settlement.position);
  121. float distanceCeil = Vector2Int.Distance(mapPosCeil, settlement.position);
  122. Debug.Log($" '{settlement.name}' at {settlement.position}");
  123. Debug.Log($" Round distance: {distanceRound:F2}");
  124. Debug.Log($" Floor distance: {distanceFloor:F2}");
  125. Debug.Log($" Ceil distance: {distanceCeil:F2}");
  126. if (distanceRound <= 2f || distanceFloor <= 2f || distanceCeil <= 2f)
  127. {
  128. Debug.Log($"🎯 WITHIN RANGE: {settlement.name}");
  129. // Test the exact method SettlementInteractionManager uses
  130. var foundSettlement = mapData.GetSettlementAt(mapPos, 2f);
  131. Debug.Log($"🔍 GetSettlementAt result: {foundSettlement?.name ?? "null"}");
  132. }
  133. }
  134. // Try to find settlements around the exact position (122, 71)
  135. Debug.Log($"🔍 Checking exact position (122, 71):");
  136. var exactSettlement = mapData.GetSettlementAt(new Vector2Int(122, 71), 2f);
  137. Debug.Log($" Settlement at (122, 71): {exactSettlement?.name ?? "None"}");
  138. }
  139. }
  140. }
  141. [ContextMenu("Force Manual Entry")]
  142. public void ForceManualEntry()
  143. {
  144. var settlementManager = FindFirstObjectByType<SettlementInteractionManager>();
  145. if (settlementManager != null)
  146. {
  147. settlementManager.TestEnterCurrentSettlement();
  148. }
  149. else
  150. {
  151. Debug.LogError("SettlementInteractionManager not found!");
  152. }
  153. }
  154. void Update()
  155. {
  156. // Show debug info on screen
  157. if (Input.GetKeyDown(KeyCode.F9))
  158. {
  159. FixSettlementInteraction();
  160. }
  161. if (Input.GetKeyDown(KeyCode.F10))
  162. {
  163. TestSettlementDetection();
  164. }
  165. if (Input.GetKeyDown(KeyCode.F11))
  166. {
  167. ForceManualEntry();
  168. }
  169. }
  170. void OnGUI()
  171. {
  172. GUILayout.BeginArea(new Rect(10, Screen.height - 150, 400, 140));
  173. GUILayout.BeginVertical("box");
  174. GUILayout.Label("Settlement Interaction Quick Fix", new GUIStyle(GUI.skin.label) { fontSize = 14, fontStyle = FontStyle.Bold });
  175. if (GUILayout.Button("Fix Settlement Interaction (F9)"))
  176. {
  177. FixSettlementInteraction();
  178. }
  179. if (GUILayout.Button("Test Settlement Detection (F10)"))
  180. {
  181. TestSettlementDetection();
  182. }
  183. if (GUILayout.Button("Force Manual Entry (F11)"))
  184. {
  185. ForceManualEntry();
  186. }
  187. GUILayout.EndVertical();
  188. GUILayout.EndArea();
  189. }
  190. }