TopBarController.ts 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. private bindButtons() {
  45. this.moneyAddBtn?.on(Button.EventType.CLICK, () => this.addMoney(100), this);
  46. this.diamondAddBtn?.on(Button.EventType.CLICK, () => this.addDiamonds(20), this);
  47. }
  48. /* ================= 业务逻辑 ================= */
  49. private addMoney(v: number) {
  50. // 播放UI点击音效
  51. Audio.playUISound('data/弹球音效/ui play');
  52. this.sdm.addMoney(v, 'ad');
  53. // 触发货币变化事件
  54. EventBus.getInstance().emit(GameEvents.CURRENCY_CHANGED);
  55. }
  56. private addDiamonds(v: number) {
  57. // 播放UI点击音效
  58. Audio.playUISound('data/弹球音效/ui play');
  59. this.sdm.addDiamonds(v, 'ad');
  60. // 触发货币变化事件
  61. EventBus.getInstance().emit(GameEvents.CURRENCY_CHANGED);
  62. }
  63. /* ================= 刷新 ================= */
  64. private refreshCurrency() {
  65. // 确保SaveDataManager已初始化
  66. if (!this.sdm) {
  67. console.warn('[TopBarController] SaveDataManager未初始化,跳过货币刷新');
  68. return;
  69. }
  70. // 如果SaveDataManager还没有调用initialize(),先初始化
  71. this.sdm.initialize();
  72. const currentCoins = this.sdm.getMoney();
  73. const currentDiamonds = this.sdm.getDiamonds();
  74. console.log(`[TopBarController] 刷新货币显示 - 钞票: ${currentCoins}, 钻石: ${currentDiamonds}`);
  75. const mLbl = this.moneyLabel?.getComponent(Label) || this.moneyLabel as unknown as Label;
  76. if (mLbl) {
  77. const formattedCoins = this.format(currentCoins);
  78. mLbl.string = formattedCoins;
  79. console.log(`[TopBarController] 钞票显示更新为: ${formattedCoins}`);
  80. }
  81. const dLbl = this.diamondLabel?.getComponent(Label) || this.diamondLabel as unknown as Label;
  82. if (dLbl) {
  83. const formattedDiamonds = this.format(currentDiamonds);
  84. dLbl.string = formattedDiamonds;
  85. console.log(`[TopBarController] 钻石显示更新为: ${formattedDiamonds}`);
  86. }
  87. }
  88. private refreshAll() {
  89. this.refreshCurrency();
  90. }
  91. // 供外部调用的公共刷新接口(保留以防其他地方需要手动刷新)
  92. public updateUI(): void {
  93. this.refreshAll();
  94. }
  95. /* =============== Util =============== */
  96. private format(n: number) {
  97. return n >= 1000000 ? (n / 1e6).toFixed(1) + 'M' : n >= 1000 ? (n / 1e3).toFixed(1) + 'K' : n.toString();
  98. }
  99. }