SettlementInteractionFixer.cs 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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. // Test current position
  54. if (teamMarker != null)
  55. {
  56. Vector3 teamPos = teamMarker.transform.position;
  57. Debug.Log($"📍 TeamMarker position: {teamPos}");
  58. // Force check for nearby settlements
  59. var currentSettlement = settlementManager.GetCurrentNearbySettlement();
  60. Debug.Log($"🏠 Current nearby settlement: {(currentSettlement?.name ?? "None")}");
  61. }
  62. Debug.Log("=== Fix Complete ===");
  63. Debug.Log("Now move TeamMarker near a settlement and press E");
  64. }
  65. [ContextMenu("Test Settlement Detection")]
  66. public void TestSettlementDetection()
  67. {
  68. var settlementManager = FindFirstObjectByType<SettlementInteractionManager>();
  69. var teamMarker = GameObject.Find("TeamMarker");
  70. var mapMaker = FindFirstObjectByType<MapMaker2>();
  71. if (settlementManager == null || teamMarker == null || mapMaker == null)
  72. {
  73. Debug.LogError("Missing components for testing!");
  74. return;
  75. }
  76. Vector2 teamWorldPos = new Vector2(teamMarker.transform.position.x, teamMarker.transform.position.z);
  77. Debug.Log($"🎯 TeamMarker world position: {teamWorldPos}");
  78. // Convert to map coordinates manually to check conversion
  79. float tileSize = 1f;
  80. var mapVisualizer = FindFirstObjectByType<MapVisualizer>();
  81. if (mapVisualizer != null && mapVisualizer.tileSize > 0)
  82. {
  83. tileSize = mapVisualizer.tileSize;
  84. Debug.Log($"📏 MapVisualizer tile size: {tileSize}");
  85. }
  86. else
  87. {
  88. Debug.LogWarning("⚠️ MapVisualizer not found, using default tile size 1.0");
  89. }
  90. Vector2Int mapPos = new Vector2Int(
  91. Mathf.RoundToInt(teamWorldPos.x / tileSize),
  92. Mathf.RoundToInt(teamWorldPos.y / tileSize)
  93. );
  94. Debug.Log($"🗺️ Converted map position: {mapPos}");
  95. // Test different coordinate interpretations
  96. Vector2Int mapPosFloor = new Vector2Int(
  97. Mathf.FloorToInt(teamWorldPos.x / tileSize),
  98. Mathf.FloorToInt(teamWorldPos.y / tileSize)
  99. );
  100. Vector2Int mapPosCeil = new Vector2Int(
  101. Mathf.CeilToInt(teamWorldPos.x / tileSize),
  102. Mathf.CeilToInt(teamWorldPos.y / tileSize)
  103. );
  104. Debug.Log($"🔍 Alternative positions - Floor: {mapPosFloor}, Ceil: {mapPosCeil}");
  105. // Check settlements manually
  106. var mapData = mapMaker.GetMapData();
  107. if (mapData != null)
  108. {
  109. var settlements = mapData.GetAllSettlements();
  110. Debug.Log($"📊 Total settlements: {settlements?.Count ?? 0}");
  111. if (settlements != null)
  112. {
  113. Debug.Log("🏘️ Checking all settlements:");
  114. foreach (var settlement in settlements)
  115. {
  116. float distanceRound = Vector2Int.Distance(mapPos, settlement.position);
  117. float distanceFloor = Vector2Int.Distance(mapPosFloor, settlement.position);
  118. float distanceCeil = Vector2Int.Distance(mapPosCeil, settlement.position);
  119. Debug.Log($" '{settlement.name}' at {settlement.position}");
  120. Debug.Log($" Round distance: {distanceRound:F2}");
  121. Debug.Log($" Floor distance: {distanceFloor:F2}");
  122. Debug.Log($" Ceil distance: {distanceCeil:F2}");
  123. if (distanceRound <= 2f || distanceFloor <= 2f || distanceCeil <= 2f)
  124. {
  125. Debug.Log($"🎯 WITHIN RANGE: {settlement.name}");
  126. // Test the exact method SettlementInteractionManager uses
  127. var foundSettlement = mapData.GetSettlementAt(mapPos, 2f);
  128. Debug.Log($"🔍 GetSettlementAt result: {foundSettlement?.name ?? "null"}");
  129. }
  130. }
  131. // Try to find settlements around the exact position (122, 71)
  132. Debug.Log($"🔍 Checking exact position (122, 71):");
  133. var exactSettlement = mapData.GetSettlementAt(new Vector2Int(122, 71), 2f);
  134. Debug.Log($" Settlement at (122, 71): {exactSettlement?.name ?? "None"}");
  135. }
  136. }
  137. }
  138. [ContextMenu("Force Manual Entry")]
  139. public void ForceManualEntry()
  140. {
  141. var settlementManager = FindFirstObjectByType<SettlementInteractionManager>();
  142. if (settlementManager != null)
  143. {
  144. settlementManager.TestEnterCurrentSettlement();
  145. }
  146. else
  147. {
  148. Debug.LogError("SettlementInteractionManager not found!");
  149. }
  150. }
  151. void Update()
  152. {
  153. // Show debug info on screen
  154. if (Input.GetKeyDown(KeyCode.F9))
  155. {
  156. FixSettlementInteraction();
  157. }
  158. if (Input.GetKeyDown(KeyCode.F10))
  159. {
  160. TestSettlementDetection();
  161. }
  162. if (Input.GetKeyDown(KeyCode.F11))
  163. {
  164. ForceManualEntry();
  165. }
  166. }
  167. void OnGUI()
  168. {
  169. GUILayout.BeginArea(new Rect(10, Screen.height - 150, 400, 140));
  170. GUILayout.BeginVertical("box");
  171. GUILayout.Label("Settlement Interaction Quick Fix", new GUIStyle(GUI.skin.label) { fontSize = 14, fontStyle = FontStyle.Bold });
  172. if (GUILayout.Button("Fix Settlement Interaction (F9)"))
  173. {
  174. FixSettlementInteraction();
  175. }
  176. if (GUILayout.Button("Test Settlement Detection (F10)"))
  177. {
  178. TestSettlementDetection();
  179. }
  180. if (GUILayout.Button("Force Manual Entry (F11)"))
  181. {
  182. ForceManualEntry();
  183. }
  184. GUILayout.EndVertical();
  185. GUILayout.EndArea();
  186. }
  187. }