TopBarController.ts 4.1 KB

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