BallAni.ts 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  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. const sprite = blockNode.getComponent(Sprite);
  93. // 保存原始材质和Sprite缩放
  94. // 优先保存customMaterial,如果没有则保存当前material
  95. const originalMaterial = sprite ? (sprite.customMaterial || sprite.material) : null;
  96. const originalSpriteScale = sprite ? new Vec3(1, 1, 1) : new Vec3(1, 1, 1);
  97. if (hasActiveEnemies) {
  98. // 有敌人时播放Sprite缩放动画(不影响碰撞体积)
  99. const shrinkSpriteScale = new Vec3(0.7, 0.7, 1);
  100. // 应用白色描边材质
  101. console.log('[BallAni] 检查材质状态:', {
  102. hasSprite: !!sprite,
  103. hasWhiteMaterial: !!BallAni.whiteMaterial,
  104. currentMaterial: sprite ? sprite.material : null
  105. });
  106. if (sprite && BallAni.whiteMaterial) {
  107. sprite.material = BallAni.whiteMaterial;
  108. console.log('[BallAni] 应用白色描边材质成功');
  109. } else if (sprite) {
  110. console.log('[BallAni] 白色描边材质未加载,跳过材质应用');
  111. }
  112. // 对Sprite节点进行缩放动画(不影响方块碰撞体积)
  113. const animationTween = tween(sprite.node)
  114. .to(0.2, { scale: shrinkSpriteScale })
  115. .to(0.2, { scale: originalSpriteScale })
  116. .call(() => {
  117. // 动画完成时恢复原始材质
  118. console.log('[BallAni] 恢复材质状态:', {
  119. hasSprite: !!sprite,
  120. hasOriginalMaterial: !!originalMaterial,
  121. currentMaterial: sprite ? sprite.material : null
  122. });
  123. if (sprite && originalMaterial) {
  124. // 如果原始材质是customMaterial,则恢复customMaterial
  125. if (sprite.customMaterial === originalMaterial) {
  126. sprite.material = sprite.customMaterial;
  127. } else {
  128. sprite.material = originalMaterial;
  129. }
  130. console.log('[BallAni] 恢复原始材质成功');
  131. } else if (sprite) {
  132. // 如果没有保存的原始材质,尝试恢复到customMaterial
  133. if (sprite.customMaterial) {
  134. sprite.material = sprite.customMaterial;
  135. console.log('[BallAni] 恢复到customMaterial');
  136. } else {
  137. sprite.material = null;
  138. console.log('[BallAni] 恢复到默认材质');
  139. }
  140. } else {
  141. console.log('[BallAni] 无法恢复原始材质 - Sprite组件丢失');
  142. }
  143. console.log('[BallAni] 方块动画完成,恢复原状');
  144. // 动画完成,从活动列表中移除
  145. this.activeBlockAnimations.delete(blockNode);
  146. })
  147. .start();
  148. // 添加到活动动画列表
  149. this.activeBlockAnimations.set(blockNode, animationTween);
  150. } else {
  151. // 没有敌人时直接结束动画,不应用材质
  152. const animationTween = tween({})
  153. .delay(0.2)
  154. .call(() => {
  155. console.log('[BallAni] 无敌人,直接恢复原状');
  156. // 动画完成,从活动列表中移除
  157. this.activeBlockAnimations.delete(blockNode);
  158. })
  159. .start();
  160. // 添加到活动动画列表
  161. this.activeBlockAnimations.set(blockNode, animationTween);
  162. }
  163. }
  164. /**
  165. * 预加载撞击特效资源
  166. */
  167. private preloadImpactEffect() {
  168. if (BallAni.impactEffectSkeleton || BallAni.isLoading) {
  169. return;
  170. }
  171. BallAni.isLoading = true;
  172. const path = 'Animation/WeaponTx/tx0005/tx0005';
  173. resources.load(path, sp.SkeletonData, (err, sData: sp.SkeletonData) => {
  174. BallAni.isLoading = false;
  175. if (err || !sData) {
  176. console.warn('[BallAni] 加载撞击特效失败:', err);
  177. return;
  178. }
  179. BallAni.impactEffectSkeleton = sData;
  180. console.log('[BallAni] 撞击特效资源加载成功');
  181. });
  182. }
  183. /**
  184. * 预加载白色描边材质
  185. */
  186. private preloadWhiteMaterial() {
  187. if (BallAni.whiteMaterial || BallAni.isMaterialLoading) {
  188. return;
  189. }
  190. BallAni.isMaterialLoading = true;
  191. const materialPath = 'shaders/ui-sprite-white-material';
  192. resources.load(materialPath, Material, (err, material: Material) => {
  193. BallAni.isMaterialLoading = false;
  194. if (err || !material) {
  195. console.warn('[BallAni] 加载白色描边材质失败:', err);
  196. return;
  197. }
  198. BallAni.whiteMaterial = material;
  199. console.log('[BallAni] 白色描边材质加载成功');
  200. });
  201. }
  202. /**
  203. * 在指定位置播放撞击特效
  204. * @param worldPosition 世界坐标位置
  205. */
  206. public playImpactEffect(worldPosition: Vec3) {
  207. // 如果资源未加载,直接加载并播放
  208. if (!BallAni.impactEffectSkeleton) {
  209. const path = 'Animation/WeaponTx/tx0005/tx0005';
  210. resources.load(path, sp.SkeletonData, (err, sData: sp.SkeletonData) => {
  211. if (err || !sData) {
  212. console.warn('[BallAni] 加载撞击特效失败:', err);
  213. return;
  214. }
  215. BallAni.impactEffectSkeleton = sData;
  216. this.createAndPlayEffect(worldPosition, sData);
  217. });
  218. return;
  219. }
  220. this.createAndPlayEffect(worldPosition, BallAni.impactEffectSkeleton);
  221. }
  222. /**
  223. * 创建并播放特效
  224. */
  225. private createAndPlayEffect(worldPosition: Vec3, skeletonData: sp.SkeletonData) {
  226. const effectNode = new Node('ImpactEffect');
  227. const skeleton = effectNode.addComponent(sp.Skeleton);
  228. skeleton.skeletonData = skeletonData;
  229. skeleton.premultipliedAlpha = false;
  230. skeleton.setAnimation(0, 'animation', false);
  231. skeleton.setCompleteListener(() => {
  232. this.removeEffect(effectNode);
  233. });
  234. const canvas = find('Canvas');
  235. if (canvas) {
  236. canvas.addChild(effectNode);
  237. effectNode.setWorldPosition(worldPosition);
  238. // 设置特效缩放
  239. effectNode.setScale(0.8, 0.8, 1);
  240. // 添加到活动特效列表
  241. this.activeEffects.push(effectNode);
  242. } else {
  243. effectNode.destroy();
  244. }
  245. }
  246. /**
  247. * 移除特效节点
  248. * @param effectNode 要移除的特效节点
  249. */
  250. private removeEffect(effectNode: Node) {
  251. // 从活动特效列表中移除
  252. const index = this.activeEffects.indexOf(effectNode);
  253. if (index !== -1) {
  254. this.activeEffects.splice(index, 1);
  255. }
  256. // 销毁节点
  257. if (effectNode && effectNode.isValid) {
  258. effectNode.destroy();
  259. }
  260. }
  261. /**
  262. * 停止指定方块的动画
  263. * @param blockNode 方块节点
  264. */
  265. public stopBlockAnimation(blockNode: Node) {
  266. const animation = this.activeBlockAnimations.get(blockNode);
  267. if (animation) {
  268. animation.stop();
  269. this.activeBlockAnimations.delete(blockNode);
  270. }
  271. }
  272. /**
  273. * 清理所有活动的特效
  274. */
  275. public clearAllEffects() {
  276. for (const effect of this.activeEffects) {
  277. if (effect && effect.isValid) {
  278. effect.destroy();
  279. }
  280. }
  281. this.activeEffects = [];
  282. }
  283. /**
  284. * 清除所有方块动画
  285. */
  286. public clearAllBlockAnimations() {
  287. // 停止所有方块动画
  288. for (const [blockNode, animation] of this.activeBlockAnimations) {
  289. if (animation) {
  290. animation.stop();
  291. }
  292. }
  293. this.activeBlockAnimations.clear();
  294. }
  295. onDestroy() {
  296. // 解绑事件监听
  297. const eventBus = EventBus.getInstance();
  298. eventBus.off(GameEvents.ENEMY_SPAWNING_STARTED, this.onEnemySpawningStarted, this);
  299. eventBus.off(GameEvents.ENEMY_SPAWNING_STOPPED, this.onEnemySpawningStopped, this);
  300. // 组件销毁时清理所有特效和动画
  301. this.clearAllEffects();
  302. this.clearAllBlockAnimations();
  303. }
  304. /**
  305. * 获取BallAni实例
  306. * @returns BallAni实例
  307. */
  308. public static getInstance(): BallAni | null {
  309. const gameArea = find('Canvas/GameLevelUI/GameArea');
  310. if (!gameArea) {
  311. console.warn('[BallAni] 未找到GameArea节点');
  312. return null;
  313. }
  314. const ballAni = gameArea.getComponent(BallAni);
  315. if (!ballAni) {
  316. console.warn('[BallAni] GameArea节点上未找到BallAni组件');
  317. return null;
  318. }
  319. return ballAni;
  320. }
  321. }