effectManager.ts 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. import { _decorator, Component, Node, Prefab, AnimationComponent, ParticleSystemComponent, Vec3, find, AnimationState, AnimationClip, instantiate, Animation } from 'cc';
  2. import { poolManager } from './poolManager';
  3. import { resourceUtil } from './resourceUtil';
  4. const { ccclass, property } = _decorator;
  5. @ccclass('EffectManager')
  6. export class EffectManager extends Component {
  7. private _ndParent: Node = null!;
  8. public get ndParent() {
  9. if (!this._ndParent) {
  10. this._ndParent = find("effectManager") as Node;
  11. }
  12. return this._ndParent
  13. }
  14. private _ndEff: Node = null!;
  15. public get ndEff() {
  16. if (!this._ndEff) {
  17. this._ndEff = find("effParent") as Node;
  18. }
  19. return this._ndEff;
  20. }
  21. static _instance: EffectManager;
  22. static get instance() {
  23. if (this._instance) {
  24. return this._instance;
  25. }
  26. this._instance = new EffectManager();
  27. return this._instance;
  28. }
  29. /**
  30. * 播放动画
  31. * @param {string} path 动画节点路径
  32. * @param {string} aniName
  33. * @param {vec3} worPos 世界坐标
  34. * @param {boolean} isLoop 是否循环
  35. * @param {boolean} isRecycle 是否回收
  36. * @param {number} [scale=1] 缩放倍数
  37. * @param {Function} [callback=()=>{}] 回调函数
  38. */
  39. public playAni(path: string, aniName: string, worPos: Vec3 = new Vec3(), isLoop: boolean = false, isRecycle: boolean = false, scale: number = 1, callback: Function = () => { }) {
  40. let childName: string = path.split("/")[1];
  41. let ndEffect: Node | null = this.ndParent.getChildByName(childName);
  42. let cb = () => {
  43. ndEffect?.setScale(new Vec3(scale, scale, scale));
  44. ndEffect?.setWorldPosition(worPos);
  45. let ani: AnimationComponent = ndEffect?.getComponent(AnimationComponent) as AnimationComponent;
  46. ani.play(aniName);
  47. let aniState: AnimationState = ani.getState(aniName) as AnimationState;
  48. if (aniState) {
  49. if (isLoop) {
  50. aniState.wrapMode = AnimationClip.WrapMode.Loop;
  51. } else {
  52. aniState.wrapMode = AnimationClip.WrapMode.Normal;
  53. }
  54. }
  55. ani.once(AnimationComponent.EventType.FINISHED, () => {
  56. callback && callback();
  57. if (isRecycle && ndEffect) {
  58. poolManager.instance.putNode(ndEffect);
  59. }
  60. })
  61. }
  62. if (!ndEffect) {
  63. resourceUtil.loadModelRes(path).then((prefab: any) => {
  64. ndEffect = poolManager.instance.getNode(prefab as Prefab, this.ndParent) as Node;
  65. ndEffect.setScale(new Vec3(scale, scale, scale));
  66. ndEffect.setWorldPosition(worPos);
  67. cb();
  68. })
  69. } else {
  70. cb();
  71. }
  72. }
  73. /**
  74. * 移除特效
  75. * @param {string} name 特效名称
  76. * @param {Node}} ndParent 特效父节点
  77. */
  78. public removeEffect(name: string, ndParent: Node = this.ndParent) {
  79. let ndEffect: Node | null = ndParent.getChildByName(name);
  80. if (ndEffect) {
  81. let arrAni: AnimationComponent[] = ndEffect.getComponentsInChildren(AnimationComponent);
  82. arrAni.forEach((element: AnimationComponent) => {
  83. element.stop();
  84. })
  85. let arrParticle: [] = ndEffect?.getComponentsInChildren(ParticleSystemComponent) as any;
  86. arrParticle.forEach((element: ParticleSystemComponent) => {
  87. element?.clear();
  88. element?.stop();
  89. })
  90. poolManager.instance.putNode(ndEffect);
  91. }
  92. }
  93. /**
  94. * 播放粒子特效
  95. * @param {string} path 特效路径
  96. * @param {vec3}worPos 特效世界坐标
  97. * @param {number} [recycleTime=0] 特效节点回收时间,如果为0,则使用默认duration
  98. * @param {number} [scale=1] 缩放倍数
  99. * @param {vec3} eulerAngles 特效角度
  100. * @param {Function} [callback=()=>{}] 回调函数
  101. */
  102. public playParticle(path: string, worPos: Vec3, recycleTime: number = 0, scale: number = 1, eulerAngles?: Vec3, callback?: Function) {
  103. resourceUtil.loadEffectRes(path).then((prefab: any) => {
  104. let ndEffect: Node = poolManager.instance.getNode(prefab as Prefab, this.ndParent) as Node;
  105. ndEffect.setScale(new Vec3(scale, scale, scale));
  106. ndEffect.setWorldPosition(worPos);
  107. if (eulerAngles) {
  108. ndEffect.eulerAngles = eulerAngles;
  109. }
  110. let maxDuration: number = 0;
  111. let arrParticle: ParticleSystemComponent[] = ndEffect.getComponentsInChildren(ParticleSystemComponent);
  112. arrParticle.forEach((item: ParticleSystemComponent) => {
  113. item.simulationSpeed = 1;
  114. item?.clear();
  115. item?.stop();
  116. item?.play()
  117. let duration: number = item.duration;
  118. maxDuration = duration > maxDuration ? duration : maxDuration;
  119. })
  120. let seconds: number = recycleTime && recycleTime > 0 ? recycleTime : maxDuration;
  121. setTimeout(() => {
  122. if (ndEffect.parent) {
  123. callback && callback();
  124. poolManager.instance.putNode(ndEffect);
  125. }
  126. }, seconds * 1000)
  127. })
  128. }
  129. /**
  130. * 播放节点下面的动画和粒子
  131. *
  132. * @param {Node} targetNode 特效挂载节点
  133. * @param {string} effectPath 特效路径
  134. * @param {boolean} [isPlayAni=true] 是否播放动画
  135. * @param {boolean} [isPlayParticle=true] 是否播放特效
  136. * @param {number} [recycleTime=0] 特效节点回收时间,如果为0,则使用默认duration
  137. * @param {number} [scale=1] 缩放倍数
  138. * @param {Vec3} [pos=new Vec3()] 位移
  139. * @param {Function} [callback=()=>{}] 回调函数
  140. * @returns
  141. * @memberof EffectManager
  142. */
  143. public playEffect(targetNode: Node, effectPath: string, isPlayAni: boolean = true, isPlayParticle: boolean = true, recycleTime: number = 0, scale: number = 1, pos: Vec3 = new Vec3(), eulerAngles?: Vec3, callback?: Function) {
  144. if (!targetNode.parent) {//父节点被回收的时候不播放
  145. return;
  146. }
  147. resourceUtil.loadEffectRes(effectPath).then((prefab: any) => {
  148. let ndEffect: Node = poolManager.instance.getNode(prefab as Prefab, targetNode) as Node;
  149. ndEffect.setScale(new Vec3(scale, scale, scale));
  150. ndEffect.setPosition(pos);
  151. if (eulerAngles) {
  152. ndEffect.eulerAngles = eulerAngles;
  153. }
  154. let maxDuration: number = 0;
  155. if (isPlayAni) {
  156. let arrAni: AnimationComponent[] = ndEffect.getComponentsInChildren(AnimationComponent);
  157. arrAni.forEach((element: AnimationComponent, idx: number) => {
  158. element?.play();
  159. let aniName = element?.defaultClip?.name;
  160. if (aniName) {
  161. let aniState = element.getState(aniName);
  162. if (aniState) {
  163. let duration = aniState.duration;
  164. maxDuration = duration > maxDuration ? duration : maxDuration;
  165. aniState.speed = 1;
  166. }
  167. }
  168. })
  169. }
  170. if (isPlayParticle) {
  171. let arrParticle: ParticleSystemComponent[] = ndEffect.getComponentsInChildren(ParticleSystemComponent);
  172. arrParticle.forEach((element: ParticleSystemComponent) => {
  173. element.simulationSpeed = 1;
  174. element?.clear();
  175. element?.stop();
  176. element?.play()
  177. let duration: number = element.duration;
  178. maxDuration = duration > maxDuration ? duration : maxDuration;
  179. })
  180. }
  181. let seconds: number = recycleTime && recycleTime > 0 ? recycleTime : maxDuration;
  182. setTimeout(() => {
  183. if (ndEffect.parent) {
  184. callback && callback();
  185. poolManager.instance.putNode(ndEffect);
  186. }
  187. }, seconds * 1000)
  188. })
  189. }
  190. public playGetPresent(parent: Node) {
  191. resourceUtil.loadEffectRes('propSynthesis').then((prefab: any) => {
  192. let ndEffect: Node = poolManager.instance.getNode(prefab as Prefab, parent) as Node;
  193. // ndEffect.setScale(new Vec3(scale, scale, scale));
  194. // ndEffect.setPosition(pos);
  195. let maxDuration: number = 0;
  196. let arrParticle: ParticleSystemComponent[] = ndEffect.getComponentsInChildren(ParticleSystemComponent);
  197. arrParticle.forEach((item: ParticleSystemComponent) => {
  198. item.simulationSpeed = 1;
  199. item?.clear();
  200. item?.stop();
  201. item?.play()
  202. let duration: number = item.duration;
  203. maxDuration = duration > maxDuration ? duration : maxDuration;
  204. })
  205. let seconds: number = maxDuration;
  206. setTimeout(() => {
  207. if (ndEffect.parent) {
  208. poolManager.instance.putNode(ndEffect);
  209. // ndEffect.destroy()
  210. }
  211. }, seconds * 1000)
  212. })
  213. }
  214. /**
  215. * 播放待机动画
  216. */
  217. public playIdle(isCelebrateToIdle?: boolean) {
  218. for (let i = 0; i < this.ndEff.children.length; i++) {
  219. const item = this.ndEff.children[i];
  220. if (isCelebrateToIdle && item.name === 'machineTop') {
  221. item.getComponent(AnimationComponent)?.play('idleIn');
  222. item.getComponent(AnimationComponent)?.once(Animation.EventType.FINISHED, () => {
  223. item.getComponent(AnimationComponent)?.play('idle');
  224. })
  225. continue;
  226. }
  227. item.getComponent(AnimationComponent)?.play('idle');
  228. }
  229. }
  230. /**
  231. * 播放庆祝动画——获得特殊奖品
  232. */
  233. public playCelebrate() {
  234. for (let i = 0; i < this.ndEff.children.length; i++) {
  235. this.ndEff.children[i].getComponent(AnimationComponent)?.play('celebrate');
  236. }
  237. setTimeout(() => {
  238. this.playIdle(true);
  239. }, 3000);
  240. //庆祝礼花
  241. let ndEffect: Node;
  242. resourceUtil.loadEffectRes('celebrate').then((prefab: any) => {
  243. ndEffect = instantiate(prefab) as Node;
  244. ndEffect.parent = this.ndParent;
  245. ndEffect.setPosition(new Vec3(0, 0, 0));
  246. let maxDuration = 0;
  247. let arrParticle: ParticleSystemComponent[] = ndEffect.getComponentsInChildren(ParticleSystemComponent);
  248. arrParticle.forEach((item: ParticleSystemComponent) => {
  249. item.simulationSpeed = 1;
  250. item?.clear();
  251. item?.stop();
  252. item?.play()
  253. let duration: number = item.duration;
  254. maxDuration = duration > maxDuration ? duration : maxDuration;
  255. })
  256. //礼花需要置空消失的时间
  257. let seconds: number = maxDuration + 2.5;
  258. setTimeout(() => {
  259. if (ndEffect.parent) {
  260. ndEffect.destroy();
  261. }
  262. }, seconds * 1000)
  263. })
  264. }
  265. }