Game.ts 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. const { ccclass, property } = _decorator;
  13. @ccclass('Game')
  14. export class Game extends BaseExp {
  15. public static I: Game;
  16. @autoBind({type: Node,tooltip: "map节点的位置"})
  17. public map_pos: Node = null!;
  18. /*是否暂停游戏*/
  19. public isPause: boolean = false;
  20. /**是否游戏结束*/
  21. public _isGameOver: boolean = false;
  22. public set isGameOver(value: boolean) {
  23. if(value){this.gameEnd();}
  24. this._isGameOver = value;
  25. }
  26. public get isGameOver(){
  27. return this._isGameOver;
  28. }
  29. //当前地图场景
  30. public map: GMap = null!;
  31. //创建敌人的组建
  32. public buildEnemys: BuildEnemys = null!;
  33. //玩家组建
  34. public player: Player = null!;
  35. //主角摄像机
  36. public camera: Camera = null!;
  37. //Canvas2D节点
  38. public canvas: Node = null!;
  39. start() {
  40. Game.I = this;
  41. this.canvas = this.node.parent.getChildByName('Canvas')
  42. this.canvas.active = true;
  43. this.map_pos.active = true;
  44. //加载map
  45. this.loadMap(userIns.getCurLevelData().map);
  46. }
  47. /**
  48. * 下一关
  49. */
  50. public nextLevel(){
  51. }
  52. /**
  53. * 重玩
  54. */
  55. public restart(){
  56. //this.loadMap(userIns.getCurLevelData().map);
  57. }
  58. /**
  59. * 加载map
  60. */
  61. public loadMap(name: string){
  62. if(Utils.isNull(name))return;
  63. if(this.map){
  64. if(this.map.name === name)return;
  65. PoolManager.putNode(this.map.node);
  66. this.map = null!;
  67. }
  68. Logger.log(`加载地图:${name}`);
  69. ResUtil.loadRes(`map/${name}`).then((map:Node)=>{
  70. map.parent = this.map_pos.parent;
  71. map.worldPosition = this.map_pos.worldPosition;
  72. this.map = map.getComponent(GMap);
  73. //创建敌人的组建
  74. this.buildEnemys = this.map.getComponent(BuildEnemys);
  75. //玩家组建
  76. this.player = this.map.getComponentInChildren(Player);
  77. //主角摄像机
  78. let cameraNode: Node = Utils.findName(map, 'mainCamera');
  79. this.camera = cameraNode.getComponent(Camera);
  80. });
  81. }
  82. /**
  83. * 游戏结束回收
  84. */
  85. public gameEnd(){
  86. this.buildEnemys.recycle();
  87. }
  88. }