BallAni.ts 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  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 static whiteMaterial: Material = null;
  15. private static isMaterialLoading: boolean = false;
  16. // 当前播放中的特效节点列表
  17. private activeEffects: Node[] = [];
  18. // 当前播放中的方块动画列表
  19. private activeBlockAnimations: Map<Node, any> = new Map();
  20. // 敌人生成状态
  21. private enemySpawningActive: boolean = false;
  22. start() {
  23. // 预加载撞击特效资源
  24. this.preloadImpactEffect();
  25. // 预加载白色描边材质
  26. this.preloadWhiteMaterial();
  27. // 监听敌人生成状态事件
  28. const eventBus = EventBus.getInstance();
  29. eventBus.on(GameEvents.ENEMY_SPAWNING_STARTED, this.onEnemySpawningStarted, this);
  30. eventBus.on(GameEvents.ENEMY_SPAWNING_STOPPED, this.onEnemySpawningStopped, this);
  31. }
  32. private onEnemySpawningStarted() {
  33. this.enemySpawningActive = true;
  34. }
  35. private onEnemySpawningStopped() {
  36. this.enemySpawningActive = false;
  37. // 当敌人生成停止时,立即检查是否需要发射子弹
  38. const eventBus = EventBus.getInstance();
  39. eventBus.emit(GameEvents.BALL_FIRE_BULLET, {
  40. canFire: (canFire: boolean) => { }
  41. });
  42. }
  43. /**
  44. * 播放方块被撞击动画
  45. * @param blockNode 方块节点
  46. */
  47. public playBlockHitAnimation(blockNode: Node) {
  48. if (!blockNode || !blockNode.isValid) {
  49. console.warn('[BallAni] 方块节点无效');
  50. return;
  51. }
  52. // 如果该方块已经在播放动画,跳过
  53. if (this.activeBlockAnimations.has(blockNode)) {
  54. console.log('[BallAni] 方块动画已在播放中,跳过');
  55. return;
  56. }
  57. // 检查是否有活跃敌人,只有有敌人时才播放缩放动画
  58. const eventBus = EventBus.getInstance();
  59. let hasActiveEnemies = false;
  60. // 通过事件系统检查是否有活跃敌人
  61. eventBus.emit(GameEvents.BALL_FIRE_BULLET, {
  62. canFire: (canFire: boolean) => {
  63. hasActiveEnemies = canFire;
  64. }
  65. });
  66. console.log('[BallAni] 开始播放方块撞击动画', blockNode.name, '有敌人:', hasActiveEnemies);
  67. console.log('[BallAni] hasActiveEnemies 检查结果:', hasActiveEnemies);
  68. // 立即执行动画
  69. this.executeBlockAnimation(blockNode, hasActiveEnemies);
  70. // 如果敌人生成未激活,立即检查是否需要发射子弹
  71. if (!this.enemySpawningActive) {
  72. eventBus.emit(GameEvents.BALL_FIRE_BULLET, {
  73. canFire: (canFire: boolean) => { }
  74. });
  75. }
  76. }
  77. /**
  78. * 执行方块动画的具体逻辑
  79. * @param blockNode 方块节点
  80. * @param hasActiveEnemies 是否有活跃敌人
  81. */
  82. private executeBlockAnimation(blockNode: Node, hasActiveEnemies: boolean) {
  83. if (!blockNode || !blockNode.isValid) {
  84. console.warn('[BallAni] 方块节点无效');
  85. return;
  86. }
  87. // 如果该方块已经在播放动画,跳过
  88. if (this.activeBlockAnimations.has(blockNode)) {
  89. console.log('[BallAni] 方块动画已在播放中,跳过');
  90. return;
  91. }
  92. // 新的预制体结构:Sprite组件在Node子节点上
  93. const nodeChild = blockNode.getChildByName('Node');
  94. const sprite = nodeChild ? nodeChild.getComponent(Sprite) : null;
  95. // 保存原始材质和Sprite缩放
  96. // 优先保存customMaterial,如果没有则保存当前material
  97. const originalMaterial = sprite ? (sprite.customMaterial || sprite.material) : null;
  98. const originalSpriteScale = sprite ? new Vec3(1, 1, 1) : new Vec3(1, 1, 1);
  99. if (hasActiveEnemies) {
  100. // 有敌人时播放Sprite缩放动画(不影响碰撞体积)
  101. const shrinkSpriteScale = new Vec3(0.7, 0.7, 1);
  102. // 应用白色描边材质
  103. console.log('[BallAni] 检查材质状态:', {
  104. hasSprite: !!sprite,
  105. hasWhiteMaterial: !!BallAni.whiteMaterial,
  106. currentMaterial: sprite ? sprite.material : null
  107. });
  108. if (sprite && BallAni.whiteMaterial) {
  109. sprite.material = BallAni.whiteMaterial;
  110. console.log('[BallAni] 应用白色描边材质成功');
  111. } else if (sprite) {
  112. console.log('[BallAni] 白色描边材质未加载,跳过材质应用');
  113. }
  114. // 对Sprite节点进行缩放动画(不影响方块碰撞体积)
  115. const animationTween = tween(sprite.node)
  116. .to(0.2, { scale: shrinkSpriteScale })
  117. .to(0.2, { scale: originalSpriteScale })
  118. .call(() => {
  119. // 动画完成时恢复原始材质
  120. console.log('[BallAni] 恢复材质状态:', {
  121. hasSprite: !!sprite,
  122. hasOriginalMaterial: !!originalMaterial,
  123. currentMaterial: sprite ? sprite.material : null
  124. });
  125. if (sprite && originalMaterial) {
  126. // 如果原始材质是customMaterial,则恢复customMaterial
  127. if (sprite.customMaterial === originalMaterial) {
  128. sprite.material = sprite.customMaterial;
  129. } else {
  130. sprite.material = originalMaterial;
  131. }
  132. console.log('[BallAni] 恢复原始材质成功');
  133. } else if (sprite) {
  134. // 如果没有保存的原始材质,尝试恢复到customMaterial
  135. if (sprite.customMaterial) {
  136. sprite.material = sprite.customMaterial;
  137. console.log('[BallAni] 恢复到customMaterial');
  138. } else {
  139. sprite.material = null;
  140. console.log('[BallAni] 恢复到默认材质');
  141. }
  142. } else {
  143. console.log('[BallAni] 无法恢复原始材质 - Sprite组件丢失');
  144. }
  145. console.log('[BallAni] 方块动画完成,恢复原状');
  146. // 动画完成,从活动列表中移除
  147. this.activeBlockAnimations.delete(blockNode);
  148. })
  149. .start();
  150. // 添加到活动动画列表
  151. this.activeBlockAnimations.set(blockNode, animationTween);
  152. } else {
  153. // 没有敌人时直接结束动画,不应用材质
  154. const animationTween = tween({})
  155. .delay(0.2)
  156. .call(() => {
  157. console.log('[BallAni] 无敌人,直接恢复原状');
  158. // 动画完成,从活动列表中移除
  159. this.activeBlockAnimations.delete(blockNode);
  160. })
  161. .start();
  162. // 添加到活动动画列表
  163. this.activeBlockAnimations.set(blockNode, animationTween);
  164. }
  165. }
  166. /**
  167. * 预加载撞击特效资源
  168. */
  169. private preloadImpactEffect() {
  170. if (BallAni.impactEffectSkeleton || BallAni.isLoading) {
  171. return;
  172. }
  173. BallAni.isLoading = true;
  174. const path = 'Animation/WeaponTx/tx0005/tx0005';
  175. resources.load(path, sp.SkeletonData, (err, sData: sp.SkeletonData) => {
  176. BallAni.isLoading = false;
  177. if (err || !sData) {
  178. console.warn('[BallAni] 加载撞击特效失败:', err);
  179. return;
  180. }
  181. BallAni.impactEffectSkeleton = sData;
  182. console.log('[BallAni] 撞击特效资源加载成功');
  183. });
  184. }
  185. /**
  186. * 预加载白色描边材质
  187. */
  188. private preloadWhiteMaterial() {
  189. if (BallAni.whiteMaterial || BallAni.isMaterialLoading) {
  190. return;
  191. }
  192. BallAni.isMaterialLoading = true;
  193. const materialPath = 'shaders/ui-sprite-white-material';
  194. resources.load(materialPath, Material, (err, material: Material) => {
  195. BallAni.isMaterialLoading = false;
  196. if (err || !material) {
  197. console.warn('[BallAni] 加载白色描边材质失败:', err);
  198. return;
  199. }
  200. BallAni.whiteMaterial = material;
  201. console.log('[BallAni] 白色描边材质加载成功');
  202. });
  203. }
  204. /**
  205. * 在指定位置播放撞击特效
  206. * @param worldPosition 世界坐标位置
  207. */
  208. public playImpactEffect(worldPosition: Vec3) {
  209. // 如果资源未加载,直接加载并播放
  210. if (!BallAni.impactEffectSkeleton) {
  211. const path = 'Animation/WeaponTx/tx0005/tx0005';
  212. resources.load(path, sp.SkeletonData, (err, sData: sp.SkeletonData) => {
  213. if (err || !sData) {
  214. console.warn('[BallAni] 加载撞击特效失败:', err);
  215. return;
  216. }
  217. BallAni.impactEffectSkeleton = sData;
  218. this.createAndPlayEffect(worldPosition, sData);
  219. });
  220. return;
  221. }
  222. this.createAndPlayEffect(worldPosition, BallAni.impactEffectSkeleton);
  223. }
  224. /**
  225. * 创建并播放特效
  226. */
  227. private createAndPlayEffect(worldPosition: Vec3, skeletonData: sp.SkeletonData) {
  228. const effectNode = new Node('ImpactEffect');
  229. const skeleton = effectNode.addComponent(sp.Skeleton);
  230. skeleton.skeletonData = skeletonData;
  231. skeleton.premultipliedAlpha = false;
  232. skeleton.setAnimation(0, 'animation', false);
  233. skeleton.setCompleteListener(() => {
  234. this.removeEffect(effectNode);
  235. });
  236. const canvas = find('Canvas');
  237. if (canvas) {
  238. canvas.addChild(effectNode);
  239. effectNode.setWorldPosition(worldPosition);
  240. // 设置特效缩放
  241. effectNode.setScale(0.8, 0.8, 1);
  242. // 添加到活动特效列表
  243. this.activeEffects.push(effectNode);
  244. } else {
  245. effectNode.destroy();
  246. }
  247. }
  248. /**
  249. * 移除特效节点
  250. * @param effectNode 要移除的特效节点
  251. */
  252. private removeEffect(effectNode: Node) {
  253. // 从活动特效列表中移除
  254. const index = this.activeEffects.indexOf(effectNode);
  255. if (index !== -1) {
  256. this.activeEffects.splice(index, 1);
  257. }
  258. // 销毁节点
  259. if (effectNode && effectNode.isValid) {
  260. effectNode.destroy();
  261. }
  262. }
  263. /**
  264. * 停止指定方块的动画
  265. * @param blockNode 方块节点
  266. */
  267. public stopBlockAnimation(blockNode: Node) {
  268. const animation = this.activeBlockAnimations.get(blockNode);
  269. if (animation) {
  270. animation.stop();
  271. this.activeBlockAnimations.delete(blockNode);
  272. }
  273. }
  274. /**
  275. * 清理所有活动的特效
  276. */
  277. public clearAllEffects() {
  278. for (const effect of this.activeEffects) {
  279. if (effect && effect.isValid) {
  280. effect.destroy();
  281. }
  282. }
  283. this.activeEffects = [];
  284. }
  285. /**
  286. * 清除所有方块动画
  287. */
  288. public clearAllBlockAnimations() {
  289. // 停止所有方块动画
  290. for (const [blockNode, animation] of this.activeBlockAnimations) {
  291. if (animation) {
  292. animation.stop();
  293. }
  294. }
  295. this.activeBlockAnimations.clear();
  296. }
  297. onDestroy() {
  298. // 解绑事件监听
  299. const eventBus = EventBus.getInstance();
  300. eventBus.off(GameEvents.ENEMY_SPAWNING_STARTED, this.onEnemySpawningStarted, this);
  301. eventBus.off(GameEvents.ENEMY_SPAWNING_STOPPED, this.onEnemySpawningStopped, this);
  302. // 组件销毁时清理所有特效和动画
  303. this.clearAllEffects();
  304. this.clearAllBlockAnimations();
  305. }
  306. /**
  307. * 获取BallAni实例
  308. * @returns BallAni实例
  309. */
  310. public static getInstance(): BallAni | null {
  311. const gameArea = find('Canvas/GameLevelUI/GameArea');
  312. if (!gameArea) {
  313. console.warn('[BallAni] 未找到GameArea节点');
  314. return null;
  315. }
  316. const ballAni = gameArea.getComponent(BallAni);
  317. if (!ballAni) {
  318. console.warn('[BallAni] GameArea节点上未找到BallAni组件');
  319. return null;
  320. }
  321. return ballAni;
  322. }
  323. }