TopBarController.ts 3.8 KB

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