| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331 |
- import { _decorator, Color, Component, Node, Sprite, find, Vec3 } from 'cc';
- import EventBus, { GameEvents } from '../Core/EventBus';
- import { GameStartMove } from '../Animations/GameStartMove';
- const { ccclass, property } = _decorator;
- @ccclass('NavBarController')
- export class NavBarController extends Component {
- @property({ type: [Node] }) panels: Node[] = []; // 依次给 Main、Shop、Upgrade、Skill
- @property({ type: [Node] }) buttons: Node[] = []; // 依次给 Battle、Shop、Upgrade、Skill
- @property({ type: [Node] }) buttonBorders: Node[] = []; // 按钮边框节点数组,需要手动配置
-
- private normalColor = new Color(255, 255, 255, 255);
- private lockedColor = new Color(128, 128, 128, 255); // 灰色(锁定状态)
-
- // 按钮锁定状态数组 - 对应 Battle、Shop、Upgrade、Skill 四个按钮
- private buttonLockStates: boolean[] = [false, false, false, false]; // 默认所有按钮都解锁
-
- // 存储按钮原始位置
- private buttonOriginalPositions: Vec3[] = [];
-
- // 当前激活的按钮索引
- private currentActiveButtonIndex: number = -1;
-
- // GameStartMove组件引用,用于重置镜头位置
- private gameStartMoveComponent: GameStartMove = null;
- start () {
- // 保存按钮原始位置
- this.saveButtonOriginalPositions();
- // 默认打开 MainUI
- this.switchTo(0);
- // 初始化按钮状态,设置shop按钮为锁定状态
- this.updateButtonStates();
- // 设置事件监听
- this.setupEventListeners();
- }
-
- onDestroy() {
- // 移除事件监听
- this.removeEventListeners();
- }
-
- /**
- * 设置事件监听器
- */
- private setupEventListeners() {
- const eventBus = EventBus.getInstance();
-
- // 监听返回主菜单事件
- eventBus.on(GameEvents.RETURN_TO_MAIN_MENU, this.onReturnToMainMenu, this);
- }
-
- /**
- * 移除事件监听器
- */
- private removeEventListeners() {
- const eventBus = EventBus.getInstance();
-
- eventBus.off(GameEvents.RETURN_TO_MAIN_MENU, this.onReturnToMainMenu, this);
- }
-
- /**
- * 保存按钮原始位置
- */
- private saveButtonOriginalPositions() {
- this.buttonOriginalPositions = [];
- this.buttons.forEach(btn => {
- this.buttonOriginalPositions.push(btn.position.clone());
- });
- console.log('[NavBarController] 已保存按钮原始位置');
- }
-
- /**
- * 处理返回主菜单事件
- */
- private onReturnToMainMenu() {
- console.log('[NavBarController] 收到返回主菜单事件,切换到主界面');
-
- // 重置镜头位置到原始位置
- this.resetCameraPosition();
-
- // 切换到主界面(Battle按钮对应的Main面板,索引0)
- this.switchTo(0);
- }
-
- /**
- * 重置镜头位置到原始位置
- * 确保从游戏中返回主界面时镜头位置正常
- */
- private resetCameraPosition() {
- // 如果还没有获取GameStartMove组件,尝试获取
- if (!this.gameStartMoveComponent) {
- const cameraNode = find('Canvas/Main Camera');
- if (cameraNode) {
- this.gameStartMoveComponent = cameraNode.getComponent(GameStartMove);
- }
- }
-
- // 如果成功获取到组件,重置镜头位置
- if (this.gameStartMoveComponent) {
- console.log('[NavBarController] 重置镜头位置到原始位置');
- this.gameStartMoveComponent.resetCameraToOriginalPosition(0.3);
- } else {
- console.warn('[NavBarController] 未找到GameStartMove组件,无法重置镜头位置');
- }
- }
- onBattleClick () {
- // 检查Battle按钮是否被锁定(索引0)
- if (this.buttonLockStates[0]) {
- console.log('[NavBarController] Battle按钮被锁定,无法点击');
- // 发送Toast事件显示锁定提示
- EventBus.getInstance().emit(GameEvents.SHOW_TOAST, {
- message: '暂未解锁该功能!',
- duration: 2.0
- });
- return;
- }
- this.switchTo(0);
- }
- onUpgradeClick () {
- // 检查Upgrade按钮是否被锁定(索引2)
- if (this.buttonLockStates[2]) {
- console.log('[NavBarController] Upgrade按钮被锁定,无法点击');
- // 发送Toast事件显示锁定提示
- EventBus.getInstance().emit(GameEvents.SHOW_TOAST, {
- message: '暂未解锁该功能!',
- duration: 2.0
- });
- return;
- }
- this.switchTo(1);
- }
- onShopClick () {
- // 检查shop按钮是否被锁定(索引1)
- if (this.buttonLockStates[1]) {
- console.log('[NavBarController] Shop按钮被锁定,无法点击');
- // 发送Toast事件显示锁定提示
- EventBus.getInstance().emit(GameEvents.SHOW_TOAST, {
- message: '暂未解锁该功能!',
- duration: 2.0
- });
- return;
- }
- this.switchTo(2);
- }
- onSkillClick () {
- // 检查Skill按钮是否被锁定(索引3)
- if (this.buttonLockStates[3]) {
- console.log('[NavBarController] Skill按钮被锁定,无法点击');
- // 发送Toast事件显示锁定提示
- EventBus.getInstance().emit(GameEvents.SHOW_TOAST, {
- message: '暂未解锁该功能!',
- duration: 2.0
- });
- return;
- }
- this.switchTo(3);
- // 注意:滚动功能现在由SkillNodeGenerator组件通过装饰器直接处理
- }
- private switchTo (index: number) {
- // 显示对应面板
- this.panels.forEach((p, i) => p.active = i === index);
- // 按钮索引到面板索引的映射:根据实际的点击事件调用
- // 按钮索引: [0:Battle, 1:Shop, 2:Upgrade, 3:Skill]
- // 面板索引: [0:Main, 1:Shop, 2:Upgrade, 3:Skill]
- // 实际映射: Battle->Main(0), Shop->Upgrade(2), Upgrade->Shop(1), Skill->Skill(3)
- const buttonToPanelMap = [0, 2, 1, 3]; // Battle->Main(0), Shop->Upgrade(2), Upgrade->Shop(1), Skill->Skill(3)
-
- // 触发UI面板切换事件,通知MenuController更新MenueButton显示状态
- EventBus.getInstance().emit(GameEvents.UI_PANEL_SWITCHED, { panelIndex: index });
-
- // 重置所有按钮到原始位置和状态
- this.buttons.forEach((btn, buttonIndex) => {
- const sp = btn.getComponent(Sprite);
- if (sp) {
- // 重置按钮位置到原始位置
- if (this.buttonOriginalPositions[buttonIndex]) {
- btn.position = this.buttonOriginalPositions[buttonIndex].clone();
- }
-
- // 设置按钮颜色(只处理锁定状态)
- if (this.buttonLockStates[buttonIndex]) {
- sp.color = this.lockedColor; // 锁定状态显示灰色
- } else {
- sp.color = this.normalColor; // 正常状态显示白色
- }
- }
- });
-
- // 隐藏所有边框
- this.buttonBorders.forEach(border => {
- if (border) {
- border.active = false;
- }
- });
-
- // 找到当前激活的按钮索引
- let activeButtonIndex = -1;
- for (let buttonIndex = 0; buttonIndex < buttonToPanelMap.length; buttonIndex++) {
- if (buttonToPanelMap[buttonIndex] === index) {
- activeButtonIndex = buttonIndex;
- break;
- }
- }
-
- // 如果找到激活按钮且未被锁定,则应用激活效果
- if (activeButtonIndex !== -1 && !this.buttonLockStates[activeButtonIndex]) {
- const activeBtn = this.buttons[activeButtonIndex];
- if (activeBtn && this.buttonOriginalPositions[activeButtonIndex]) {
- // 向上移动20px
- const newPos = this.buttonOriginalPositions[activeButtonIndex].clone();
- newPos.y += 20;
- activeBtn.position = newPos;
-
- // 显示对应的边框
- if (this.buttonBorders[activeButtonIndex]) {
- this.buttonBorders[activeButtonIndex].active = true;
- }
-
- this.currentActiveButtonIndex = activeButtonIndex;
- }
- } else {
- this.currentActiveButtonIndex = -1;
- }
- }
-
- /**
- * 更新按钮状态,主要用于设置锁定按钮的视觉效果
- */
- private updateButtonStates() {
- this.buttons.forEach((btn, buttonIndex) => {
- const sp = btn.getComponent(Sprite);
- if (sp) {
- if (this.buttonLockStates[buttonIndex]) {
- sp.color = this.lockedColor; // 设置锁定按钮为锁定颜色
- // 确保锁定的按钮在原始位置
- if (this.buttonOriginalPositions[buttonIndex]) {
- btn.position = this.buttonOriginalPositions[buttonIndex].clone();
- }
- // 隐藏锁定按钮的边框
- if (this.buttonBorders[buttonIndex]) {
- this.buttonBorders[buttonIndex].active = false;
- }
- } else {
- sp.color = this.normalColor; // 设置未锁定按钮为正常颜色
- }
- }
- });
- }
-
- /**
- * 解锁指定按钮
- * @param buttonIndex 按钮索引 (0:Battle, 1:Shop, 2:Upgrade, 3:Skill)
- */
- public unlockButton(buttonIndex: number) {
- if (buttonIndex >= 0 && buttonIndex < this.buttonLockStates.length) {
- this.buttonLockStates[buttonIndex] = false;
- this.updateButtonStates();
- const buttonNames = ['Battle', 'Shop', 'Upgrade', 'Skill'];
- console.log(`[NavBarController] ${buttonNames[buttonIndex]}按钮已解锁`);
- }
- }
-
- /**
- * 锁定指定按钮
- * @param buttonIndex 按钮索引 (0:Battle, 1:Shop, 2:Upgrade, 3:Skill)
- */
- public lockButton(buttonIndex: number) {
- if (buttonIndex >= 0 && buttonIndex < this.buttonLockStates.length) {
- this.buttonLockStates[buttonIndex] = true;
- this.updateButtonStates();
- const buttonNames = ['Battle', 'Shop', 'Upgrade', 'Skill'];
- console.log(`[NavBarController] ${buttonNames[buttonIndex]}按钮已锁定`);
- }
- }
-
- /**
- * 检查指定按钮是否被锁定
- * @param buttonIndex 按钮索引 (0:Battle, 1:Shop, 2:Upgrade, 3:Skill)
- */
- public isButtonLocked(buttonIndex: number): boolean {
- if (buttonIndex >= 0 && buttonIndex < this.buttonLockStates.length) {
- return this.buttonLockStates[buttonIndex];
- }
- return false;
- }
-
- /**
- * 批量设置按钮锁定状态
- * @param lockStates 锁定状态数组 [Battle, Shop, Upgrade, Skill]
- */
- public setButtonLockStates(lockStates: boolean[]) {
- if (lockStates.length === 4) {
- this.buttonLockStates = [...lockStates];
- this.updateButtonStates();
- console.log('[NavBarController] 按钮锁定状态已更新:', lockStates);
- }
- }
-
- /**
- * 获取当前所有按钮的锁定状态
- */
- public getButtonLockStates(): boolean[] {
- return [...this.buttonLockStates];
- }
-
- /**
- * 获取当前激活的按钮索引
- */
- public getCurrentActiveButtonIndex(): number {
- return this.currentActiveButtonIndex;
- }
- /**
- * 公开方法:直接打开商店面板(忽略按钮锁定)
- */
- public openShopPanel(): void {
- this.switchTo(2);
- }
- /**
- * 公开方法:当前是否处于商店面板
- */
- public isShopPanelActive(): boolean {
- const shopPanel = this.panels && this.panels.length > 2 ? this.panels[2] : null;
- return !!(shopPanel && shopPanel.active);
- }
- }
|