GainUI.ts 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. import { _decorator, Component, Node, Label, Sprite, Button } from 'cc';
  2. import EventBus, { GameEvents } from '../Core/EventBus';
  3. import { JsonConfigLoader } from '../Core/JsonConfigLoader';
  4. import { BundleLoader } from '../Core/BundleLoader';
  5. const { ccclass, property } = _decorator;
  6. @ccclass('GainUI')
  7. export class GainUI extends Component {
  8. @property(Node) panel: Node = null; // 弹窗根节点
  9. @property(Label) titleLabel: Label = null; // 标题,如“新武器”
  10. @property(Sprite) iconSprite: Sprite = null; // 武器图标
  11. @property(Label) nameLabel: Label = null; // 武器名称
  12. @property(Button) confirmBtn: Button = null; // 确认按钮
  13. private weaponsConfig: any = null;
  14. private bundleLoader: BundleLoader = null;
  15. // 等待展示的武器名称队列(来自 UpgradeController 派发的 NEW_WEAPONS_UNLOCKED)
  16. private pendingWeapons: string[] = [];
  17. // 是否已经完成奖励动画(由 MoneyAni 派发 REWARD_ANIMATION_COMPLETED)
  18. private rewardAnimationCompleted = false;
  19. // 当前是否正在显示弹窗
  20. private showing = false;
  21. async onLoad() {
  22. this.bundleLoader = BundleLoader.getInstance();
  23. // 默认隐藏弹窗
  24. if (this.panel) this.panel.active = false;
  25. // 绑定事件
  26. EventBus.getInstance().on(GameEvents.NEW_WEAPONS_UNLOCKED, this.onNewWeaponsUnlocked, this);
  27. EventBus.getInstance().on(GameEvents.REWARD_ANIMATION_COMPLETED, this.onRewardAnimationCompleted, this);
  28. // 绑定确认按钮
  29. this.confirmBtn?.node.on(Button.EventType.CLICK, this.onConfirm, this);
  30. }
  31. async start() {
  32. // 预加载武器配置,便于查找图标与名称
  33. try {
  34. this.weaponsConfig = await JsonConfigLoader.getInstance().loadConfig('weapons');
  35. } catch (err) {
  36. console.warn('[GainUI] 加载武器配置失败:', err);
  37. }
  38. }
  39. onDestroy() {
  40. EventBus.getInstance().off(GameEvents.NEW_WEAPONS_UNLOCKED, this.onNewWeaponsUnlocked, this);
  41. EventBus.getInstance().off(GameEvents.REWARD_ANIMATION_COMPLETED, this.onRewardAnimationCompleted, this);
  42. this.confirmBtn?.node.off(Button.EventType.CLICK, this.onConfirm, this);
  43. }
  44. // 处理新武器解锁事件
  45. private onNewWeaponsUnlocked = (weaponNames: string[]) => {
  46. if (!weaponNames || weaponNames.length === 0) return;
  47. this.pendingWeapons.push(...weaponNames);
  48. this.tryShowPopup();
  49. }
  50. // 处理奖励动画完成事件
  51. private onRewardAnimationCompleted = () => {
  52. this.rewardAnimationCompleted = true;
  53. this.tryShowPopup();
  54. }
  55. // 在满足条件时显示弹窗:需要奖励动画已完成且队列非空
  56. private async tryShowPopup() {
  57. if (this.showing) return;
  58. if (!this.rewardAnimationCompleted) return;
  59. if (this.pendingWeapons.length === 0) return;
  60. const nextName = this.pendingWeapons.shift();
  61. await this.renderWeapon(nextName);
  62. this.panel.active = true;
  63. this.showing = true;
  64. }
  65. // 确认按钮逻辑:继续展示队列中的下一个或关闭弹窗
  66. private async onConfirm() {
  67. if (this.pendingWeapons.length > 0) {
  68. const nextName = this.pendingWeapons.shift();
  69. await this.renderWeapon(nextName);
  70. } else {
  71. this.panel.active = false;
  72. this.showing = false;
  73. }
  74. }
  75. // 根据武器名称渲染弹窗内容(标题、图标、名称)
  76. private async renderWeapon(weaponName: string) {
  77. if (this.titleLabel) this.titleLabel.string = '新武器';
  78. if (this.nameLabel) this.nameLabel.string = weaponName || '';
  79. // 查找武器配置,加载图标
  80. const config = this.findWeaponConfigByName(weaponName);
  81. if (config && config.visualConfig && config.visualConfig.weaponSprites) {
  82. let spritePath: string;
  83. const sprites = config.visualConfig.weaponSprites;
  84. if (typeof sprites === 'string') {
  85. spritePath = sprites;
  86. } else {
  87. spritePath = sprites['I'] || sprites['H-I'] || sprites['L'] || sprites['S'] || sprites['D-T'];
  88. }
  89. if (spritePath && this.iconSprite) {
  90. await this.loadWeaponSprite(this.iconSprite, spritePath);
  91. }
  92. }
  93. }
  94. private findWeaponConfigByName(name: string) {
  95. if (!this.weaponsConfig || !this.weaponsConfig.weapons) return null;
  96. return this.weaponsConfig.weapons.find((w: any) => w.name === name);
  97. }
  98. private async loadWeaponSprite(sprite: Sprite, spritePath: string) {
  99. const bundlePath = spritePath.replace(/^images\//, '');
  100. try {
  101. const spriteFrame = await this.bundleLoader.loadSpriteFrame(bundlePath + '/spriteFrame');
  102. if (spriteFrame && sprite && sprite.isValid) {
  103. sprite.spriteFrame = spriteFrame;
  104. }
  105. } catch (err) {
  106. console.warn(`[GainUI] 加载武器图标失败: ${spritePath}`, err);
  107. }
  108. }
  109. }