nestPlay.ts 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. import { ResMgr } from "../res/resMgr";
  2. import { ConfigConst } from "../cfg/ConfigDef";
  3. import { ConfigMgr } from "../cfg/configMgr";
  4. import { NestItem } from "../cfg/nestItem";
  5. import { Utils } from "../common/utils";
  6. const patternArr = [
  7. 'monster',
  8. 'pattern',
  9. 'pattern2',
  10. 'monsterboss',
  11. ];
  12. const aniArr = [
  13. ['line-1700'], // 直线, 顶部到底部 1
  14. ['yingji', 'yingji1'], // 从上飞入,左右循环摆动
  15. ['jiejizhiyuan_L'], // 从左弧线飞入,飞出屏幕
  16. ['jiejizhiyuan_R'], // 从右弧线飞入,飞出屏幕
  17. ['zhuiji_L'], // 从左下往右上飞入,折线从右侧飞出 5
  18. ['zhuiji_R'], //
  19. ['yingji', 'loopCricle'], // 从上飞入,圆弧运动
  20. ['ani8'],// 从坐上往右下飞入,从右下飞出
  21. ['ani9'], // 8 的反向
  22. ['yingji'], // 从上飞入 10
  23. ['ani11'],
  24. ['ani12'],
  25. ['ani13'], // 直线向下,横向交替--左
  26. ['ani14'],
  27. ['ani15'], // 向下飞入,弧线转弯,向上飞出
  28. ['yingji'], // 16
  29. ['ani17', 'ani17_1'],
  30. ['ani18', 'ani18_1'],
  31. ]
  32. const {ccclass, property, executeInEditMode, playOnFocus} = cc._decorator;
  33. @ccclass
  34. export class NestPlay extends cc.Component {
  35. monsterNode: cc.Node;
  36. timeArr = [];
  37. monsterCnt: number;
  38. num: number;
  39. monster: cc.Prefab;
  40. aniClip: cc.AnimationClip[];
  41. time: number;
  42. async init(id: string){
  43. let nestItem = ConfigMgr.inst.getCfgClassById<NestItem>(ConfigConst.nest, id, NestItem);
  44. // let aniName = 'level/line-1700';
  45. let patternId = Utils.intDefault(nestItem.data.pattern);
  46. let monsterName = 'monster/' + patternArr[patternId-1];
  47. this.num = Utils.intDefault(nestItem.data.num, 1);
  48. let delay = Utils.intDefault(nestItem.data.delay);
  49. this.time = Utils.intDefault(nestItem.data.time);
  50. let type = Utils.intDefault(nestItem.data.type, 1);
  51. let aniName = aniArr[type-1].map(v=>{return 'level/'+v});
  52. this.node.setPosition(Utils.pos2vec2(nestItem.data.pos));
  53. this.aniClip = await ResMgr.inst.loadAniArr(aniName, this.node);
  54. this.monster = await ResMgr.inst.getPrefab(monsterName);
  55. this.monster.addRef();
  56. let monsterNode = new cc.Node('monsterGroup');
  57. this.monsterNode = monsterNode;
  58. this.node.addChild(monsterNode);
  59. if(delay > 0) {
  60. let tId = setTimeout(() => {
  61. this.createMonster();
  62. }, delay * 1000);
  63. this.timeArr.push(tId);
  64. }
  65. else{
  66. this.createMonster();
  67. }
  68. }
  69. protected onDestroy(): void {
  70. this.monster.decRef();
  71. }
  72. createMonster(){
  73. this.createMonster2();
  74. // if(this.aniClip.length == 1) {
  75. // }
  76. // else{
  77. // this.createMonster2();
  78. // }
  79. }
  80. createMonster2(){
  81. this.monsterCnt = 0;
  82. for (let index = 0; index < this.num; index++) {
  83. let tId = setTimeout(() => {
  84. this.monsterCnt++;
  85. let monsterNode = ResMgr.inst.createNode(this.monster, this.monsterNode);
  86. let cnt = 0;
  87. let callAni = ()=>{
  88. if(!cc.isValid(monsterNode, true)){
  89. return;
  90. }
  91. let aniNode = new cc.Node('AniItem');
  92. let parent = monsterNode.parent ? monsterNode.parent : this.monsterNode;
  93. monsterNode.removeFromParent(false);
  94. aniNode.addChild(monsterNode);
  95. parent.addChild(aniNode);
  96. let ani = aniNode.addComponent(cc.Animation);
  97. ani.addClip(this.aniClip[cnt], 'path')
  98. ani.play('path');
  99. ani.once(cc.Animation.EventType.FINISHED, ()=>{
  100. cnt++;
  101. if(cnt < this.aniClip.length){
  102. aniNode.angle = 0;
  103. callAni();
  104. }
  105. else{
  106. let nodeBox = aniNode.getBoundingBoxToWorld();
  107. let viewsize = cc.view.getVisibleSize();
  108. let viewBox = cc.rect(0,0,viewsize.width,viewsize.height);
  109. cc.log('@@', nodeBox.toString(), viewBox.toString());
  110. if(!viewBox.containsRect(nodeBox)) {
  111. aniNode.destroy();
  112. }
  113. }
  114. });
  115. }
  116. callAni();
  117. }, 1000 * this.time * index);
  118. this.timeArr.push(tId);
  119. }
  120. }
  121. createMonster1(){
  122. this.monsterCnt = 0;
  123. for (let index = 0; index < this.num; index++) {
  124. let tId = setTimeout(() => {
  125. let aniNode = new cc.Node('AniItem');
  126. ResMgr.inst.createNode(this.monster, aniNode);
  127. let ani = aniNode.addComponent(cc.Animation);
  128. ani.addClip(this.aniClip[0], "path");
  129. this.monsterNode.addChild(aniNode);
  130. ani.play('path');
  131. this.monsterCnt++;
  132. ani.once(cc.Animation.EventType.FINISHED, ()=>{
  133. let nodeBox = aniNode.getBoundingBoxToWorld();
  134. let viewBox = cc.view.getViewportRect();
  135. if(!viewBox.containsRect(nodeBox)) {
  136. aniNode.destroy();
  137. }
  138. });
  139. }, 1000 * this.time * index);
  140. this.timeArr.push(tId);
  141. }
  142. }
  143. isCreateDone(){
  144. return this.monsterCnt == this.num;
  145. }
  146. waitDone(): Promise<void> {
  147. return new Promise(resolve=>{
  148. let id = setInterval(()=>{
  149. if(this.isCreateDone()){
  150. clearInterval(id);
  151. resolve();
  152. }
  153. },100);
  154. });
  155. }
  156. }