BattleUiScript.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. using UnityEngine;
  2. using UnityEngine.UIElements; // Changed from UnityEngine.UI
  3. using System.Collections;
  4. using System;
  5. public class BattleUIManager : MonoBehaviour
  6. {
  7. public static BattleUIManager Instance { get; private set; }
  8. [Header("UI Toolkit References")]
  9. [Tooltip("Assign the UIDocument component from the scene.")]
  10. public UIDocument uiDocument;
  11. // UI Toolkit Element references
  12. private Button runPauseButtonElement;
  13. private Button advanceButtonElement;
  14. // Coroutine handle for the advance action
  15. private Coroutine advanceCoroutineHandle;
  16. void Awake()
  17. {
  18. if (Instance == null)
  19. {
  20. Instance = this;
  21. }
  22. else
  23. {
  24. Debug.LogWarning("Multiple instances of BattleUIManager found. Destroying this one.");
  25. Destroy(gameObject);
  26. return;
  27. }
  28. }
  29. void Start()
  30. {
  31. uiDocument = GetComponent<UIDocument>();
  32. if (uiDocument == null)
  33. {
  34. Debug.LogError("BattleUIManager: UIDocument is not assigned in the Inspector. Please assign it.");
  35. SetButtonsInteractable(false); // Attempt to disable if elements were somehow found
  36. enabled = false;
  37. return;
  38. }
  39. var rootVisualElement = uiDocument.rootVisualElement;
  40. if (rootVisualElement == null)
  41. {
  42. Debug.LogError("BattleUIManager: UIDocument has no rootVisualElement. Ensure UXML is correctly set up.");
  43. enabled = false;
  44. return;
  45. }
  46. // Query for the buttons by their names defined in UXML
  47. runPauseButtonElement = rootVisualElement.Q<Button>("RunPauseButton");
  48. advanceButtonElement = rootVisualElement.Q<Button>("AdvanceActionButton");
  49. if (runPauseButtonElement == null || advanceButtonElement == null)
  50. {
  51. Debug.LogError("BattleUIManager: Could not find 'RunPauseButton' or 'AdvanceActionButton' in the UXML. Check names.");
  52. SetButtonsInteractable(false);
  53. enabled = false;
  54. return;
  55. }
  56. if (GameManager.Instance == null)
  57. {
  58. Debug.LogError("BattleUIManager: GameManager.Instance is null. Ensure GameManager is in the scene and initialized.");
  59. SetButtonsInteractable(false);
  60. enabled = false;
  61. return;
  62. }
  63. // Initialize UI based on GameManager's expected initial state
  64. runPauseButtonElement.text = "Run";
  65. advanceButtonElement.SetEnabled(true); // Can advance from initial paused state
  66. runPauseButtonElement.SetEnabled(true);
  67. runPauseButtonElement.RegisterCallback<ClickEvent>(evt => OnRunPauseButtonPressed());
  68. advanceButtonElement.RegisterCallback<ClickEvent>(evt => OnAdvanceActionButtonPressed());
  69. }
  70. public void OnRunPauseButtonPressed()
  71. {
  72. if (GameManager.Instance == null) return;
  73. if (GameManager.Instance.isContinuousRunActive) // Was running continuously, now pause
  74. {
  75. GameManager.Instance.isContinuousRunActive = false;
  76. GameManager.Instance.PauseGame();
  77. runPauseButtonElement.text = "Run";
  78. advanceButtonElement.SetEnabled(true); // Allow advance when paused
  79. }
  80. else // Was paused or not in continuous run, now run continuously
  81. {
  82. if (advanceCoroutineHandle != null) // Stop any pending "Advance" action
  83. {
  84. StopCoroutine(advanceCoroutineHandle);
  85. advanceCoroutineHandle = null;
  86. }
  87. GameManager.Instance.isContinuousRunActive = true;
  88. GameManager.Instance.ResumeGame();
  89. runPauseButtonElement.text = "Pause";
  90. advanceButtonElement.SetEnabled(false); // Disable advance when in continuous run
  91. }
  92. }
  93. public void OnAdvanceActionButtonPressed()
  94. {
  95. //
  96. if (GameManager.Instance == null) return;
  97. if (GameManager.Instance.isContinuousRunActive)
  98. {
  99. return;
  100. }
  101. // Distable advance button immediately
  102. SetButtonsInteractable(false);
  103. CinemachineCameraController.Instance.FrameAllCharacters(GameManager.Instance.GetAllCharacters());
  104. // If already advancing, stop the current coroutine
  105. GameManager.Instance.AdvanceAction();
  106. }
  107. public void SetButtonsInteractable(bool interactable)
  108. {
  109. runPauseButtonElement?.SetEnabled(interactable);
  110. advanceButtonElement?.SetEnabled(interactable);
  111. }
  112. // Call this from GameManager when the battle ends
  113. public void DisableBattleControls()
  114. {
  115. SetButtonsInteractable(false);
  116. if (runPauseButtonElement != null)
  117. {
  118. runPauseButtonElement.text = "Return to Map";
  119. runPauseButtonElement.SetEnabled(true); // Re-enable this button for map return
  120. // Add new callback for returning to map
  121. runPauseButtonElement.RegisterCallback<ClickEvent>(evt => OnReturnToMapButtonPressed());
  122. }
  123. }
  124. /// <summary>
  125. /// Handle the return to map button press after battle ends
  126. /// </summary>
  127. private void OnReturnToMapButtonPressed()
  128. {
  129. // Try to use the existing GameManager victory completion flow
  130. if (GameManager.Instance != null)
  131. {
  132. // Use the new public method for manual return
  133. GameManager.Instance.ManualReturnToMap();
  134. }
  135. else
  136. {
  137. // Fallback: try to find and use the battle setup system
  138. var battleSetup = FindFirstObjectByType<EnhancedBattleSetup>();
  139. if (battleSetup != null)
  140. {
  141. battleSetup.EndBattleSession(true); // Assume player victory
  142. }
  143. else
  144. {
  145. // Last resort: directly load the map scene
  146. try
  147. {
  148. UnityEngine.SceneManagement.SceneManager.LoadScene("MapScene2");
  149. }
  150. catch (System.Exception e)
  151. {
  152. Debug.LogError($"Failed to load MapScene2 via fallback: {e.Message}");
  153. }
  154. }
  155. }
  156. }
  157. void OnDestroy()
  158. {
  159. // Clean up listeners if the object is destroyed
  160. // For UI Toolkit, callbacks are typically managed well by the system, but explicit unregistering can be done if needed.
  161. // runPauseButtonElement?.UnregisterCallback<ClickEvent>(...); // if you stored the exact lambda or method
  162. if (Instance == this) // Singleton cleanup
  163. {
  164. Instance = null;
  165. }
  166. }
  167. }