| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126 |
- // TopBarController.ts
- import { _decorator, Component, Node, Button, Label, find } from 'cc';
- import { SaveDataManager } from '../LevelSystem/SaveDataManager';
- import EventBus, { GameEvents } from '../Core/EventBus';
- import { Audio } from '../AudioManager/AudioManager';
- const { ccclass, property } = _decorator;
- @ccclass('TopBarController')
- export class TopBarController extends Component {
- /* 顶部资源 & 关卡 */
- @property(Node) moneyAddBtn: Node = null; // 绿色 +
- @property(Node) diamondAddBtn: Node = null; // 紫色 +
- @property(Node) moneyLabel: Node = null; // 钞票数字
- @property(Node) diamondLabel: Node = null; // 钻石数字
- private sdm: SaveDataManager = null;
- onLoad() {
- this.sdm = SaveDataManager.getInstance();
- this.bindButtons();
- this.refreshAll();
- // 监听货币变化事件
- this.setupEventListeners();
- }
- onDestroy() {
- // 移除事件监听
- this.removeEventListeners();
- }
- /* 设置事件监听 */
- private setupEventListeners() {
- const eventBus = EventBus.getInstance();
-
- // 监听游戏开始事件,初始显示货币
- eventBus.on(GameEvents.GAME_START, this.refreshCurrency, this);
-
- // 监听货币变化事件
- eventBus.on(GameEvents.CURRENCY_CHANGED, this.refreshCurrency, this);
-
- // 监听UI重置事件
- eventBus.on(GameEvents.RESET_UI_STATES, this.refreshAll, this);
- }
-
- /* 移除事件监听 */
- private removeEventListeners() {
- const eventBus = EventBus.getInstance();
-
- eventBus.off(GameEvents.GAME_START, this.refreshCurrency, this);
- eventBus.off(GameEvents.CURRENCY_CHANGED, this.refreshCurrency, this);
- eventBus.off(GameEvents.RESET_UI_STATES, this.refreshAll, this);
- }
- /* 绑定按钮事件 */
- private bindButtons() {
- this.moneyAddBtn?.on(Button.EventType.CLICK, () => this.addMoney(100), this);
- this.diamondAddBtn?.on(Button.EventType.CLICK, () => this.addDiamonds(20), this);
- }
- /* ================= 业务逻辑 ================= */
- private addMoney(v: number) {
- // 播放UI点击音效
- Audio.playUISound('data/弹球音效/ui play');
- this.sdm.addMoney(v, 'ad');
- // 触发货币变化事件
- EventBus.getInstance().emit(GameEvents.CURRENCY_CHANGED);
- }
-
- private addDiamonds(v: number) {
- // 播放UI点击音效
- Audio.playUISound('data/弹球音效/ui play');
- this.sdm.addDiamonds(v, 'ad');
- // 触发货币变化事件
- EventBus.getInstance().emit(GameEvents.CURRENCY_CHANGED);
- }
- /* ================= 刷新 ================= */
- private refreshCurrency() {
- // 确保SaveDataManager已初始化
- if (!this.sdm) {
- console.warn('[TopBarController] SaveDataManager未初始化,跳过货币刷新');
- return;
- }
-
- // 如果SaveDataManager还没有调用initialize(),先初始化
- this.sdm.initialize();
-
- const currentCoins = this.sdm.getMoney();
- const currentDiamonds = this.sdm.getDiamonds();
-
- console.log(`[TopBarController] 刷新货币显示 - 钞票: ${currentCoins}, 钻石: ${currentDiamonds}`);
-
- const mLbl = this.moneyLabel?.getComponent(Label) || this.moneyLabel as unknown as Label;
- if (mLbl) {
- const formattedCoins = this.format(currentCoins);
- mLbl.string = formattedCoins;
- console.log(`[TopBarController] 钞票显示更新为: ${formattedCoins}`);
- }
-
- const dLbl = this.diamondLabel?.getComponent(Label) || this.diamondLabel as unknown as Label;
- if (dLbl) {
- const formattedDiamonds = this.format(currentDiamonds);
- dLbl.string = formattedDiamonds;
- console.log(`[TopBarController] 钻石显示更新为: ${formattedDiamonds}`);
- }
- }
-
- private refreshAll() {
- this.refreshCurrency();
- }
- // 供外部调用的公共刷新接口(保留以防其他地方需要手动刷新)
- public updateUI(): void {
- this.refreshAll();
- }
-
- /* =============== Util =============== */
- private format(n: number) {
- return n >= 1000000 ? (n / 1e6).toFixed(1) + 'M' : n >= 1000 ? (n / 1e3).toFixed(1) + 'K' : n.toString();
- }
- }
|