BallAni.ts 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. import { _decorator, Component, Node, Vec3, tween, Color,find, Sprite, resources, sp, Material } from 'cc';
  2. import EventBus, { GameEvents } from '../Core/EventBus';
  3. const { ccclass, property } = _decorator;
  4. /**
  5. * 小球撞击动画管理器
  6. * 负责在小球撞击时播放特效动画
  7. */
  8. @ccclass('BallAni')
  9. export class BallAni extends Component {
  10. // 撞击特效预制体缓存
  11. private static impactEffectSkeleton: sp.SkeletonData = null;
  12. private static isLoading: boolean = false;
  13. // 当前播放中的特效节点列表
  14. private activeEffects: Node[] = [];
  15. // 当前播放中的方块动画列表
  16. private activeBlockAnimations: Map<Node, any> = new Map();
  17. start() {
  18. // 预加载撞击特效资源
  19. this.preloadImpactEffect();
  20. }
  21. /**
  22. * 播放方块被撞击动画
  23. * @param blockNode 方块节点
  24. */
  25. public playBlockHitAnimation(blockNode: Node) {
  26. if (!blockNode || !blockNode.isValid) {
  27. console.warn('[BallAni] 方块节点无效');
  28. return;
  29. }
  30. // 如果该方块已经在播放动画,跳过
  31. if (this.activeBlockAnimations.has(blockNode)) {
  32. console.log('[BallAni] 方块动画已在播放中,跳过');
  33. return;
  34. }
  35. // 检查是否有活跃敌人,只有有敌人时才播放缩放动画
  36. const eventBus = EventBus.getInstance();
  37. let hasActiveEnemies = false;
  38. // 通过事件系统检查是否有活跃敌人
  39. eventBus.emit(GameEvents.BALL_FIRE_BULLET, {
  40. canFire: (canFire: boolean) => {
  41. hasActiveEnemies = canFire;
  42. }
  43. });
  44. console.log('[BallAni] 开始播放方块撞击动画', blockNode.name, '有敌人:', hasActiveEnemies);
  45. console.log('[BallAni] hasActiveEnemies 检查结果:', hasActiveEnemies);
  46. const sprite = blockNode.getComponent(Sprite);
  47. // 保存原始材质和Sprite缩放
  48. const originalMaterial = sprite ? sprite.material : null;
  49. const originalSpriteScale = sprite ? new Vec3(1, 1, 1) : new Vec3(1, 1, 1);
  50. console.log('[BallAni] 开始方块视觉缩放动画');
  51. if (hasActiveEnemies) {
  52. // 有敌人时播放Sprite缩放动画(不影响碰撞体积)
  53. const shrinkSpriteScale = new Vec3(0.5, 0.5, 1);
  54. // 应用材质
  55. console.log('[BallAni] 检查材质状态:', {
  56. hasSprite: !!sprite,
  57. hasCustomMaterial: sprite ? !!sprite.customMaterial : false,
  58. currentMaterial: sprite ? sprite.material : null
  59. });
  60. if (sprite && sprite.customMaterial) {
  61. sprite.material = sprite.customMaterial;
  62. console.log('[BallAni] 应用自定义材质成功');
  63. } else if (sprite) {
  64. // 动态加载灰色材质
  65. const grayMaterialPath = 'shaders/ui-sprite-white-material.mtl';
  66. resources.load(grayMaterialPath, Material, (err, material) => {
  67. if (!err && material && sprite && sprite.isValid) {
  68. sprite.material = material;
  69. console.log('[BallAni] 动态加载并应用灰色材质成功');
  70. } else {
  71. console.log('[BallAni] 无法加载灰色材质,不使用任何材质');
  72. }
  73. });
  74. }
  75. // 对Sprite节点进行缩放动画(不影响方块碰撞体积)
  76. const animationTween = tween(sprite.node)
  77. .to(0.2, { scale: shrinkSpriteScale })
  78. .to(0.2, { scale: originalSpriteScale })
  79. .call(() => {
  80. // 动画完成时恢复原始材质
  81. console.log('[BallAni] 恢复材质状态:', {
  82. hasSprite: !!sprite,
  83. hasOriginalMaterial: !!originalMaterial,
  84. currentMaterial: sprite ? sprite.material : null
  85. });
  86. if (sprite && originalMaterial) {
  87. sprite.material = originalMaterial;
  88. console.log('[BallAni] 恢复原始材质成功');
  89. } else {
  90. console.log('[BallAni] 无法恢复原始材质 - 原始材质丢失');
  91. }
  92. console.log('[BallAni] 方块动画完成,恢复原状');
  93. // 动画完成,从活动列表中移除
  94. this.activeBlockAnimations.delete(blockNode);
  95. })
  96. .start();
  97. // 添加到活动动画列表
  98. this.activeBlockAnimations.set(blockNode, animationTween);
  99. } else {
  100. // 没有敌人时直接结束动画,不应用材质
  101. const animationTween = tween({})
  102. .delay(0.2)
  103. .call(() => {
  104. console.log('[BallAni] 无敌人,直接恢复原状');
  105. // 动画完成,从活动列表中移除
  106. this.activeBlockAnimations.delete(blockNode);
  107. })
  108. .start();
  109. // 添加到活动动画列表
  110. this.activeBlockAnimations.set(blockNode, animationTween);
  111. }
  112. }
  113. /**
  114. * 预加载撞击特效资源
  115. */
  116. private preloadImpactEffect() {
  117. if (BallAni.impactEffectSkeleton || BallAni.isLoading) {
  118. return;
  119. }
  120. BallAni.isLoading = true;
  121. const path = 'Animation/WeaponTx/tx0005/tx0005';
  122. resources.load(path, sp.SkeletonData, (err, sData: sp.SkeletonData) => {
  123. BallAni.isLoading = false;
  124. if (err || !sData) {
  125. console.warn('[BallAni] 加载撞击特效失败:', err);
  126. return;
  127. }
  128. BallAni.impactEffectSkeleton = sData;
  129. console.log('[BallAni] 撞击特效资源加载成功');
  130. });
  131. }
  132. /**
  133. * 在指定位置播放撞击特效
  134. * @param worldPosition 世界坐标位置
  135. */
  136. public playImpactEffect(worldPosition: Vec3) {
  137. // 如果资源未加载,直接加载并播放
  138. if (!BallAni.impactEffectSkeleton) {
  139. const path = 'Animation/WeaponTx/tx0005/tx0005';
  140. resources.load(path, sp.SkeletonData, (err, sData: sp.SkeletonData) => {
  141. if (err || !sData) {
  142. console.warn('[BallAni] 加载撞击特效失败:', err);
  143. return;
  144. }
  145. BallAni.impactEffectSkeleton = sData;
  146. this.createAndPlayEffect(worldPosition, sData);
  147. });
  148. return;
  149. }
  150. this.createAndPlayEffect(worldPosition, BallAni.impactEffectSkeleton);
  151. }
  152. /**
  153. * 创建并播放特效
  154. */
  155. private createAndPlayEffect(worldPosition: Vec3, skeletonData: sp.SkeletonData) {
  156. const effectNode = new Node('ImpactEffect');
  157. const skeleton = effectNode.addComponent(sp.Skeleton);
  158. skeleton.skeletonData = skeletonData;
  159. skeleton.premultipliedAlpha = false;
  160. skeleton.setAnimation(0, 'animation', false);
  161. skeleton.setCompleteListener(() => {
  162. this.removeEffect(effectNode);
  163. });
  164. const canvas = find('Canvas');
  165. if (canvas) {
  166. canvas.addChild(effectNode);
  167. effectNode.setWorldPosition(worldPosition);
  168. // 设置特效缩放
  169. effectNode.setScale(0.8, 0.8, 1);
  170. // 添加到活动特效列表
  171. this.activeEffects.push(effectNode);
  172. } else {
  173. effectNode.destroy();
  174. }
  175. }
  176. /**
  177. * 移除特效节点
  178. * @param effectNode 要移除的特效节点
  179. */
  180. private removeEffect(effectNode: Node) {
  181. // 从活动特效列表中移除
  182. const index = this.activeEffects.indexOf(effectNode);
  183. if (index !== -1) {
  184. this.activeEffects.splice(index, 1);
  185. }
  186. // 销毁节点
  187. if (effectNode && effectNode.isValid) {
  188. effectNode.destroy();
  189. }
  190. }
  191. /**
  192. * 停止指定方块的动画
  193. * @param blockNode 方块节点
  194. */
  195. public stopBlockAnimation(blockNode: Node) {
  196. const animation = this.activeBlockAnimations.get(blockNode);
  197. if (animation) {
  198. animation.stop();
  199. this.activeBlockAnimations.delete(blockNode);
  200. }
  201. }
  202. /**
  203. * 清理所有活动的特效
  204. */
  205. public clearAllEffects() {
  206. for (const effect of this.activeEffects) {
  207. if (effect && effect.isValid) {
  208. effect.destroy();
  209. }
  210. }
  211. this.activeEffects = [];
  212. }
  213. /**
  214. * 清除所有方块动画
  215. */
  216. public clearAllBlockAnimations() {
  217. // 停止所有方块动画
  218. for (const [blockNode, animation] of this.activeBlockAnimations) {
  219. if (animation) {
  220. animation.stop();
  221. }
  222. }
  223. this.activeBlockAnimations.clear();
  224. }
  225. onDestroy() {
  226. // 组件销毁时清理所有特效和动画
  227. this.clearAllEffects();
  228. this.clearAllBlockAnimations();
  229. }
  230. /**
  231. * 获取BallAni实例
  232. * @returns BallAni实例
  233. */
  234. public static getInstance(): BallAni | null {
  235. const gameArea = find('Canvas/GameLevelUI/GameArea');
  236. if (!gameArea) {
  237. console.warn('[BallAni] 未找到GameArea节点');
  238. return null;
  239. }
  240. const ballAni = gameArea.getComponent(BallAni);
  241. if (!ballAni) {
  242. console.warn('[BallAni] GameArea节点上未找到BallAni组件');
  243. return null;
  244. }
  245. return ballAni;
  246. }
  247. }