SettlementInteractionDebugger.cs 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. using UnityEngine;
  2. /// <summary>
  3. /// Debug script to diagnose settlement interaction issues
  4. /// Add this to a GameObject in MapScene2 to test the settlement interaction system
  5. /// </summary>
  6. public class SettlementInteractionDebugger : MonoBehaviour
  7. {
  8. [Header("Debug Settings")]
  9. public bool enableDebugOverlay = true;
  10. public bool logEveryFrame = false;
  11. private SettlementInteractionManager interactionManager;
  12. private SettlementInteractionUI interactionUI;
  13. private Transform teamMarker;
  14. void Start()
  15. {
  16. Debug.Log("=== Settlement Interaction Debugger Started ===");
  17. // Find components
  18. interactionManager = FindFirstObjectByType<SettlementInteractionManager>();
  19. interactionUI = FindFirstObjectByType<SettlementInteractionUI>();
  20. teamMarker = GameObject.Find("TeamMarker")?.transform;
  21. // Initial diagnostics
  22. DiagnoseComponents();
  23. }
  24. void Update()
  25. {
  26. if (logEveryFrame)
  27. {
  28. LogCurrentState();
  29. }
  30. // Test manual input
  31. if (Input.GetKeyDown(KeyCode.F1))
  32. {
  33. TestSettlementDetection();
  34. }
  35. if (Input.GetKeyDown(KeyCode.F2))
  36. {
  37. TestManualEntry();
  38. }
  39. }
  40. private void DiagnoseComponents()
  41. {
  42. Debug.Log("=== Component Diagnosis ===");
  43. // Check SettlementInteractionManager
  44. if (interactionManager != null)
  45. {
  46. Debug.Log("✅ SettlementInteractionManager found");
  47. Debug.Log($" - GameObject: {interactionManager.gameObject.name}");
  48. Debug.Log($" - Enabled: {interactionManager.enabled}");
  49. Debug.Log($" - Active: {interactionManager.gameObject.activeSelf}");
  50. Debug.Log($" - TeamMarker assigned: {interactionManager.teamMarker != null}");
  51. Debug.Log($" - Interaction Distance: {interactionManager.interactionDistance}");
  52. Debug.Log($" - Enter Key: {interactionManager.enterSettlementKey}");
  53. Debug.Log($" - Debug Logs Enabled: {interactionManager.enableDebugLogs}");
  54. // Check if MapData is accessible
  55. var mapMaker = FindFirstObjectByType<MapMaker2>();
  56. if (mapMaker != null)
  57. {
  58. var mapData = mapMaker.GetMapData();
  59. Debug.Log($" - MapData available: {mapData != null}");
  60. if (mapData != null)
  61. {
  62. var settlements = mapData.GetAllSettlements();
  63. Debug.Log($" - Settlements count: {settlements?.Count ?? 0}");
  64. }
  65. }
  66. else
  67. {
  68. Debug.LogWarning(" - MapMaker2 not found!");
  69. }
  70. }
  71. else
  72. {
  73. Debug.LogError("❌ SettlementInteractionManager NOT FOUND!");
  74. Debug.LogError(" Make sure to add SettlementInteractionManager component to a GameObject in MapScene2");
  75. }
  76. // Check SettlementInteractionUI
  77. if (interactionUI != null)
  78. {
  79. Debug.Log("✅ SettlementInteractionUI found");
  80. Debug.Log($" - GameObject: {interactionUI.gameObject.name}");
  81. }
  82. else
  83. {
  84. Debug.LogWarning("⚠️ SettlementInteractionUI not found");
  85. }
  86. // Check TeamMarker
  87. if (teamMarker != null)
  88. {
  89. Debug.Log("✅ TeamMarker found");
  90. Debug.Log($" - Position: {teamMarker.position}");
  91. }
  92. else
  93. {
  94. Debug.LogError("❌ TeamMarker GameObject not found!");
  95. }
  96. Debug.Log("=== End Diagnosis ===");
  97. Debug.Log("Press F1 to test settlement detection");
  98. Debug.Log("Press F2 to test manual settlement entry");
  99. }
  100. private void LogCurrentState()
  101. {
  102. if (interactionManager == null || teamMarker == null) return;
  103. var currentSettlement = interactionManager.GetCurrentNearbySettlement();
  104. Debug.Log($"[Frame] TeamMarker: {teamMarker.position}, Current Settlement: {(currentSettlement?.name ?? "None")}");
  105. }
  106. [ContextMenu("Test Settlement Detection")]
  107. public void TestSettlementDetection()
  108. {
  109. Debug.Log("=== Testing Settlement Detection ===");
  110. if (interactionManager == null)
  111. {
  112. Debug.LogError("No SettlementInteractionManager found!");
  113. return;
  114. }
  115. if (teamMarker == null)
  116. {
  117. Debug.LogError("No TeamMarker found!");
  118. return;
  119. }
  120. var currentSettlement = interactionManager.GetCurrentNearbySettlement();
  121. Debug.Log($"Current nearby settlement: {(currentSettlement?.name ?? "None")}");
  122. // Check if UI is showing
  123. if (interactionUI != null)
  124. {
  125. Debug.Log("SettlementInteractionUI is present and should be handling events");
  126. }
  127. // Manual coordinate check
  128. Vector2 teamWorldPos = new Vector2(teamMarker.position.x, teamMarker.position.z);
  129. Debug.Log($"TeamMarker world position: {teamWorldPos}");
  130. // Try to find MapData and check settlements manually
  131. var mapMaker = FindFirstObjectByType<MapMaker2>();
  132. if (mapMaker != null)
  133. {
  134. var mapData = mapMaker.GetMapData();
  135. if (mapData != null)
  136. {
  137. var settlements = mapData.GetAllSettlements();
  138. Debug.Log($"Total settlements in map: {settlements?.Count ?? 0}");
  139. if (settlements != null && settlements.Count > 0)
  140. {
  141. foreach (var settlement in settlements)
  142. {
  143. float distance = Vector2.Distance(teamWorldPos, new Vector2(settlement.position.x, settlement.position.y));
  144. Debug.Log($"Settlement '{settlement.name}' at {settlement.position} - Distance: {distance:F2}");
  145. }
  146. }
  147. }
  148. }
  149. }
  150. [ContextMenu("Test Manual Entry")]
  151. public void TestManualEntry()
  152. {
  153. Debug.Log("=== Testing Manual Settlement Entry ===");
  154. if (interactionManager != null)
  155. {
  156. interactionManager.TestEnterCurrentSettlement();
  157. }
  158. else
  159. {
  160. Debug.LogError("No SettlementInteractionManager found!");
  161. }
  162. }
  163. [ContextMenu("Force Enable Debug Logging")]
  164. public void ForceEnableDebugLogging()
  165. {
  166. if (interactionManager != null)
  167. {
  168. interactionManager.enableDebugLogs = true;
  169. Debug.Log("Debug logging enabled on SettlementInteractionManager");
  170. }
  171. }
  172. [ContextMenu("Test Input Detection")]
  173. public void TestInputDetection()
  174. {
  175. Debug.Log("=== Testing Input Detection ===");
  176. Debug.Log($"Input.GetKeyDown(KeyCode.E): {Input.GetKeyDown(KeyCode.E)}");
  177. Debug.Log($"Input.GetKey(KeyCode.E): {Input.GetKey(KeyCode.E)}");
  178. Debug.Log($"Input.inputString: '{Input.inputString}'");
  179. }
  180. void OnGUI()
  181. {
  182. if (!enableDebugOverlay) return;
  183. GUILayout.BeginArea(new Rect(10, 10, 400, 300));
  184. GUILayout.BeginVertical("box");
  185. GUILayout.Label("Settlement Interaction Debug", new GUIStyle(GUI.skin.label) { fontSize = 16, fontStyle = FontStyle.Bold });
  186. if (interactionManager != null)
  187. {
  188. var currentSettlement = interactionManager.GetCurrentNearbySettlement();
  189. GUILayout.Label($"Current Settlement: {(currentSettlement?.name ?? "None")}");
  190. if (teamMarker != null)
  191. {
  192. GUILayout.Label($"TeamMarker Position: {teamMarker.position}");
  193. }
  194. GUILayout.Space(10);
  195. if (GUILayout.Button("Test Settlement Detection (F1)"))
  196. {
  197. TestSettlementDetection();
  198. }
  199. if (GUILayout.Button("Test Manual Entry (F2)"))
  200. {
  201. TestManualEntry();
  202. }
  203. if (GUILayout.Button("Force Debug Logging"))
  204. {
  205. ForceEnableDebugLogging();
  206. }
  207. }
  208. else
  209. {
  210. GUILayout.Label("❌ SettlementInteractionManager NOT FOUND!", new GUIStyle(GUI.skin.label) { normal = { textColor = Color.red } });
  211. GUILayout.Label("Add SettlementInteractionManager component to a GameObject in this scene");
  212. }
  213. GUILayout.EndVertical();
  214. GUILayout.EndArea();
  215. }
  216. }