nestPlay.ts 11 KB

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