| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173 |
- // 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';
- import { AdManager } from '../Ads/AdManager';
- import { NavBarController } from './NavBarController';
- 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.onPlusClick, this);
- this.diamondAddBtn?.on(Button.EventType.CLICK, this.onPlusClick, this);
- }
- private onPlusClick() {
- this.openShopIfNotAlready();
- }
- private openShopIfNotAlready() {
- const navBarNode = find('Canvas/NavBar');
- if (!navBarNode) {
- console.warn('[TopBarController] 未找到 Canvas/NavBar 节点,无法打开商店');
- return;
- }
- const navBar = navBarNode.getComponent(NavBarController);
- if (!navBar) {
- console.warn('[TopBarController] NavBarController 未挂载,无法打开商店');
- return;
- }
- // 如果已经是商店面板,什么都不做
- if (navBar.isShopPanelActive()) {
- return;
- }
- // 直接打开商店面板(忽略按钮锁定状态)
- navBar.openShopPanel();
- }
- /* ================= 业务逻辑 ================= */
- private addMoney(v: number) {
- // 播放UI点击音效
- Audio.playUISound('data/弹球音效/ui play');
-
- // 显示激励视频广告
- AdManager.getInstance().showRewardedVideoAd(
- () => {
- // 广告观看完成,给予奖励
- this.sdm.addMoney(v, 'ad');
- // 触发货币变化事件
- EventBus.getInstance().emit(GameEvents.CURRENCY_CHANGED);
- },
- (error) => {
- console.error('[TopBarController] 广告显示失败:', error);
- // 可以选择不给予奖励或给予部分奖励
- }
- );
- }
-
- private addDiamonds(v: number) {
- // 播放UI点击音效
- Audio.playUISound('data/弹球音效/ui play');
-
- // 显示激励视频广告
- AdManager.getInstance().showRewardedVideoAd(
- () => {
- // 广告观看完成,给予奖励
- this.sdm.addDiamonds(v, 'ad');
- // 触发货币变化事件
- EventBus.getInstance().emit(GameEvents.CURRENCY_CHANGED);
- },
- (error) => {
- console.error('[TopBarController] 广告显示失败:', error);
- // 可以选择不给予奖励或给予部分奖励
- }
- );
- }
- /* ================= 刷新 ================= */
- 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();
- }
- }
|