| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254 |
- using UnityEngine;
- /// <summary>
- /// Debug script to diagnose settlement interaction issues
- /// Add this to a GameObject in MapScene2 to test the settlement interaction system
- /// </summary>
- public class SettlementInteractionDebugger : MonoBehaviour
- {
- [Header("Debug Settings")]
- public bool enableDebugOverlay = true;
- public bool logEveryFrame = false;
- private SettlementInteractionManager interactionManager;
- private SettlementInteractionUI interactionUI;
- private Transform teamMarker;
- void Start()
- {
- Debug.Log("=== Settlement Interaction Debugger Started ===");
- // Find components
- interactionManager = FindFirstObjectByType<SettlementInteractionManager>();
- interactionUI = FindFirstObjectByType<SettlementInteractionUI>();
- teamMarker = GameObject.Find("TeamMarker")?.transform;
- // Initial diagnostics
- DiagnoseComponents();
- }
- void Update()
- {
- if (logEveryFrame)
- {
- LogCurrentState();
- }
- // Test manual input
- if (Input.GetKeyDown(KeyCode.F1))
- {
- TestSettlementDetection();
- }
- if (Input.GetKeyDown(KeyCode.F2))
- {
- TestManualEntry();
- }
- }
- private void DiagnoseComponents()
- {
- Debug.Log("=== Component Diagnosis ===");
- // Check SettlementInteractionManager
- if (interactionManager != null)
- {
- Debug.Log("✅ SettlementInteractionManager found");
- Debug.Log($" - GameObject: {interactionManager.gameObject.name}");
- Debug.Log($" - Enabled: {interactionManager.enabled}");
- Debug.Log($" - Active: {interactionManager.gameObject.activeSelf}");
- Debug.Log($" - TeamMarker assigned: {interactionManager.teamMarker != null}");
- Debug.Log($" - Interaction Distance: {interactionManager.interactionDistance}");
- Debug.Log($" - Enter Key: {interactionManager.enterSettlementKey}");
- Debug.Log($" - Debug Logs Enabled: {interactionManager.enableDebugLogs}");
- // Check if MapData is accessible
- var mapMaker = FindFirstObjectByType<MapMaker2>();
- if (mapMaker != null)
- {
- var mapData = mapMaker.GetMapData();
- Debug.Log($" - MapData available: {mapData != null}");
- if (mapData != null)
- {
- var settlements = mapData.GetAllSettlements();
- Debug.Log($" - Settlements count: {settlements?.Count ?? 0}");
- }
- }
- else
- {
- Debug.LogWarning(" - MapMaker2 not found!");
- }
- }
- else
- {
- Debug.LogError("❌ SettlementInteractionManager NOT FOUND!");
- Debug.LogError(" Make sure to add SettlementInteractionManager component to a GameObject in MapScene2");
- }
- // Check SettlementInteractionUI
- if (interactionUI != null)
- {
- Debug.Log("✅ SettlementInteractionUI found");
- Debug.Log($" - GameObject: {interactionUI.gameObject.name}");
- }
- else
- {
- Debug.LogWarning("⚠️ SettlementInteractionUI not found");
- }
- // Check TeamMarker
- if (teamMarker != null)
- {
- Debug.Log("✅ TeamMarker found");
- Debug.Log($" - Position: {teamMarker.position}");
- }
- else
- {
- Debug.LogError("❌ TeamMarker GameObject not found!");
- }
- Debug.Log("=== End Diagnosis ===");
- Debug.Log("Press F1 to test settlement detection");
- Debug.Log("Press F2 to test manual settlement entry");
- }
- private void LogCurrentState()
- {
- if (interactionManager == null || teamMarker == null) return;
- var currentSettlement = interactionManager.GetCurrentNearbySettlement();
- Debug.Log($"[Frame] TeamMarker: {teamMarker.position}, Current Settlement: {(currentSettlement?.name ?? "None")}");
- }
- [ContextMenu("Test Settlement Detection")]
- public void TestSettlementDetection()
- {
- Debug.Log("=== Testing Settlement Detection ===");
- if (interactionManager == null)
- {
- Debug.LogError("No SettlementInteractionManager found!");
- return;
- }
- if (teamMarker == null)
- {
- Debug.LogError("No TeamMarker found!");
- return;
- }
- var currentSettlement = interactionManager.GetCurrentNearbySettlement();
- Debug.Log($"Current nearby settlement: {(currentSettlement?.name ?? "None")}");
- // Check if UI is showing
- if (interactionUI != null)
- {
- Debug.Log("SettlementInteractionUI is present and should be handling events");
- }
- // Manual coordinate check
- Vector2 teamWorldPos = new Vector2(teamMarker.position.x, teamMarker.position.z);
- Debug.Log($"TeamMarker world position: {teamWorldPos}");
- // Try to find MapData and check settlements manually
- var mapMaker = FindFirstObjectByType<MapMaker2>();
- if (mapMaker != null)
- {
- var mapData = mapMaker.GetMapData();
- if (mapData != null)
- {
- var settlements = mapData.GetAllSettlements();
- Debug.Log($"Total settlements in map: {settlements?.Count ?? 0}");
- if (settlements != null && settlements.Count > 0)
- {
- foreach (var settlement in settlements)
- {
- float distance = Vector2.Distance(teamWorldPos, new Vector2(settlement.position.x, settlement.position.y));
- Debug.Log($"Settlement '{settlement.name}' at {settlement.position} - Distance: {distance:F2}");
- }
- }
- }
- }
- }
- [ContextMenu("Test Manual Entry")]
- public void TestManualEntry()
- {
- Debug.Log("=== Testing Manual Settlement Entry ===");
- if (interactionManager != null)
- {
- interactionManager.TestEnterCurrentSettlement();
- }
- else
- {
- Debug.LogError("No SettlementInteractionManager found!");
- }
- }
- [ContextMenu("Force Enable Debug Logging")]
- public void ForceEnableDebugLogging()
- {
- if (interactionManager != null)
- {
- interactionManager.enableDebugLogs = true;
- Debug.Log("Debug logging enabled on SettlementInteractionManager");
- }
- }
- [ContextMenu("Test Input Detection")]
- public void TestInputDetection()
- {
- Debug.Log("=== Testing Input Detection ===");
- Debug.Log($"Input.GetKeyDown(KeyCode.E): {Input.GetKeyDown(KeyCode.E)}");
- Debug.Log($"Input.GetKey(KeyCode.E): {Input.GetKey(KeyCode.E)}");
- Debug.Log($"Input.inputString: '{Input.inputString}'");
- }
- void OnGUI()
- {
- if (!enableDebugOverlay) return;
- GUILayout.BeginArea(new Rect(10, 10, 400, 300));
- GUILayout.BeginVertical("box");
- GUILayout.Label("Settlement Interaction Debug", new GUIStyle(GUI.skin.label) { fontSize = 16, fontStyle = FontStyle.Bold });
- if (interactionManager != null)
- {
- var currentSettlement = interactionManager.GetCurrentNearbySettlement();
- GUILayout.Label($"Current Settlement: {(currentSettlement?.name ?? "None")}");
- if (teamMarker != null)
- {
- GUILayout.Label($"TeamMarker Position: {teamMarker.position}");
- }
- GUILayout.Space(10);
- if (GUILayout.Button("Test Settlement Detection (F1)"))
- {
- TestSettlementDetection();
- }
- if (GUILayout.Button("Test Manual Entry (F2)"))
- {
- TestManualEntry();
- }
- if (GUILayout.Button("Force Debug Logging"))
- {
- ForceEnableDebugLogging();
- }
- }
- else
- {
- GUILayout.Label("❌ SettlementInteractionManager NOT FOUND!", new GUIStyle(GUI.skin.label) { normal = { textColor = Color.red } });
- GUILayout.Label("Add SettlementInteractionManager component to a GameObject in this scene");
- }
- GUILayout.EndVertical();
- GUILayout.EndArea();
- }
- }
|