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;
}
///
/// 启用时注册金币变动事件
///
private void OnEnable()
{
_wallet = FindObjectOfType();
_wallet.PlayerMoneyEvent += UpdateMoneyText;
}
///
/// 禁用时注销金币变动事件
///
private void OnDisable()
{
_wallet.PlayerMoneyEvent -= UpdateMoneyText;
// 解绑再来一次按钮事件,防止多次绑定
if (retryButton != null)
{
retryButton.onClick.RemoveListener(LoseButton);
}
}
///
/// 初始化关卡、设置ID、显示关卡等级
///
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);
}
}
///
/// 播放金币音效
///
public void CoiAudioPlay()
{
coinAudioSource.pitch = Random.Range(.9f, 1.1f);
coinAudioSource.Play();
}
///
/// 设置胜利和失败奖励,并刷新UI
///
/// 胜利奖励
/// 失败奖励
public void SetRewards(int win, int lose)
{
_rewardLose = lose;
_rewardWin = win;
rewardLoseDisplayText.SetText("+" + lose);
rewardWinDisplayText.SetText("+" + win);
}
///
/// 玩家胜利时显示胜利界面并结算
///
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); // 胜利时隐藏
}
}
///
/// 玩家失败时显示失败界面并结算
///
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); // 失败时隐藏
}
}
///
/// 刷新金币显示文本
///
/// 当前金币数量
private void UpdateMoneyText(int value)
{
moneyDisplayText.SetText(value.ToString());
}
///
/// 胜利按钮点击,发放奖励、升级关卡、切换场景
///
public void WinButton()
{
_wallet.AddMoney(_rewardWin);
ComponentsManager.PlayerData.AddLevel();
ChangeSetting();
// 保存最终金币数量
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
//SceneManager.LoadScene(0);
}
///
/// 失败按钮点击,发放失败奖励、重开场景
///
public void LoseButton()
{
if (_isReloading) return; // 先判断,彻底防抖
_isReloading = true;
if (retryButton != null)
retryButton.interactable = false; // 禁用按钮
_wallet.AddMoney(_rewardLose);
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
}
///
/// 结算时的额外事件(预留扩展)
///
private void SetEndEvent()
{
}
///
/// 切换场景设置ID(用于不同主题或难度)
///
private void ChangeSetting()
{
_tempSettingID++;
if (_tempSettingID >= 2)
{
_tempSettingID = 0;
if (_settingID >= 2)
_settingID = 0;
else
_settingID++;
PlayerPrefs.SetInt("_settingID", _settingID);
}
PlayerPrefs.SetInt("_tempSettingID", _tempSettingID);
}
}