Game.ts 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. * 加载map
  49. */
  50. public loadMap(name: string){
  51. if(Utils.isNull(name))return;
  52. if(this.map){
  53. if(this.map.name === name)return;
  54. PoolManager.putNode(this.map.node);
  55. this.map = null!;
  56. }
  57. Logger.log(`加载地图:${name}`);
  58. ResUtil.loadRes(`map/${name}`).then((map:Node)=>{
  59. map.parent = this.map_pos.parent;
  60. map.worldPosition = this.map_pos.worldPosition;
  61. this.map = map.getComponent(GMap);
  62. //创建敌人的组建
  63. this.buildEnemys = this.map.getComponent(BuildEnemys);
  64. //玩家组建
  65. this.player = this.map.getComponentInChildren(Player);
  66. //主角摄像机
  67. let cameraNode: Node = Utils.findName(map, 'mainCamera');
  68. this.camera = cameraNode.getComponent(Camera);
  69. });
  70. }
  71. /**
  72. * 游戏结束回收
  73. */
  74. public gameEnd(){
  75. this.buildEnemys.recycle();
  76. }
  77. }