nestPlay.ts 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  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.createMonster3();
  93. this.createMonster2();
  94. }
  95. setPattern(monsterNode: cc.Node){
  96. let arr = [];
  97. this.pattern.forEach(v => {
  98. for (let index = 0; index < v[0]; index++) {
  99. arr.push(v[1]);
  100. }
  101. });
  102. if(arr.length == 0){
  103. cc.error('[nestPlay] [pattern] 配置空',`[nestId:${this._id}]`);
  104. arr.push('1001');
  105. }
  106. let bulletArr = [];
  107. let json = Utils.strToJson(this.bullet);
  108. json.forEach(v=>{
  109. if(v.length <= 0) return;
  110. for (let index = 0; index < v[0]; index++) {
  111. bulletArr.push(v);
  112. }
  113. });
  114. let models = monsterNode.getComponentsInChildren(MonsterModel);
  115. models.forEach((v, k)=>{
  116. let id = '1001';
  117. if(k < arr.length){
  118. id = arr[k];
  119. }
  120. else{
  121. id = arr[arr.length-1];
  122. }
  123. v.init(id, this._level);
  124. if(k< bulletArr.length){
  125. v.setBullet(bulletArr[k])
  126. }
  127. else{
  128. v.setBullet(null);
  129. }
  130. });
  131. }
  132. createMonster3(){
  133. this.monsterCnt = 0;
  134. for (let index = 0; index < this.num; index++) {
  135. let tId = setTimeout(() => {
  136. this.monsterCnt++;
  137. cc.log('[nestPlay] [create]',`[num:${this.monsterCnt} / ${this.num}]`);
  138. let monsterNode = ResMgr.inst.createNode(this.monster, this.monsterNode);
  139. this.setPattern(monsterNode);
  140. let models = monsterNode.getComponentsInChildren(MonsterModel);
  141. models.forEach(v=>{
  142. let modleNode = v.node;
  143. let aniTop = new cc.Node('AniTop');
  144. let parent = modleNode.parent;
  145. modleNode.removeFromParent(false);
  146. parent.addChild(aniTop);
  147. aniTop.addChild(modleNode);
  148. aniTop.setPosition(modleNode.getPosition());
  149. modleNode.setPosition(cc.Vec2.ZERO);
  150. });
  151. let cnt = 0;
  152. let callAni = ()=>{
  153. if(!cc.isValid(monsterNode, true)){
  154. return;
  155. }
  156. let models = monsterNode.getComponentsInChildren(MonsterModel);
  157. models.forEach(v=>{
  158. let modleNode = v.node;
  159. let aniNode = new cc.Node('AniItem');
  160. let parent = modleNode.parent;
  161. modleNode.removeFromParent(false);
  162. aniNode.addChild(modleNode);
  163. parent.addChild(aniNode);
  164. // aniNode.setPosition(modleNode.getPosition())
  165. // modleNode.setPosition(cc.v2(0,0));
  166. let ani = aniNode.addComponent(cc.Animation);
  167. ani.addClip(this.aniClip[cnt], 'path')
  168. ani.play('path').speed = 0.3;
  169. ani.once(cc.Animation.EventType.FINISHED, ()=>{
  170. cnt++;
  171. if(cnt < this.aniClip.length){
  172. aniNode.angle = 0;
  173. callAni();
  174. }
  175. else{
  176. let nodeBox = aniNode.getBoundingBoxToWorld();
  177. let viewsize = cc.view.getVisibleSize();
  178. let viewBox = cc.rect(0,0,viewsize.width,viewsize.height);
  179. // cc.log('@@', nodeBox.toString(), viewBox.toString());
  180. if(!viewBox.containsRect(nodeBox)) {
  181. aniNode.destroy();
  182. }
  183. }
  184. });
  185. });
  186. }
  187. callAni();
  188. }, 1000 * this.time * index);
  189. this.timeArr.push(tId);
  190. }
  191. }
  192. createMonster2(){
  193. this.monsterCnt = 0;
  194. for (let index = 0; index < this.num; index++) {
  195. let tId = setTimeout(() => {
  196. this.monsterCnt++;
  197. cc.log('[nestPlay] [create]',`[num:${this.monsterCnt} / ${this.num}]`);
  198. let monsterNode = ResMgr.inst.createNode(this.monster, this.monsterNode);
  199. this.setPattern(monsterNode);
  200. let cnt = 0;
  201. let callAni = ()=>{
  202. if(!cc.isValid(monsterNode, true)){
  203. return;
  204. }
  205. let aniNode = new cc.Node('AniItem');
  206. let parent = monsterNode.parent ? monsterNode.parent : this.monsterNode;
  207. monsterNode.removeFromParent(false);
  208. aniNode.addChild(monsterNode);
  209. parent.addChild(aniNode);
  210. let ani = aniNode.addComponent(cc.Animation);
  211. ani.addClip(this.aniClip[cnt], 'path')
  212. ani.play('path').speed = 0.3;
  213. ani.once(cc.Animation.EventType.FINISHED, ()=>{
  214. cnt++;
  215. if(cnt < this.aniClip.length){
  216. aniNode.angle = 0;
  217. callAni();
  218. }
  219. else{
  220. let nodeBox = aniNode.getBoundingBoxToWorld();
  221. let viewsize = cc.view.getVisibleSize();
  222. let viewBox = cc.rect(0,0,viewsize.width,viewsize.height);
  223. // cc.log('@@', nodeBox.toString(), viewBox.toString());
  224. if(!viewBox.containsRect(nodeBox)) {
  225. aniNode.destroy();
  226. }
  227. }
  228. });
  229. }
  230. callAni();
  231. }, 1000 * this.time * index);
  232. this.timeArr.push(tId);
  233. }
  234. }
  235. createMonster1(){
  236. this.monsterCnt = 0;
  237. for (let index = 0; index < this.num; index++) {
  238. let tId = setTimeout(() => {
  239. let aniNode = new cc.Node('AniItem');
  240. ResMgr.inst.createNode(this.monster, aniNode);
  241. let ani = aniNode.addComponent(cc.Animation);
  242. ani.addClip(this.aniClip[0], "path");
  243. this.monsterNode.addChild(aniNode);
  244. ani.play('path');
  245. this.monsterCnt++;
  246. ani.once(cc.Animation.EventType.FINISHED, ()=>{
  247. let nodeBox = aniNode.getBoundingBoxToWorld();
  248. let viewBox = cc.view.getViewportRect();
  249. if(!viewBox.containsRect(nodeBox)) {
  250. aniNode.destroy();
  251. }
  252. });
  253. }, 1000 * this.time * index);
  254. this.timeArr.push(tId);
  255. }
  256. }
  257. isDone(){
  258. return this.monsterCnt == this.num;
  259. }
  260. waitDone(): Promise<void> {
  261. return new Promise(resolve=>{
  262. let id = setInterval(()=>{
  263. if(this.isDone()){
  264. clearInterval(id);
  265. resolve();
  266. }
  267. },100);
  268. });
  269. }
  270. }