UpgradeAni.ts 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. import { _decorator, Component, Node, Tween, tween, Vec3, Material, Sprite, resources, Label, JsonAsset } from 'cc';
  2. import { JsonConfigLoader } from '../../Core/JsonConfigLoader';
  3. import { Audio } from '../../AudioManager/AudioManager';
  4. import BlinkScaleAnimator from '../../Animations/BlinkScaleAnimator';
  5. const { ccclass, property } = _decorator;
  6. /**
  7. * 植物升级动画控制器
  8. * 负责管理植物升级相关的动画效果
  9. */
  10. @ccclass('UpgradeAni')
  11. export class UpgradeAni extends Component {
  12. @property(Node)
  13. weaponSpriteNode: Node = null;
  14. @property(Node)
  15. upgradeBtnNode: Node = null; // Canvas/UpgradePanel/UpgradeBtn/UP节点
  16. @property(Label)
  17. currentDamageLabel: Label = null; // Canvas/UpgradePanel/NumberBack/CurrentDamage节点
  18. private blinkComp: BlinkScaleAnimator | null = null; // 闪烁动画组件引用
  19. private weaponsConfig: any = null; // 武器配置数据
  20. /**
  21. * 植物升级成功动画
  22. * 播放植物图标的放大缩小动画,并在升级期间应用扫描效果材质,同时播放升级音效
  23. * @param weaponIconNode 植物图标节点
  24. */
  25. public playWeaponUpgradeAnimation(weaponIconNode: Node): Promise<void> {
  26. return new Promise((resolve) => {
  27. if (!weaponIconNode) {
  28. resolve();
  29. return;
  30. }
  31. // 停止之前的动画,防止快速点击时动画冲突
  32. Tween.stopAllByTarget(weaponIconNode);
  33. // 重置到原始缩放状态,确保动画从正确的状态开始
  34. weaponIconNode.setScale(Vec3.ONE);
  35. // 保存原始缩放值
  36. const originalScale = Vec3.ONE.clone();
  37. let weaponSprite: Sprite = null;
  38. let originalMaterial: Material = null;
  39. console.log('[UpgradeAni] 开始武器升级动画,weaponSpriteNode:', this.weaponSpriteNode);
  40. // 播放植物升级音效
  41. Audio.playUISound('data/弹球音效/equipment level up finish');
  42. // 使用装饰器引用的WeaponSprite节点,获取其Sprite组件并保存原始材质
  43. if (this.weaponSpriteNode) {
  44. console.log('[UpgradeAni] 找到weaponSpriteNode,开始获取Sprite组件');
  45. weaponSprite = this.weaponSpriteNode.getComponent(Sprite);
  46. if (weaponSprite) {
  47. console.log('[UpgradeAni] 找到Sprite组件,开始加载材质');
  48. // 保存原始材质,优先保存customMaterial,如果没有则保存当前material
  49. originalMaterial = weaponSprite.customMaterial || weaponSprite.material;
  50. console.log('[UpgradeAni] 原始材质:', originalMaterial);
  51. // 加载并应用扫描效果材质
  52. resources.load('shaders/scan-effect', Material, (err, scanMaterial) => {
  53. console.log('[UpgradeAni] 材质加载回调,err:', err, 'scanMaterial:', scanMaterial);
  54. if (!err && scanMaterial && weaponSprite) {
  55. weaponSprite.material = scanMaterial;
  56. console.log('[UpgradeAni] 应用扫描效果材质成功');
  57. } else {
  58. console.warn('[UpgradeAni] 加载扫描效果材质失败:', err);
  59. }
  60. });
  61. } else {
  62. console.warn('[UpgradeAni] weaponSpriteNode上没有找到Sprite组件');
  63. }
  64. } else {
  65. console.warn('[UpgradeAni] weaponSpriteNode为null,请在编辑器中设置');
  66. }
  67. // 创建缩放动画:放大到1.5倍再立即缩小回原始大小
  68. const comp = BlinkScaleAnimator.ensure(weaponIconNode, {
  69. scaleFactor: 1.5,
  70. upDuration: 0.25,
  71. downDuration: 0.25,
  72. easingUp: 'sineOut',
  73. easingDown: 'sineIn',
  74. playOnEnable: false,
  75. });
  76. comp.play();
  77. this.scheduleOnce(() => {
  78. comp.stop();
  79. // 动画结束后恢复原始材质
  80. if (weaponSprite && originalMaterial) {
  81. if (weaponSprite.customMaterial === originalMaterial) {
  82. weaponSprite.material = weaponSprite.customMaterial;
  83. } else {
  84. weaponSprite.material = originalMaterial;
  85. }
  86. console.log('[UpgradeAni] 恢复原始材质成功');
  87. } else if (weaponSprite) {
  88. if (weaponSprite.customMaterial) {
  89. weaponSprite.material = weaponSprite.customMaterial;
  90. console.log('[UpgradeAni] 恢复到customMaterial');
  91. } else {
  92. weaponSprite.material = null;
  93. console.log('[UpgradeAni] 恢复到默认材质');
  94. }
  95. }
  96. resolve();
  97. }, 0.25 + 0.25);
  98. // 播放缩放动画
  99. // 使用 BlinkScaleAnimator 播放动画,无需调用 scaleAnimation.start();
  100. });
  101. }
  102. /**
  103. * 显示升级面板动画
  104. */
  105. public showPanel(): Promise<void> {
  106. return new Promise((resolve) => {
  107. const panel = this.node;
  108. if (!panel) {
  109. resolve();
  110. return;
  111. }
  112. // 确保面板激活
  113. panel.active = true;
  114. // 停止之前的动画
  115. Tween.stopAllByTarget(panel);
  116. // 设置初始状态:缩放为0
  117. panel.setScale(Vec3.ZERO);
  118. // 播放弹出动画
  119. tween(panel)
  120. .to(0.3, { scale: Vec3.ONE }, {
  121. easing: 'backOut'
  122. })
  123. .call(() => {
  124. resolve();
  125. })
  126. .start();
  127. });
  128. }
  129. /**
  130. * 隐藏升级面板动画
  131. */
  132. public hidePanel(): Promise<void> {
  133. return new Promise((resolve) => {
  134. const panel = this.node;
  135. if (!panel) {
  136. resolve();
  137. return;
  138. }
  139. // 停止之前的动画
  140. Tween.stopAllByTarget(panel);
  141. // 播放缩小动画
  142. tween(panel)
  143. .to(0.2, { scale: Vec3.ZERO }, {
  144. easing: 'backIn'
  145. })
  146. .call(() => {
  147. // 动画结束后隐藏面板
  148. panel.active = false;
  149. resolve();
  150. })
  151. .start();
  152. });
  153. }
  154. /**
  155. * 立即隐藏面板(无动画)
  156. */
  157. public hidePanelImmediate(): void {
  158. const panel = this.node;
  159. if (panel) {
  160. // 停止所有动画
  161. Tween.stopAllByTarget(panel);
  162. // 立即隐藏
  163. panel.active = false;
  164. panel.setScale(Vec3.ONE);
  165. }
  166. }
  167. /**
  168. * 开始升级按钮闪烁动画
  169. * 当钞票足够升级时调用此方法
  170. */
  171. public startUpgradeBtnBlink(): void {
  172. if (!this.upgradeBtnNode) {
  173. console.warn('[UpgradeAni] upgradeBtnNode为null,请在编辑器中设置Canvas/UpgradePanel/UpgradeBtn/UP节点');
  174. return;
  175. }
  176. // 停止之前的闪烁动画
  177. this.stopUpgradeBtnBlink();
  178. // 重置到原始缩放状态
  179. this.upgradeBtnNode.setScale(Vec3.ONE);
  180. // 使用通用组件进行循环闪烁(放大缩小)
  181. this.blinkComp = BlinkScaleAnimator.ensure(this.upgradeBtnNode, {
  182. scaleFactor: 1.5,
  183. upDuration: 0.5,
  184. downDuration: 0.5,
  185. easingUp: 'sineInOut',
  186. easingDown: 'sineInOut',
  187. });
  188. this.blinkComp.play();
  189. console.log('[UpgradeAni] 开始升级按钮闪烁动画');
  190. }
  191. /**
  192. * 停止升级按钮闪烁动画
  193. * 当钞票不够升级时调用此方法
  194. */
  195. public stopUpgradeBtnBlink(): void {
  196. if (this.blinkComp) {
  197. this.blinkComp.stop();
  198. this.blinkComp = null;
  199. }
  200. if (this.upgradeBtnNode) {
  201. Tween.stopAllByTarget(this.upgradeBtnNode);
  202. this.upgradeBtnNode.setScale(Vec3.ONE);
  203. }
  204. console.log('[UpgradeAni] 停止升级按钮闪烁动画');
  205. }
  206. /**
  207. * 检查升级按钮是否正在闪烁
  208. * @returns 是否正在闪烁
  209. */
  210. public isUpgradeBtnBlinking(): boolean {
  211. return this.blinkComp !== null;
  212. }
  213. /**
  214. * 加载武器配置数据
  215. */
  216. public async loadWeaponsConfig(): Promise<void> {
  217. try {
  218. this.weaponsConfig = await JsonConfigLoader.getInstance().loadConfig('weapons');
  219. console.log('[UpgradeAni] 武器配置加载成功:', this.weaponsConfig);
  220. } catch (error) {
  221. console.error('[UpgradeAni] 加载武器配置失败:', error);
  222. throw error;
  223. }
  224. }
  225. /**
  226. * 从JSON配置中获取武器在指定等级的伤害值
  227. * @param weaponId 武器ID
  228. * @param level 武器等级
  229. * @returns 伤害值
  230. */
  231. public getWeaponDamageFromConfig(weaponId: string, level: number): number {
  232. if (!this.weaponsConfig || !this.weaponsConfig.weapons) {
  233. console.warn('[UpgradeAni] 武器配置未加载');
  234. return 0;
  235. }
  236. const weaponConfig = this.weaponsConfig.weapons.find((w: any) => w.id === weaponId);
  237. if (!weaponConfig) {
  238. console.warn(`[UpgradeAni] 未找到武器配置: ${weaponId}`);
  239. return 0;
  240. }
  241. if (level === 0) {
  242. return 0; // 未解锁武器伤害为0
  243. }
  244. // 优先从upgradeConfig中获取伤害值
  245. if (weaponConfig.upgradeConfig && weaponConfig.upgradeConfig.levels) {
  246. const levelConfig = weaponConfig.upgradeConfig.levels[level.toString()];
  247. if (levelConfig && typeof levelConfig.damage === 'number') {
  248. return levelConfig.damage;
  249. }
  250. }
  251. // 如果upgradeConfig中没有伤害值,使用基础伤害 + 等级加成
  252. const baseDamage = weaponConfig.stats?.damage || 0;
  253. return baseDamage + (level - 1);
  254. }
  255. /**
  256. * 更新当前伤害显示
  257. * @param weaponId 武器ID
  258. * @param level 武器等级
  259. */
  260. public updateCurrentDamageDisplay(weaponId: string, level: number): void {
  261. if (!this.currentDamageLabel) {
  262. console.warn('[UpgradeAni] currentDamageLabel未设置,请在编辑器中设置Canvas/UpgradePanel/NumberBack/CurrentDamage节点');
  263. return;
  264. }
  265. const damage = this.getWeaponDamageFromConfig(weaponId, level);
  266. this.currentDamageLabel.string = damage.toString();
  267. console.log(`[UpgradeAni] 更新伤害显示: ${weaponId} 等级${level} 伤害${damage}`);
  268. }
  269. }