Game.ts 3.6 KB

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