Game.ts 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. audioMgr.play(Constants.audios.BGM,true);
  49. });
  50. }
  51. /**
  52. * 下一关
  53. */
  54. public nextLevel(){
  55. userIns.data.level += 1;
  56. //加载新的map
  57. this.loadMap(()=>{
  58. this.isGameOver = this.isPause = false;
  59. //刷新新的任务
  60. uiMgr.getPageComponent(Constants.mainUIs.main).loadData();
  61. //加载敌人
  62. this.buildEnemys.loadLevelEnemys();
  63. });
  64. }
  65. /**
  66. * 重玩
  67. */
  68. public restart(){
  69. //重新加载map
  70. this.loadMap(()=>{
  71. //创建玩家拥有的枪
  72. this.player.restart();
  73. //加载敌人
  74. this.buildEnemys.loadLevelEnemys();
  75. });
  76. }
  77. /**
  78. * 加载map
  79. * @param cb 回调
  80. */
  81. public loadMap(cb: Function = null){
  82. let levelData:any = userIns.getCurLevelData();
  83. const name: string = levelData.map;
  84. if(Utils.isNull(name))return
  85. Logger.log(`加载地图:${name}`);
  86. ResUtil.loadRes(`map/${name}`).then((map:Node)=>{
  87. //把旧的地图回收 PoolManager.putNode(this.map.node);
  88. //这儿不回收了 懒得处理初始状态 全部重新加载一次
  89. if(this.map){
  90. this.map.node.removeFromParent();
  91. this.map.node.destroy();
  92. this.map = null!;
  93. }
  94. map.parent = this.map_pos.parent;
  95. map.worldPosition = this.map_pos.worldPosition;
  96. this.map = map.getComponent(GMap);
  97. //创建敌人的组建
  98. this.buildEnemys = this.map.getComponent(BuildEnemys);
  99. //玩家组建
  100. this.player = this.map.getComponentInChildren(Player);
  101. //设置玩家位置
  102. this.player.setPlayerPos();
  103. //主角摄像机
  104. const cameraNode: Node = Utils.findName(map, 'mainCamera');
  105. this.camera = cameraNode.getComponent(Camera);
  106. cb?.();
  107. });
  108. //去加载下一关map所需要的地图 这儿只要加载过 就会缓存 意思就这一次加载的
  109. //把下一次要用的到的地图提前预加载一遍
  110. const nextLevelData = userIns.levelsTable.find(e=>e.id == (levelData.id + 1));
  111. if(nextLevelData && name != nextLevelData.map){
  112. ResUtil.loadRes(`map/${nextLevelData.map}`);
  113. }
  114. }
  115. /**
  116. * 游戏结束回收
  117. */
  118. public gameEnd(){
  119. this.buildEnemys.recycle();
  120. }
  121. }