levelMgr.ts 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. import { LQCollide } from "./Collide/lq_collide_system/lq_collide";
  2. import { EConfigConst } from "./game/cfg/ConfigDef";
  3. import { ConfigMgr } from "./game/cfg/configMgr";
  4. import { SmallLevelItem } from "./game/cfg/smallLevelItem";
  5. import { Utils } from "./game/common/utils";
  6. import levelempty from "./game/level/levelempty";
  7. import { NestCondition } from "./game/level/nestCondition";
  8. import { NestConditionMgr } from "./game/level/nestConditionMgr";
  9. import { NestPlay } from "./game/nest/nestPlay";
  10. import { ResMgr } from "./game/res/resMgr";
  11. import { GameSysLogic } from "./game/update/gameSysLogic";
  12. /**
  13. * 1 关卡有多个 飞机巢穴
  14. * 2 一个飞机巢穴属性:动画,位置,怪物
  15. * 类型1 数量,时间间隔,延时
  16. */
  17. let pass = 5;
  18. enum LevelState {
  19. none,
  20. loading,
  21. playing,
  22. }
  23. /**
  24. * 1 切换关卡
  25. */
  26. export class LevelMgr extends GameSysLogic {
  27. layer: cc.Node;
  28. title: cc.Label;
  29. passCnt: number = 0;
  30. state = LevelState.none;
  31. nestArr: NestPlay[] = [];
  32. conditionArr: NestCondition[] = [];
  33. private static _inst: LevelMgr;
  34. mgr: NestConditionMgr;
  35. public static get inst(): LevelMgr {
  36. if (this._inst == null) {
  37. this._inst = new LevelMgr();
  38. this._inst.init();
  39. }
  40. return this._inst;
  41. }
  42. init(){
  43. }
  44. updateSec(dt){
  45. if(this.state != LevelState.playing) return;
  46. if(this.layer.getComponentsInChildren(LQCollide).length > 0) return;
  47. if(!this.mgr.isDone()) return;
  48. for (let index = 0; index < this.nestArr.length; index++) {
  49. const element = this.nestArr[index];
  50. if(!element.isDone()) return;
  51. }
  52. this.next();
  53. }
  54. getMonsterCnt(){
  55. return this.layer.getComponentsInChildren(LQCollide).length;
  56. }
  57. async begin(){
  58. this.passCnt = 0;
  59. pass = ConfigMgr.inst.getCfgKeys(EConfigConst.smallLevel).length;
  60. this.load();
  61. }
  62. load(){
  63. this.title.string = `level${this.passCnt + 1}`;
  64. this.loadSmallLevel();
  65. }
  66. async loadSmallLevel(){
  67. this.layer.destroyAllChildren();
  68. this.nestArr = [];
  69. this.conditionArr = [];
  70. this.state = LevelState.loading;
  71. this.mgr && this.mgr.destroy();
  72. let node = await ResMgr.inst.loadPrefab(`level/levelEmpty`, this.layer);
  73. let levelView = node.getComponent(levelempty);
  74. let levelData = ConfigMgr.inst.getCfgClassById<SmallLevelItem>(EConfigConst.smallLevel, this.passCnt+1, SmallLevelItem);
  75. // this.nestCreate(levelData);
  76. let task = [];
  77. let obj = Utils.strToJson(levelData.data.nest1);
  78. obj.forEach(v=>{
  79. let nestNode = Utils.createNode(levelView.$top_node, `nest-[level:${this.passCnt+1}]nest1`);
  80. let nestPlay = nestNode.addComponent(NestPlay);
  81. let p = nestPlay.init(v[0],v[1]);
  82. task.push(p);
  83. this.nestArr.push(nestPlay);
  84. });
  85. await Promise.all(task);
  86. this.state = LevelState.playing;
  87. this.mgr = new NestConditionMgr();
  88. this.mgr.init(levelData);
  89. this.mgr.start();
  90. this.mgr.call=(cnt: number) =>{
  91. let nestKey = `nest${cnt+2}`;
  92. let obj = Utils.strToJson(levelData.data[nestKey]);
  93. obj.forEach(v=>{
  94. let nestNode = Utils.createNode(levelView.$top_node, `nest-[level:${this.passCnt+1}]${nestKey}`);
  95. let nestPlay = nestNode.addComponent(NestPlay);
  96. nestPlay.init(v[0],v[1]);
  97. this.nestArr.push(nestPlay);
  98. });
  99. }
  100. // for (let index = 2; index < 6; index++) {
  101. // let nestKey = `nest${index}`;
  102. // let intervalKey = `interval${index}`;
  103. // if(levelData.data[nestKey] == '' || levelData.data[intervalKey] == '') break;
  104. // let condition = new NestCondition();
  105. // condition.fill(levelData.data[intervalKey]);
  106. // condition.call = ()=>{
  107. // let obj = Utils.strToJson(levelData.data[nestKey]);
  108. // obj.forEach(v=>{
  109. // let nestNode = Utils.createNode(levelView.$top_node, `nest-[level:${this.passCnt+1}]${nestKey}`);
  110. // let nestPlay = nestNode.addComponent(NestPlay);
  111. // nestPlay.init(v[0]);
  112. // this.nestArr.push(nestPlay);
  113. // });
  114. // }
  115. // condition.start();
  116. // condition.name = `[level:${this.passCnt+1}]${intervalKey}`;
  117. // this.conditionArr.push(condition);
  118. // }
  119. // let nestPos = levelData.getNestId();
  120. // let task = [];
  121. // nestPos.forEach(v=>{
  122. // let nestNode = Utils.createNode(levelView.$top_node, 'nest');
  123. // let p = this.fillNest(nestNode, v);
  124. // task.push(p);
  125. // });
  126. // await Promise.all(task);
  127. // this.state = LevelState.playing;
  128. }
  129. skip(num: number){
  130. cc.log('skip-', num);
  131. if(num > pass) return;
  132. if(num < 1) return;
  133. this.passCnt = num-1;
  134. this.load();
  135. }
  136. next(){
  137. if(this.passCnt + 1 >= pass) return;
  138. this.passCnt ++;
  139. this.load();
  140. }
  141. pre(){
  142. if(this.passCnt <= 0) return;
  143. this.passCnt--;
  144. this.load();
  145. }
  146. getMax(){
  147. return pass;
  148. }
  149. private loadPrefabLevel() {
  150. this.state = LevelState.loading
  151. this.title.string = `level${this.passCnt + 1}`;
  152. this.layer.destroyAllChildren();
  153. ResMgr.inst.loadPrefab(`level/level${this.passCnt + 1}`, this.layer).then(()=>{
  154. this.state = LevelState.playing;
  155. });
  156. }
  157. }