Game.ts 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. import { _decorator,Camera,Node} from 'cc';
  2. import { GMap } from './GMap';
  3. import { Player } from './Player';
  4. import { Utils } from '../utils/Utils';
  5. import { Logger } from '../extend/Logger';
  6. import { ResUtil } from '../utils/ResUtil';
  7. import { userIns } from '../data/UserData';
  8. import { BuildEnemys } from './BuildEnemys';
  9. import { autoBind } from '../extend/AutoBind';
  10. import { BaseExp } from '../core/base/BaseExp';
  11. import { uiMgr } from '../core/manager/UIManager';
  12. import { Constants } from '../data/Constants';
  13. import { audioMgr } from '../core/manager/AudioManager';
  14. const { ccclass, property } = _decorator;
  15. @ccclass('Game')
  16. export class Game extends BaseExp {
  17. public static I: Game;
  18. @autoBind({type: Node,tooltip: "map节点的位置"})
  19. public map_pos: Node = null!;
  20. /*是否暂停游戏*/
  21. public isPause: boolean = false;
  22. /**是否游戏结束*/
  23. public _isGameOver: boolean = false;
  24. public set isGameOver(value: boolean) {
  25. if(value){this.gameEnd();}
  26. this._isGameOver = value;
  27. }
  28. public get isGameOver(){
  29. return this._isGameOver;
  30. }
  31. //当前地图场景
  32. public map: GMap = null!;
  33. //创建敌人的组建
  34. public buildEnemys: BuildEnemys = null!;
  35. //玩家组建
  36. public player: Player = null!;
  37. //主角摄像机
  38. public camera: Camera = null!;
  39. //Canvas2D节点
  40. public canvas: Node = null!;
  41. start() {
  42. Game.I = this;
  43. this.canvas = this.node.parent.getChildByName('Canvas')
  44. this.canvas.active = true;
  45. this.map_pos.active = true;
  46. //加载map
  47. this.loadMap();
  48. }
  49. /**
  50. * 下一关
  51. */
  52. public nextLevel(){
  53. userIns.data.level += 1;
  54. this.loadMap(()=>{
  55. this.isGameOver = this.isPause = false;
  56. //刷新新的任务
  57. uiMgr.getPageComponent(Constants.mainUIs.main).loadData();
  58. //加载敌人
  59. this.buildEnemys.loadLevelEnemys();
  60. });
  61. }
  62. /**
  63. * 重玩
  64. */
  65. public restart(){
  66. this.loadMap(()=>{
  67. //创建玩家拥有的枪
  68. this.player.restart();
  69. //加载敌人
  70. this.buildEnemys.loadLevelEnemys();
  71. });
  72. }
  73. /**
  74. * 加载map
  75. * @param cb 回调
  76. */
  77. public loadMap(cb: Function = null){
  78. let levelData:any = userIns.getCurLevelData();
  79. const name: string = levelData.map;
  80. if(Utils.isNull(name))return
  81. Logger.log(`加载地图:${name}`);
  82. ResUtil.loadRes(`map/${name}`).then((map:Node)=>{
  83. //把旧的地图回收 这儿不回收了 懒得处理初始状态 全部重新加载一次 PoolManager.putNode(this.map.node);
  84. if(this.map){
  85. this.map.node.removeFromParent();
  86. this.map.node.destroy();
  87. this.map = null!;
  88. }
  89. map.parent = this.map_pos.parent;
  90. map.worldPosition = this.map_pos.worldPosition;
  91. this.map = map.getComponent(GMap);
  92. //创建敌人的组建
  93. this.buildEnemys = this.map.getComponent(BuildEnemys);
  94. //玩家组建
  95. this.player = this.map.getComponentInChildren(Player);
  96. //设置玩家位置
  97. this.player.setPlayerPos();
  98. //主角摄像机
  99. const cameraNode: Node = Utils.findName(map, 'mainCamera');
  100. this.camera = cameraNode.getComponent(Camera);
  101. cb?.();
  102. });
  103. //去加载下一关map所需要的地图 这儿只要加载过 就会缓存 意思就这一次加载的
  104. //把下一次要用的到的地图提前预加载一遍
  105. const nextLevelData = userIns.levelsTable.find(e=>e.id == (levelData.id + 1));
  106. if(nextLevelData && name != nextLevelData.map){
  107. ResUtil.loadRes(`map/${nextLevelData.map}`);
  108. }
  109. }
  110. /**
  111. * 游戏结束回收
  112. */
  113. public gameEnd(){
  114. this.buildEnemys.recycle();
  115. }
  116. }