GameUI.cs 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. using Player.Wallet;
  2. using TMPro;
  3. using UnityEngine;
  4. using UnityEngine.SceneManagement;
  5. public class GameUI : MonoBehaviour
  6. {
  7. [SerializeField] private GameObject winnerWrapper, loseWrapper;
  8. [SerializeField]
  9. private TextMeshProUGUI moneyDisplayText, rewardLoseDisplayText, rewardWinDisplayText, levelDisplayText;
  10. [SerializeField] private AudioSource coinAudioSource;
  11. private Wallet _wallet;
  12. private int _rewardLose;
  13. private int _rewardWin;
  14. private bool _isStop;
  15. private int _tempSettingID;
  16. private int _settingID;
  17. private MathQuizManager quizManager;
  18. private void OnEnable()
  19. {
  20. _wallet = FindObjectOfType<Wallet>();
  21. _wallet.PlayerMoneyEvent += UpdateMoneyText;
  22. }
  23. private void OnDisable()
  24. {
  25. _wallet.PlayerMoneyEvent -= UpdateMoneyText;
  26. }
  27. private void Start()
  28. {
  29. _settingID = PlayerPrefs.GetInt("_settingID");
  30. _tempSettingID = PlayerPrefs.GetInt("_tempSettingID");
  31. print("SETTING ID - " + _settingID);
  32. print("_tempSetting ID - " + _tempSettingID);
  33. levelDisplayText.SetText(I2.Loc.LocalizationManager.GetTranslation("Level") + " " + (ComponentsManager.PlayerData.GetLevel + 1));
  34. quizManager = FindObjectOfType<MathQuizManager>();
  35. if (quizManager != null) quizManager.ShowQuizUI(false); // 游戏未开始时隐藏
  36. }
  37. public void CoiAudioPlay()
  38. {
  39. coinAudioSource.pitch = Random.Range(.9f, 1.1f);
  40. coinAudioSource.Play();
  41. }
  42. public void SetRewards(int win, int lose)
  43. {
  44. _rewardLose = lose;
  45. _rewardWin = win;
  46. rewardLoseDisplayText.SetText("+" + lose);
  47. rewardWinDisplayText.SetText("+" + win);
  48. }
  49. public void PlayerWinner()
  50. {
  51. if (!_isStop)
  52. {
  53. winnerWrapper.SetActive(true);
  54. SetEndEvent();
  55. PlayerPrefs.DeleteKey("_stageLevel");
  56. _isStop = true;
  57. if (quizManager != null) quizManager.ShowQuizUI(false); // 胜利时隐藏
  58. }
  59. }
  60. public void PlayerLose()
  61. {
  62. if (!_isStop)
  63. {
  64. loseWrapper.SetActive(true);
  65. _isStop = true;
  66. SetEndEvent();
  67. if (quizManager != null) quizManager.ShowQuizUI(false); // 失败时隐藏
  68. }
  69. }
  70. private void UpdateMoneyText(int value)
  71. {
  72. moneyDisplayText.SetText(value.ToString());
  73. }
  74. public void WinButton()
  75. {
  76. _wallet.AddMoney(_rewardWin);
  77. ComponentsManager.PlayerData.AddLevel();
  78. ChangeSetting();
  79. SceneManager.LoadScene(0);
  80. }
  81. public void LoseButton()
  82. {
  83. _wallet.AddMoney(_rewardLose);
  84. SceneManager.LoadScene(0);
  85. }
  86. private void SetEndEvent()
  87. {
  88. }
  89. private void ChangeSetting()
  90. {
  91. _tempSettingID++;
  92. if (_tempSettingID >= 2)
  93. {
  94. _tempSettingID = 0;
  95. if (_settingID >= 2)
  96. _settingID = 0;
  97. else
  98. _settingID++;
  99. PlayerPrefs.SetInt("_settingID", _settingID);
  100. }
  101. PlayerPrefs.SetInt("_tempSettingID", _tempSettingID);
  102. }
  103. }