UpgradeAni.ts 12 KB

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