Game.ts 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. //刷新新的任务
  58. uiMgr.getPageComponent(Constants.mainUIs.main).loadData();
  59. this.isGameOver = this.isPause = false;
  60. //加载敌人
  61. this.buildEnemys.loadLevelEnemys();
  62. }
  63. /**
  64. * 重玩
  65. */
  66. public restart(){
  67. this.isGameOver = this.isPause = false;
  68. //加载敌人
  69. this.buildEnemys.loadLevelEnemys();
  70. }
  71. /**
  72. * 加载map
  73. */
  74. public loadMap(){
  75. const name: string = userIns.getCurLevelData().map;
  76. if(Utils.isNull(name)
  77. ||(this.map && this.map.name == name)){
  78. return;
  79. }
  80. Logger.log(`加载地图:${name}`);
  81. ResUtil.loadRes(`map/${name}`).then((map:Node)=>{
  82. //把旧的地图回收
  83. if(this.map){
  84. PoolManager.putNode(this.map.node);
  85. this.map = null!;
  86. }
  87. map.parent = this.map_pos.parent;
  88. map.worldPosition = this.map_pos.worldPosition;
  89. this.map = map.getComponent(GMap);
  90. //创建敌人的组建
  91. this.buildEnemys = this.map.getComponent(BuildEnemys);
  92. //玩家组建
  93. this.player = this.map.getComponentInChildren(Player);
  94. //主角摄像机
  95. const cameraNode: Node = Utils.findName(map, 'mainCamera');
  96. this.camera = cameraNode.getComponent(Camera);
  97. });
  98. }
  99. /**
  100. * 游戏结束回收
  101. */
  102. public gameEnd(){
  103. this.buildEnemys.recycle();
  104. }
  105. }