GameUI.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. using Player.Wallet;
  2. using TMPro;
  3. using UnityEngine;
  4. using UnityEngine.SceneManagement;
  5. using UnityEngine.UI; // Added for Button
  6. public class GameUI : MonoBehaviour
  7. {
  8. // 胜利和失败界面容器
  9. [SerializeField] private GameObject winnerWrapper, loseWrapper;
  10. // 金币、奖励、关卡等UI文本
  11. [SerializeField]
  12. private TextMeshProUGUI moneyDisplayText, rewardLoseDisplayText, rewardWinDisplayText, levelDisplayText;
  13. // 金币音效
  14. [SerializeField] private AudioSource coinAudioSource;
  15. // 钱包引用
  16. private Wallet _wallet;
  17. // 失败奖励
  18. private int _rewardLose;
  19. // 胜利奖励
  20. private int _rewardWin;
  21. // 是否已结算(防止重复)
  22. private bool _isStop;
  23. // 临时场景设置ID
  24. private int _tempSettingID;
  25. // 当前场景设置ID
  26. private int _settingID;
  27. private bool _isReloading = false; // 新增
  28. [SerializeField] private Button retryButton; // 在Inspector面板拖拽赋值
  29. [SerializeField] private MathQuizManager mathQuizManager; // 在Inspector拖拽赋值
  30. private void Awake()
  31. {
  32. _isStop = false;
  33. _isReloading = false;
  34. }
  35. /// <summary>
  36. /// 启用时注册金币变动事件
  37. /// </summary>
  38. private void OnEnable()
  39. {
  40. _wallet = FindObjectOfType<Wallet>();
  41. _wallet.PlayerMoneyEvent += UpdateMoneyText;
  42. }
  43. /// <summary>
  44. /// 禁用时注销金币变动事件
  45. /// </summary>
  46. private void OnDisable()
  47. {
  48. _wallet.PlayerMoneyEvent -= UpdateMoneyText;
  49. // 解绑再来一次按钮事件,防止多次绑定
  50. if (retryButton != null)
  51. {
  52. retryButton.onClick.RemoveListener(LoseButton);
  53. }
  54. }
  55. /// <summary>
  56. /// 初始化关卡、设置ID、显示关卡等级
  57. /// </summary>
  58. private void Start()
  59. {
  60. _settingID = PlayerPrefs.GetInt("_settingID");
  61. _tempSettingID = PlayerPrefs.GetInt("_tempSettingID");
  62. print("SETTING ID - " + _settingID);
  63. print("_tempSetting ID - " + _tempSettingID);
  64. levelDisplayText.SetText(I2.Loc.LocalizationManager.GetTranslation("Level") + " " + (ComponentsManager.PlayerData.GetLevel + 1));
  65. // 绑定再来一次按钮事件
  66. if (retryButton != null)
  67. {
  68. retryButton.onClick.RemoveListener(LoseButton);
  69. retryButton.onClick.AddListener(LoseButton);
  70. }
  71. }
  72. /// <summary>
  73. /// 播放金币音效
  74. /// </summary>
  75. public void CoiAudioPlay()
  76. {
  77. coinAudioSource.pitch = Random.Range(.9f, 1.1f);
  78. coinAudioSource.Play();
  79. }
  80. /// <summary>
  81. /// 设置胜利和失败奖励,并刷新UI
  82. /// </summary>
  83. /// <param name="win">胜利奖励</param>
  84. /// <param name="lose">失败奖励</param>
  85. public void SetRewards(int win, int lose)
  86. {
  87. _rewardLose = lose;
  88. _rewardWin = win;
  89. rewardLoseDisplayText.SetText("+" + lose);
  90. rewardWinDisplayText.SetText("+" + win);
  91. }
  92. /// <summary>
  93. /// 玩家胜利时显示胜利界面并结算
  94. /// </summary>
  95. public void PlayerWinner()
  96. {
  97. if (!_isStop)
  98. {
  99. winnerWrapper.SetActive(true);
  100. if (mathQuizManager != null)
  101. mathQuizManager.HideQuestionWindow(); // 隐藏答题界面
  102. SetEndEvent();
  103. PlayerPrefs.DeleteKey("_stageLevel");
  104. // 保存当前金币数量
  105. _isStop = true;
  106. // if (mathQuizManager != null) mathQuizManager.ShowQuizUI(false); // 胜利时隐藏
  107. }
  108. }
  109. /// <summary>
  110. /// 玩家失败时显示失败界面并结算
  111. /// </summary>
  112. public void PlayerLose()
  113. {
  114. Debug.Log("PlayerLose() called");
  115. if (!_isStop)
  116. {
  117. loseWrapper.SetActive(true);
  118. if (mathQuizManager != null)
  119. mathQuizManager.HideQuestionWindow(); // 隐藏答题界面
  120. _isStop = true;
  121. SetEndEvent();
  122. // if (mathQuizManager != null) mathQuizManager.ShowQuizUI(false); // 失败时隐藏
  123. }
  124. }
  125. /// <summary>
  126. /// 刷新金币显示文本
  127. /// </summary>
  128. /// <param name="value">当前金币数量</param>
  129. private void UpdateMoneyText(int value)
  130. {
  131. moneyDisplayText.SetText(value.ToString());
  132. }
  133. /// <summary>
  134. /// 胜利按钮点击,发放奖励、升级关卡、切换场景
  135. /// </summary>
  136. public void WinButton()
  137. {
  138. _wallet.AddMoney(_rewardWin);
  139. ComponentsManager.PlayerData.AddLevel();
  140. ChangeSetting();
  141. // 保存最终金币数量
  142. SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
  143. //SceneManager.LoadScene(0);
  144. }
  145. /// <summary>
  146. /// 失败按钮点击,发放失败奖励、重开场景
  147. /// </summary>
  148. public void LoseButton()
  149. {
  150. if (_isReloading) return; // 先判断,彻底防抖
  151. _isReloading = true;
  152. if (retryButton != null)
  153. retryButton.interactable = false; // 禁用按钮
  154. _wallet.AddMoney(_rewardLose);
  155. SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
  156. }
  157. /// <summary>
  158. /// 结算时的额外事件(预留扩展)
  159. /// </summary>
  160. private void SetEndEvent()
  161. {
  162. }
  163. /// <summary>
  164. /// 切换场景设置ID(用于不同主题或难度)
  165. /// </summary>
  166. private void ChangeSetting()
  167. {
  168. _tempSettingID++;
  169. if (_tempSettingID >= 2)
  170. {
  171. _tempSettingID = 0;
  172. if (_settingID >= 2)
  173. _settingID = 0;
  174. else
  175. _settingID++;
  176. PlayerPrefs.SetInt("_settingID", _settingID);
  177. }
  178. PlayerPrefs.SetInt("_tempSettingID", _tempSettingID);
  179. }
  180. }