Game.ts 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. this.player.restart();
  72. }
  73. /**
  74. * 加载map
  75. */
  76. public loadMap(){
  77. const name: string = userIns.getCurLevelData().map;
  78. if(Utils.isNull(name)
  79. ||(this.map && this.map.name == name)){
  80. return;
  81. }
  82. Logger.log(`加载地图:${name}`);
  83. ResUtil.loadRes(`map/${name}`).then((map:Node)=>{
  84. //把旧的地图回收
  85. if(this.map){
  86. PoolManager.putNode(this.map.node);
  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. const cameraNode: Node = Utils.findName(map, 'mainCamera');
  98. this.camera = cameraNode.getComponent(Camera);
  99. });
  100. }
  101. /**
  102. * 游戏结束回收
  103. */
  104. public gameEnd(){
  105. this.buildEnemys.recycle();
  106. }
  107. }