UpgradeAni.ts 11 KB

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