NavBarController.ts 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. import { _decorator, Color, Component, Node, Sprite } from 'cc';
  2. const { ccclass, property } = _decorator;
  3. @ccclass('NavBarController')
  4. export class NavBarController extends Component {
  5. @property({ type: [Node] }) panels: Node[] = []; // 依次给 Main、Shop、Upgrade、Skill
  6. @property({ type: [Node] }) buttons: Node[] = []; // 依次给 Battle、Shop、Upgrade、Skill
  7. private normalColor = new Color(255, 255, 255, 255);
  8. private activeColor = new Color(255, 0, 0, 255); // 红色
  9. private lockedColor = new Color(128, 128, 128, 255); // 灰色(锁定状态)
  10. // 按钮锁定状态数组 - 对应 Battle、Shop、Upgrade、Skill 四个按钮
  11. private buttonLockStates: boolean[] = [false, true, false, false]; // 默认只锁定Shop按钮
  12. start () {
  13. // 默认打开 MainUI
  14. this.switchTo(0);
  15. // 初始化按钮状态,设置shop按钮为锁定状态
  16. this.updateButtonStates();
  17. }
  18. onBattleClick () {
  19. // 检查Battle按钮是否被锁定(索引0)
  20. if (this.buttonLockStates[0]) {
  21. console.log('[NavBarController] Battle按钮被锁定,无法点击');
  22. return;
  23. }
  24. this.switchTo(0);
  25. }
  26. onUpgradeClick () {
  27. // 检查Upgrade按钮是否被锁定(索引2)
  28. if (this.buttonLockStates[2]) {
  29. console.log('[NavBarController] Upgrade按钮被锁定,无法点击');
  30. return;
  31. }
  32. this.switchTo(1);
  33. }
  34. onShopClick () {
  35. // 检查shop按钮是否被锁定(索引1)
  36. if (this.buttonLockStates[1]) {
  37. console.log('[NavBarController] Shop按钮被锁定,无法点击');
  38. // 可以在这里添加提示音效或震动效果
  39. return;
  40. }
  41. this.switchTo(2);
  42. }
  43. onSkillClick () {
  44. // 检查Skill按钮是否被锁定(索引3)
  45. if (this.buttonLockStates[3]) {
  46. console.log('[NavBarController] Skill按钮被锁定,无法点击');
  47. return;
  48. }
  49. this.switchTo(3);
  50. // 注意:滚动功能现在由SkillNodeGenerator组件通过装饰器直接处理
  51. }
  52. private switchTo (index: number) {
  53. // 显示对应面板
  54. this.panels.forEach((p, i) => p.active = i === index);
  55. // 按钮索引到面板索引的映射:根据实际的点击事件调用
  56. // 按钮索引: [0:Battle, 1:Shop, 2:Upgrade, 3:Skill]
  57. // 面板索引: [0:Main, 1:Shop, 2:Upgrade, 3:Skill]
  58. // 实际映射: Battle->Main(0), Shop->Upgrade(2), Upgrade->Shop(1), Skill->Skill(3)
  59. const buttonToPanelMap = [0, 2, 1, 3]; // Battle->Main(0), Shop->Upgrade(2), Upgrade->Shop(1), Skill->Skill(3)
  60. // 设置按钮颜色,考虑锁定状态
  61. this.buttons.forEach((btn, buttonIndex) => {
  62. const sp = btn.getComponent(Sprite);
  63. if (sp) {
  64. // 检查按钮是否被锁定
  65. if (this.buttonLockStates[buttonIndex]) {
  66. sp.color = this.lockedColor; // 锁定状态显示灰色
  67. } else {
  68. // 检查当前按钮对应的面板是否是激活的面板
  69. const panelIndex = buttonToPanelMap[buttonIndex];
  70. sp.color = (panelIndex === index) ? this.activeColor : this.normalColor;
  71. }
  72. }
  73. });
  74. }
  75. /**
  76. * 更新按钮状态,主要用于设置锁定按钮的视觉效果
  77. */
  78. private updateButtonStates() {
  79. // 按钮索引到面板索引的映射:根据实际的点击事件调用
  80. const buttonToPanelMap = [0, 2, 1, 3]; // Battle->Main(0), Shop->Upgrade(2), Upgrade->Shop(1), Skill->Skill(3)
  81. this.buttons.forEach((btn, buttonIndex) => {
  82. const sp = btn.getComponent(Sprite);
  83. if (sp) {
  84. if (this.buttonLockStates[buttonIndex]) {
  85. sp.color = this.lockedColor; // 设置锁定按钮为锁定颜色
  86. } else {
  87. // 对于未锁定的按钮,设置为正常颜色(除非它是当前激活的)
  88. // 这里我们需要知道当前激活的面板,但为了简化,先设置为正常颜色
  89. // 实际的激活状态会在switchTo中正确设置
  90. sp.color = this.normalColor;
  91. }
  92. }
  93. });
  94. }
  95. /**
  96. * 解锁指定按钮
  97. * @param buttonIndex 按钮索引 (0:Battle, 1:Shop, 2:Upgrade, 3:Skill)
  98. */
  99. public unlockButton(buttonIndex: number) {
  100. if (buttonIndex >= 0 && buttonIndex < this.buttonLockStates.length) {
  101. this.buttonLockStates[buttonIndex] = false;
  102. this.updateButtonStates();
  103. const buttonNames = ['Battle', 'Shop', 'Upgrade', 'Skill'];
  104. console.log(`[NavBarController] ${buttonNames[buttonIndex]}按钮已解锁`);
  105. }
  106. }
  107. /**
  108. * 锁定指定按钮
  109. * @param buttonIndex 按钮索引 (0:Battle, 1:Shop, 2:Upgrade, 3:Skill)
  110. */
  111. public lockButton(buttonIndex: number) {
  112. if (buttonIndex >= 0 && buttonIndex < this.buttonLockStates.length) {
  113. this.buttonLockStates[buttonIndex] = true;
  114. this.updateButtonStates();
  115. const buttonNames = ['Battle', 'Shop', 'Upgrade', 'Skill'];
  116. console.log(`[NavBarController] ${buttonNames[buttonIndex]}按钮已锁定`);
  117. }
  118. }
  119. /**
  120. * 检查指定按钮是否被锁定
  121. * @param buttonIndex 按钮索引 (0:Battle, 1:Shop, 2:Upgrade, 3:Skill)
  122. */
  123. public isButtonLocked(buttonIndex: number): boolean {
  124. if (buttonIndex >= 0 && buttonIndex < this.buttonLockStates.length) {
  125. return this.buttonLockStates[buttonIndex];
  126. }
  127. return false;
  128. }
  129. /**
  130. * 批量设置按钮锁定状态
  131. * @param lockStates 锁定状态数组 [Battle, Shop, Upgrade, Skill]
  132. */
  133. public setButtonLockStates(lockStates: boolean[]) {
  134. if (lockStates.length === 4) {
  135. this.buttonLockStates = [...lockStates];
  136. this.updateButtonStates();
  137. console.log('[NavBarController] 按钮锁定状态已更新:', lockStates);
  138. }
  139. }
  140. /**
  141. * 获取当前所有按钮的锁定状态
  142. */
  143. public getButtonLockStates(): boolean[] {
  144. return [...this.buttonLockStates];
  145. }
  146. }