nestPlay.ts 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. import { ResMgr } from "../res/resMgr";
  2. import { ConfigConst, EConfigConst } from "../cfg/ConfigDef";
  3. import { ConfigMgr } from "../cfg/configMgr";
  4. import { NestItem } from "../cfg/nestItem";
  5. import { Utils } from "../common/utils";
  6. import { MonsterModel } from "../monster/monsterModel";
  7. const patternArr = [
  8. 'monster',
  9. 'pattern',
  10. 'pattern2',
  11. 'monsterboss',
  12. ];
  13. const Shape = {
  14. 101: 'monster',
  15. 701: 'pattern',
  16. 501: 'pattern2',
  17. 103: 'monsterboss',
  18. 102: 'monsterboss2',
  19. 201: 'pattern201',
  20. 202: 'pattern202',
  21. }
  22. const aniArr = [
  23. ['line-1700'], // 直线, 顶部到底部 1
  24. ['yingji', 'yingji1'], // 从上飞入,左右循环摆动
  25. ['jiejizhiyuan_L'], // 从左弧线飞入,飞出屏幕
  26. ['jiejizhiyuan_R'], // 从右弧线飞入,飞出屏幕
  27. ['zhuiji_L'], // 从左下往右上飞入,折线从右侧飞出 5
  28. ['zhuiji_R'], //
  29. ['yingji', 'loopCricle'], // 从上飞入,圆弧运动
  30. ['ani8'],// 从坐上往右下飞入,从右下飞出
  31. ['ani9'], // 8 的反向
  32. ['yingji'], // 从上飞入 10
  33. ['ani11'],
  34. ['ani12'],
  35. ['ani13'], // 直线向下,横向交替--左
  36. ['ani14'],
  37. ['ani15'], // 向下飞入,弧线转弯,向上飞出
  38. ['yingji'], // 16
  39. ['ani17', 'ani17_1'],
  40. ['ani18', 'ani18_1'],
  41. ]
  42. const {ccclass, property, executeInEditMode, playOnFocus} = cc._decorator;
  43. @ccclass
  44. export class NestPlay extends cc.Component {
  45. monsterNode: cc.Node;
  46. timeArr = [];
  47. monsterCnt: number;
  48. num: number;
  49. monster: cc.Prefab;
  50. aniClip: cc.AnimationClip[];
  51. time: number;
  52. pattern: any;
  53. private _id: string;
  54. private _level: number;
  55. bullet: string;
  56. async init(id: string, level: number){
  57. this._id = id;
  58. this._level = level;
  59. let nestItem = ConfigMgr.inst.getCfgClassById<NestItem>(EConfigConst.nest, id, NestItem);
  60. let shapeId = Utils.intDefault(nestItem.data.shape);
  61. let monsterName = 'monster/' + Shape[shapeId];
  62. this.num = Utils.intDefault(nestItem.data.num, 1);
  63. let delay = Utils.intDefault(nestItem.data.delay);
  64. this.time = Utils.intDefault(nestItem.data.time) / 100;
  65. let type = Utils.intDefault(nestItem.data.type, 1);
  66. this.pattern = Utils.strToJson(nestItem.data.pattern);
  67. this.bullet = nestItem.data.bullet;
  68. let aniName = aniArr[type-1].map(v=>{return 'level/'+v});
  69. let posJson = Utils.pos2vec2(nestItem.data.pos);
  70. this.node.setPosition(posJson);
  71. this.aniClip = await ResMgr.inst.loadAniArr(aniName, this.node);
  72. this.monster = await ResMgr.inst.getPrefab(monsterName);
  73. this.monster.addRef();
  74. let monsterNode = new cc.Node('monsterGroup');
  75. this.monsterNode = monsterNode;
  76. this.node.addChild(monsterNode);
  77. cc.log('[nestPlay] [init]',`[num:${this.num}] [time:${this.time}]`);
  78. if(delay > 0) {
  79. let tId = setTimeout(() => {
  80. this.createMonster();
  81. }, delay * 1000);
  82. this.timeArr.push(tId);
  83. }
  84. else{
  85. this.createMonster();
  86. }
  87. }
  88. protected onDestroy(): void {
  89. this.monster.decRef();
  90. }
  91. createMonster(){
  92. this.createMonster2();
  93. }
  94. setPattern(monsterNode: cc.Node){
  95. let arr = [];
  96. this.pattern.forEach(v => {
  97. for (let index = 0; index < v[0]; index++) {
  98. arr.push(v[1]);
  99. }
  100. });
  101. if(arr.length == 0){
  102. cc.error('[nestPlay] [pattern] 配置空',`[nestId:${this._id}]`);
  103. arr.push('1001');
  104. }
  105. let bulletArr = [];
  106. let json = Utils.strToJson(this.bullet);
  107. json.forEach(v=>{
  108. if(v.length <= 0) return;
  109. for (let index = 0; index < v[0]; index++) {
  110. bulletArr.push(v);
  111. }
  112. });
  113. let models = monsterNode.getComponentsInChildren(MonsterModel);
  114. models.forEach((v, k)=>{
  115. let id = '1001';
  116. if(k < arr.length){
  117. id = arr[k];
  118. }
  119. else{
  120. id = arr[arr.length-1];
  121. }
  122. v.init(id, this._level);
  123. if(k< bulletArr.length){
  124. v.setBullet(bulletArr[k])
  125. }
  126. else{
  127. v.setBullet(null);
  128. }
  129. });
  130. }
  131. createMonster2(){
  132. this.monsterCnt = 0;
  133. for (let index = 0; index < this.num; index++) {
  134. let tId = setTimeout(() => {
  135. this.monsterCnt++;
  136. cc.log('[nestPlay] [create]',`[num:${this.monsterCnt} / ${this.num}]`);
  137. let monsterNode = ResMgr.inst.createNode(this.monster, this.monsterNode);
  138. this.setPattern(monsterNode);
  139. let cnt = 0;
  140. let callAni = ()=>{
  141. if(!cc.isValid(monsterNode, true)){
  142. return;
  143. }
  144. let aniNode = new cc.Node('AniItem');
  145. let parent = monsterNode.parent ? monsterNode.parent : this.monsterNode;
  146. monsterNode.removeFromParent(false);
  147. aniNode.addChild(monsterNode);
  148. parent.addChild(aniNode);
  149. let ani = aniNode.addComponent(cc.Animation);
  150. ani.addClip(this.aniClip[cnt], 'path')
  151. ani.play('path');
  152. ani.once(cc.Animation.EventType.FINISHED, ()=>{
  153. cnt++;
  154. if(cnt < this.aniClip.length){
  155. aniNode.angle = 0;
  156. callAni();
  157. }
  158. else{
  159. let nodeBox = aniNode.getBoundingBoxToWorld();
  160. let viewsize = cc.view.getVisibleSize();
  161. let viewBox = cc.rect(0,0,viewsize.width,viewsize.height);
  162. // cc.log('@@', nodeBox.toString(), viewBox.toString());
  163. if(!viewBox.containsRect(nodeBox)) {
  164. aniNode.destroy();
  165. }
  166. }
  167. });
  168. }
  169. callAni();
  170. }, 1000 * this.time * index);
  171. this.timeArr.push(tId);
  172. }
  173. }
  174. createMonster1(){
  175. this.monsterCnt = 0;
  176. for (let index = 0; index < this.num; index++) {
  177. let tId = setTimeout(() => {
  178. let aniNode = new cc.Node('AniItem');
  179. ResMgr.inst.createNode(this.monster, aniNode);
  180. let ani = aniNode.addComponent(cc.Animation);
  181. ani.addClip(this.aniClip[0], "path");
  182. this.monsterNode.addChild(aniNode);
  183. ani.play('path');
  184. this.monsterCnt++;
  185. ani.once(cc.Animation.EventType.FINISHED, ()=>{
  186. let nodeBox = aniNode.getBoundingBoxToWorld();
  187. let viewBox = cc.view.getViewportRect();
  188. if(!viewBox.containsRect(nodeBox)) {
  189. aniNode.destroy();
  190. }
  191. });
  192. }, 1000 * this.time * index);
  193. this.timeArr.push(tId);
  194. }
  195. }
  196. isDone(){
  197. return this.monsterCnt == this.num;
  198. }
  199. waitDone(): Promise<void> {
  200. return new Promise(resolve=>{
  201. let id = setInterval(()=>{
  202. if(this.isDone()){
  203. clearInterval(id);
  204. resolve();
  205. }
  206. },100);
  207. });
  208. }
  209. }