TopBarController.ts 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. // TopBarController.ts
  2. import { _decorator, Component, Node, Button, Label, find } from 'cc';
  3. import { SaveDataManager } from '../LevelSystem/SaveDataManager';
  4. import EventBus, { GameEvents } from '../Core/EventBus';
  5. import { Audio } from '../AudioManager/AudioManager';
  6. import { AdManager } from '../Ads/AdManager';
  7. const { ccclass, property } = _decorator;
  8. @ccclass('TopBarController')
  9. export class TopBarController extends Component {
  10. /* 顶部资源 & 关卡 */
  11. @property(Node) moneyAddBtn: Node = null; // 绿色 +
  12. @property(Node) diamondAddBtn: Node = null; // 紫色 +
  13. @property(Node) moneyLabel: Node = null; // 钞票数字
  14. @property(Node) diamondLabel: Node = null; // 钻石数字
  15. private sdm: SaveDataManager = null;
  16. onLoad() {
  17. this.sdm = SaveDataManager.getInstance();
  18. this.bindButtons();
  19. this.refreshAll();
  20. // 监听货币变化事件
  21. this.setupEventListeners();
  22. }
  23. onDestroy() {
  24. // 移除事件监听
  25. this.removeEventListeners();
  26. }
  27. /* 设置事件监听 */
  28. private setupEventListeners() {
  29. const eventBus = EventBus.getInstance();
  30. // 监听游戏开始事件,初始显示货币
  31. eventBus.on(GameEvents.GAME_START, this.refreshCurrency, this);
  32. // 监听货币变化事件
  33. eventBus.on(GameEvents.CURRENCY_CHANGED, this.refreshCurrency, this);
  34. // 监听UI重置事件
  35. eventBus.on(GameEvents.RESET_UI_STATES, this.refreshAll, this);
  36. }
  37. /* 移除事件监听 */
  38. private removeEventListeners() {
  39. const eventBus = EventBus.getInstance();
  40. eventBus.off(GameEvents.GAME_START, this.refreshCurrency, this);
  41. eventBus.off(GameEvents.CURRENCY_CHANGED, this.refreshCurrency, this);
  42. eventBus.off(GameEvents.RESET_UI_STATES, this.refreshAll, this);
  43. }
  44. /* 绑定按钮事件 */
  45. //看广告才能获取的货币
  46. private bindButtons() {
  47. this.moneyAddBtn?.on(Button.EventType.CLICK, () => this.addMoney(100), this);
  48. this.diamondAddBtn?.on(Button.EventType.CLICK, () => this.addDiamonds(20), this);
  49. }
  50. /* ================= 业务逻辑 ================= */
  51. private addMoney(v: number) {
  52. // 播放UI点击音效
  53. Audio.playUISound('data/弹球音效/ui play');
  54. // 显示激励视频广告
  55. AdManager.getInstance().showRewardedVideoAd(
  56. () => {
  57. // 广告观看完成,给予奖励
  58. this.sdm.addMoney(v, 'ad');
  59. // 触发货币变化事件
  60. EventBus.getInstance().emit(GameEvents.CURRENCY_CHANGED);
  61. },
  62. (error) => {
  63. console.error('[TopBarController] 广告显示失败:', error);
  64. // 可以选择不给予奖励或给予部分奖励
  65. }
  66. );
  67. }
  68. private addDiamonds(v: number) {
  69. // 播放UI点击音效
  70. Audio.playUISound('data/弹球音效/ui play');
  71. // 显示激励视频广告
  72. AdManager.getInstance().showRewardedVideoAd(
  73. () => {
  74. // 广告观看完成,给予奖励
  75. this.sdm.addDiamonds(v, 'ad');
  76. // 触发货币变化事件
  77. EventBus.getInstance().emit(GameEvents.CURRENCY_CHANGED);
  78. },
  79. (error) => {
  80. console.error('[TopBarController] 广告显示失败:', error);
  81. // 可以选择不给予奖励或给予部分奖励
  82. }
  83. );
  84. }
  85. /* ================= 刷新 ================= */
  86. private refreshCurrency() {
  87. // 确保SaveDataManager已初始化
  88. if (!this.sdm) {
  89. console.warn('[TopBarController] SaveDataManager未初始化,跳过货币刷新');
  90. return;
  91. }
  92. // 如果SaveDataManager还没有调用initialize(),先初始化
  93. this.sdm.initialize();
  94. const currentCoins = this.sdm.getMoney();
  95. const currentDiamonds = this.sdm.getDiamonds();
  96. console.log(`[TopBarController] 刷新货币显示 - 钞票: ${currentCoins}, 钻石: ${currentDiamonds}`);
  97. const mLbl = this.moneyLabel?.getComponent(Label) || this.moneyLabel as unknown as Label;
  98. if (mLbl) {
  99. const formattedCoins = this.format(currentCoins);
  100. mLbl.string = formattedCoins;
  101. console.log(`[TopBarController] 钞票显示更新为: ${formattedCoins}`);
  102. }
  103. const dLbl = this.diamondLabel?.getComponent(Label) || this.diamondLabel as unknown as Label;
  104. if (dLbl) {
  105. const formattedDiamonds = this.format(currentDiamonds);
  106. dLbl.string = formattedDiamonds;
  107. console.log(`[TopBarController] 钻石显示更新为: ${formattedDiamonds}`);
  108. }
  109. }
  110. private refreshAll() {
  111. this.refreshCurrency();
  112. }
  113. // 供外部调用的公共刷新接口(保留以防其他地方需要手动刷新)
  114. public updateUI(): void {
  115. this.refreshAll();
  116. }
  117. /* =============== Util =============== */
  118. private format(n: number) {
  119. return n >= 1000000 ? (n / 1e6).toFixed(1) + 'M' : n >= 1000 ? (n / 1e3).toFixed(1) + 'K' : n.toString();
  120. }
  121. }