levelMgr.ts 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. import { LQCollide } from "./Collide/lq_collide_system/lq_collide";
  2. import { ConfigConst } 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 { NestPlay } from "./game/nest/nestPlay";
  8. import { ResMgr } from "./game/res/resMgr";
  9. import { GameSysLogic } from "./game/update/gameSysLogic";
  10. /**
  11. * 1 关卡有多个 飞机巢穴
  12. * 2 一个飞机巢穴属性:动画,位置,怪物
  13. * 类型1 数量,时间间隔,延时
  14. */
  15. let pass = 5;
  16. enum LevelState {
  17. none,
  18. loading,
  19. playing,
  20. }
  21. /**
  22. * 1 切换关卡
  23. */
  24. export class LevelMgr extends GameSysLogic {
  25. parent: cc.Node;
  26. title: cc.Label;
  27. passCnt: number = 0;
  28. state = LevelState.none;
  29. private static _inst: LevelMgr;
  30. public static get inst(): LevelMgr {
  31. if (this._inst == null) {
  32. this._inst = new LevelMgr();
  33. this._inst.init();
  34. }
  35. return this._inst;
  36. }
  37. init(){
  38. }
  39. updateSec(dt){
  40. if(this.state != LevelState.playing) return;
  41. if(this.parent.getComponentsInChildren(LQCollide).length > 0) return;
  42. this.next();
  43. }
  44. async begin(){
  45. this.passCnt = 0;
  46. pass = ConfigMgr.inst.getCfgKeys(ConfigConst.smallLevel).length;
  47. this.load();
  48. }
  49. load(){
  50. this.title.string = `level${this.passCnt + 1}`;
  51. this.loadSmallLevel();
  52. }
  53. async loadSmallLevel(){
  54. this.parent.destroyAllChildren();
  55. this.state = LevelState.loading
  56. let node = await ResMgr.inst.loadPrefab(`level/levelEmpty`, this.parent);
  57. let levelView = node.getComponent(levelempty);
  58. let levelData = ConfigMgr.inst.getCfgClassById<SmallLevelItem>(ConfigConst.smallLevel, this.passCnt+1, SmallLevelItem);
  59. let nestPos = levelData.getNestId();
  60. let task = [];
  61. nestPos.forEach(v=>{
  62. let nestNode = Utils.createNode(levelView.$top_node, 'nest');
  63. let p = this.fillNest(nestNode, v);
  64. task.push(p);
  65. });
  66. await Promise.all(task);
  67. this.state = LevelState.playing;
  68. }
  69. async fillNest(nestNode: cc.Node, id: string){
  70. let nestPlay = nestNode.addComponent(NestPlay);
  71. await nestPlay.init(id);
  72. await nestPlay.waitDone();
  73. }
  74. skip(num: number){
  75. cc.log('skip-', num);
  76. if(num > pass) return;
  77. if(num < 1) return;
  78. this.passCnt = num-1;
  79. this.load();
  80. }
  81. next(){
  82. if(this.passCnt + 1 >= pass) return;
  83. this.passCnt ++;
  84. this.load();
  85. }
  86. pre(){
  87. if(this.passCnt <= 0) return;
  88. this.passCnt--;
  89. this.load();
  90. }
  91. getMax(){
  92. return pass;
  93. }
  94. private loadPrefabLevel() {
  95. this.state = LevelState.loading
  96. this.title.string = `level${this.passCnt + 1}`;
  97. this.parent.destroyAllChildren();
  98. ResMgr.inst.loadPrefab(`level/level${this.passCnt + 1}`, this.parent).then(()=>{
  99. this.state = LevelState.playing;
  100. });
  101. }
  102. }