123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204 |
- using Player.Wallet;
- using TMPro;
- using UnityEngine;
- using UnityEngine.SceneManagement;
- using UnityEngine.UI; // Added for Button
- public class GameUI : MonoBehaviour
- {
- // 胜利和失败界面容器
- [SerializeField] private GameObject winnerWrapper, loseWrapper;
- // 金币、奖励、关卡等UI文本
- [SerializeField]
- private TextMeshProUGUI moneyDisplayText, rewardLoseDisplayText, rewardWinDisplayText, levelDisplayText;
- // 金币音效
- [SerializeField] private AudioSource coinAudioSource;
- // 钱包引用
- private Wallet _wallet;
- // 失败奖励
- private int _rewardLose;
- // 胜利奖励
- private int _rewardWin;
- // 是否已结算(防止重复)
- private bool _isStop;
- // 临时场景设置ID
- private int _tempSettingID;
- // 当前场景设置ID
- private int _settingID;
- private bool _isReloading = false; // 新增
- [SerializeField] private Button retryButton; // 在Inspector面板拖拽赋值
- [SerializeField] private MathQuizManager mathQuizManager; // 在Inspector拖拽赋值
- private void Awake()
- {
- _isStop = false;
- _isReloading = false;
- }
- /// <summary>
- /// 启用时注册金币变动事件
- /// </summary>
- private void OnEnable()
- {
- _wallet = FindObjectOfType<Wallet>();
- _wallet.PlayerMoneyEvent += UpdateMoneyText;
- }
- /// <summary>
- /// 禁用时注销金币变动事件
- /// </summary>
- private void OnDisable()
- {
- _wallet.PlayerMoneyEvent -= UpdateMoneyText;
- // 解绑再来一次按钮事件,防止多次绑定
- if (retryButton != null)
- {
- retryButton.onClick.RemoveListener(LoseButton);
- }
- }
- /// <summary>
- /// 初始化关卡、设置ID、显示关卡等级
- /// </summary>
- private void Start()
- {
- _settingID = PlayerPrefs.GetInt("_settingID");
- _tempSettingID = PlayerPrefs.GetInt("_tempSettingID");
- print("SETTING ID - " + _settingID);
- print("_tempSetting ID - " + _tempSettingID);
- levelDisplayText.SetText(I2.Loc.LocalizationManager.GetTranslation("Level") + " " + (ComponentsManager.PlayerData.GetLevel + 1));
- // 绑定再来一次按钮事件
- if (retryButton != null)
- {
- retryButton.onClick.RemoveListener(LoseButton);
- retryButton.onClick.AddListener(LoseButton);
- }
- }
- /// <summary>
- /// 播放金币音效
- /// </summary>
- public void CoiAudioPlay()
- {
- coinAudioSource.pitch = Random.Range(.9f, 1.1f);
- coinAudioSource.Play();
- }
- /// <summary>
- /// 设置胜利和失败奖励,并刷新UI
- /// </summary>
- /// <param name="win">胜利奖励</param>
- /// <param name="lose">失败奖励</param>
- public void SetRewards(int win, int lose)
- {
- _rewardLose = lose;
- _rewardWin = win;
- rewardLoseDisplayText.SetText("+" + lose);
- rewardWinDisplayText.SetText("+" + win);
- }
- /// <summary>
- /// 玩家胜利时显示胜利界面并结算
- /// </summary>
- public void PlayerWinner()
- {
- if (!_isStop)
- {
- winnerWrapper.SetActive(true);
- if (mathQuizManager != null)
- mathQuizManager.HideQuestionWindow(); // 隐藏答题界面
- SetEndEvent();
- PlayerPrefs.DeleteKey("_stageLevel");
- // 保存当前金币数量
-
- _isStop = true;
- // if (mathQuizManager != null) mathQuizManager.ShowQuizUI(false); // 胜利时隐藏
- }
- }
- /// <summary>
- /// 玩家失败时显示失败界面并结算
- /// </summary>
- public void PlayerLose()
- {
- Debug.Log("PlayerLose() called");
- if (!_isStop)
- {
- loseWrapper.SetActive(true);
- if (mathQuizManager != null)
- mathQuizManager.HideQuestionWindow(); // 隐藏答题界面
- _isStop = true;
- SetEndEvent();
- // if (mathQuizManager != null) mathQuizManager.ShowQuizUI(false); // 失败时隐藏
- }
- }
- /// <summary>
- /// 刷新金币显示文本
- /// </summary>
- /// <param name="value">当前金币数量</param>
- private void UpdateMoneyText(int value)
- {
- moneyDisplayText.SetText(value.ToString());
- }
- /// <summary>
- /// 胜利按钮点击,发放奖励、升级关卡、切换场景
- /// </summary>
- public void WinButton()
- {
- _wallet.AddMoney(_rewardWin);
- ComponentsManager.PlayerData.AddLevel();
- ChangeSetting();
- // 保存最终金币数量
-
- SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
- //SceneManager.LoadScene(0);
- }
- /// <summary>
- /// 失败按钮点击,发放失败奖励、重开场景
- /// </summary>
- public void LoseButton()
- {
- if (_isReloading) return; // 先判断,彻底防抖
- _isReloading = true;
- if (retryButton != null)
- retryButton.interactable = false; // 禁用按钮
- _wallet.AddMoney(_rewardLose);
- SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
- }
- /// <summary>
- /// 结算时的额外事件(预留扩展)
- /// </summary>
- private void SetEndEvent()
- {
- }
- /// <summary>
- /// 切换场景设置ID(用于不同主题或难度)
- /// </summary>
- private void ChangeSetting()
- {
- _tempSettingID++;
- if (_tempSettingID >= 2)
- {
- _tempSettingID = 0;
- if (_settingID >= 2)
- _settingID = 0;
- else
- _settingID++;
- PlayerPrefs.SetInt("_settingID", _settingID);
- }
- PlayerPrefs.SetInt("_tempSettingID", _tempSettingID);
- }
- }
|